2014-03-03 21 views
0

我一直在尝试在Applet中制作一个关于两个玩具球的游戏。其中一个由计算机使用,另一个由用户使用。一切正常,但我必须提供重新启动游戏或关闭游戏的选项,但我不知道如何操作。这是代码:如何在Java中重新启动我的Applet?

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

如果有人能告诉我如何使它重新启动,它会很好。谢谢!

回答

0

拥有程序的整个主要方法体以及while-loopif-statement中的变量声明。

创建boolean play = true;并有while-loop具备的条件,while(play == true) 当你结束游戏,让游戏运行if-statement,检查如果玩家再次选择或退出比赛。

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

+1

while循环根据需要工作。非常感谢 – SantyCortes94

0

您还可以检查按下了哪个按钮,例如Esc(使用switch-case-statement)或其他“魔术”,然后打破游戏。休息后,你可以感谢玩和显示菜单或退出游戏。