怎么重启?

时间:2014-10-31 19:34:09

标签: java

我有这个代码,我希望用户能够通过按R重启游戏,但我不知道该怎么做,我已经尝试再次调用main方法,这显然没有&# 39;工作,但我不知道任何其他方式重新启动它,这是主类代码:

import javax.swing.*;

import java.util.*;
import java.awt.*;
import java.awt.event.*;

public class Main extends JPanel implements KeyListener {

long startT = System.currentTimeMillis();
long elapsedTimeMillis;
float elapsedT; 

private Player player;
private Stage stage;

private boolean isGameOver = false;

private EnemyManager manager;

public Main() {


    setSize(800,600);
    setPreferredSize(new Dimension(800,600));
    setFocusable(true);
    addKeyListener(this);


    stage = new Stage();
    player = new Player(this, 200, 500);
    manager = new EnemyManager(this, 10);


}

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

public void paint(Graphics g){
    g.setColor(Color.BLACK);
    g.fillRect(0, 0, getWidth(), getHeight());

    if(!isGameOver){


        player.draw(g);
        manager.draw(g);
        stage.draw(g);

        long elapsedTimeMillis = System.currentTimeMillis() - startT;
        elapsedT = elapsedTimeMillis/1000F;
        g.setColor(Color.white);
        g.setFont(new Font("Century Gothic", Font.BOLD, 24));
        g.drawString("You're alive for: " + elapsedT, 400, 50);

    }else {         
        g.setFont(new Font("Century Gothic", Font.BOLD, 55));
        g.setColor(Color.RED);
        g.drawString("Game Over" , 250, 150);
        g.setColor(Color.ORANGE);
        g.setFont(new Font("Century Gothic", Font.BOLD, 24));
        g.drawString("You lasted for: ", 300, 250);
        g.drawString("Press    to restart", 300, 300);
        g.setColor(Color.white);
        g.drawString("" + elapsedT, 480, 250);
        g.drawString("R", 364, 300);


    }

    g.dispose();
    repaint();
   }

@Override
public void keyPressed(KeyEvent e) {


    int code = e.getKeyCode();
    if (code == KeyEvent.VK_W){

    }
    if (code == KeyEvent.VK_A){
        player.setdirectionX(-1);

    }
    if (code == KeyEvent.VK_S){

    }
    if (code == KeyEvent.VK_D){
        player.setdirectionX(1);
    }       

    if (code == KeyEvent.VK_R){

    }

  }

public void setGameOver(boolean flag){
    isGameOver = flag;
}
@Override
public void keyReleased(KeyEvent e){
    player.setdirectionX(0);
    player.setdirectionY(0);
}

@Override
public void keyTyped(KeyEvent e) {
}

public Stage getStage(){
    return stage;
}

public EnemyManager getEnemyManager(){
    return manager;
}

public static void main(String[] args){
    JFrame frame = new JFrame();
    frame.setTitle("Dodge the bloody rectangles");
    frame.add(new Main());
    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(new Dimension(800,600));
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

}   

1 个答案:

答案 0 :(得分:0)

public void restart() {
    stage = new Stage();
    player = new Player(this, 200, 500);
    manager = new EnemyManager(this, 10);
    isGameOver = false;
}
...

@Override
public void keyPressed(KeyEvent e) {
...
    if (code == KeyEvent.VK_R){
        restart();
    }
  }