help with breakout game

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 10741 Views - Last Post: 30 July 2010 - 02:21 AM Rate Topic: -----

#1 Guest_fantobros*


Reputation:

help with breakout game

Posted 27 July 2010 - 03:25 PM

Hi,i have an assignment ,making breakout game.

my problem right now that i moves the paddle with the motion of the mouse using mousePressed and mouseDragged Methods ,but all the object on the screen can move like bricks and ball ..so how can i make only the paddle moves with the condition i want and the rest can't move ??
Is This A Good Question/Topic? 0

Replies To: help with breakout game

#2 bcranger   User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: help with breakout game

Posted 27 July 2010 - 04:08 PM

You will need to show us some code for us to diagnose your problem but I believe your problem is that you are redrawing more than just the paddle when there is a valid mouse event. You should only redraw the paddle when there is a valid paddle event. Leave the bricks, etc until they are to be removed.
Was This Post Helpful? 1
  • +
  • -

#3 m-e-g-a-z   User is offline

  • Winning
  • member icon


Reputation: 497
  • View blog
  • Posts: 1,457
  • Joined: 19-October 09

Re: help with breakout game

Posted 27 July 2010 - 04:14 PM

Could you post some code so we can see what the problem is as we are not Psychic. :)
Was This Post Helpful? 1
  • +
  • -

#4 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: help with breakout game

Posted 27 July 2010 - 04:19 PM

View Postm-e-g-a-z, on 27 July 2010 - 06:14 PM, said:

Could you post some code so we can see what the problem is as we are not Psychic. :)


Maybe you're not... ;)

@OP: I have a blog that starts out with MANY blog posts on game programming with Java. Give it a look! http://www.dreaminco...og/page__st__10
Was This Post Helpful? 1
  • +
  • -

#5 Guest_fantobros*


Reputation:

Re: help with breakout game

Posted 28 July 2010 - 01:12 AM

sure ,here is the code i wrote till now:-

/*
 * File: Breakout.java
 * -------------------
 * Name:
 * Section Leader:
 * 
 * This file will eventually implement the game of Breakout.
 */

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 int WIDTH = APPLICATION_WIDTH;
	private static final int 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 int 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 = 10;

/** 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;

/* Method: run() */
/** Runs the Breakout program. */
	public void run() {
		//Set Up The Bricks in the following code
		SetUpTheBricks();
		createthepaddle();
		addMouseListeners();
	}
	
	private void SetUpTheBricks(){
		for (int  i=0;i<10;i++){
			for (int j=0;j<10;j++){
		    	int x =1+BRICK_WIDTH*i+i*4;
		    	int y =BRICK_Y_OFFSET + (j*BRICK_HEIGHT)+4*j;
		         GRect brick = new GRect (x,y,BRICK_WIDTH,BRICK_HEIGHT);
		         if (j==0||j==1){
		    		   brick.setFilled(true);
		        	   brick.setFillColor(Color.RED);
		    		   add(brick);
		         }
		         else if (j==2||j==3){
		    		   brick.setFilled(true);
		        	   brick.setFillColor(Color.ORANGE);
		    		   add(brick);
		         }
		         else if (j==4||j==5){
		    		   brick.setFilled(true);
		        	   brick.setFillColor(Color.YELLOW);
		    		   add(brick);
		         }
		         else if (j==6||j==7){
		    		   brick.setFilled(true);
		        	   brick.setFillColor(Color.GREEN);
		    		   add(brick);
		         }
		         else {
		    		   brick.setFilled(true);
		        	   brick.setFillColor(Color.CYAN);
		    		   add(brick);
		         }
		         
		       } 
			}
		}
	 // create the paddle   
	public void createthepaddle() {
		GRect paddle = new GRect(60,10);
        paddle.setFilled(true);
        paddle.setFillColor(Color.BLACK);
        add(paddle,170,550);
		
	}
	// moving the paddle 
	public void mousePressed(MouseEvent e){
   	 last = new GPoint (e.getPoint());
   	 paddle = getElementAt(last);
    }
	     
	     public void mouseDragged(MouseEvent e) {
	    	  if(paddle !=null) {
	    		  paddle.move(e.getX() - last.getX(), 0);
	    		  last = new GPoint(e.getPoint());
	    	  }
	    	  if(paddle.getX() < 0){ // Sets the paddle to the leftmost side of the app window if the cursor leaves the window on the left.
	  			paddle.setLocation(0, 550);
	  		} 
	    	  else if((paddle.getX() + PADDLE_WIDTH) > WIDTH) { // Sets the paddle to the rightmost side of the app window if the cursor leaves the window on the right.
	  			paddle.setLocation((WIDTH - PADDLE_WIDTH), 550);
	  		}

	     }
	 
	  
private GObject paddle;
private GPoint last;
}



Was This Post Helpful? 0

#6 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: help with breakout game

Posted 28 July 2010 - 03:36 PM

Not sure many of us (the volunteers here) know what acm... is
Better to stay pure vanilla Java if you want help here
The standard Java API should have all the functionalities you require
Was This Post Helpful? 0
  • +
  • -

#7 YasuoDancez   User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 135
  • Joined: 30-September 09

Re: help with breakout game

Posted 28 July 2010 - 06:43 PM

The courses from Stanfords video tutorials on Programming Methodologies uses this ACM extension for the library as there way of teaching. I don't really like that they teach that way. I wish they had stayed with the Java API and used Swing instead of there own created libraries.
Other than that the video tutorials are great.
Was This Post Helpful? 0
  • +
  • -

#8 YasuoDancez   User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 135
  • Joined: 30-September 09

Re: help with breakout game

Posted 28 July 2010 - 06:49 PM

double

This post has been edited by YasuoDancez: 28 July 2010 - 06:52 PM

Was This Post Helpful? 0
  • +
  • -

#9 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: help with breakout game

Posted 28 July 2010 - 06:58 PM

View PostYasuoDancez, on 28 July 2010 - 07:43 PM, said:

The courses from Stanfords video tutorials on Programming Methodologies uses this ACM extension for the library as there way of teaching. I don't really like that they teach that way. I wish they had stayed with the Java API and used Swing instead of there own created libraries.
Other than that the video tutorials are great.

Yes, understand that but I am not to download it, if available, to fix fantobros problem
Was This Post Helpful? 0
  • +
  • -

#10 Luckless   User is offline

  • </luck>
  • member icon

Reputation: 293
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: help with breakout game

Posted 28 July 2010 - 08:02 PM

I have a tutorial on Dogstopper's link that might be of use to you. Many of the same concepts are involved in both Pong and breakout
Was This Post Helpful? 0
  • +
  • -

#11 Guest_fantobros*


Reputation:

Re: help with breakout game

Posted 28 July 2010 - 11:53 PM

Ok.Thanks anyway :D
Was This Post Helpful? 0

#12 YasuoDancez   User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 135
  • Joined: 30-September 09

Re: help with breakout game

Posted 29 July 2010 - 03:59 AM

View Postpbl, on 28 July 2010 - 05:58 PM, said:

View PostYasuoDancez, on 28 July 2010 - 07:43 PM, said:

The courses from Stanfords video tutorials on Programming Methodologies uses this ACM extension for the library as there way of teaching. I don't really like that they teach that way. I wish they had stayed with the Java API and used Swing instead of there own created libraries.
Other than that the video tutorials are great.

Yes, understand that but I am not to download it, if available, to fix fantobros problem


I was not saying that you should.
:)
Was This Post Helpful? 0
  • +
  • -

#13 Guest_fantobros*


Reputation:

Re: help with breakout game

Posted 29 July 2010 - 06:19 AM

btw there is big difference between Java API and acm package that stanford used??
Was This Post Helpful? 0

#14 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12316
  • View blog
  • Posts: 45,416
  • Joined: 27-December 08

Re: help with breakout game

Posted 29 July 2010 - 06:27 AM

There is a huge difference. Academic APIs like the ACM package are designed to further abstract the standard Java library to make it "easier" for students. In my opinion, it has the opposite effect of handicapping students into thinking these libraries are used in the real world, when they are not. And they really don't help students learn to program in terms of logic not just knowledge of the standard Java library, in my opinion.

I understand if you are required to use it. But our support for 3rd party libraries is limited to the knowledge and experience of those familiar with it, especially academic ones.
Was This Post Helpful? 1
  • +
  • -

#15 Luckless   User is offline

  • </luck>
  • member icon

Reputation: 293
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: help with breakout game

Posted 29 July 2010 - 07:18 AM

Where are our manners though,

@OP, in answer to your question about moving the paddle, set up a MouseMotionListener that gets the x coordinates of the pointer then sets the x coordinate of your paddle(within the bounds of your window or applet. To utilize this listener you simple .addMouseMotionListener(this) to your paddle object. I would avoid using the mouseDragged method and use the mouseMoved method
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2