Code
/*
Breaker. You have 3 balls in which to destroy the wall. Along your path, you have helpers and obstructers. Obstructers are Death bricks that reduce the size of your paddle,
making it harder to reach the ball. Helpers are bricks that will increase the size of the paddle, and "More Balls" are bricks that releases an extra ball to help you.
Mind you, there is also a secret brick that contains a power which could help you, or destroy your chances of winning.
PRESS the "SPACEBAR" key to create a new ball, if you lose one. ENJOY!
*/
int numRows = 8;
int numCols = 4;
int xdisplacement=2;
int ydisplacement=2;
Ball b;
Paddle p;
Ball tempBall;
LifeBrick lBrick;
boolean makeNewBall = false;
int ballCount = 2;
Brick[][] brickWall = new Brick[numCols][numRows];
int brickCount;
int score = 0;
int level = 1;
Vector balls;
Vector MagicBricks;
BFont font;
int bricks = 0;
String brickType= "Regular";
void setup(){
size(850,450);
background(0);
brickCount = (numRows*numCols);
font = loadFont("Curlz_MT.vlw");
textFont(font,50);
makeRightPanel();
//Creates a Brick Wall.
int startX = 20;
int startY = 20;
MagicBricks = new Vector();
for(int i=0;i<numCols;i++){
for(int j=0;j<numRows;j++){
Brick brick = new Brick(startX,startY,"Regular");
brickWall[i][j] = brick;
startX +=70;
}
startX=20;
startY+=35;
}
//Random code will setup Life and Death Bricks, as well as Bricks make more than 2 balls appear.
brickWall[1][4].setType("Life");
brickWall[1][4].setColor(50,50,255);
brickWall [2][7].setType("Death");
brickWall[2][7].setColor(255,50,50);
brickWall[3][3].setType("Balls");
brickWall[3][3].setColor(50,255,50);
brickWall[0][6].setType("Secret");
for(int i=0;i<numCols;i++){
for(int j=0;j<numRows;j++){
brickWall[i][j].draw();
}
}
balls = new Vector();
b = new Ball(300,300);
balls.add(b);
makeNewBall = false;
p = new Paddle(400);
}
void makeRightPanel(){
fill(0);
rect(600,0,250,450);
fill(255,255,50);
text("B r e a k e r ",650,50);
text("Score "+" = "+score, 620,100);
text("Level "+" = "+level, 620,150);
text("Lives left = "+" "+ballCount,620,200);
text("Bricks Left = "+" "+brickCount,620,250);
text("You hit ' "+brickType+ " !",620,300);
}
void loop(){
background(0);
if(brickCount>0){
if(ballCount < 0){
fill(255,255,250,200);
text("Game Over, You Lose!",190,250);
}
for(int i=0;i<numCols;i++){
for(int j=0;j<numRows;j++){
brickWall[i][j].draw();
}
}
for(int i=0;i<balls.size();i++){
tempBall = (Ball)balls.get(i);
if(tempBall.dead){
balls.remove(i);
}
else{
tempBall.phloat();
}
}
for(int i = 0 ;i<MagicBricks.size();i++){
lBrick = (LifeBrick)MagicBricks.get(i);
lBrick.draw();
}
}
else{
fill(255,255,250,200);
text("Game Over, You Win!",190,250);
}
makeRightPanel();
p.move();
}
void keyPressed(){
if(key==32 && makeNewBall){
Ball b2=new Ball(100,225);
balls.add(b2);
makeNewBall = false;
}
}
class Ball{
String str;
int xpos;
int ypos;
float velocity=random(0.5f,0.9f);
int r=(int)random(100,150);
int g=(int)random(50,150);
int b=(int)random(50,150);
int xdirection;
int ydirection;
boolean dead = false;
int count;
public Ball(int x,int y){
this.xpos=x;
this.ypos=y;
xdirection= 2;
ydirection = 2;
count =0;
}
void phloat(){
noStroke();
if(checkCollision()){
this.ydirection = -ydisplacement;
}
if(checkCollisionWithBrick()){
//if the collision was of a certain type. it should change parameters differently.
//See what direction the ball, if it is either of 4 states, it should behave accordingly.
if(this.xdirection >0 && this.ydirection > 2){
this.ydirection = -ydisplacement;
}
else if(this.xdirection < 0 && this.ydirection > 0){
this.ydirection = -ydisplacement;
}
else if(this.xdirection > 0 && this.ydirection < 0){
this.ydirection = ydisplacement;
}
else if(this.xdirection < 0 && this.ydirection < 0){
this.ydirection = ydisplacement;
}
this.count = this.count + 1;
}
if(this.count > 3){
xdisplacement = xdisplacement + 1;
ydisplacement = ydisplacement + 1;
this.count = 0;
}
if(this.xpos >=550 ){
this.xdirection = -xdisplacement;
velocity = random(0.5f,0.9f);
}
if(this.xpos<0){
this.xdirection = xdisplacement;
velocity = random(0.5f,0.9f);
}
if(this.ypos<0){
this.ydirection = 2;
velocity = random(0.5f,0.9f);
}
if(this.ypos >450){
makeNewBall = true;
ballCount = ballCount - 1;
this.dead = true;
}
this.ypos = (int)(this.ypos + ydirection);
this.xpos = (int)(this.xpos + xdirection);
fill(255,255,255);
ellipse(this.xpos,this.ypos,25,25);
}
boolean checkCollision(){
int dx = abs((this.xpos) - (p.x + 30));
int dy = abs(this.ypos - p.y);
//If dy is a certain distance say 10. Check the distance from the center of the paddle. If the distance is more than
if( dy < 25){
if( dx <50){
return true;
}
else{
return false;
}
}
else{
return false;
}
}
boolean checkCollisionWithBrick(){
int dx,dy;
boolean collision = false;
for(int i=0;i<numCols;i++){
for(int j=0;j<numRows;j++){
if(brickWall[i][j].dead){
collision = false;
continue;
}
else{
dx = abs( (this.xpos) - brickWall[i][j].xpos);
dy = abs( (this.ypos) - brickWall[i][j].ypos);
if(dy<15){
if(dx<100){
if(brickWall[i][j].type == "Life"){
score = score + 40;
brickType = "Life";
print("Collided with life brick");
LifeBrick b = new LifeBrick(brickWall[i][j].xpos,brickWall[i][j].ypos,"Life");
b.setColor(255,255,255);
MagicBricks.add(b);
}
else
if(brickWall[i][j].type == "Death"){
score = score - 10;
brickType = "Death";
print("Collided with death brick");
LifeBrick b = new LifeBrick(brickWall[i][j].xpos,brickWall[i][j].ypos,"Death");
b.setColor(255,10,10);
MagicBricks.add(b);
}
else
if(brickWall[i][j].type == "Balls"){
score = score + 20;
brickType= "New Balls";
println("Making new balls");
Ball b = new Ball(brickWall[i][j].xpos,brickWall[i][j].ypos);
ballCount = ballCount + 1;
balls.add(b);
}
else
if(brickWall[i][j].type == "Secret"){
score = score + 40;
brickType = "Secret..";
print("Collided with secret brick");
LifeBrick b = new LifeBrick(brickWall[i][j].xpos,brickWall[i][j].ypos,"Death");
b.setDefaultColor();
MagicBricks.add(b);
}
else{
//collided with a Regular brick.
score = score +10;
}
collision = true;
brickWall[i][j].setColor(0,0,0);
brickWall[i][j].dead= true;
brickCount = brickCount - 1;
return collision;
} //dy<70
} //dx<15
} //else
} //For j
}//For i
return collision;
}
}
/*************************************************************
CLASS PADDLE
Class represents the paddle used to deflect the balls with.
**************************************************************/
class Paddle{
int w;
int h;
int y;
int x;
public Paddle(int y){
w=80;
h=20;
this.y=y;
fill(100,110,180);
x=300;
}
void move(){
noStroke();
fill(100,110,180);
if(keyPressed){
if(key==39){ //Change the paddle to move to the right a bit.
if(this.x+w-10<=600){
this.x=this.x + 8;
}
}
else
if(key==37){ //Change the position to move the paddle to the left a bit.
if(this.x>=10){
this.x = this.x - 8;
}
}
}
for(int i = 0;i < MagicBricks.size();i++){
LifeBrick b = (LifeBrick)MagicBricks.get(i);
int dy = abs(b.ypos - this.y);
int dx = abs(b.xpos - this.x +w);
//println(b.type);
if(dy<25){
if(dx<100){
//Collision happened.
println(" Collided with Life Brick ");
println("Type"+b.type);
if(b.type == "Life"){
w = w + 40;
MagicBricks.remove(i);
}
else
if(b.type == "Death"){
println("Death Brick@");
w = w - 40;
MagicBricks.remove(i);
}
}
}
}
//println (" Drawing W "+w);
rect(x,this.y,w,h);
ellipse(x-10,(this.y),20,18);
ellipse(x+w-10,(this.y),20,18);
}
public void setH(int h){
this.h = h ;
}
public void setW(int w){
this.w = w;
}
public int getW(){
return w;
}
public int getH(){
return h;
}
}
class Brick{
/*
The 'type' parameter is utilised to identify special bricks, that offer more life
or take away life from the paddle.
*/
int xpos,ypos,w,h;
color c;
String type;
int r,g,b;
boolean dead = false;
public Brick(int x,int y,String type){
this.xpos = x;
this.ypos = y;
this.w=70;
this.h=35;
r = 250;
g = 250;
b = 20;
}
public void setDefaultColor(){
r = 250;
g = 250;
b = 20;
}
public void setH(int h){
this.h = h ;
}
public void setW(int w){
this.w = w;
}
public int getW(){
return w;
}
public int getH(){
return h;
}
void setColor(int r,int g, int b){
this.r = r;
this.g = g;
this.b = b;
}
void setType(String type){
this.type = type;
}
void draw(){
// stroke(5);
fill(0);
rect(this.xpos,this.ypos,w,h);
fill(r,g,b,200);
rect(this.xpos+2,this.ypos+2,w-2,h-2);
}
}
class LifeBrick extends Brick{
//int x,y,w,h;
String type= null;
public LifeBrick(int x,int y,String type){
super(x,y,type);
this.type = type;
// setColor(255,255,255);
}
public void setH(int h){
this.h = h ;
}
public void setW(int w){
this.w = w;
}
public int getW(){
return w;
}
public int getH(){
return h;
}
void draw(){
if(ypos>600){
return;
}
else{
super.draw();
ypos+=2;
}
}
}
Project 6: Create a simple game.
Statement:While computer-based interactive games (a.k.a. video games) have been a pop-cultural force since the arcade scene of the late 1970s and early 1980s, in recent years video games have been recognized as a major emerging art form, poised to have as much cultural impact on the 21st century as cinema did on the 20th. The game industry is making Hollywood-sized amounts of money, with designers of the most popular games achieving a geeky sort of celebrity. Museums and galleries are offering exhibitions of “art games”, computer-scientists are beginning to treat games as technical objects worthy of serious attention, and in humanities departments around the world, games studies is a hot new topic. In short, something is going on, even if we don't yet know what it is or how to talk about it. What kind of game would you create? Create a simple game.
I decided to create a game that has been very close to my heart ever since I first started using a computer. "Bricks" as it was called on the Atari system that I used way back in the 80's. I have always wanted to recreate the game, and this was my opportunity.