Results 1 to 4 of 4
- 05-24-2010, 04:59 AM #1
Member
- Join Date
- May 2010
- Posts
- 2
- Rep Power
- 0
need help with brick breaker (breakout)
I am completing brick breaker (breakout) as my project in computer class and I need some help in creating an Arraylist of bricks. I am also trying to figure out exactly how to create separate object classes for the bricks and maybe the paddle and balls. This is what I have.
Java Code:import java.applet.*; import java.awt.*; import java.awt.geom.*; import java.awt.event.*; public class BrickApplet extends Applet implements Runnable, KeyListener, MouseListener, MouseMotionListener { //These rectangles will be our paddles. private AnimatedShape paddle; private AnimatedShape ball; private AnimatedShape brick; private int player1Points; private int balls; private int numberOfLives = 3; private boolean gameOver; private boolean clickStart; //Thread controls the animation. private Thread brickAnimator; //delay(in milliseconds) will control the step by step // animation time. private int delay = 15; private int counter = 0; private AudioClip gameMusic; private AudioClip gameOverMusic; //This sets the size of the paddles and the ball and determines the // max points for the game (a.k.a. what a player must reach to win). public static final int BALL_DIAMETER = 10; public static final int PADDLE_WIDTH = 60; public static final int PADDLE_HEIGHT = 10; // public static final int MAX_BALLS = 3; public static final int BRICK_WIDTH = 50; public static final int BRICK_HEIGHT = 15; /** * Set up the initial values for the paddles and the ball. * This is the first method called when the Applet is running. */ public void init() { /********************************************************* * CODE TO BE ADDED BY YOU TO SET INITIAL POSITIONS FOR * BALL AND PADDLES. REMEMBER TO USE THE VARIABLES ABOVE. *********************************************************/ gameMusic = getAudioClip (getCodeBase(), "gamemusic.mid"); gameOverMusic = getAudioClip (getCodeBase(), "gameover.mid"); gameMusic.loop(); paddle = new AnimatedShape ( new Rectangle2D.Double (PADDLE_HEIGHT, this.getWidth()/2 - PADDLE_WIDTH/2, PADDLE_WIDTH, PADDLE_HEIGHT), PADDLE_HEIGHT, this.getHeight() - 20,//source PADDLE_WIDTH, PADDLE_HEIGHT, 0,0,0,0, //No direction or rise/run for paddles Color.BLACK, Color.RED); brick = new AnimatedShape ( new Rectangle2D.Double (10, 10, BRICK_WIDTH, BRICK_HEIGHT), this.getHeight()/50, this.getWidth()/50, BRICK_WIDTH, BRICK_HEIGHT, 0,0,0,0, //No direction or rise/run for paddles Color.BLACK, Color.BLUE); ball = new AnimatedShape ( new Ellipse2D.Double (this.getWidth()/2 - BALL_DIAMETER/2, this.getHeight()/2 - BALL_DIAMETER/2, BALL_DIAMETER, BALL_DIAMETER), this.getWidth() /2 - BALL_DIAMETER/2, this.getHeight()/2 - BALL_DIAMETER/2, BALL_DIAMETER, BALL_DIAMETER, 1,-1,2,2, //No direction or rise/run for paddles Color.BLACK, Color.YELLOW); //This code sets the Applet up to allow for and react to keyboard input. this.setFocusable(true); this.addKeyListener(this); this.addMouseListener(this); this.addMouseMotionListener(this); // this.setResizable (false) // this.setSize(300,300);//help from source //Set the player's points to 0. player1Points = 0; gameOver = false; clickStart = false; }//end init method /** * This method starts the animation and is called after the * init method is called. */ public void start() { brickAnimator = new Thread(this); brickAnimator.start(); } /** * This method stops the animation usually when the Applet is * closed. */ public void stop() { brickAnimator = null; } /** * This method sets what is painted on the screen. * * @param g this is the older Graphics object to be converted to * a Graphics2D object. */ public void paint(Graphics g) { //recover Graphics2D Graphics2D g2 = (Graphics2D) g; if(gameOver) { /******************************************************** * CODE TO BE ADDED BY YOU. WHAT DO YOU WANT TO APPEAR * ON THE SCREEN WHEN THE GAME IS OVER? HOW DO YOU TELL * WHO WON THE GAME? *******************************************************/ gameMusic.stop(); gameOverMusic.play(); if (counter == 3) g2.drawString ("YOU LOST :-( Try again next time.", this.getWidth()/2 - 30, this.getHeight()/2 - 6); }//end if else { /******************************************************** * CODE TO BE ADDED BY YOU. WHAT DO WE NEED TO PAINT * WHILE THE GAME IS STILL BE PLAYED? PROBABLY JUST * THE BALL, THE 2 PADDLES AND THE PLAYERS' SCORES. *******************************************************/ g2.setColor (paddle.getFillColor ()); g2.fill (paddle.getShape()); g2.setColor (paddle.getOutlineColor ()); g2.draw (paddle.getShape()); g2.setColor (ball.getFillColor ()); g2.fill (ball.getShape()); g2.setColor (ball.getOutlineColor ()); g2.draw (ball.getShape()); g2.setColor (Color.BLACK); g2.drawString ("Number of Lives : " + numberOfLives, 10, 280); g2.setColor (brick.getFillColor ()); g2.fill (brick.getShape()); g2.setColor (brick.getOutlineColor ()); g2.draw (brick.getShape()); }//end else }//end paint method /** * This method will be called after the start method is called. */ public void run() { //Check that the current thread is still our pongAnimator and // that the game is not over yet. while(Thread.currentThread() == brickAnimator && !gameOver) { /******************************************************** * THERE'S A LOT TO BE ADDED HERE INCLUDING: (1) CHECKING * IF THE BALL HAS HIT ONE OF THE WALLS; (2) CHECKING IF * THE BALL HAS HIT ONE OF THE PADDLES; (3) INCREMENTING * A PLAYER'S POINTS AND CHECKING IF A PLAYER HAS WON * AND THE GAME IS OVER; (4) AND FINALLY, UPDATING THE * BALL'S POSITION USING THE moveShape METHOD. *******************************************************/ if (clickStart) ball.moveShape(); //If ball hit top or bottom wall if (ball.getY() < 0) { ball.setY(0); ball.changeVerticalDirection(); } else if (ball.getY() + ball.getHeight() > this.getHeight()) { ball.setY(this.getHeight() - ball.getHeight()); ball.changeVerticalDirection(); } repaint(); //If ball hit left or right wall if (ball.getX() < 0) { ball.setX(0); ball.changeHorizontalDirection(); // player2Points++; // resetBall(); // clickStart = false; } else if (ball.getX() + ball.getWidth() > this.getWidth()) { ball.setX(this.getWidth() - ball.getWidth()); ball.changeHorizontalDirection(); // resetBall(); // clickStart = false; } if (paddle.isIntersecting (ball)) { ball.changeVerticalDirection(); // ball.setX (paddle.getX() + paddle.getWidth()); } if(ball.getY() > paddle.getY())//if the ball falls below the paddle { // g2.removeBall; resetBall(); clickStart = false; counter++; numberOfLives--; } if (counter == 3) gameOver = true; repaint(); //Have the Thread sleep for "delay" milliseconds. try { Thread.sleep(delay); } catch(InterruptedException e) { break; } }//end while loop }//end run metho /** * This method is activate when a key is pressed and held. * * @param e this object stores information about the KeyEvent. */ public void keyPressed(KeyEvent e) { //This code checks a key was pressed to move paddle. if(e.getKeyCode() == KeyEvent.VK_UP) { /****************************************************** * CODE TO BE ADDED BY YOU TO MOVE paddle UP. MAKE * SURE THAT THE PADDLE DIDN'T GO OFF THE SCREEN! *****************************************************/ }//end if else if(e.getKeyCode() == KeyEvent.VK_DOWN) { /****************************************************** * CODE TO BE ADDED BY YOU TO MOVE paddle DOWN. MAKE * SURE THAT THE PADDLE DIDN'T GO OFF THE SCREEN! *****************************************************/ }//end else if /* if (paddle2.getY () < 0) paddle2.setY(0); else if (paddle2.getY() + paddle2.getHeight() > this.getHeight()) paddle2.setY (this.getHeight() - paddle2.getHeight()); repaint(); */ }//end mousePressed method //METHOD DOES NOT NEED CODE FOR PONG! public void keyReleased(KeyEvent k) {} //METHOD DOES NOT NEED CODE FOR PONG! public void keyTyped(KeyEvent k) {} //METHOD DOES NOT NEED CODE FOR PONG! public void mouseExited(MouseEvent m) {} //METHOD DOES NOT NEED CODE FOR PONG! public void mouseEntered(MouseEvent m) {} //METHOD DOES NOT NEED CODE FOR PONG! public void mouseClicked(MouseEvent m) {} //METHOD DOES NOT NEED CODE FOR PONG! public void mousePressed(MouseEvent m) { clickStart = true; } //METHOD DOES NOT NEED CODE FOR PONG! public void mouseReleased(MouseEvent m) {} /** * This method is activate when the mouse is moved within the * Applet. * * @param m this object stores information about the MouseEvent. */ public void mouseMoved(MouseEvent m) { /****************************************************** * CODE TO BE ADDED BY YOU TO MOVE paddle2 UP OR DOWN. * BASED ON THE x AND y COORDINATES OF THE MOUSE. MAKE * SURE THAT THE PADDLE DOESN'T GO OFF THE SCREEN. *****************************************************/ paddle.setX (m.getX() - PADDLE_WIDTH/2); if (paddle.getX () < 0) paddle.setX(0); else if (paddle.getX() + paddle.getWidth() > this.getWidth()) paddle.setX (this.getWidth() - paddle.getWidth()); repaint(); } //METHOD DOES NOT NEED CODE FOR PONG! public void mouseDragged(MouseEvent m) {} public void resetBall () { // ball.setX (paddle.getX() + paddle.getWidth()/2 - ball.getWidth()/2); ball.setX (this.getWidth()/2 - ball.getWidth()/2); // ball.setY (paddle.getY() - 30); ball.setY (this.getHeight()/2 - ball.getHeight()/2); }//end resetBall method }//end PongApplet class
- 05-24-2010, 06:41 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
You need to break your code up and then ask more specific questions about specific sections of the code.
- 05-24-2010, 06:55 AM #3
Member
- Join Date
- May 2010
- Posts
- 2
- Rep Power
- 0
well here's a specific question, how can i use an Arraylist to create the b ricks necessary? Do I create a separate object class or what?
- 05-24-2010, 07:25 AM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
An ArrayList doesn't help to create the objects. It's a container that helps to store them.
You can read more about it and other collections here.
Similar Threads
-
Brick breaker game Code
By Mahesh Ratan in forum Java GamingReplies: 3Last Post: 03-22-2010, 02:21 PM -
Brick breaker game Code
By Mahesh Ratan in forum AWT / SwingReplies: 3Last Post: 03-22-2010, 01:13 PM -
Need help with Breakout game
By tfitz666 in forum New To JavaReplies: 9Last Post: 03-22-2010, 05:26 AM -
Breakout Game code help.
By Ceasar in forum New To JavaReplies: 6Last Post: 10-10-2009, 01:30 AM -
Another Breakout question
By jumpstart in forum New To JavaReplies: 3Last Post: 07-29-2009, 04:48 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks