PASS — this code satisfies the rule
Ball ball = new Ball();
void setup() {
size(400, 400);
}
void draw() {
background(255);
ball.move();
ball.show();
}
class Ball {
float x = 200, y = 200, dx = 3, dy = 2;
void move() {
x += dx;
y += dy;
if (x < 0 || x > width) dx = -dx;
if (y < 0 || y > height) dy = -dy;
}
void show() {
ellipse(x, y, 20, 20);
}
}
FAIL — this code triggers the rule
Ball ball = new Ball();
Paddle paddle = new Paddle();
Game game = new Game();
void setup() {
size(400, 400);
}
void draw() {
game.play();
}
class Ball {
float x = 200, y = 200, dx = 3, dy = 2;
}
class Paddle {
float x = 150, y = 380;
}
class Game { // moves, bounces, scores, steers and draws everything itself
int score = 0, lives = 3, level = 1;
boolean paused = false;
void play() {
if (!paused) { moveBall(); bounce(); steer(); }
drawAll();
}
void moveBall() { ball.x += ball.dx; ball.y += ball.dy; }
void bounce() {
if (ball.x < 0 || ball.x > width) ball.dx = -ball.dx;
if (ball.y < 0) ball.dy = -ball.dy;
if (ball.y > paddle.y && ball.x > paddle.x && ball.x < paddle.x + 100) { ball.dy = -ball.dy; score++; }
if (ball.y > height) { lives--; ball.y = 200; }
if (score > 10 && level == 1 || score > 20 && level == 2 || score > 30 && level == 3) level++;
}
void steer() {
if (keyPressed && (key == 'a' || key == 'A' || keyCode == LEFT)) paddle.x -= 5;
if (keyPressed && (key == 'd' || key == 'D' || keyCode == RIGHT)) paddle.x += 5;
if (keyPressed && (key == 'p' || key == 'P' || key == ' ')) paused = !paused;
if (keyPressed && (key == 'r' || key == 'R')) { score = 0; lives = 3; level = 1; }
if (paddle.x < 0) paddle.x = 0;
if (paddle.x > width - 100) paddle.x = width - 100;
}
void drawAll() {
background(lives > 1 ? 255 : 200);
fill(level == 1 ? 0 : level == 2 ? 100 : level == 3 ? 150 : 200);
ellipse(ball.x, ball.y, 20, 20);
rect(paddle.x, paddle.y, 100, 10);
for (int i = 0; i < lives; i++) ellipse(20 + i * 20, 20, 10, 10);
hud();
}
void hud() {
fill(paused || lives == 0 ? 255 : 0);
if (paused) text("PAUSED", 170, 200);
if (lives == 0) text("GAME OVER", 150, 220);
if (score > 0 && score % 10 == 0) text("LEVEL UP!", 150, 240);
}
}