Code
float px, py, vx, vy;
void setup() {
size(300, 300);
reset();
smooth();
}
void reset() {
px = 150;
py = 150;
vx = random(-4, 4);
vy = random(-4, 4);
}
void mouseClicked() {
reset();
}
void draw() {
background(0, 0, 0);
if (px > 300 || px < 0) {
vx *= -1;
}
if (py > 300 || py < 0) {
vy *= -1;
}
px += vx;
py += vy;
ellipse(px, py, 6, 6);
}
0403 - Pong II: Towards a simple game
Statement:Modify your previous exercise (0402) so that the puck bounces off the edges of the canvas.
To achieve the bouncing effect, implement the following tests, such that they execute on every frame of animation:
hide statement