Skip to content

Instantly share code, notes, and snippets.

@kettle11
Last active August 29, 2015 14:19
Show Gist options
  • Save kettle11/e605a2a682e864cd9776 to your computer and use it in GitHub Desktop.
Save kettle11/e605a2a682e864cd9776 to your computer and use it in GitHub Desktop.
A simple breakout framework intended for teaching early programming concepts.
//Classes
class Block
{
public int xPosition = 0;
public int yPosition = 0;
public int rightEdge = 10;
public int bottomEdge = 10;
public int myWidth = 10;
public int myHeight = 10;
public boolean destroyed = false;
public boolean invincible = false;
public Block(int xSet, int ySet, int widthSet, int heightSet)
{
xPosition = xSet;
yPosition = ySet;
myWidth = widthSet;
myHeight = heightSet;
rightEdge = xPosition + myWidth;
bottomEdge = yPosition + myHeight;
}
public void Destroy()
{
if(invincible == false)
{
destroyed = true;
}
}
}
//The ball that bounces around
class Ball
{
public int xPosition = 0;
public int yPosition = 0;
public int xMove = 0;
public int yMove = 0;
public int radius = 20;
}
//Game running code
Block[] blocks = new Block[7];
Ball ball = new Ball();
int screenWidth = 200;
int screenHeight = 200;
void setup()
{
//Setup screen size
size(screenWidth,screenHeight);
//Blocks Row 1
Block block0 = new Block(20,20,50,20);
Block block1 = new Block(80,20,50,20);
Block block2 = new Block(140,20,50,20);
//Row 2
Block block3 = new Block(20,50,50,20);
Block block4 = new Block(80,50,50,20);
Block block5 = new Block(140,50,50,20);
blocks[0] = block0;
blocks[1] = block1;
blocks[2] = block2;
blocks[3] = block3;
blocks[4] = block4;
blocks[5] = block5;
//Make the block the player controls
blocks[6] = new Block(100, 150, 70, 15);
blocks[6].invincible = true;
//Position the ball and start it moving
ball.xPosition = 100;
ball.yPosition = 130;
ball.radius = 10;
ball.yMove = -2;
ball.xMove = 2;
}
void draw()
{
//Erase the screen
background(255,255,255);
//Draw the ball
ellipse(ball.xPosition, ball.yPosition, ball.radius, ball.radius);
//Draw the blocks
for(int i = 0; i < 7; i += 1)
{
if(blocks[i].destroyed == false)
{
rect( blocks[i].xPosition , blocks[i].yPosition , blocks[i].myWidth , blocks[i].myHeight) ;
}
}
//Move the ball
ball.xPosition = ball.xPosition + ball.xMove;
ball.yPosition = ball.yPosition + ball.yMove;
//Bounce the ball when it hits a screen edge
if(ball.xPosition < 0)
{
ball.xMove = 2;
}
if(ball.xPosition > screenWidth)
{
ball.xMove = -2; //If the ball hits the right edge, move it left
}
if(ball.yPosition < 0)
{
ball.yMove = 2;
}
if(ball.yPosition > screenHeight)
{
ball.yMove = -2;
}
//Bounce ball off of blocks
for(int i = 0; i < 7; i += 1)
{
if(blocks[i].destroyed == false)
{
int nextX = ball.xPosition + ball.xMove;
int nextY = ball.yPosition + ball.yMove;
//Bounce the ball horizontally.
if(ball.yPosition > blocks[i].yPosition && ball.yPosition < blocks[i].bottomEdge)
{
if(nextX > blocks[i].xPosition && nextX < blocks[i].rightEdge)
{
ball.xMove = -ball.xMove;
blocks[i].Destroy(); //If the ball hits a block, destroy it!
}
}
//Bounce the ball vertically
if(ball.xPosition > blocks[i].xPosition && ball.xPosition < blocks[i].rightEdge)
{
if(nextY > blocks[i].yPosition && nextY < blocks[i].bottomEdge)
{
ball.yMove = -ball.yMove;
blocks[i].Destroy(); //If the ball hits a block, destroy it!
}
}
}
}
}
//Player input controls
void keyPressed()
{
if(keyCode == LEFT)
{
blocks[6].xPosition -= 4;
}
if(keyCode == RIGHT)
{
blocks[6].xPosition += 4;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment