让键盘控件在我的Pong游戏中工作

时间:2015-03-20 01:58:21

标签: java javafx user-controls scenebuilder pong

我的问题是如何修复我的代码,以便在运行应用程序时它将接受键盘输入。现在,我确实需要大部分代码,但我不确定我缺少什么才能让它工作。

现在请注意,这个程序是使用Eclipse Luna(最新版本)和使用Scenebuilder构建GUI的JavaFX制作的,所以请确保解决方案的所有内容都考虑到这些方面。

现在我的代码。另外,只要你能解释它,我只需要拿走我的代码并用更正来重新编写代码。只有一个或另一个不会像两者一样有效。

我的PongController类,其中包含所有内容

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.fxml.FXML;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.util.Duration;
import java.util.Random;

public class PongController {

final int PADDLE_MOVEMENT_INCREMENT = 7;
final int BALL_MOVEMENT_INCREMENT = 3;

double centerTableY;

DoubleProperty currentUserPaddleY = new SimpleDoubleProperty();
DoubleProperty currentComputerPaddleY = new SimpleDoubleProperty();
double initialComputerPaddleY;

DoubleProperty ballCenterX = new SimpleDoubleProperty();
DoubleProperty ballCenterY = new SimpleDoubleProperty();

double allowedPaddleTopY;
double allowedPaddleBottomY;

Timeline timeline;

@FXML
Rectangle table;
@FXML  
Rectangle compPaddle;
@FXML  
Rectangle userPaddle;
@FXML  
Circle ball;

public void initialize() {

    currentUserPaddleY.set(userPaddle.getLayoutY());
    userPaddle.layoutYProperty().bind(currentUserPaddleY);

    ballCenterX.set(ball.getCenterX());
    ballCenterY.set(ball.getCenterY());

    ball.centerXProperty().bind(ballCenterX);
    ball.centerYProperty().bind(ballCenterY);


    initialComputerPaddleY = compPaddle.getLayoutY();
    currentComputerPaddleY.set(initialComputerPaddleY);
    compPaddle.layoutYProperty().bind(currentComputerPaddleY);


    allowedPaddleTopY = PADDLE_MOVEMENT_INCREMENT;
    allowedPaddleBottomY = table.getHeight() - userPaddle.getHeight() - PADDLE_MOVEMENT_INCREMENT;

    centerTableY = table.getHeight()/2;
 }

public void keyReleasedHandler(KeyEvent event){

    KeyCode keyCode = event.getCode();

    switch (keyCode){
        case UP:
            process_key_Up();
            break;
        case DOWN:
            process_key_Down();
            break;
        case S:
            process_key_S();
            break;
    default:
        break;
    }
}


private void process_key_Up() {

    if (currentUserPaddleY.get() > allowedPaddleTopY) {
        currentUserPaddleY.set(currentUserPaddleY.get() - PADDLE_MOVEMENT_INCREMENT);
    }
}

private void process_key_Down() {

    if (currentUserPaddleY.get()< allowedPaddleBottomY) {
        currentUserPaddleY.set(currentUserPaddleY.get() + PADDLE_MOVEMENT_INCREMENT);
    }
}

private void process_key_S() {
    ballCenterY.set(currentUserPaddleY.doubleValue() + userPaddle.getHeight()/2);
    ballCenterX.set(userPaddle.getLayoutX());
    moveTheBall();
}

private void moveTheBall(){

    Random randomYGenerator = new Random();
    double randomYincrement = randomYGenerator.nextInt(BALL_MOVEMENT_INCREMENT);

    final boolean isServingFromTop = (ballCenterY.get() <= centerTableY)?true:false;

    KeyFrame keyFrame = new KeyFrame(new Duration(10), event -> {
        if (ballCenterX.get() >= -20) {
            ballCenterX.set(ballCenterX.get() - BALL_MOVEMENT_INCREMENT);

            if (isServingFromTop) {
                ballCenterY.set(ballCenterY.get() + randomYincrement);
                currentComputerPaddleY.set( currentComputerPaddleY.get() + 1);

            } 

            else {
                ballCenterY.set(ballCenterY.get() - randomYincrement);

                currentComputerPaddleY.set(currentComputerPaddleY.get() - 1);
            }

            if(checkForBallPaddleContact(compPaddle)){
                timeline.stop();
                currentComputerPaddleY.set(initialComputerPaddleY);
                bounceTheBall();
            };

         } 
        else {
            timeline.stop();
            currentComputerPaddleY.set(initialComputerPaddleY);
         }
        updateScore();
     });

    timeline = new Timeline(keyFrame);
    timeline.setCycleCount(Timeline.INDEFINITE);

    timeline.play();
 }


 private boolean checkForBallPaddleContact(Rectangle paddle){
     if (ball.intersects(paddle.getBoundsInParent())){
         return true;
     } else {
          return false;
     }
 }

 private void bounceTheBall() {
     double theBallOffTheTableX = table.getWidth() + 20;

     KeyFrame keyFrame = new KeyFrame(new Duration(10), event -> {

         if (ballCenterX.get() < theBallOffTheTableX) {
             ballCenterX.set(ballCenterX.get() + BALL_MOVEMENT_INCREMENT);
             if (checkForBallPaddleContact(userPaddle)){
                 timeline.stop();
                 moveTheBall();
             };

         } 
         else {
             timeline.stop();
         }
         updateScore();
     });

     timeline = new Timeline(keyFrame);
     timeline.setCycleCount(Timeline.INDEFINITE);

     timeline.play();
   }

 private void updateScore(){
     int computerScore = 0;
     int userScore = 0;
      if (ballCenterX.get() > table.getWidth()){
          // Computer bounced the ball and the User didn't hit it back
          computerScore ++;
      } else if (ballCenterY.get() > 0 && ballCenterY.get() <= table.getHeight()){
          // The User served the ball and Computer didn't hit it back
          userScore++;
      } else{
          // The User served the ball off the table
          computerScore++;
      }


      System.out.println("Computer: " + computerScore + ", User: " + userScore);
    }
}

这是我的主要课程

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.fxml.FXMLLoader;

public class PongMain extends Application {

@Override
public void start(Stage primaryStage) {
    try {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(PongMain.class.getResource("GUI.fxml"));
        Group root = (Group) loader.load();
        Scene scene = new Scene(root,400,250);

        primaryStage.setScene(scene);
        primaryStage.show();
    } catch(Exception e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) {
    launch(args);
}
}

我只想提前感谢,这是第一次使用本网站,虽然我过去曾使用过这个网站来获取想法。

0 个答案:

没有答案