Turn your Mobile Apps into m-commerce apps – Learn More!

You're Browsing As A Guest! Register Now...
Become a Java Expert!

Join 416,731 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,917 people online right now.Registration is fast and FREE... Join Now!



Page 1 of 1

Breakout java code help I need someone to check my code and help with the menu opions on my co Rate Topic: -----

#1 cchristians14  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 09-November 08


Dream Kudos: 0

Share |

Breakout java code help

Post icon  Posted 09 November 2008 - 06:21 PM

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class Breakout implements ActionListener, Runnable, MouseMotionListener
{

	/** Width of the game display, which doubles as the court */ 
	private final int WIDTH = 400;
	private final int HEIGHT = 500;

	/** Pause time in milliseconds */
	public int pauseTime = 8;
	
	/** the game board */
	private GCanvas gc;

	/** the game ball */
	private GOval ball, ballx;
	private final static int DIAMETER = 25;
	
	/** the game paddle */
	private GRect paddle;
	
	/** the game bricks */
	private GRect brick;
	
	/** displacement vectors */
	private int dx;
	private int dy;
	
	private JButton serveButton;
	private JFrame myGUI;
	MouseMotionListener e;
	Random r = new Random();
	
	/** menu items */
	JMenuBar menus;
	JMenu file;
	JMenuItem start, quit;
	JMenuItem restartItem;
	
/****************************************************
Sets up the game like usual.
****************************************************/
public Breakout() {

	// create the GUI window
	myGUI = new JFrame();
	myGUI.setTitle("Courtney's Simple Breakout Game");
	myGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	myGUI.setLocation(100,100);
	myGUI.setSize(WIDTH, HEIGHT);
	myGUI.setLayout(new BorderLayout());

	// create a GCanvas and register the Mouse Listener
	gc = new GCanvas();
	myGUI.add(gc);
	gc.addMouseMotionListener(this);
	
	// set up the serve button
	serveButton = new JButton("Serve");
	serveButton.addActionListener(this);
	myGUI.add(BorderLayout.SOUTH, serveButton);

	// set up File menu
	JMenuBar menus = new JMenuBar();
	file = new JMenu("File");
	menus.add(file);
	myGUI.setJMenuBar(menus);
	start = new JMenuItem("Start");
	quit = new JMenuItem("Quit");
	restartItem = new JMenuItem("Restart");
	file.add(start);
	start.addActionListener(this);
	file.add(quit);
	quit.addActionListener(this);
	file.add(restartItem);  
	restartItem.addActionListener(this);
	
	//paddle and bricks are placed on gameboard
	makePaddle();
	makeBricks();
	makeBall();
	myGUI.add(gc);
	myGUI.setVisible(true);
	myGUI.validate();

	
}

/****************************************************
Set the ball in play until the player misses or the
game is over.
****************************************************/
private void playRound() 
{
   // add a ball to the Gcanvas
   makeBall();  

   // set initial values for the two displacement vectors (X and Y)
   dx = r.nextInt(3)-1;
   dy = r.nextInt(3)+1;
   pauseTime = 8;
   
   // for starters, loop forever and have a ball bounce around
  while (isBallInPlay()){//loop checks internal method that ball is 
						 //playable and on the gameboard
	   if(ball.getX() > gc.getWidth() - DIAMETER)//if ball hits the right
		  dx = dx  - 1;  //wall of the gameboard, change the ball direction
	   if(ball.getX() <= 0)//if the ball hits the left wall
		  dx = dx + 1;   //change ball direction
	   if(ball.getY() <= 0)//if the ball hits the top of the gameboard
		  dy = dy + 1;   //change ball direction
	   if(ball.getY() > paddle.getY())
		  gc.remove(ball);
	   
	   checkPaddleBounces();//internal method call	 
	   checkBrickBounces(); //internal method call 
	   
	   ball.move(dx, dy);//ball's moves set by displacement vectors dx, dy
	   pause(pauseTime);//slows game movements to accomodate faster processors
	}
}
		
	//make ball
	private GOval makeBall()
	{
		ball = new GOval(100 + DIAMETER, 100 + DIAMETER, DIAMETER, DIAMETER);
		ball.setFilled(true);
		ball.setColor(Color.GREEN);
		gc.add(ball);
		ball.setLocation(100, 250);
		return ball;
		
	}
	
	//make paddle
	public void makePaddle()
	{
		paddle = new GRect(70,10);
		paddle.setFilled(true);
		paddle.setColor(Color.magenta);
		gc.add(paddle);
		paddle.setLocation(100,410);
		myGUI.setVisible(true);
		myGUI.validate();
	}
	
	private void makeBrick(int x, int y)
	{
		GRect b1 = new GRect(50,10);
		b1.setFilled(true);
		b1.setColor(Color.YELLOW);
		gc.add(b1);
		b1.setLocation(x,y);
		b1.setVisible(true);
	}
   
	private void makeBricks()
	{
		int n = 0;
		int ROWS = 8;
		int COLUMNS = 4;
		for(int m = 0; m <= COLUMNS;  m++){
			for(int l = 0; l <= ROWS; l++){
				makeBrick((2+51)*l,(10+11)*m);
			}
		}		
	}
   
		
	private void checkPaddleBounces()
{
	int hits = 0;//count the ball hitting the paddle
	
	//each if statement checks the corners of the ball
	if(paddle.contains(ball.getX()+DIAMETER, ball.getY()+DIAMETER)){
			dx = dx - 1;
			dy = dy - 1;
		   }//end if loop
	else if(paddle.contains(ball.getX(), ball.getY())){
			dx = dx + 1;
			dy = dy -1;
			}//end if loop
	else if(paddle.contains(ball.getX()+DIAMETER, ball.getY())){
			dx = dx - 1;
			dy = dy - 1;
			}//end if loop
	else if(paddle.contains(ball.getX(), ball.getY()+DIAMETER)){
			dx = dx + 1;
			dy = dy - 1;
		}//end if loop 
	for(int p = 0; p < hits; p++){//have the ball speed up after 10 hits
		hits++; //count each hit
		if(hits%10 == 0){//find the tenth hit using modulus
			dx++;//increment displacement vectors
			dy++;
		}//end if loop
	}//end for loop	 
}//close
	
private boolean isBallInPlay()
{
	int turnsleft = 0;//player gets three turns to play, this int counts down 
					  //the number of turns
	boolean inPlay = true;//if true, the ball is in play and game continues
	
	if(ball.getY() >  paddle.getY()){//if the ball falls below the paddle
		gc.remove(ball);//remove the ball and . .
		turnsleft = turnsleft++;//decrease players turns by one
		   if(turnsleft == 3){//check if player is out of turns
			   gc.remove(paddle);//end the game by removing the paddle, 
			   serveButton.setEnabled(false);//disable the serve button and . .
			   System.out.print("Sorry, Game Over!");//tell the player
		}//close if statement
		return false;//false return tells method the ball is not in play
	}//close if
	if(gc.getElementCount() == 2){//if the ball and paddle are the only 
								  //elements left on the board, the player wins
			System.out.print("Congratulations!  You won!");
			gc.remove(ball);
			for(int i = 0; i < r.nextInt(50); i++){
			ballx = new GOval(r.nextInt(100), r.nextInt(100), DIAMETER, DIAMETER);
			ballx.setColor( new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256)));
			ballx.setFilled(true);
			gc.add(ballx);
			dx = dx + r.nextInt(3);
			dy = dy - r.nextInt(3);
			 }
			return false;//return tells the method that the ball is not in play
		}
	
	return true;//for any other event the ball is still in play and playRound 
				//loop continues
} 
private void checkBrickBounces()
{
	//create local variables for each corner of big_ball interacting
	//with any element on the gameboard
	GObject thisBrick1 = gc.getElementAt(ball.getX(), ball.getY());
	GObject thisBrick2 = gc.getElementAt(ball.getX() + DIAMETER,
						ball.getY() + DIAMETER);
	GObject thisBrick3 = gc.getElementAt(ball.getX() + DIAMETER, ball.getY());
	GObject thisBrick4 = gc.getElementAt(ball.getX(), ball.getY() + DIAMETER);
	
	//each if loop checks that local variables are interacting with brick elements
	//(not nothing/null), and that the element is also not the paddle.  Then
	//the loop bounces the ball off of that element and removes the element
	if(thisBrick1 != null && thisBrick1 != paddle){
		dx = - 1;
		dy = - 1;
		gc.remove(thisBrick1);
	}
	else if(thisBrick2 != null && thisBrick2 != paddle){
		dx = - 1;
		dy = - 1;
		gc.remove(thisBrick2);
	} 
	else if(thisBrick3 != null && thisBrick3 != paddle){
		dx = - 1;
		dy = - 1;
		gc.remove(thisBrick3);
	}
	else if(thisBrick4 != null && thisBrick4 != paddle){
		dx = - 1;
		dy = - 1;
		gc.remove(thisBrick4);
	}
}	   
		
			
/****************************************************
Move the Paddle as the Mouse moves
****************************************************/
public void mouseMoved(MouseEvent e)
	{
	   if(e.getX() > WIDTH - paddle.getWidth()){//if loop keeps paddle on the board
		  paddle.setLocation(WIDTH - paddle.getWidth(), paddle.getY());
	}else{
		paddle.setLocation(e.getX(), paddle.getY());//otherwise the paddle location
	}	   //follows the mouse on X axis. Y position is set in createBoard method
}

public void mouseDragged(MouseEvent e){
   // no need to complete this method
}

/****************************************************
Respond to a button click
Do not perform any actions directly in
this method.  Instead, invoke a new thread so this
method will return immediately.
****************************************************/
public void actionPerformed(ActionEvent e){

   JComponent itemPressed = (JComponent) e.getSource();
	// create a new thread and indirectly call the run() method
	
	// quit the game
	if (e.getSource() == quit){//player selects quit in file on menu bar
		System.exit(1);//
	 
	}
	// start a new game	
	 if (e.getSource() == start){//player selects start in file on menu bar
		   System.exit(1);//reset the gamoard
		}
	// serves a ball by invoking the run( ) method	
	 if (itemPressed == serveButton){
			Thread theThread = new Thread(this);
			theThread.start();
   }
}




/****************************************************
A new round is started and runs in a separate thread.

No need to change this method.
****************************************************/
public void run(){
   playRound();
}

/****************************************************
This will pause the current thread the provided
number of milliseconds.

No need to change this method.
****************************************************/
private void pause(int msecs){
   try{
	  Thread.sleep(msecs);
  }catch (InterruptedException e){
	  // ignore the exception
  }
}

/****************************************************
Used to simplify testing

No need to change this method.
****************************************************/
public static void main(String[] args){
	new Breakout();
}

}



*Edited to add the [ code] tags in the furure please :code:

This post has been edited by pbl: 09 November 2008 - 06:50 PM

Was This Post Helpful? 1
  • +
  • -


#2 pbl  Icon User is offline

  • Java Lover who loves defau1t:
  • Icon

Reputation: 2130
  • View blog
  • Posts: 13,803
  • Joined: 06-March 08


Dream Kudos: 550

Re: Breakout java code help

Posted 09 November 2008 - 06:53 PM

Welcome at DIC and please follow the rules
And :code:
And what do you have a problem with ?
- code does not compile (what are the error messages ?)
- code compiles but I have a run time error (which error ?)
- code runs but does not have the expected behaviour (what is happening what do you expected ?)
Was This Post Helpful? 0
  • +
  • -

#3 manuelfloresjr  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 01-July 10


Dream Kudos: 0

Re: Breakout java code help

Posted 03 July 2010 - 01:54 AM

View Postpbl, on 09 November 2008 - 06:53 PM, said:

Welcome at DIC and please follow the rules
And :code:
And what do you have a problem with ?
- code does not compile (what are the error messages ?)
- code compiles but I have a run time error (which error ?)
- code runs but does not have the expected behaviour (what is happening what do you expected ?)


**************************
Hi good day
I noticed that the source code for GCanvas, GOval, and GRect classes were not included in the code provided.
I would like to understand the behavior of each class and would like to ask if you can give at least the API of these classes.


Thank you so much, sir... I would really appreciate it. :)
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • Icon

Reputation: 1916
  • View blog
  • Posts: 10,357
  • Joined: 27-December 08


Dream Kudos: 1750

Expert In: Java, Data Structures, Economics

Re: Breakout java code help

Posted 03 July 2010 - 01:08 PM

As this thread is almost two years old and the OP only has one post, I doubt they will be checking back anytime soon or that they even have the source code still.

Also, please avoid necroposting in the future.

Topic closed.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users