Pong: How does the paddle know where the ball will hit?

后端 未结 7 2056
旧巷少年郎
旧巷少年郎 2021-02-05 04:16

After implementing Pacman and Snake I\'m implementing the next very very classic game: Pong.

The implementation is really simple, but I just have one little problem rema

7条回答
  •  闹比i
    闹比i (楼主)
    2021-02-05 05:01

    after several iterations, if your paddle (that is yours or the AI's depending on your perspective) is too close to the top or bottom of the screen it is simply impossible to cover the distance required. The paddle can easily just follow the y-value of the ball. If you are too far you'll miss.

    Alternatively, you can have the AI reset to center after each hit, or travel toward the center as long as the ball is moving away (i.e. toward the opponent)

    Or more briefly:
    - move toward the center while the ball is moving away - imitate y-coordinate of ball while it is approaching.

    from here you can change the difficulty by making the y-coord imitation mechanism slower than the ball (more consistent, and probably what the original implementation was like; as difficulties are abstracted into simple speed coefficients), or adding random error to each movement or otherwise.

提交回复
热议问题