有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何在Java中重新启动小程序?

我一直在尝试在一个Applet中制作一个关于两个palets玩一个球的游戏。其中一个由计算机使用,另一个由用户使用。一切都很好,但我必须给出重启游戏或关闭游戏的选项,我不知道该怎么做。以下是代码:

public class Entrada extends Applet{

private int[] limites; 
int keydown, puntajej, puntajec;
String nombrej;
private Jugador jugador;
private Compu compu;
private Bola ball;
private Raqueta raque, raque2;
private boolean perdio;


/**
 * Initialization method that will be called after the applet is loaded into
 * the browser.
 */
public void init() {

    this.resize(400,400);
    this.setBackground(Color.black);
    this.limites= new int[4];
    this.limites[0]=15;  
    this.limites[1]= this.getSize().width-20;
    this.limites[2]=20;
    this.limites[3]= this.getSize().height-20;

    this.perdio=false;
    this.keydown=0;
    this.puntajec=0;

    this.nombrej=JOptionPane.showInputDialog("Ingrese Nombre del Jugador"); 
    this.puntajej=Integer.parseInt(JOptionPane.showInputDialog("A cuantos puntos se jugara?"));       

    this.jugador= new Jugador(nombrej, puntajej);
    this.compu=new Compu(puntajec);
    this.ball=null;
    this.raque=null;
    this.raque2=null;

    this.crearRaqueta();
    this.crearBola();
    this.crearHilo();


}

public void destroy(){


     int a = Integer.parseInt(JOptionPane.showInputDialog("Quiere volver a jugar?\n"
            + "Si.\n"
            + "Acabar juego."));
    switch(a){
        case 1:
            this.init();
            break;
        case 2:
            this.destroy();
            break;
    }

}





      private void crearRaqueta(){

       int ancho=10;
       int alto=40;
       int posinx=5;
       int posiny=30;

       this.raque=new Raqueta(ancho, alto, posinx, posiny);

       this.raque2=new Raqueta(ancho, alto, posinx+364, posiny);
   }

    private void crearBola(){
        int radio=10;
        int posinx =this.getSize().width/2-30;
        int posiny =this.getSize().height/2;

        this.ball=new Bola(radio,posinx,posiny);

    }

    public boolean keyDown(Event evt, int key)
  {
      this.keydown=(char)key;
      System.out.println("Pulsó: "+keydown);
      return true;


  }

    public int getKey()
    {
        return keydown;
    }

    public void setBola(Bola ball)
    {
       this.ball=ball;
       repaint();
    }

    public Bola getBola(){
       return ball; 
    }


    public void setRaquetaJugador(Raqueta raque){
        this.raque2=raque;
        repaint();
    }

    public void setRaqueta(Raqueta raque){
        this.raque=raque;
        repaint();
    }

    private void crearHilo(){
        moveBola mb= new moveBola(this,ball,limites,raque2, jugador, compu);
        mb.start();
        moveRaquetaJugador mrj = new moveRaquetaJugador(limites, this.raque2, this);
        mrj.start();
        moveRaqueta mr = new moveRaqueta(limites, this.raque, this);
        mr.start();

    }

    public boolean getPerdio() {
        return perdio;
    }

    public int getPuntajej() {
        return puntajej;
    }




    public void paint(Graphics f){

        f.setColor(Color.yellow);
        f.drawLine(3, 3, this.limites[1], 3);// horizontal primera
        f.drawLine(this.limites[0],this.limites[2],this.limites[1],this.limites[2]);// horizontal segunda
        f.drawLine(this.limites[0],this.limites[3],this.limites[1],this.limites[3]);//horizontal ultima
        f.drawLine(this.limites[0],3,this.limites[0],this.limites[3]);//izquierda
        f.drawLine(this.limites[0]+this.getSize().width/2-8, 3, this.limites[0]+this.getSize().width/2-8, this.limites[3]);//media
        f.drawLine(this.limites[1],3,this.limites[1],this.limites[3]);// derecha


        //dibuja de bola
        f.setColor(this.ball.getColor());
        f.fillOval(this.ball.getPosx()-ball.getRadio(),this.ball.getPosy()-ball.getRadio(), ball.getRadio()*2,ball.getRadio()*2);

        //dibuja raqueta compu
        f.setColor(Color.red);
        f.drawRect(this.raque.getPosx()+10,this.raque.getPosy(), this.raque.getAncho(),this.raque.getAlto());
        f.setColor(Color.white);
        f.fillRect(this.raque.getPosx()+12,this.raque.getPosy()+2, this.raque.getAncho()-3, this.raque.getAlto()-3);


        //dibuja raqueta jugador
        f.setColor(Color.blue);
        f.drawRect(this.raque2.getPosx(),this.raque2.getPosy(), this.raque2.getAncho(),this.raque2.getAlto());
        f.setColor(Color.white);
        f.fillRect(this.raque2.getPosx()+2,this.raque2.getPosy()+2, this.raque2.getAncho()-3, this.raque2.getAlto()-3);
        this.keydown=0;

        //dibujo de puntaje compu
        f.setColor(Color.white);
        f.drawString("Compu="+this.compu.getPuntaje(), 20, 15);

        //dibujo puntaje jugador
        f.setColor(Color.blue);
        f.drawString(jugador.getNombre()+"="+jugador.getPuntaje(), this.limites[0]+this.getSize().width/2-5, 15);

       if(this.jugador.getPuntaje()==0){
           this.perdio=true;
           f.setColor(Color.red);
        f.drawString("GAME OVER...modafaka", this.getHeight()/2-85, this.getWidth()/2); 
        this.stop();
        this.repaint();

       }

    }




    public void update(Graphics f){
        Graphics dbg =null;
        //obtener el pantallazo
        Image dbImage=null;
        if(dbImage==null){
            dbImage= createImage (this.getSize().width, this.getSize().height);
            dbg =dbImage.getGraphics();
        }
        dbg.setColor(getBackground());
        dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
        dbg.setColor(getForeground());

        paint (dbg);
        f.drawImage(dbImage, 0, 0, this);
    }
}

如果有人能告诉我如何重新启动,那就太好了。谢谢


共 (1) 个答案

  1. # 1 楼答案

    将程序的整个主要方法体以及变量声明包含在while-loopif-statement

    创建一个boolean play = true;并让while-loop拥有条件while(play == true) 游戏结束后,让游戏运行一个if-statement,检查玩家是否选择再次玩或退出

    如果选择了play again,请将play设置为true,然后再次运行。如果选择了exit,则将play设置为falsebreak;