Need help with simple ping pong game

Discussion in 'Game programming' started by StressedStudent, Feb 18, 2012.

  1. StressedStudent

    StressedStudent New Member

    Joined:
    Feb 18, 2012
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi, I am new to the forum and as from my name you can guess why i signed up, i realise i am not as experianced as some poeple that may visit this site, actually i litterally only use eclipse and JAVA in my education but I am struggling to understand any of it, i have made it through simple tasks and this last one i cannot grasp. I am not looking for answers just explanations of what i need to do so I have some clue of what it means, I would appriciate readers taking a look at the following code an possibly tell me how to get it working, no fancy changes need to be made to the layout or game itself just the code, Thankyou for taking the time to help me out.
    Code:
     
    package engine;
    import java.awt.event.MouseMotionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.KeyEvent;
    import screens.GameConstants;
    import screens.PingPongGreenTable;
    /**
    * This class is a mouse and keyboard listener. 
    * It calculates ball and racket movements, 
    * changes their coordinates.
    */
    public class PingPongGameEngine implements Runnable, 
    MouseMotionListener, KeyListener, GameConstants{
     
    private PingPongGreenTable table;//reference to table
    private int kidRacket_Y = KID_RACKET_Y_START;
    private int computerRacket_Y=COMPUTER_RACKET_Y_START;
    private int kidScore; 
    private int computerScore;
    private int ballX; // ball's X position
    private int ballY; // ball's Y position
    private boolean movingLeft = true;
    private boolean ballServed = false;
     
    //Value in pixels of the vertical ball movement 
    private int verticalSlide; 
    // Constructor. Stores a reference to the table
    public PingPongGameEngine(
    PingPongGreenTable greenTable){
    table = greenTable;
    Thread worker = new Thread(this);
    worker.start();
    }
    // Methods required by MouseMotionListener 
    // interface (some of them are empty, but must be
    //included in the class anyway)
    public void mouseDragged(MouseEvent e) {
    }
     
    public void mouseMoved(MouseEvent e) {
    int mouse_Y = e.getY();
    // If a mouse is above the kid's racket and the racket did not go over the 
    // table top move it up, otherwise move it down 
    if (mouse_Y<kidRacket_Y && kidRacket_Y>TABLE_TOP){
    kidRacket_Y -= RACKET_INCREMENT;
    }else if (kidRacket_Y < TABLE_BOTTOM) {
    kidRacket_Y += RACKET_INCREMENT;
    }
     
    // Set the new position of the racket on the table 
    table.setKidRacket_Y(kidRacket_Y);
    }
     
    // Methods required by KeyListener interface
    public void keyPressed(KeyEvent e){
    char key = e.getKeyChar();
    if ('n' == key || 'N' == key){ 
    startNewGame();
    } else if ('q' == key || 'Q' == key){
    endGame();
    } else if ('s' == key || 'S' == key){
    kidServe();
    }
    }
    public void keyReleased(KeyEvent e){}
     
    public void keyTyped(KeyEvent e){}
     
    // Start a new Game 
    public void startNewGame(){
    computerScore=0;
    kidScore=0;
    table.setMessageText("Score Computer: 0 Kid: 0");
    kidServe();
    }
    // End the game
    public void endGame(){
    System.exit(0);
    }
    //Method run() is required by [URL=http://www.go4expert.com/articles/creating-java-thread-implementing-t4202/]Runnable interface[/URL] 
    public void run(){
     
    boolean canBounce=false; 
    while (true) {
     
    if(ballServed){ // if ball is moving
     
    //Step 1. Is ball moving o the left?
    if ( movingLeft && ballX > BALL_MIN_X){
     
    canBounce = (ballY >= computerRacket_Y && 
    ballY < (computerRacket_Y + RACKET_LENGTH)?
    true: false);
    ballX-=BALL_INCREMENT;
    // Add up or down slide to any left/right ball 
    // movement 
    ballY-=verticalSlide; 
    table.setBallPosition(ballX,ballY);
    // Can bounce? 
    if (ballX <= COMPUTER_RACKET_X && canBounce){
    movingLeft=false; 
    }
    }
    // Step 2. Is ball moving to the right?
    if ( !movingLeft && ballX <= BALL_MAX_X){
    canBounce = (ballY >= kidRacket_Y && ballY < 
    (kidRacket_Y + RACKET_LENGTH)?true:false);
     
    ballX+=BALL_INCREMENT;
    table.setBallPosition(ballX,ballY);
    // Can bounce? 
    if (ballX >= KID_RACKET_X_START && canBounce){ movingLeft=true;
    }
    }
     
    // Step 3. Move computer's racket up or down
    // to block the ball
    if (computerRacket_Y < ballY 
    && computerRacket_Y < TABLE_BOTTOM){
    computerRacket_Y +=RACKET_INCREMENT;
    }else if (computerRacket_Y > TABLE_TOP){
    computerRacket_Y -=RACKET_INCREMENT;
    }
    table.setComputerRacket_Y(computerRacket_Y);
    // Step 4. Sleep a little 
    try {
    Thread.sleep(SLEEP_TIME);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
     
    // Step 5. Update the score if the ball is in the 
    // green area but is not moving
    if (isBallOnTheTable()){
    if (ballX > BALL_MAX_X ){
    computerScore++;
    displayScore();
    }else if (ballX < BALL_MIN_X){
    kidScore++;
    displayScore();
    }
    } 
    } // End if ballServed 
    } // End while
    }// End run()
    // Serve from the current position of the kid's racket
    private void kidServe(){
    ballServed = true;
    ballX = KID_RACKET_X_START-1;
    ballY=kidRacket_Y;
    if (ballY > TABLE_HEIGHT/2){
    verticalSlide=-1;
    }else{
    verticalSlide=1;
    }
    table.setBallPosition(ballX,ballY);
    table.setKidRacket_Y(kidRacket_Y);
    }
    private void displayScore(){
    ballServed = false;
     
    if (computerScore ==WINNING_SCORE){
    table.setMessageText("Computer won! " +
    computerScore + ":" + kidScore);
    }else if (kidScore ==WINNING_SCORE){
    table.setMessageText("You won! "+ kidScore +
    ":" + computerScore);
    }else{
    table.setMessageText("Computer: "+ computerScore
    + " Kid: " + kidScore);
    }
    }
    // check if ball did not cross the top or bottom 
    // borders of the table
    private boolean isBallOnTheTable(){
    if (ballY >= BALL_MIN_Y && ballY <= BALL_MAX_Y){
    return true;
    }else {
    return false;
    }
    }
    }
     
    Last edited by a moderator: Feb 19, 2012

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice