简体   繁体   中英

How can I restart my Applet in Java?

I've been trying to make a game in an Applet about two palets playing with a ball. One of them is used by the computer and the other by the user. Everything works fine, but I have to give the option to restart the game or to close it, and I don't know how to. This is the code:

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);
    }
}

If someone can tell me how to make it restart, it'll be great. Thanks!

Have the entire main method body of the program as well as the variable declarations incased in a while-loop and an if-statement .

Create a boolean play = true; and have the while-loop have the condition, while(play == true) When you game finishes, have the game run an if-statement which checks if the player selects play again or exit.

If play again is selected, set play to true and run again. If exit is selected, set play to false and break; .

You can also check which button was pressed eg Esc (use switch-case-statement ) or other "magic" one and then break a game. After break you can thanks for playing and display menu or exit a game.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM