i can post the code here but very long
here is the main code that is calling all the classes
- /********************GLOBAL VARIABLES********************************************/
- // variable of type Rectangle:
- //This variable represents the game "board" - a game is played within the gameFrame...
- float r = random (0, 255);
- float g = random (0, 255);
- float b = random (0, 255);
- Rectangle gameFrame;
- // variable to hold an array of objects of type Brick
- Brick[] bricks;
- // variable of type Paddle that represents a paddle
- Paddle paddle;
- // variable to hold an array of objects of type Ball
- Ball[] balls;
- int frameNum = 0;
- // THE SCREEN PROPERTIES :
- int screenWidth = 800;
- int screenHeight = 400;
- color backgroundColor = #333F48;
- boolean backgroundRefreshes = true;
- // THE GAME FRAME PROPERTIES:
- int gameFrameWidth = 400;
- int gameFrameHeight = 400;
- color gameFrameStroke = #333F48;
- boolean gameFrameHasStroke = false;
- color gameFrameFill = #333F48;
- int opacityOfRefresh = 255;
- boolean gameFrameRefreshes = true;
- float spin = 0.0;
- int recX = (screenWidth-gameFrameWidth)/2;
- int recY = (screenHeight-gameFrameHeight)/2;
- /********************SET UP FUNCTION********************************************
- This function sets up the startup properties for a new game session
- - occurs before a new game starts
- ******************************************************************************/
- void setup() {
- size(screenWidth, screenHeight, P3D);
- background(r, g, b);
- frameRate(60);
- // Instantiate the necessary objects for a new game
- // create a rectanle object to represnt the game frame
- gameFrame = new Rectangle(gameFrameWidth, gameFrameHeight, gameFrameHasStroke, gameFrameStroke, true, gameFrameFill);
- gameFrame.opacity = opacityOfRefresh;
- // call the function to create the Bricks for a new game session
- createBricks();
- // call the function to create the Balls for a new game session
- createBalls();
- paddle = new Paddle();
- // call the function to refresh the screen
- refreshScreen();
- }
- /********************DRAW FUNCTION*******************************************
- This function is the function that starts a new game and the progam will
- continuously loop in this function for the duration of the program lifecycle
- - (until we quit a game)
- ***************************************************************************/
- void draw() {
-
- // translate(width, height/2, 50);
- // rotateY(map(mouseX, 0, width, 0, PI));
- // rotateX(map(mouseY, 0, height, 0, PI));
- // box(150);
- rotateX(radians(35));
- rotateZ(radians(10));
- translate(50,-170,20);
- rotateY(radians(25));
- // scale(.9);
- // rotateZ(radians(28));
- // translate(110, -200);
- // call the refreshScreen function
- //(will update all the necessary properties, objects and game state)
- refreshScreen();
- // call saveScreenShots()
- saveScreenshots();
- }
- /********************createBalls() FUNCTION**********************************
- This function is called when we want to define and create the balls.
- This function takes no arguements and does not return anything
- ***************************************************************************/
- void createBalls() {
- // Local Variables for Ball Properties
- //make changes here to the ball numbers
- int numberOfBalls = 30;
- int yBalls = 60;
- //assign the balls variable to be an array of Ball objects -
- //the array will hold "numberOfBalls" = 1 object ( have only 1 ball at the time in a game)
- balls = new Ball[numberOfBalls];
- for (int i=0; i<numberOfBalls; i++) {
- int x = i*20;
- /***** This line is fundamental: as it will create a new Ball object with the specified parameters
- and will put the instantiated Ball object into our "ball" array.************************************/
- balls[i] = new Ball(x, yBalls);
- }
- }
- /********************createBricks() FUNCTION**********************************
- This function is called when we want to define and create the bricks
- The function takes no arguments and does not return anything
- ****************************************************************************/
- void createBricks() {
- // Local Variables for Brick Group Properties
- int numberOfBricks = 80;//80 make change here
- int bricksPerRow = 6; //6 make change here
- int brickWidth = gameFrameWidth/bricksPerRow;
- int brickHeight = 20; //60 make change here
- boolean brickHasStroke = false;
- color brickStroke = #8E8B82; //60 make change here
- boolean brickHasFill = true;
- color brickFill = #ff0000; //60 make change here
- int yBricks = 50;
- //make change here
- color[] rowsColors = {
- #01DAF5, #A6D7DE, #ff9900, #ff9900, #00DAF5, #ff9900, #00DAF5
- };
-
- // Create the bricks
- //assign the brick variable to be an array of Brick objects - the array will hold "numberOfBricks" =60 objects
- bricks = new Brick[numberOfBricks];
- // use a for loop to put 60 Brick objects into the array named "brick"
- for (int i=0; i<numberOfBricks; i++)
- {
- // to keep track of the row we are on
- int rowNum = i/bricksPerRow;
- // coords for creating a brick
- int x = brickWidth*i;
- x -= rowNum*bricksPerRow*brickWidth;
- int y = yBricks+i/bricksPerRow*brickHeight;
- // variable used for determination of the colour of a brick
- // rowColors = an array of colors
- // min() is an inbuilt function which will return the minimum value between its two arguements
- int num = min(rowNum, rowsColors.length-1);
- // choose a colour based on the value of num
- color rowColor = rowsColors[num];
- /***** This line is fundamental: as it will create a new Brick object with the specified parameters
- and will put the instantiated Brick object into our "bricks" array.************************************/
- bricks[i] = new Brick(x, y, brickWidth, brickHeight, brickHasStroke, brickStroke, brickHasFill, rowColor);
- }
- }
- /********************refreshScreen() FUNCTION**********************************
- This function is called when we want to refresh the game properties and/or update
- the state of the game.
- This function takes no arguements and does not return anything
- ******************************************************************************/
- void refreshScreen() {
- // condition for refreshing the Background
- // check if this (global) boolean is TRUE...
- if (backgroundRefreshes) {
- background (backgroundColor);
- //background(r,g,b);
- }
- // condition for refreshing the GameFrame
- // check if this (global) boolean is TRUE...
- if (gameFrameRefreshes) {
- // call this function: located in the Rectangle class - because the gameFrame is an object of type Rectangle
- gameFrame.drawYourself();
- }
- // PADDLE
- // Update paddle Properties
- // call the refresh() function for the Paddle object (the function is in the Paddle class...)
- paddle.refresh();
- // BRICKS
- // Update the bricks array
- // within the for loop call the refresh() function for every Brick oject currently within the bricks array
- //(the function is within the Bricks class...)
- for (int i=0; i<bricks.length; i++) {
- bricks[i].refresh();
- }
- // BALLS
- //Update the balls array
- // within the for loop call the refresh() function for every Ball oject currently within the balls array
- //(the function is within the Ball class...)
- for (int i=0; i<balls.length; i++) {
- balls[i].refresh();
- }
- }
- /********************saveScreenShots() FUNCTION**********************************
- This function is for saving screen shots of the game ...
- Be careful with this function - only change if you know what you're doing
- the hard disk could fill up with images in a few minutes
- press the 'G' key to save frames in TGA pictures in 'saved' folder
- *******************************************************************************/
- void saveScreenshots() {
- frameNum++;
- if (keyPressed) {
- if (key == 'g' || key == 'G') {
- if (frameNum%2==0) {
- saveFrame("saved/frame-####.tga");
- }
- }
- }
- }
this is ball class
- // import the necessary library
- import java.awt.geom.*;
- /********************************************************************************
- Defintion of the Ball Class:
- The Ball class has dual functionality:
- First: It is used to create an instance of a ball object
- Second: A ball instance will detect if it is touching a brick,
- the paddle or the bounding limits of the game frame. Therefore the ball class contains
- the necessary functions for detecting any collision with the other above mentioned
- objects that are instantiated in the game session ...
- *********************************************************************************/
- public class Ball {
- // Define an a local variable "rectangle" to represent an object of type Rectangle
- Rectangle rectangle;
- // local Variables that represent the properties of a Brick object
- int width = 10; // make change here to the ball
- int height = 10; // make change here to the ball
- boolean hasStroke = true;
- color strokeColor = #550F48; //#333F48 make change here
- boolean hasFill = true;
- color fillColor = #102F48; //#333F48 make change here
- // velocity
- int velX = 4; //4 make change here
- int velY = 2; //4 make change here
- // positioning variables
- int x;
- int y;
- int ox;
- int oy;
- int xcentre;
- int ycentre;
- /********************************************************************************
- Ball Class Default Constructor:
- In order to create an Instance of a Ball Object, one needs to invoke
- the Default Constructor in the following manner: new Ball(tempX,tempY).
- The Ball Constructor makes an instance of the Rectangle Class.
- /********************************************************************************/
- Ball(int newX, int newY) {
- x = newX;
- y = newY;
- // make an instance of the Rectangle object (the ball is a rectangle...)
- rectangle = new Rectangle(width, height, hasStroke, strokeColor, hasFill, fillColor);
- // call the setPosition() function of the rectangle ( the function is within the Rectangle class...)
- rectangle.setPosition(x, y);
-
- }
- /********************************************************************************
- Function: refresh()
- Returns: nothing.
- Maintains the state of the given Ball on the Screen.
- The refresh() method calls the updatePosition() method, and there after
- the setPosition() and finally redraws itself to the screen (with the drawYourself()).
- **********************************************************************************/
- void refresh() {
- // call this function that is local to this class
- updatePosition();
- rectangle.setPosition(x, y);
- rectangle.drawYourself();
- // Remember that a Brick has been defined as an object rectangle of type Rectangle.
- // Therefore the drawYourself()&setPosition() functions are within the Rectangle Class...
- }
- /********************************************************************************
- Function: updatePosition()
- Returns: nothing.
- Calculates the "real" time positioing of the Ball. Within this function
- are also the calls to the function to check if the ball
- has collided with another object.
- *********************************************************************************/
- void updatePosition() {
- // add velocity to position- this simulates the ball movemnet( we update the x and y position of the ball...
- x+=velX;
- y+=velY;
- // collision with limits for "x"
- if (x<=0 || x>=gameFrameWidth-width) {
- // set the velocity for the x Position
- velX = -velX;
- // the constrain() will make sure that "x" does not go less than 0 and not greater than (gameFrameWidth-width)
- x = constrain(x, 0, gameFrameWidth-width);
- }
- // collision with limits for "y"
- if (y<=0 || y>=gameFrameHeight-height) {
- velY = -velY;
- y = constrain(y, 0, gameFrameHeight-height);
- }
- xcentre = x+width/2;
- ycentre = y+height/2;
-
- // We check if the ball is colliding with the paddle
- // input parameter is the "rectangle" variables defined in the Paddle class
- // Remember: the "paddle" is an object of type Paddle - and this class has an instance variable named "rectangle" ...
- // This function will return "result"
- // collision with paddle
- int result = checkCollisionWithRectangle(paddle.rectangle);
- // if collides on top, control direction of ball
- // if (result == 1){
- //if(xcentre < paddle.rectangle.x1+paddle.rectangle.width/2){
- // if(velX>0){
- // velX = -velX*2;
- // }
- // }else{
- // if(velX<0){
- // velX = -velX/3;
- //}
- //}
- // }
- //We detect collision with the bricks if the result is "0"
- if (result == 0) {
- // iterate through the array of Bricks
- for (int i=0; i<bricks.length; i++) {
- // Check if the brick is alive ( imAlive is a boolean - so if TRUE...)
- if (bricks[i].imAlive) {
- /*We check if the ball is collidiing with the paddle
- Input parameter is the "rectangle" variables defined in the Brick class
- Remember: the "bricks[i]" is an object of type Brick - and this class has
- an instance variable named "rectangle" (Every brick instance has its own "rectangle" var)*/
- // This function will return "res"
- int res = checkCollisionWithRectangle(bricks[i].rectangle);
- // if the returned "res" s not 0 then kill this brick.
- if (res != 0) {
- bricks[i].die();
- break;
- }
- }
- }
- }
- // keep track of the current x and y vars (make a copy)
- ox = x;
- oy = y;
- }
- /********************************************************************************
- Function: checkCollisionWithRectangle()
- Input Paramer: A Rectangle "tempRect"
- Returns: an integer:
- result: 0: no collision 1: top 2: right 3: bottom 4: left 5: couldn't detect which side
- *********************************************************************************/
- int checkCollisionWithRectangle (Rectangle R) {
- // define the result variable
- int result = 0;
- // check if the rectangle given (ie the paddle) is being touched by the ball
- // xcentre, ycentre are ball position variables
- // doesPointTouchMe() is located in the Rectangle class...
- // if it returns TRUE then execute this clause
- if (R.doesPointTouchMe(xcentre, ycentre)) {
- //check which side of the tempRect object has the ball touched.
- // This line is easier to work with when we want to check how
- //the ball has collided with the Rectangle object instead of using the ball itself...
- Line2D lineaBola = new Line2D.Float(xcentre, ycentre, ox+width/2, oy+height/2);
- // whatSideDoesLineTouch() is located in the Rectangle class...
- // if it returns TRUE then execute this clause
- result = R.whatSideDoesLineTouch(lineaBola, velX, velY);
- // have to perhaps change the position & velocity parameters of the ball
- // 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 = 20;
- }
- }
- // return the correct result ... also remember
- //if this result!=0 then the brick touched will die...
- return result;
- }
- /*********************************************************************************/
- }// End of Ball Class Definition
Brick class
- /********************************************************************************
- Defintion of the Brick Class:
- The Brick Class is used to define a Brick object in the game.
- It makes use of the Rectangle Class.
- **********************************************************************************/
- public class Brick
- {
- // Define an a local variable "rectangle" to represent an object of type Rectangle
- Rectangle rectangle;
- // local Variables that represent the properties of a Brick object
- boolean hasStroke = true; //make change here
- color strokeColor = 0; // make change here
- boolean hasFill = true; //make change here
- color fillColor = 157; // make change here
- //
- int x = 500; //make change here
- int y = 100; //make change here
- //
- boolean respawns = true; //make change here
- int timeToRespawn = 20; // time is in frames
- // time is in frames
- int frame;
- boolean imAlive;
- /********************************************************************************
- Brick Class Default Constructor:
- In order to create an Instance of a Brick Object, one needs to invoke
- the Default Constructor in the following manner:
- new Brick(rectX, rectY, rectWidth,rectHeight, hasStroke, strokeDef, hasFill, fillColour).
- The Brick Constructor makes an instance of the Rectangle Class.
- /********************************************************************************/
- Brick(int X, int Y, int W, int H, boolean HASSTROKE, color STROKE, boolean HASFILL, color FILL) {
- // make an instance of the Rectangle object
- rectangle = new Rectangle(W, H, HASSTROKE, STROKE, HASFILL, FILL);
- // call the setPosition() function of the rectangle ( the function is within the Rectangle class...)
- rectangle.setPosition(X, Y);
- imAlive = false;
- }
- /********************************************************************************
- Function: refresh()
- Returns: nothing.
- Maintains the state of the given Brick on the Screen.
- The refresh() method checks the state of the Brick (alive or dead)
- / if a new one should be created - if it is alive it will call the drawYourself().
- **********************************************************************************/
- void refresh() {
- if (imAlive) {
- // Remember that a Brick has been defined as an object rectangle of type Rectangle.
- // Therefore the drawYourself() function is within the Rectangle Class...
- rectangle.drawYourself();
- }
- else {
- if (respawns) {
- frame++;
- if (frame>timeToRespawn-10) {
- // rise up from your grave, brick
- imAlive=true;
- }
- }
- }
- }
- /********************************************************************************
- Function: die()
- Returns: nothing.
- Maintains the state of a Brick. If a Brick is "dead", it is removed from the screen.
- *********************************************************************************/
- void die() {
- imAlive = false;
- frame = 2;
- }
- }// End of Brick Class Definition
paddle class
/********************************************************************************
Defintion of the Paddle Class:
The Paddle Class is used to define the Paddle object in the game.
It makes use of the Rectangle Class.
*********************************************************************************/
public class Paddle {
- /********************************************************************************
- Paddle Class Instance Variables:
- These Instance Variables will be initialised(defined)when an Instance of the
- Paddle Class is invoked by calling the default constructor new Paddle().
- /*********************************************************************************/
- // Define an a local variable "rectangle" to represent an object of type Rectangle
- Rectangle rectangle;
- // Paddle Variables
- int width = 200;
- int height = 50;
- // boolean variables
- boolean hasStroke = false;
- boolean hasFill = true;
- // color variables make change here
- //#333F48
- color strokeColor = #102F48;
- //#333F48
- color fillColor = #100F48;
- //
- int x = gameFrameWidth/2;
- int y = 370;
- /********************************************************************************
- Paddle Class Default Constructor -
- In order to create an Instance of a Paddle Object, one needs to invoke
- the Default Constructor in the following manner new Paddle().
- The Paddle Class makes an instance of the Rectangle Class.
- *********************************************************************************/
- Paddle() {
- rectangle = new Rectangle(width, height, hasStroke, strokeColor, hasFill, fillColor);
- rectangle.setPosition(x, y);
- }
- /********************************************************************************
- Function: refresh()
- Returns: nothing.
- Maintains the Position of the Paddle on the Screen.
- The refresh() method call the updatePosition() method, and there after
- the setPosition() and redraws itself to the screen (with the drawYourself()).
- /******************************************************************************** */
- void refresh() {
- updatePosition();
- // Update the Position of the Rectangle (Paddle) Object
- // this function is in THIS class...
- rectangle.setPosition(x, y);
- // now set the position ...
- rectangle.drawYourself();
- // Remember that a Paddle has been defined as an object rectangle of type Rectangle.
- // Therefore the drawYourself() function is within the Rectangle Class...
- }
- /********************************************************************************
- Function: updatePosition()
- Returns: nothing.
- Calculates the "real" time positioning of the Paddle.
- *********************************************************************************/
- void updatePosition() {
- x = mouseX-recX-width/2;
- x = constrain(x, 0, gameFrameWidth-width);
- }
- /********************************************************************************/
- } // End of Paddle Class Definition
rectangle class
- // import the necessary library
- import java.awt.geom.*;
- /********************************************************************************
- Defintion of the Rectangle Class:
- The Rectangle Class is used to define every visible object in the game
- (The game frame, the bricks, the paddle and the ball)
- *********************************************************************************/
- public class Rectangle {
- int width;
- int height;
- //change here for the color
- boolean hasStroke = false;
- color strokeColor;
- boolean hasFill = true;
- color fillColor;
- color opacity;
- int x1;
- int y1;
- int x2;
- int y2;
- /********************************************************************************
- Rectangle Class Default Constructor -
- In order to create an Instance of a Rectangle Object,
- one needs to invoke the Default Constructor in the following manner:
- Rectangle myRectangle = new Rectangle(rectWidth, rectHeight, temp_hasStroke,temp_strokeColor,temp_hasFill,temp_fillColor)
- /*********************************************************************************/
- Rectangle(int widthOfRect, int heightOfRect, boolean hasStroke, color strokeColor, boolean fillIsHere, color tempFILL) {
- // set the values of the instance variables to the given arguements.
- width = widthOfRect;
- height = heightOfRect;
- hasStroke = hasStroke;
- strokeColor = strokeColor;
- hasFill = fillIsHere;
- fillColor = tempFILL;
- //change here 205
- opacity = 505;
- }
- /*********************************************************************************
- Function: setPosition(int posX, int posY)
- Returns: nothing.
- Set the position of the Rectangle Instance Object and defines the dimensions
- *********************************************************************************/
- void setPosition(int newX, int newY) {
- x1 = newX;
- y1 = newY;
- x2 = x1+width;
- y2 = y1+height;
- }
- /*********************************************************************************
- Function: drawYourself()
- Returns: nothing.
- The drawYourself() function is invoked each time the Rectangle is to be drawn on the Screen.
- The drawYourself() function also checks and applies styling parameters to the Rectangle
- i.e. Stroke, Fill and the of Dimensions a rectangle.
- It will also call the inbuilt rect() to draw the shape to the screen...
- *********************************************************************************/
- void drawYourself() {
- // Check if this Rectangle has to a stroke property enabled, if so, apply the stroke property.
- if (hasStroke) {
- stroke(strokeColor);
- }
- else {
- noStroke();
- }
- // Check if this Rectangle is fill property enabled, if so, apply the fill property.
- if (hasFill) {
- fill(fillColor, opacity);
- }
- else {
- noFill();
- }
- // Draw the Rectangle and enabled propoerties. make change here
- rect(recX+x1, recY+y1, width/2, height/2);
- }
- /*********************************************************************************
- Function: doesPointTouchMe()
- Returns: boolean.
- The doesPointTouchMe Method implements a collision detetction algorithm, if the points (pX, pY)
- are discovered to be within the bounds of the Rectangle Object(i.e a Brick, Paddle...).
- Then the object (ball object) represented by position (pX, pY), indeed touches that Rectangle object.
- *********************************************************************************/
- boolean doesPointTouchMe (int PX, int PY) {
- // Set the return variable to false, assume that the Rectangle is not Touched.
- boolean result = false;
- // Check the if pX is found inside the x coordinates of the current Rectangle
- if (PX >= x1 && PX <= x2) {
- // Check if pY is found inside the y coordinates of the current Rectangle
- if (PY >= y1 && PY <= y2) {
- // If we reach this statement, then the Rectangle has been Touched.
- result = true;
- }
- }
- return result;
- }
-
- /*********************************************************************************
- Function: whatSideDoesLineTouch()
- Input Parameters: tempLine (represents the side of the ball that has touched the other Rectangle object)
- velX, velY (velocity parameters of the ball)
- Returns: an int (represnts the side of the touched Rectangle oject) - touched BY the ball..
- This function determines which side of the Rectangle object that called this function (i.e. a Brick)
- has been touched by the Ball object
- /*********************************************************************************/
- int whatSideDoesLineTouch (Line2D LINE, int VELX, int VELY) {
- // local variable to represent the side
- Line2D side;
- // condition check based on velY value
- // top touched will return (1) and bottom touched will return(3)
- if (VELY>0) {
- // create an instance of a 2D Line
- side = new Line2D.Float(x1, y1, x2, y1);
- // check if our line representing the side of the "ball" intersects the side.
- // Note: the function intersectsLine is deined in the Line2D class which is from the
- // java.awt.geom library
- 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 touched will return (4) / right touched will return (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;
- }
- }