How the heck do you move around???
// in this game every visible object is a Rectangle
// (ball, paddle, bricks, even the game frame)
// are represented by a Rectangle:
// CAJA
import java.awt.geom.*;
public class Rectangle {
//
int width;
int height;
boolean hasStroke = false;
color strokeColor;
boolean hasFill = false;
color fillColor;
color opacity;
//
int x1;
int y1;
int x2;
int y2;
//
//
//
Rectangle(int W, int H, boolean HASSTROKE, color STROKE, boolean HASFILL, color FILL) {
width = W;
height = H;
hasStroke = HASSTROKE;
strokeColor = STROKE;
hasFill = HASFILL;
fillColor = FILL;
//
opacity = 255;
}
//
void setPosition(int X, int Y) {
x1 = X;
y1 = Y;
x2 = x1+width;
y2 = y1+height;
}
//
void drawYourself(){
// stroke
if (hasStroke) {
stroke(strokeColor);
} else{
noStroke();
}
// fill
if (hasFill) {
fill(fillColor, opacity);
} else{
noFill();
}
rect(recX+x1, recY+y1, width, height);
}
// COLLISION DETECTION FUNCTIONS
boolean doesPointTouchMe (int PX, int PY){
boolean result = false;
if (PX >= x1 && PX <= x2) {
if (PY >= y1 && PY <= y2) {
result = true;
}
}
return result;
}
int whatSideDoesLineTouch (Line2D LINE, int VELX, int VELY){
Line2D side;
// top (1) / bottom (3)
if (VELY>0){
side = new Line2D.Float(x1,y1,x2,y1);
if(LINE.intersectsLine(side)){
return 1;
}
} else if (VELY<0){
side = new Line2D.Float(x1,y2,x2,y2);
if(LINE.intersectsLine(side)){
return 3;
}
}
// left (4) / right (2)
if (VELX>0){
side = new Line2D.Float(x1,y1,x1,y2);
if(LINE.intersectsLine(side)){
return 4;
}
} else if (VELX<0){
side = new Line2D.Float(x2,y1,x2,y2);
if(LINE.intersectsLine(side)){
return 2;
}
}
return 0;
}
}
// PADDLE
public class Paddle {
Rectangle rectangle;
// PADDLE PROPERTIES --
public int width = 60;
int height = 5;
boolean hasStroke = false;
color strokeColor = #FFFFFF;
boolean hasFill = true;
color fillColor = #ffffff;
//
public int x = (gameFrameWidth-width)/2;
public int y = screenHeight-screenHeight/5;
//
//
Paddle() {
rectangle = new Rectangle(width, height, hasStroke, strokeColor, hasFill, fillColor);
rectangle.setPosition(x, y);
}
public void lifeLost(){
if(lifes>0){
lifes--;
createBalls();
pausa=true;
}else {
frameNum=0;
lifes=3;
createBricks();
createBalls();
estado=2;
}
backgroundRefreshes = true;
paddle = new Paddle();
}
void refresh(){
change=false;
if(!pausa)
updatePosition();
rectangle.setPosition(x, y);
rectangle.drawYourself();
}
boolean[] keys = new boolean[526];
boolean checkKey(String k)
{
for(int i = 0; i < keys.length; i++)
if(KeyEvent.getKeyText(i).toLowerCase().equals(k.toLowerCase())) return keys[i];
return false;
}
void keyPressed()
{ keys[keyCode] = true; }
void keyReleased()
{ keys[keyCode] = false; }
void updatePosition() {
if(checkKey("Izquierda"))
x-=6;
if(checkKey("Derecha"))
x+=6;
//x = mouseX-recX-width/2;
x = constrain(x, 0, gameFrameWidth-width);
}
}
// Breakout // // 28/02/2007
// Steph Thirion - Game Mod workshop: < http://trsp.net/teaching/gamemod >
//
// english version :)
// made with Processing 0124 Beta
Rectangle gameFrame;
Brick[] bricks;
Paddle paddle;
Ball[] balls;
Paddle[] lifeIcons;
int lifes;
int estado=0;
boolean pausa=false;
boolean restart=false;
boolean change;
String[] introFile= {"intro1.png", "intro2.png", "intro3.png", "intro4.png"};
String[] winFile = {"win1.png","win2.png","win3.png"};
String[]loseFile = {"lose1.png", "lose2.png", "lose3.png",};
PFont fuente;
Pantalla intro;
Pantalla win;
Pantalla lose;
//
//
int frameNum = 0;
//
// SCREEN PROPERTIES --
int screenWidth = 640;
int screenHeight = 480;
color backgroundColor = #303030;
boolean backgroundRefreshes = false;
//
// GAME FRAME PROPERTIES --
int gameFrameWidth = screenWidth-screenWidth/10;
int gameFrameHeight = screenHeight-screenHeight/10;
color gameFrameStroke = #FFFFFF;
boolean gameFrameHasStroke = false;
color gameFrameFill = #000000;
int opacityOfRefresh = 255;
boolean gameFrameRefreshes = true;
//
//
int recX = (screenWidth-gameFrameWidth)/2;
int recY = (screenHeight-gameFrameHeight)/2;
//
// SETUP FUNCTION --
void setup() {
size(screenWidth,screenHeight,P3D);
background(backgroundColor);
frameRate(60);
fuente = loadFont("OCRAExtended-30.vlw");
textFont(fuente);
// create objects
intro = new Pantalla(640, 1, 0, 20, introFile);
win = new Pantalla(600, 0, 0, 50, winFile);
lose = new Pantalla(600, 0, 0, 50, loseFile);
gameFrame = new Rectangle(gameFrameWidth, gameFrameHeight, gameFrameHasStroke, gameFrameStroke, true, gameFrameFill);
gameFrame.opacity = opacityOfRefresh;
createBricks();
createBalls();
createLifes();
paddle = new Paddle();
//
refreshScreen();
}
// DRAW FUNCTION --
void draw() {
frameNum++;
refreshScreen();
if(pausa){
if(frameNum%120 <60)
{
text("Ready?", 280, 320);
}
}
}
void createBalls(){
// BALL(S) PROPERTIES --
int numberOfBalls = 1;
int yBalls = 3*height/4+20;
//
balls = new Ball[numberOfBalls];
for (int i=0; i<numberOfBalls; i++){
int x = width/2-35;
balls[i] = new Ball(x, yBalls);
}
}
void createLifes()
{
int maxLifes = 3;
lifeIcons=new Paddle[maxLifes];
lifes = maxLifes;
int x = width*9/10;
int y = height/20;
for(int i=0 ; i < lifes ; i++)
{
x-=width/10;
lifeIcons[i] = new Paddle();
lifeIcons[i].x=x;
lifeIcons[i].y=y;
}
}
void createBricks(){
int[][] texto = {{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,1,1,1,0,1,1,0,0,1,1,1,0,1,1,1,0,1,0,0,0,1,1,1,0,1,1,1,0,1,1,1,0,1,1,1,0},
{0,1,0,1,0,1,0,1,0,1,0,0,0,1,0,1,0,1,0,0,0,1,0,0,0,0,1,0,0,1,0,1,0,1,0,0,0},
{0,1,0,1,0,1,1,0,0,1,1,1,0,1,0,1,0,1,0,0,0,1,1,1,0,0,1,0,0,1,0,1,0,1,1,1,0},
{0,1,0,1,0,1,0,1,0,0,0,1,0,1,0,1,0,1,0,0,0,1,0,0,0,0,1,0,0,1,0,1,0,0,0,1,0},
{0,1,1,1,0,1,1,0,0,1,1,1,0,1,1,1,0,1,1,1,0,1,1,1,0,0,1,0,0,1,1,1,0,1,1,1,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} };
//int[][] texto = {{0,0}};
// BRICK GROUP PROPERTIES --
int numberOfBricks = texto[0].length*texto.length;
println(texto.length);
int bricksPerRow = texto[0].length;
int brickWidth = gameFrameWidth/(bricksPerRow);
int brickHeight = 10;
boolean brickHasStroke = true;
color brickStroke = #000000;
boolean brickHasFill = true;
color brickFill = #ff0000;
int yBricks = 50;
color[] rowsColors = {#0000FF, #FF00FF, #ff9900, #ffff00, #00ff00, #00ffff};
//
//
// CREATE BRICKS --
bricks = new Brick[numberOfBricks];
for (int i=0; i<numberOfBricks; i++){
int rowNum = i/bricksPerRow;
// coords
int x = brickWidth*i+10;
x -= rowNum*bricksPerRow*brickWidth;
int y = yBricks+i/bricksPerRow*brickHeight;
// color
int num = min(rowNum, rowsColors.length-1);
color rowColor = rowsColors[texto[rowNum][i%bricksPerRow]];
// create brick
bricks[i] = new Brick(x, y, brickWidth, brickHeight, brickHasStroke, brickStroke, brickHasFill, rowColor);
}
}
void refreshScreen() {
if(estado==0)
{
intro.refresh();
}
if(estado==2)
{
lose.refresh();
}
if(estado==3)
{
win.refresh();
}
if(estado==1)
{
// BACKGROUND
if(backgroundRefreshes){
background(backgroundColor);
backgroundRefreshes = false;
}
// GAME FRAME
if(gameFrameRefreshes){
gameFrame.drawYourself();
}
// PADDLE
paddle.refresh();
//
// BRICKS
boolean fin=true;
for (int i=0; i<bricks.length; i++){
bricks[i].refresh();
if(bricks[i].imAlive)
fin=false;
}
if(fin)
{
fin = false;
estado=3;
createBricks();
createBalls();
paddle=new Paddle();
frameNum=0;
}
// BALLS
for (int i=0; i<balls.length; i++){
balls[i].refresh();
}
//LIFES
for (int i=0; i<lifes; i++){
lifeIcons[i].refresh();
}
}
}
void keyPressed()
{
if(key==' ')
change =true;
if(estado==1)
{
if(pausa)
pausa=false;
paddle.keyPressed();
}
}
void keyReleased()
{ paddle.keyReleased(); }
// BALL
import java.awt.geom.*;
public class Ball {
Rectangle rectangle;
// BALL PROPERTIES --
int width = 5;
int height = 5;
boolean hasStroke = false;
color strokeColor = #FFFFFF;
boolean hasFill = true;
color fillColor = #ffffff;
// velocity
double velX = 0;
int velY = 5;
//
double x;
int y;
int ox;
int oy;
int xcentre;
int ycentre;
//
Ball(int X, int Y) {
x = X;
y = Y;
rectangle = new Rectangle(width, height, hasStroke, strokeColor, hasFill, fillColor);
rectangle.setPosition((int)x, y);
}
void refresh(){
if(!pausa)
updatePosition();
rectangle.setPosition((int)x, y);
rectangle.drawYourself();
}
void updatePosition() {
// add velocity to position
x+=velX;
y+=velY;
// collision with limits
if(x<=0 || x>=gameFrameWidth-width){
velX = -velX;
x = constrain((int)x, 0, gameFrameWidth-width);
}
if(y<=0){
velY = -velY;
y = constrain(y, 0, gameFrameHeight-height);
}
if(y>=(gameFrameHeight-height)){
paddle.lifeLost();
}
xcentre = (int)x+width/2;
ycentre = y+height/2;
// collision with paddle
int result = checkCollisionWithRectangle(paddle.rectangle);
// if collides on top, control direction of ball
if (result == 1){
velX= (xcentre - recX*2 - paddle.rectangle.x1+paddle.rectangle.width/2)/5.0;
}
// collision with bricks
if (result == 0) {
for (int i=0; i<bricks.length; i++){
if(bricks[i].imAlive){
int res = checkCollisionWithRectangle(bricks[i].rectangle);
if (res != 0){
bricks[i].die();
break;
}
}
}
}
ox = (int)x;
oy = y;
}
// FUNCION DETECCION DE COLISION --
// result: 0: no collision 1: top 2: right 3: bottom 4: left 5: couldn't detect which side
int checkCollisionWithRectangle (Rectangle R){
int result = 0;
if (R.doesPointTouchMe(xcentre, ycentre)){
// which side did it collide
Line2D lineaBola = new Line2D.Float(xcentre,ycentre,ox+width/2,oy+height/2);
result = R.whatSideDoesLineTouch(lineaBola, (int)velX, velY);
// top
if(result==1){
velY = -velY;
y = R.y1-height;
// right
}else if(result==2){
velX = -velX;
x = R.x2;
// bottom
}else if(result==3){
velY = -velY;
y = R.y2;
// left
}else if(result==4){
velX = -velX;
x = R.x1-width;
}else{
result = 5;
}
}
return result;
}
}
// BRICK
public class Brick {
Rectangle rectangle;
// BRICK PROPERTIES --
boolean hasStroke = false;
color strokeColor = #FFFFFF;
boolean hasFill = true;
color fillColor = #ffffff;
//
int x = gameFrameWidth/2;
int y = 270;
//
boolean respawns = false;
int timeToRespawn = 60; // time is in frames
//
int frame;
public boolean imAlive;
//
Brick(int X, int Y, int W, int H, boolean HASSTROKE, color STROKE, boolean HASFILL, color FILL) {
rectangle = new Rectangle(W, H, HASSTROKE, STROKE, HASFILL, FILL);
rectangle.setPosition(X, Y);
imAlive = true;
}
void refresh(){
if (imAlive){
rectangle.drawYourself();
}else{
if (respawns){
frame++;
if(frame>timeToRespawn){
// rise up from your grave, brick
imAlive=true;
}
}
}
}
void die() {
imAlive = false;
frame = 0;
}
}
public class Pantalla {
int retardo;
int onKeyPress;
int onTime;
int frameTime;
int frame;
PImage[] imagenes;
Pantalla(int _retardo, int _onKeyPress, int _onTime, int _frameTime, String[] _imagenes)
{
retardo=_retardo;
onKeyPress=_onKeyPress;
onTime=_onTime;
frameTime =_frameTime;
imagenes = new PImage[_imagenes.length];
for(int i=0; i<_imagenes.length;i++)
imagenes[i] = loadImage(_imagenes[i]);
}
public void refresh()
{
frame=(frameNum/frameTime)%imagenes.length;
image(imagenes[frame], 0, 0, width, height);
if(frameNum>retardo)
{
estado=onTime;
frameNum=0;
backgroundRefreshes = true;
}
if(change)
{
change=false;
estado=onKeyPress;
backgroundRefreshes = true;
}
if(estado==1)
pausa=true;
}
}
OpenProcessing is an online community platform devoted to sharing and discussing Processing sketches in a collaborative, open-source environment.
Download Processing
Terms of Service
To contact, send an email to:

See the feedback forum and vote!
Follow OpenProcessing on Twitter.
All sketches are licensed under Creative Commons Attribution-Share Alike 3.0.
Syntax highlighting and Processing brush under LGPL 3.
All the source code is licensed under Creative Commons GNU GPL.
Comments engine by Scriptsmill Comments Script.


