Skip to content

Instantly share code, notes, and snippets.

@racecraftr
Last active October 7, 2019 15:27
Show Gist options
  • Save racecraftr/ff64f532d477ba3c6dc7cd95754a4e69 to your computer and use it in GitHub Desktop.
Save racecraftr/ff64f532d477ba3c6dc7cd95754a4e69 to your computer and use it in GitHub Desktop.
// global constants
int wallAcross = 19;
int wallDown = 4;
int brickWidth = 50;
int brickHeight = 20;
/*
this is the class that makes the ball
the ints define the xy position, the radius of the ball
and the change in x and y position
*/
class Ball {
int x, y, radius;
int xspeed, yspeed;
Ball(int initX, int initY, int initXS, int initYS, int radius) {
fill(255);
this.x = initX;
this.y = initY;
this.xspeed = initXS;
this.yspeed = initYS;
this.radius = radius;
}
void ballUpdate(Collision c) {
switch(c) {
case UP:// collision upward
case DN:// collision downward
// cases of vertical collision
yspeed *= -1;// inverts the direction of the change on the y axis
break;//breaks the process, doing it only once
case RT:// collision to the right
case LT://collison to the left
// cases of horizontal collision
xspeed *= -1;// inverts the direction of the change on the x axis
break;
default://case where there is no collison
break;
}
}
void display() {
this.x += xspeed;// changes the x
this.y += yspeed;// changes the y
ellipse(x, y, radius*2, radius*2);
/* radius * 2 because
last 2 inputs specify x diameter and y diameter respectively
not radius
*/
}
} // end class Ball
enum Collision {
UP, DN, RT, LT, NO// NO = No collision
} // end enum Collision
/* this is the class for brick
ints specify xy coordinates, width, height
boolean fixed, which states that a brick can't be destroyed
points, which we have not added yet
and boolean visible, which is set to "true" if not destroyed
and "false" if destroyed"
*/
class Brick {
int x, y, w, h, r, g, b;
boolean fixed;
int points;
boolean visible;
Brick(int x, int y, int w, int h, int r, int g, int b, boolean fixed, int points) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.r = r;
this.g = g;
this.b = b;
this.fixed = fixed;
this.points = points;
this.visible = true;
}
void makeInvisible() {// makes it destroyed
this.visible = false;
}
Collision detectCollision(Ball ball) {
if (visible) {
Collision c = detectCollisionUtil(ball, this.x, this.y, this.w, this.h);
if (c != Collision.NO) {
this.visible = false;
}// states that if there is a collision, it we become invisible
return c;
}
return Collision.NO;
}// returns if Collison.NO (no collision)
void display() {
if (visible) {
fill(r, g, b);
rect(x, y, w, h);
} else {
// fill(0);
// rect(1000, 1000, w, h);
}
}
} //end class brick
/*This is the class for paddle
ints declare the xy position of the paddle
also declares the width and height of paddle
*/
class Paddle {
int px, py, pw, ph;
Paddle(int px, int py, int pw, int ph) {
this.px = px;
this.py = py;
this.pw = pw;
this.ph = ph;
}
void display(Ball ball) {
fill(255);
rect(ball.x - pw/2, py, pw, ph);//paddle is centered with ball along x-axis
}
Collision detectCollision(Ball ball) {
if ((ball.y > py - ball.radius) &&
(ball.y < py + ph + ball.radius)) {
return Collision.DN;
}// says if ball is touching paddle
// then ball will bounce upward
return Collision.NO;// returns if there is no collision
}
} // end class Paddle
Collision detectCollisionUtil(Ball ball, int x, int y, int w, int h) {
if ((ball.x > x - ball.radius) &&
(ball.x < x + w + ball.radius) &&
(ball.y > y - ball.radius) &&
(ball.y < y + h + ball.radius)) {
if (ball.x > x + w) {
return Collision.LT;
} else if (ball.x < x) {
return Collision.RT;
} else if (ball.y < y) {
return Collision.DN;
} else if (ball.y > y + h) {
return Collision.UP;
}
}
return Collision.NO;
}
Collision detectWall(Ball ball) {
if (ball.x < 0) {
return Collision.LT;
} else if ((ball.x + (ball.radius*2))> width) {
return Collision.RT;
} else if (ball.y < 0) {
return Collision.UP;
}
return Collision.NO;
}
/*
declares an array of bricks,
a paddle,
and a ball
*/
Brick[][] wall;
Paddle paddle;
Ball ball;
void setup() {// generates start of game
frameRate(1000);// the framerate of the game
size(1000, 400);// the width and height of the game screen respectively
background(0);// greyscale input, so the background will be blac, or rgb (0, 0, 0)
wall = new Brick[wallAcross][wallDown];// see global variables
int xOffset = (width - (brickWidth * wallAcross))/2;// sets x spacing for the bricks
int yOffset = 20;// sets y spacing for the bricks
for (int across = 0; across < wallAcross; across++) {
for (int down = 0; down < wallDown; down++) {
int xBrick = xOffset + (brickWidth*across) ;
int yBrick = yOffset + ((brickHeight + 5)*down);
wall[across][down] =
new Brick(xBrick, yBrick,
brickWidth, brickHeight,
(int)random(100, 150), (int)random(150, 200), (int)random(200, 255),
false, 10);// makes bricks of random colors
}
}
// makes a 2D array of bricks with the dimensions specified in the global variables
paddle = new Paddle((width - 50)/2, height-50, 50, 5);
ball = new Ball(width/2, height/2, -3, 3, 5);
// makes the paddle and the ball.
}
void draw() {
background(0);
for (int across = 0; across < wallAcross; across++) {
for (int down = 0; down < wallDown; down++) {
Brick b = wall[across][down];
Collision c = b.detectCollision(ball);
ball.ballUpdate(c);
wall[across][down].display();
}// renders the bricks
}
paddle.display(ball);
Collision c2 = paddle.detectCollision(ball);
ball.ballUpdate(c2);
Collision c3 = detectWall(ball);
ball.ballUpdate(c3);
ball.display();
}// reders the paddle and the ball
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment