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

后端 未结 7 2054
旧巷少年郎
旧巷少年郎 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条回答
  • 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.

    0 讨论(0)
  • 2021-02-05 05:02

    We made a (pseudo-)3D ping-pong game for our high school CS class. What we did was, we made the computer always move the paddle toward the ball, but with a maximum speed -- that way, it could miss the ball if it's too far, but it's still smart. Does this help?

    0 讨论(0)
  • 2021-02-05 05:07

    I would think you should have the paddle always move from its current position to a specific point, which would be where the ball is expected to align vertically with the paddle. You could then have a 50% chance that the paddle will move to this exact point and deflect the ball, a 25% chance that it will overshoot, perhaps by some X pixels, and 25% chance that it will undershoot. An even better way to do it might be to have it move to that position on a bell curve so that it misses by different amounts each time.

    0 讨论(0)
  • 2021-02-05 05:13

    It just so happens I wrote a pong clone the other day just for fun.

    You can play it here and view the source code here.

    The AI takes the ball's current speed and multiplies the x-distance away from the wall. It then moves towards that calculated position at a capped speed. It doesn't account for vertical bounces, but that's sort of intended (to make the AI beatable).

    Here is the relevant snippet:

    /* Update enemy based on simple AI */
    enemy.update = function (delta) {
        var impactDistance, impactTime, targetY, speed;
        speed = .25; // a little slower than the human player
    
        if (ball.vx < 0) {
            // Ball is moving away, AI takes a nap ..
            return;
        }
    
        // Figure out linear trajectory ..
        impactDistance = width - ball.width - ball.x;
        impactTime = impactDistance / (ball.vx * .25 * 1000);
        targetY = ball.y + (ball.vy * .25 * 1000) * impactTime;
    
        if (Math.abs(targetY - (this.y + this.height/2)) < 10) {
            // AI doesn't need to move
            return;
        }
    
        if (targetY < this.y + (this.height / 2)) {
            // Move up if ball is going above paddle
            speed = -speed;
        }
    
        this.y += speed * delta;
        this.keepInBounds();
    };
    
    0 讨论(0)
  • 2021-02-05 05:14

    Other answers discuss how to decide the correct direction to move in in different circumstances. You can also add a lag time before the computer player "reacts" by starting to move in this direction -- that mirrors the typical response of human players.

    0 讨论(0)
  • 2021-02-05 05:17

    I'm not sure what the 'official' way to do this is, but I've always just used (pseudocode)

    if (ball is above paddle) {
        move paddle up
    }
    if (ball is below paddle) {
        move paddle down
    }
    

    Then I gave the paddle a slightly varying speed that was slow enough that it couldn't always keep up with the ball. Kind of crude, but it works.

    This thread also has some interesting ideas you might want to look at: http://www.gamedev.net/community/forums/topic.asp?topic_id=439576

    0 讨论(0)
提交回复
热议问题