Skip to content

Instantly share code, notes, and snippets.

@nrubin29
Created June 23, 2016 20:13
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 nrubin29/d6431892b07c3b27a39cc80565e7b231 to your computer and use it in GitHub Desktop.
Save nrubin29/d6431892b07c3b27a39cc80565e7b231 to your computer and use it in GitHub Desktop.
Breakout
//setting up
boolean[][] bricks = new boolean[14][10];
float bx, by, bdiameter, bxspeed, byspeed;
int paddleX, speed;
boolean right, left;
int lives, score;
void setup() {
size(500, 450);
background(0);
bx = width / 2;
by = 330;
bdiameter = 20;
bxspeed = 4;
byspeed = 4;
lives = 3;
score = 0;
//rectangle
speed = 4;
paddleX = width / 2 - 50;
//making bricks and setting bricks as true
for (int x = 0; x < 14; x++) {
for (int y = 0; y < 10; y++) {
bricks[x][y] = true;
}
}
}
void draw() {
clear();
for (int x = 0; x < bricks.length; x++) {
for (int y = 0; y < bricks[x].length; y++) {
if (bricks[x][y]) {
int xcoord = x * 30 + 45;
int ycoord = y * 15 + 50;
rect(xcoord, ycoord, 30, 15);
if(bx <= xcoord + 25 && bx >= xcoord + 5 && by < ycoord+15 && by > ycoord){
bricks[x][y] = false;
byspeed *= -1;
}
else if(bx <= xcoord + 30 && bx >= xcoord && by < ycoord+15 && by > ycoord){
bricks[x][y] = false;
bxspeed *= -1;
}
}
}
}
textSize(30);
text("SCORE:", 10, 30);
text(score, 120, 30);
text(lives, 460, 30);
text("LIVES:", 370, 30);
rect(paddleX, 360, 100, 20);
if (left && paddleX > 0) {
paddleX -= speed;
}
if (right && paddleX + 100 < width) {
paddleX += speed;
}
ellipse(bx, by, bdiameter, bdiameter);
// moving the ball
bx = bx + bxspeed;
by = by + byspeed;
//bouncing the ball against the walls
if (bx < 0 + 20 / 2) {
bxspeed = bxspeed * -1;
}
if (bx >= width - 20 / 2) {
bxspeed = bxspeed * -1;
}
if (by >= height - bdiameter / 2) {
lives = lives - 1;
bx = paddleX + 50;
by = 330;
}
if (by < 0 + bdiameter / 2 + 50) {
byspeed = byspeed * -1;
}
//collision with paddle
if (bx - bdiameter / 2 >= paddleX && by + bdiameter / 2 >= 360 && bx + bdiameter / 2 <= paddleX + 100) {
byspeed = byspeed * -1;
}
if (lives == 0) {
clear();
textSize(50);
text("Game Over", 110, height / 2 - 30);
textSize(30);
text("Click to Restart", 135, height / 2 + 30);
if (mousePressed) {
//making bricks and setting bricks as true
for (int x = 0; x < 14; x++) {
for (int y = 0; y < 10; y++) {
bricks[x][y] = true;
}
}
lives = 3;
score = 0;
}
}
}
void keyPressed() {
if (keyPressed) {
if (keyCode == RIGHT) {
right = true;
} else if (keyCode == LEFT) {
left = true;
}
}
}
void keyReleased() {
if (keyCode == RIGHT) {
right = false;
} else if (keyCode == LEFT) {
left = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment