java——我正试图通过打乒乓球来学习javafx但是我不知道如何用键盘来移动划桨

kx1ctssn  于 2021-08-25  发布在  Java
关注(0)|答案(0)|浏览(174)

此问题已在此处找到答案

javafx:如何同时移动两个矩形((1个答案)
如何为javafx编写keylistener(3个答案)
两天前关门了。
我制作乒乓球游戏是为了学习java。我使用javafx来实现这一点,但为了我自己,我不知道如何控制桨。我想用“w”和“s”来表示上下,用“空格键”来开始游戏循环。是不是我的代码都搞乱了,我需要用更多的类来重做?我现在正处于一个困难的时刻,试图学习我需要做什么。谢谢你的帮助,这是我的代码。

import java.util.Random;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.event.EventHandler;
import javafx.util.Duration;

public class Pong extends Application {

    private static final int width = 1000;
    private static final int height = 1000;
    private static final int PLAYER_HEIGHT = 100;
    private static final int PLAYER_WIDTH = 15;
    private static final double BALL_RADIUS = 15;
    private int ballYSpeed = 1;
    private int ballXSpeed = 1;
    private double playerOneYPos = (height / 2) - PLAYER_HEIGHT;
    private double playerTwoYPos = (height / 2) - PLAYER_HEIGHT;
    private double ballXPos = width  / 2;
    private double ballYPos = height / 2;
    private int scorePlayer1 = 0;
    private int scorePlayer2 = 0;
    private boolean gameStarted;
    private int playerOneXPos = 15;
    private double playerTwoXPos = width - PLAYER_WIDTH;

    public void start(Stage stage) throws Exception {

        Image icon = new Image("pong_logo.jpeg");
        stage.setTitle("PONG");
        stage.getIcons().add(icon);

        Canvas canvas = new Canvas(width, height);
        GraphicsContext gc = canvas.getGraphicsContext2D();

        // Animation
        Timeline tl = new Timeline(new KeyFrame(Duration.millis(10), e -> run(gc)));
        tl.setCycleCount(Timeline.INDEFINITE);

        //Controls
        canvas.setOnKeyPressed(e -> {
            if (e.getCode() == KeyCode.W) {
                playerOneYPos++;
            }
        });

        canvas.setOnKeyReleased(e -> {
            if (e.getCode() == KeyCode.W) {
               playerOneYPos--; 
            }
        });

        stage.setScene(new Scene(new StackPane(canvas)));
        stage.show();
        tl.play();
    }

    private void run(GraphicsContext gc) {
        // set graphics
        // set background color
        gc.setFill(Color.BLACK);
        gc.fillRect(0, 0, width, height);

        // set text
        gc.setFill(Color.WHITE);
        gc.setFont(Font.font("Faster One", 50));

        if (gameStarted) {
            // set ball movement
            ballXPos += ballXSpeed;
            ballYPos += ballYSpeed;

            //ai();
            if (ballXPos < width - width / 4) {
                playerTwoYPos = ballYPos - PLAYER_HEIGHT / 2;
            } else {
                playerTwoYPos = ballYPos > playerTwoYPos + PLAYER_HEIGHT / 2
                    ?playerTwoYPos += 1:playerTwoYPos - 1;
            }

            //draw ball
            gc.fillOval(ballXPos, ballYPos, BALL_RADIUS, BALL_RADIUS);

        } else {
            // set start text
            gc.setStroke(Color.YELLOW);
            gc.setTextAlign(TextAlignment.CENTER);
            gc.strokeText("Press Space\n To Start!", width / 2, height / 2);

            //reset ball position
            ballXPos = width / 2;
            ballYPos = height / 2;

            // reset ball speed and direction
            ballXSpeed = new Random().nextInt(2) == 0 ? 1: -1;
            ballYSpeed = new Random().nextInt(2) == 0 ? 1: -1;
        }

        // walls to keep ball in the canvas
        if (ballYPos > height || ballYPos < 0) ballYSpeed *= -1;

        // playerTwo gets a point
        if (ballXPos < playerOneXPos - PLAYER_WIDTH) {
            scorePlayer2++;
            gameStarted = false;
        }

        // playerOne get a point
        if (ballXPos > playerTwoXPos + PLAYER_WIDTH) {
            scorePlayer1++;
            gameStarted = false;
        }
    }

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

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题