Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 爪哇乒乓球游戏从球拍上弹起球_Java_Game Physics - Fatal编程技术网

Java 爪哇乒乓球游戏从球拍上弹起球

Java 爪哇乒乓球游戏从球拍上弹起球,java,game-physics,Java,Game Physics,我想用爪哇做一个乒乓球游戏,但我有一个从球拍上弹起球的小问题 我有两个桨:左桨1,右桨2 我的想法如下:球的高度应在桨和球的高度之间,接触点应为桨宽PP1或PP2+/-球半径 球拍2工作正常,但球穿过球拍1,我看不出我的错误 下面是它的样子的视频 以下是桨叶弹跳部分: if(ball_Y > P1_Y && ball_Y < P1_Hit_Y){ if(ball_X < P1_Hit_X){ ball_V

我想用爪哇做一个乒乓球游戏,但我有一个从球拍上弹起球的小问题

我有两个桨:左桨1,右桨2

我的想法如下:球的高度应在桨和球的高度之间,接触点应为桨宽PP1或PP2+/-球半径

球拍2工作正常,但球穿过球拍1,我看不出我的错误

下面是它的样子的视频

以下是桨叶弹跳部分:

if(ball_Y > P1_Y && ball_Y < P1_Hit_Y){
            if(ball_X < P1_Hit_X){

                ball_Velocity_X *= -1;

                ball_X = P1_Hit_X;
            }
        }

        if (ball_Y > P2_Y && ball_Y < P2_Hit_Y){
            if(ball_X > P2_Hit_X){

                ball_Velocity_X *= -1;

                ball_X = P2_Hit_X;


            }
        }
以下是所有代码:

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.*;


public class PongGame extends GameEngine{

    public static void main(String args[]) {
        // Warning - don't edit this function.

        // Create a new game.
        createGame(new PongGame());
    }

    int game_width, game_height;
    double PP1_width, PP1_height, PP2_width, PP2_height;
    double P1_X, P1_Y, P2_X, P2_Y;
    double PP_Velocity;
    boolean PP1_UP, PP1_DOWN, PP2_UP, PP2_DOWN;
    double ball_X, ball_Y, ball_Radius, ball_Velocity_X, ball_Velocity_Y;
    double P1_Hit_X, P1_Hit_Y, P2_Hit_X, P2_Hit_Y;
    AudioClip wall_bounce;







    //TODO: Create paddles and a ball
    //Two paddles for two players: They will be controlled with W-S and UP-DOWN Keys
    public void paddle1(){
        drawSolidRectangle(P1_X, P1_Y, PP1_width, PP1_height);

    }
    public void paddle2(){
        drawSolidRectangle(P2_X, P2_Y, PP2_width, PP2_height);

    }

    public void ball(){
        drawSolidCircle(ball_X, ball_Y, ball_Radius);
    }

    //TODO: Update Paddles
    //DONE: Sources from PhysicsDemo lecture
    /**
     * These methods allow us the update the window with each framerate so we will be able to move the 
     * paddles
     */
    public void paddle1_update(double dt){
        // Paddle 1 Up with W
        if(PP1_UP == true){
            P1_Y -= PP_Velocity * dt; 

            // Check paddle inside wall
            if((P1_Y < 0) ) {
                P1_Y = 0;

            }
        }
        // Paddle 1 Down with S
        if(PP1_DOWN == true){
            P1_Y += PP_Velocity * dt; 

            // Check paddle inside wall
            if(P1_Y > game_height-PP1_height){
                P1_Y = game_height-PP1_height;
            }

        }





    }
    public void paddle2_update(double dt){

         // Paddle 2 Up with Up Key
         if(PP2_UP == true){
            P2_Y -= PP_Velocity * dt; 

            // Check paddle inside wall
            if((P2_Y < 0) ) {
                P2_Y = 0;

            }
        }
        // Paddle 2 Down with Down Key
        if(PP2_DOWN == true){
            P2_Y += PP_Velocity * dt; 

            // Check paddle inside wall
            if(P2_Y > game_height-PP2_height){
                P2_Y = game_height-PP2_height;
            }
        }

    }

    public void ball_update(double dt){
        ball_X += ball_Velocity_X * dt;
        ball_Y += ball_Velocity_Y * dt;

        // Bouncing the ball from edge of the wals
        if(ball_Y < ball_Radius) {
            ball_Velocity_Y *= -1;
            //Play wall bounce sound
            playAudio(wall_bounce);
        }else if (ball_Y > (game_height - ball_Radius)){
            ball_Velocity_Y *= -1;
             //Play wall bounce sound
            playAudio(wall_bounce);

        } 



    }


    // Background
    //TODO: Override init method in GameEngine

    public void init(){
        //Initialise Window size
        game_width = 750;
        game_height = 450;

        //Position for Paddles
        PP_Velocity = 250;
        PP1_width = 10;
        PP1_height = 100;
        P1_X = 0;
        P1_Y = 100;

        PP2_width = 10;
        PP2_height = 100;
        P2_X = game_width - PP2_width;
        P2_Y = 100;

        //Initialise Keyboard variables
        PP1_UP = false;
        PP1_DOWN = false;
        PP2_UP = false;
        PP2_DOWN= false;

        //Initialise Ball
        ball_X = 250;
        ball_Y = 250;
        ball_Radius = 10;
        ball_Velocity_X = 200;
        ball_Velocity_Y = 200;

        //Paddle ball bounce point

        P1_Hit_X = PP1_width + ball_Radius;
        P1_Hit_Y = P1_Y + PP1_height;
        P2_Hit_X = P2_X - ball_Radius;
        P2_Hit_Y = P2_Y + PP2_height;

        /* NOTE: Sound Resources
        * https://freesound.org/people/NoiseCollector/sounds/4391/
        * https://freesound.org/people/NoiseCollector/sounds/4386/
        */
        wall_bounce = loadAudio("Audio/wall_bounce.wav");
      //  paddle = loadAudio("Audio/paddle_bounce.wav");
        //background = loadAudio("Audio/background.wav");




    }

    // Override abstract method update(double) in GameEngine
    public void update(double dt){
        paddle1_update(dt);
        paddle2_update(dt);
        ball_update(dt);


        if(ball_Y > P1_Y && ball_Y < P1_Hit_Y){
            if(ball_X < P1_Hit_X){

                ball_Velocity_X *= -1;

                ball_X = P1_Hit_X;
            }
        }

        if (ball_Y > P2_Y && ball_Y < P2_Hit_Y){
            if(ball_X > P2_Hit_X){

                ball_Velocity_X *= -1;

                ball_X = P2_Hit_X;


            }
        }


    }
    public void paintComponent() {
        // Clear the background to black
        setWindowSize(game_width, game_height);

        changeBackgroundColor(black);
        clearBackground(game_width, game_height);

        changeColor(white);
        drawLine(game_width/2, 0, game_width/2, game_height);

        paddle1();
        paddle2();
        ball();

        // Draw in black
        changeColor(black);

    }

       //TODO: Key Listener for paddles ()
       //DONE: KeyListener -> Space game example
       // KeyPressed Event
    public void keyPressed(KeyEvent event) {
        // Left Arrow
        if(event.getKeyCode() == KeyEvent.VK_W) {
            PP1_UP = true;
        }
        // Right Arrow
        if(event.getKeyCode() == KeyEvent.VK_S) {
            PP1_DOWN = true;
        }
        // Space Button
        if(event.getKeyCode() == KeyEvent.VK_UP) {
            PP2_UP = true;
        }
        if(event.getKeyCode() == KeyEvent.VK_DOWN) {
            PP2_DOWN = true;
        }
    }

    // KeyReleased Event
    public void keyReleased(KeyEvent event) {
        // Left Arrow
        if(event.getKeyCode() == KeyEvent.VK_W) {
            PP1_UP = false;
        }
        // Right Arrow
        if(event.getKeyCode() == KeyEvent.VK_S) {
            PP1_DOWN = false;
        }
        // Space Button
        if(event.getKeyCode() == KeyEvent.VK_UP) {
            PP2_UP = false;
        }
        if(event.getKeyCode() == KeyEvent.VK_DOWN) {
            PP2_DOWN = false;
        }
    }


}

我想你的代码写得很整齐,我花了很长时间才找到问题所在。但是,我认为这是由于没有更新桨更新方法中的P1_Hit_Y和P2_Hit_Y造成的。因此:

if(PP1_UP == true){
        P1_Y -= PP_Velocity * dt; 
        P1_Hit_Y -= PP_Velocity *dt;
        // Check paddle inside wall
        if((P1_Y < 0) ) {
            P1_Y = 0;
            P1_Hit_Y = 0;
        }
    }
    // Paddle 1 Down with S
    if(PP1_DOWN == true){
        P1_Y += PP_Velocity * dt; 
        P1_Hit_Y += PP_Velocity *dt;
        // Check paddle inside wall
        if(P1_Y > game_height-PP1_height){
            P1_Y = game_height-PP1_height;
            P1_Hit_Y = game_height;
        }

    }
划桨2也一样


希望这有帮助

你还没有将它们包括在GameEngine类中。如果你有新问题,请发布新问题。否定现有答案是不公平的。对,我没有从这个角度思考。我想我可以把它整理一下。谢谢你的评论,没错,我明白你的意思了。谢谢你@Samson Daniel。我应该玩它,因为我仍然有错过点时,划桨在上面的桨划桨= = 0桨失踪球也有几次随机失踪在中间。奇怪的我不知道为什么?他们可能是不同的问题,但如果你有一个想法,我想听听