Skip to content

Instantly share code, notes, and snippets.

@ybakos
Created February 14, 2014 14:46
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/9002242 to your computer and use it in GitHub Desktop.
Save ybakos/9002242 to your computer and use it in GitHub Desktop.
// YOUR NAME
// Breakout/Arkanoid Remixed.
// HW 11: Functions that return values
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;
final int PADDLE_SPEED = 5;
int paddleX;
int paddleY;
void setup() {
size(400, 400);
ballX = int(random(0, width));
ballY = int(random(0, height * 0.25));
paddleX = width / 2;
paddleY = height - 50;
}
void draw() {
if (displayStartScreen) {
drawStartScreen();
} else {
drawMainScreen();
}
}
void keyPressed() {
if (displayStartScreen) {
displayStartScreen = false;
} else {
if (keyCode == LEFT) {
movePaddleLeft();
} else if (keyCode == RIGHT) {
movePaddleRight();
}
}
}
void drawStartScreen() {
background(100, 100, 100);
textSize(50);
textAlign(CENTER);
text("BREAKOUT!", width / 2, height / 2);
textSize(10);
text("Press any key to begin.", width / 2, height / 2 + 100);
}
void drawMainScreen() {
background(0);
moveBall();
drawBall();
drawPaddle();
}
void moveBall() {
ballX = ballX + (ballXDirection * BALL_SPEED);
ballY = ballY + (ballYDirection * BALL_SPEED);
// If the ball hits the sides of the window, change direction.
if (ballX >= width || ballX <= 0) {
ballXDirection = ballXDirection * -1;
}
if (ballY >= height || ballY <= 0) {
ballYDirection = ballYDirection * -1;
}
if (ballX > paddleX - PADDLE_WIDTH / 2 && ballX < paddleX + PADDLE_WIDTH / 2 && ballY > paddleY - PADDLE_HEIGHT / 2 && ballY < paddleY + PADDLE_HEIGHT / 2) {
ballYDirection = ballYDirection * -1;
}
}
void drawBall() {
ellipse(ballX, ballY, BALL_SIZE, BALL_SIZE);
}
void drawPaddle() {
rectMode(CENTER);
rect(paddleX, paddleY, PADDLE_WIDTH, PADDLE_HEIGHT);
}
// These next two functions are intentionally obtuse.
// We'll improve them later.
void movePaddleLeft() {
paddleX = paddleX - PADDLE_SPEED;
}
void movePaddleRight() {
paddleX = paddleX + PADDLE_SPEED;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment