Skip to content

Instantly share code, notes, and snippets.

@bakercp
Created December 17, 2014 03:40
Show Gist options
  • Save bakercp/cd0535ec282b1db68eaa to your computer and use it in GitHub Desktop.
Save bakercp/cd0535ec282b1db68eaa to your computer and use it in GitHub Desktop.
HelloWorld ofSketch Gist.
class Ball{
public:
ofVec2f position;
ofVec2f velocity;
int radius;
ofColor color;
Ball() {
// Choose a random radius.
radius = ofRandom(5, 10);
// Choose a random position.
position = ofVec2f(ofRandom(radius, ofGetWidth() - radius),
ofRandom(radius, ofGetHeight() - radius));
// Choose a random velocity.
velocity = ofVec2f(ofRandom(-5, 5), ofRandom(-5, 5));
// Choose a random color between red and yellow.
color = ofColor(255, ofRandom(255), 0, 255);
}
void update() {
// Update the x velocity.
if (position.x + radius >= ofGetWidth() ||
position.x - radius <= 0) {
velocity.x *= -1;
}
// Update the y velocity.
if (position.y + radius >= ofGetHeight() ||
position.y - radius <= 0) {
velocity.y *= -1;
}
// Update the position.
position += velocity;
}
void draw() {
ofFill();
// Set the fill color for this ball.
ofSetColor(color.r, color.g, color.b, 127);
// Draw the ball.
ofCircle(position, radius);
ofNoFill();
// Set the outline for this ball.
ofSetColor(color);
// Draw the ball.
ofCircle(position, radius);
}
};
#include "Ball.h"
int numBalls; // The total number of balls we'll use.
vector<Ball> balls; // A collection of all balls.
void setup() {
ofSetFrameRate(60);
numBalls = 750; // The total number of balls we'll use.
// Enable vertical sync.
ofSetVerticalSync(true);
// Set the window size.
ofSetWindowShape(500, 500);
// Set the background color.
ofBackground(255);
// Create the balls
for (int i = 0; i < numBalls; i++) {
balls.push_back(Ball());
}
}
void update() {
// Update all balls.
for (int i = 0; i < balls.size(); i++) {
balls[i].update();
}
}
void draw() {
// Draw all balls.
for (int i = 0; i < balls.size(); i++) {
balls[i].draw();
}
// Log a message.
ofLogNotice("draw()") << ofGetTimestampString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment