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 :
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());
}
}