Java Applet Pong游戏,如果语句不停止

时间:2019-05-08 19:00:40

标签: java netbeans applet

我正在尝试用Java Applet制作此游戏,我已经做了所有事情,但是唯一的问题是,当球从“屏幕”出来时有人输了(例如玩家或PC)时,我输了,显示了“游戏结束”对话框。 ”。但这总是向我显示,并且不会关闭或从头开始使用。.

package pongproiect;

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *
 * @author negruandrei
 */
public class Tennis extends Applet implements Runnable, KeyListener{
    final int WIDTH=700, HEIGHT=500;
    Thread thread;
    Player1Paddle p1;
    Player2Paddle p2; //de adaugat player2
    PCPaddle pcp;
    Ball b1;
    int restart = 250;
    boolean gameStarted; 
    int p1score=0, p2score=0;


    public void init(){
            this.resize(WIDTH,HEIGHT);
            this.addKeyListener(this);
            gameStarted = false;
            // gamePause = false;
            p1 = new Player1Paddle(1);
            b1 = new Ball();
            p2 = new Player2Paddle(2); //de adaugat player2
            pcp = new PCPaddle(2, b1);

            thread = new Thread(this);
            thread.start();
}
    public void paint(Graphics g){


        g.setColor(Color.black);
        g.fillRect(0, 0, WIDTH, HEIGHT);


         if(b1.getX()< -10 || b1.getX()> 710){
            JOptionPane.showMessageDialog(this, "Game Over boy!");



         }


         if(!gameStarted){
             g.setColor(Color.WHITE);
             g.drawString("Press Enter to start", 290, 150);
         }
             g.setColor(Color.white);
            // g.drawString("Score Player1 : " + p1score, 300, 475); //nefunctionabil
            // g.drawString("Score Player2 : " + p2score, 300, 490); //nefunctionabil
             p1.draw(g);
             b1.draw(g);
             pcp.draw(g);
             // p2.draw(g); -> de adaugat player2


    }

    public void update(Graphics g){
        paint(g);
    }




    public void run() {
        while(true){

           if(gameStarted){ 
            //p2.move(); -> de adaugat player2
            b1.move();
            p1.move();
            pcp.move();
            b1.checkPaddleCollision(p1, pcp);
            p1score=0;
            p2score=0;   
           }
            repaint(); 
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
               e.printStackTrace();
            }
        }
    }


    public void keyTyped(KeyEvent e) {

    }


    @Override
    public void keyPressed(KeyEvent e ) {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_UP:
                p1.setupAccel(true);
                break;
            case KeyEvent.VK_DOWN:
                p1.setdownAccel(true);
                break;
             //de adaugat player2
           /* case KeyEvent.VK_W:
                p2.setupAccel(true);
                break;
            case KeyEvent.VK_S:
                p2.setdownAccel(true);
                break; */
            case KeyEvent.VK_ENTER:
                gameStarted = true; 
                break;
            default:
                break;
        }
    }


    @Override
    public void keyReleased(KeyEvent e) {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_UP:
                p1.setupAccel(false);
                break;
            case KeyEvent.VK_DOWN:
                p1.setdownAccel(false);
                break;

                //de adaugat player2
           /* case KeyEvent.VK_W:
                p2.setupAccel(true);
                break;
            case KeyEvent.VK_S:
                p2.setdownAccel(true);
                break; */
            default:
                break;
        }
    }

}

玩家1划桨

package pongproiect;

import java.awt.*;
/**
 *
 * @author negruandrei
 */
public class Player1Paddle implements Paddle {
    double y, yVel;
    boolean upAccel, downAccel;
    int player, x; 
    final double GRAVITY =0.94;


    public Player1Paddle (int player) {

    upAccel=false; downAccel=false;
    y=210; yVel=0;


    }

    public void draw(Graphics g) {
        g.setColor(Color.WHITE);
        g.fillRect(20, (int)y, 20, 80);
    }


    public void move() {
        if(upAccel){
            yVel -=2;
        }
        else if(downAccel){
            yVel +=2;
        }
        else if(!upAccel && !downAccel){
             yVel *=GRAVITY;
        }
        if(yVel >= 3){
            yVel=3;
        }
        else if (yVel <=-3){
            yVel=-3;
        }
        if (y < 0){
            y =0;
        }
        if (y > 420){
            y=420;
        }

        y += yVel;
    }

    public void setupAccel (boolean input){
        upAccel = input;
    }
    public void setdownAccel (boolean input){
        downAccel = input;
    }


    public int getY() {
        return (int)y;
    }

}

Player 2球拍

package pongproiect;


import java.awt.*;
/**
 *
 * @author negruandrei
 */

    public class Player2Paddle implements Paddle {
        double y, yVel;
        boolean upAccel, downAccel;
        int player, x; 
        final double GRAVITY =0.94;


        public Player2Paddle (int player) {

        upAccel=false; downAccel=false;
        y=210; yVel=0;

        if(player == 1)
            x = 20;
         else 
            x = 660;


        }

        public void draw(Graphics g) {
            g.setColor(Color.WHITE);
            g.fillRect(660, (int)y, 20, 80);
        }


        public void move() {
            if(upAccel){
                yVel -=2;
            }
            else if(downAccel){
                yVel +=2;
            }
            else if(!upAccel && !downAccel){
                 yVel *=GRAVITY;
            }
            if(yVel >= 3){
                yVel=3;
            }
            else if (yVel <=-3){
                yVel=-3;
            }
            if (y < 0){
                y =0;
            }
            if (y > 420){
                y=420;
            }

            y += yVel;
        }

        public void setupAccel (boolean input){
            upAccel = input;
        }
        public void setdownAccel (boolean input){
            downAccel = input;
        }


        public int getY() {
            return (int)y;
        }

    }

package pongproiect;

import java.awt.Graphics;
/**
 *
 * @author negruandrei
 */
public interface Paddle {
    public void draw(Graphics g);
    public void move();
    public int getY();


}

PCPADDLE

package pongproiect;

import java.awt.*;
/**
 *
 * @author negruandrei
 */
public class PCPaddle implements Paddle {
    double y, yVel;
    boolean upAccel, downAccel;
    int player, x; 
    final double GRAVITY =0.94;
    Ball b1;


    public PCPaddle (int player, Ball b) {

    upAccel=false; downAccel=false;
    y=210; yVel=0;
    b1 = b;

    if(player == 1)
        x = 20;
     else 
        x = 660;


    }

    public void draw(Graphics g) {
        g.setColor(Color.WHITE);
        g.fillRect(x, (int)y, 20, 80);
    }


    public void move() {
        y = b1.getY() - 40;


        if (y < 0){
            y =0;
        }
        if (y > 420){
            y=420;
        }

        y += yVel;
    }


    public int getY() {
        return (int)y;
    }

}

package pongproiect;
import java.awt.*;

/**
 *
 * @author negruandrei
 */
public class Ball {
    double x, y, xVel, yVel;


    public Ball(){
        x=350;
        y=250;
        xVel= getRandomSpeed() *getRandomDirection();
        yVel= getRandomSpeed() *getRandomDirection();
    }

    public double getRandomSpeed(){
        return (Math.random() *2 + 2);
    }
    public int getRandomDirection(){
        int dir = (int)(Math.random()*2);
        if (dir == 1)
            return 1;
        else
            return -1;
    }

    public void draw(Graphics g){
        g.setColor(Color.white);
        g.fillOval((int)x-10, (int)y-10, 20, 20);

    }

    public void checkPaddleCollision(Paddle p1, Paddle p2){
     if(x<=50){
         if(y >= p1.getY() && y <= p1.getY() +80)
             xVel = -xVel;
     }
     else if(x>=650){
         if(y >= p2.getY() && y <= p2.getY() +80)
             xVel = -xVel;
     }
}

    public void move(){
        x += xVel;
        y += yVel;

        if( y<10 ){
            yVel= -yVel;
            }
        if( y>490 ){
            yVel= -yVel;
            }

}

    public int getX(){
        return (int)x;
    }
    public int getY(){
        return (int)y;
    }

}

0 个答案:

没有答案