Results 1 to 1 of 1
  1. #1
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Talking My First Java Game just for a reference

    its a little buggy but it works.
    collisions method needs to check for elements at more than one point
    ie ball.getx , ball.gety plus the other four corners of the encompasing rectangle of the ball.. this is posted as reference to another thread so as not to confuse this code with a new implementation of the code.

    i'm gonna try to implement it using classes for the Ball, Brick, BrickFormation, Paddle etc,
    see how I get on here
    Breaking out of Breakout! making new classes


    Java Code:
    import acm.graphics.*;
    import acm.program.*;
    import acm.util.*;
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Breakout extends GraphicsProgram {
    
    	/** Width and height of application window in pixels */
    	public static final int APPLICATION_WIDTH = 400;
    	public static final int APPLICATION_HEIGHT = 600;
    
    	/** Dimensions of game board (usually the same) */
    	private static final double WIDTH = APPLICATION_WIDTH;
    	private static final double HEIGHT = APPLICATION_HEIGHT;
    
    	/** Dimensions of the paddle */
    	private static final int PADDLE_WIDTH = 60;
    	private static final int PADDLE_HEIGHT = 10;
    
    	/** Offset of the paddle up from the bottom */
    	private static final int PADDLE_Y_OFFSET = 30;
    
    	/** Number of bricks per row */
    	private static final int NBRICKS_PER_ROW = 10;
    
    	/** Number of rows of bricks */
    	private static final int NBRICK_ROWS = 10;
    
    	/** Separation between bricks */
    	private static final int BRICK_SEP = 4;
    
    	/** Width of a brick */
    	private static final double BRICK_WIDTH = (WIDTH - (NBRICKS_PER_ROW + 1)
    			* BRICK_SEP)
    			/ NBRICKS_PER_ROW;
    
    	/** Height of a brick */
    	private static final int BRICK_HEIGHT = 8;
    
    	/** Radius of the ball in pixels */
    	private static final int BALL_RADIUS = 5;
    	private static final int BALL_DIAM = BALL_RADIUS*2;
    
    	/** Offset of the top brick row from the top */
    	private static final int BRICK_Y_OFFSET = 70;
    
    	/** Number of turns */
    	private static final int NTURNS = 3;
    	
    	private int bricksLeft;
    	private int livesLeft;
    	private GOval ball;
    	private double xV;
    	private double yV;
    	private double delay;
    	private GRect paddle;
    	private double xPaddle;
    	private int ballReverseX;
    	private int paddleHits;
    	private GObject gobj;
    	private RandomGenerator rgen = new RandomGenerator();
    
    	/* Method: run() */
    	/** Runs the Breakout program. */
    	public void run() {
    		setUp();
    		initialiseLevel();
    		while(livesLeft > 0){
    			playGame();
    		}
    	}
    
    	private void setUp() {
    		setUpBricks();
    		setUpPaddle();
    		
    	}
    
    	private void setUpBricks() {
    	setBackground(Color.black);
    		for (int i = 0; i < NBRICK_ROWS; i++) {
    			Color colour = getRowColour(i);
    			rowOfBricks(i, colour);
    		}
    	}
    
    	private Color getRowColour(int i) {
    		switch (i) {
    		case 1:
    			return (Color.RED);
    		case 2:
    		case 3:
    			return (Color.ORANGE);
    		case 4:
    		case 5:
    			return (Color.YELLOW);
    		case 6:
    		case 7:
    			return (Color.GREEN);
    		case 8:
    		case 9:
    			return (Color.CYAN);
    		default:
    			return (Color.RED);
    		}
    	} 
    
    	private void rowOfBricks(int i, Color colour) {
    		for (int j = 0; j < NBRICKS_PER_ROW; j++) {
    			
    			GRect brick = new GRect(
    					BRICK_SEP + (j * (BRICK_WIDTH + BRICK_SEP)), BRICK_Y_OFFSET
    							+ (i * (BRICK_HEIGHT + BRICK_SEP)), BRICK_WIDTH,
    					BRICK_HEIGHT);
    			
    			brick.setFilled(true);
    			brick.setFillColor(colour);
    			brick.setColor(colour);
    			add(brick);
    			bricksLeft++;
    		}
    	}
    
    	
    	private void setUpPaddle() {
    		xPaddle = (WIDTH - PADDLE_WIDTH) / 2;
    		paddle = new GRect(xPaddle - PADDLE_WIDTH / 2, HEIGHT - PADDLE_Y_OFFSET
    				- PADDLE_HEIGHT, PADDLE_WIDTH, PADDLE_HEIGHT);
    		paddle.setFilled(true);
    		paddle.setColor(Color.WHITE);
    		add(paddle);
    		addMouseListeners();
    	}
    	public void mouseMoved(MouseEvent e) {
    		if((xPaddle) < APPLICATION_WIDTH){
    			paddle.move(e.getX() - xPaddle, 0);
    			xPaddle = e.getX();
    		}else{
    			xPaddle = APPLICATION_WIDTH-PADDLE_WIDTH;
    		}
    		
    	}
    	
    	private void initialiseLevel() {
    		xV = 2;
    		yV = 2;
    		delay = 10;
    		ballReverseX = 20;
    		paddleHits = 0;
    		livesLeft = NTURNS;
    	}
    	
    	private void playGame() {
    		getBall(rgen.nextInt(BALL_DIAM, (int)WIDTH - BALL_DIAM));
    		yV = -yV;
    		waitForClick();
    		while(true){
    			moveBall();
    			if(ball.getY() >= HEIGHT){
    				remove(ball);
    				break;
    			}
    			checkForCollision();
    			pause(delay);
    		}
    	}
    	
    	
    
    	private void getBall(double i) {
    		ball = new GOval(i, HEIGHT - PADDLE_Y_OFFSET
    				- BALL_DIAM - PADDLE_HEIGHT, BALL_DIAM, BALL_DIAM);
    		ball.setFilled(true);
    		ball.setColor(Color.WHITE);
    		add(ball);
    	}
    	
    	private void moveBall() {
    		ball.move(xV, yV);
    		if(ball.getX() >= WIDTH-BALL_DIAM || ball.getX() <= 0)xV = -xV;
    		if(ball.getY() <= 0)yV = -yV;
    		
    	}
    	
    	private void checkForCollision() {
    		gobj = getElementAt(ball.getX(), ball.getY());
    		if (gobj != null){
    			if (gobj == paddle){
    				hitTheBall();
    			}
    			else {
    				remove(gobj);
    				bricksLeft--;
    				yV = -yV;	
    			}
    		}	
    	}
    
    	private void hitTheBall() {
    		paddleHits++;
    		yV = -yV;
    		if ((xV > 1 && (ball.getX() + BALL_RADIUS) < xPaddle - ballReverseX))
    			xV = -xV;
    		if ((xV < 1 && (ball.getX() + BALL_RADIUS) > xPaddle + ballReverseX))
    			xV = -xV;
    		
    	}
    }
    Last edited by sonny; 04-21-2010 at 01:55 AM.
    :p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts

Similar Threads

  1. Replies: 3
    Last Post: 04-17-2009, 05:07 AM
  2. removing reference
    By ajith_raj in forum Advanced Java
    Replies: 4
    Last Post: 02-12-2009, 11:46 AM
  3. Very new to Java, how to reference an object
    By tornado in forum New To Java
    Replies: 3
    Last Post: 12-05-2008, 12:51 AM
  4. A Java reference source for a quick look
    By tapies in forum New To Java
    Replies: 6
    Last Post: 10-03-2008, 06:01 PM
  5. Getting the Object Reference Name
    By Deathmonger in forum New To Java
    Replies: 2
    Last Post: 03-12-2008, 02:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •