Results 1 to 4 of 4
  1. #1
    gryd00 is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default Problem with KeyPressed any Help is appreciated

    :o im tryng to do the Breakout game done with the setup and now i decided to work on the Paddle controls and movement .. Now im stock ! dont know what really the Problem is. Code has no error . i think theres something wrong with the listeners algorithym ???? need Help!!! thanks in advance and more Power.
    here is the code :
    Java Code:
    import acm.program.*;
    import acm.graphics.*;
    import acm.util.*;
    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 GRect bricks;
    	private GOval ball;	// instance of the ball
    	private GRect paddle;
    	private int PaddleSpeed = 5;
    	
    	
    	public void run() {
    	getGCanvas().setSize(600, 800); // size of the application
    	setup();
    	addKeyListeners();
    	}
    	// paddle
    	public void Paddle(){
    		GRect paddle = new GRect(getWidth()/2,getHeight()-(PADDLE_HEIGHT + BALL_RADIUS ),PADDLE_WIDTH,PADDLE_HEIGHT);
    		paddle.setColor(Color.blue);
    		paddle.setFilled(true);
    		add(paddle);
    	}
    	// bricks set up
    	private void addBricks(){
    		Color [] colours = new Color[10];
    					colours[0] = Color.RED;
    					colours[1] = Color.RED;
    					colours[2] = Color.ORANGE;
    					colours[3] = Color.ORANGE;
    					colours[4] = Color.YELLOW;
    					colours[5] = Color.YELLOW;
    					colours[6] = Color.GREEN;
    					colours[7] = Color.GREEN;
    					colours[8] = Color.CYAN;
    					colours[9] = Color.CYAN;
    			 
    					int startX, startY = BRICK_Y_OFFSET;
    					GRect bricks;
    					// row = going downwards
    					for (int row =0; row < NBRICK_ROWS; row++){
    						startX = BRICK_SEP;
    						// column = going across
    						for (int col = 0; col < NBRICKS_PER_ROW; col++){
    							bricks = new GRect(startX,startY, BRICK_WIDTH,BRICK_HEIGHT);
    							bricks.setFillColor(colours[row]);
    							bricks.setFilled(true);
    							bricks.setColor(colours[row]);
    							add(bricks);
    							startX += BRICK_WIDTH + BRICK_SEP;
    						}
    						startY += BRICK_HEIGHT + BRICK_SEP;
    					}
    	}
    	// display needed object 
    	private void setup(){
    		Paddle();
    		GOval ball = new GOval((getWidth()+ PADDLE_WIDTH) /2,getHeight()-(PADDLE_HEIGHT + (BALL_RADIUS*2)),BALL_RADIUS,BALL_RADIUS);
    		ball.setFilled(true);
    		add(ball);
    		addBricks();
    		
    	}
    	public boolean keyDown (Event e, int key)
    	{
    
    	    // user presses left cursor key
    	    if (key == Event.LEFT)
    	    {
    	        // changing x - speed so that ball moves to the left side (x_speed negative)
    	    	PaddleSpeed = -PaddleSpeed;
    	    }
    	    // user presses right cursor key
    	    else if (key == Event.RIGHT)
    	    {
    	        // changing x - speed so that ball moves to the right side (x_speed positive)
    	    	PaddleSpeed = PaddleSpeed;
    	    }
    
    	    return true;
    
    	} 
    	// to end the game !
    	private boolean gameover(){
    		return(bricks == null) || (ball.getY() >= getHeight());
    	}
    	}
    Last edited by gryd00; 05-16-2011 at 09:29 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default

    Try adding some print statements to the code at various locations to show how variables change and where control flow goes. Then add more statements until you see what is going wrong.

    Since I don't have the acm packages, I can't do anything to help you debug the code.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    dont know what really the Problem is

    Could the problem be that the paddle doesn't move? That's just a guess: but I see you update the value of PaddleSpeed in response to key presses, but you don't use that variable anywhere. (paddleSpeed would be a better choice of variable.)

  4. #4
    gryd00 is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default Paddle is not moving !

    thanks guys. yes im working now on the paddle movement... i will try to add some println to see if the variable is responding .Hope it works ...
    :( variable paddleSpeed still is not responding to the listener when i press the arrow key it still 5: or the key.
    is there anyway i can do instead of this: Help is very much appreciated.. i want to move the paddle left or right using the arrow keys.. Thanks in advance

    Java Code:
    public boolean keyDown (Event e, int key)
    	{
    
    	    // user presses left cursor key
    	    if (key == Event.LEFT)
    	    {
    	        // changing x - speed so that ball moves to the left side (x_speed negative)
    	    	PaddleSpeed = -PaddleSpeed;
    	    }
    	    // user presses right cursor key
    	    else if (key == Event.RIGHT)
    	    {
    	        // changing x - speed so that ball moves to the right side (x_speed positive)
    	    	PaddleSpeed = PaddleSpeed;
    	    }
    
    	    return true;
    Last edited by gryd00; 05-17-2011 at 05:25 PM.

Similar Threads

  1. Java Calculator Help Would Be Greatly Appreciated
    By TommyR in forum New To Java
    Replies: 1
    Last Post: 03-14-2011, 09:30 PM
  2. Help with newb-project much appreciated
    By barr in forum New To Java
    Replies: 7
    Last Post: 12-11-2008, 11:14 AM

Posting Permissions

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