Skip to content

Instantly share code, notes, and snippets.

@ybakos
Created February 12, 2014 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ybakos/8959641 to your computer and use it in GitHub Desktop.
Save ybakos/8959641 to your computer and use it in GitHub Desktop.
In-class example of if/else, mouse/key events, and functions.
// Yong Bakos
// Breakout!
boolean displayStartScreen = true;
final int BALL_SIZE = 20;
final int BALL_SPEED = 4;
int ballX;
int ballY;
int ballXDirection = 1;
int ballYDirection = 1;
final int PADDLE_HEIGHT = 20;
final int PADDLE_WIDTH = 100;
int paddleX;
int paddleY;
void setup() {
size(400, 400);
ballX = width * 0.25;
ballY = height * 0.4;
paddleX = width / 2;
paddleY = height - 50;
}
void draw() {
if (displayStartScreen) {
background(100, 100, 100);
textSize(50);
textAlign(CENTER);
text("BREAKOUT!", width / 2, height / 2);
textSize(10);
text("Click to begin.", width / 2, height / 2 + 100);
} else {
background(0);
// Ball stuff
ellipse(ballX, ballY, BALL_SIZE, BALL_SIZE);
ballX = ballX + (ballXDirection * BALL_SPEED);
ballY = ballY + (ballYDirection * BALL_SPEED);
if (ballX >= width || ballX <= 0) {
ballXDirection = ballXDirection * -1;
}
if (ballY >= height || ballY <= 0) {
ballYDirection = ballYDirection * -1;
}
// Paddle stuff
rectMode(CENTER);
rect(paddleX, paddleY, PADDLE_WIDTH, PADDLE_HEIGHT);
// TODO: move the paddle
// TODO: handle bouncing
// TODO: draw bricks
}
}
void mousePressed() {
displayStartScreen = false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment