问题描述
我正在尝试通过按“Enter”来“重新启动”我的小蛇游戏。它可以工作,但它会打开一个新的 JFrame 并打开旧的 JFrame。如果我玩游戏 4 次,我会在最后打开 4 个窗口。我知道这听起来可能是一个愚蠢的问题,但我在 gui 世界中介绍自己,任何形式的帮助都将不胜感激。感谢大伙们!在这里,我留下了运行我的程序的 3 个类:
public class SnakeGame { //main class
public static void main(String[] args) {
GameFrame frame = new GameFrame();
}
}
import javax.swing.JFrame;
public class GameFrame extends JFrame{
GamePanel panel;
GameFrame(){
panel = new GamePanel();
this.add(panel);
this.setTitle("Snake");
this.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.pack();
this.setVisible(true);
this.setLocationRelativeto(null); //centra el JFrame en la pantalla
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
public class GamePanel extends JPanel implements ActionListener {
static final int SCREEN_ANCHO = 600;
static final int SCREEN_ALTO = 600;
static final int UNIT_SIZE = 25;
static final int GAME_UNITS = ((SCREEN_ANCHO * SCREEN_ALTO) / UNIT_SIZE);
static final int DELAY = 75;
final int[] x = new int[GAME_UNITS];
final int[] y = new int[GAME_UNITS];
int bodyParts = 6;
int applesEaten;
int appleX;
int appleY;
char direction = 'R';
boolean running = false;
Timer timer; // crea un objeto de tipo timer que sirve para iniciar acciones cada x tiempo
Random random; // objeto de tipo random para generar numeros (posiciones) aleatorias
GamePanel() {
random = new Random();
this.setPreferredSize(new Dimension(SCREEN_ANCHO,SCREEN_ALTO));
this.setBackground(Color.BLACK);
this.setFocusable(true); //hace que que los eventos incidan sobre el panel (hace que sea posible ponerle el foco)
this.addKeyListener(new MyKeyAdapter());
startGame();
}
public void startGame() {
newApple();
running = true;
timer = new Timer(DELAY,this); //contador que actua cada x tiempo sobre el objeto indicado (el panel si pones this)
timer.start(); //comienza el contador
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
draw(g);
}
public void draw(Graphics g) {
if(running){
// for (int i = 0; i < SCREEN_ALTO / UNIT_SIZE; i++) {
// g.drawLine(i * UNIT_SIZE,i * UNIT_SIZE,SCREEN_ALTO);
// g.drawLine(0,SCREEN_ANCHO,i * UNIT_SIZE);
// }
g.setColor(Color.RED);
g.filloval(appleX,appleY,UNIT_SIZE,UNIT_SIZE);
for (int i = 0; i < bodyParts; i++) {
if (i == 0) {
g.setColor(Color.green.darker().darker());
g.fillRect(x[i],y[i],UNIT_SIZE);
} else {
g.setColor(Color.green.brighter());
g.setColor(new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)));
g.fillRect(x[i],UNIT_SIZE);
}
g.setColor(Color.RED);
g.setFont(new Font("Ink Free",Font.BOLD,35));
FontMetrics metrics = getFontMetrics(g.getFont());
g.drawString("score:" + applesEaten,(SCREEN_ANCHO - metrics.stringWidth("score:" + applesEaten))/2,g.getFont().getSize());
}
}
else{
gameOver(g);
}
}
public void newApple() {
appleX = random.nextInt((int) (SCREEN_ANCHO / UNIT_SIZE)) * UNIT_SIZE;
appleY = random.nextInt((int) (SCREEN_ALTO / UNIT_SIZE)) * UNIT_SIZE;
}
public void move() {
for (int i = bodyParts; i > 0; i--) {
x[i] = x[i - 1];
y[i] = y[i - 1];
}
switch (direction) {
case 'U':
y[0] = y[0] - UNIT_SIZE;
break;
case 'D':
y[0] = y[0] + UNIT_SIZE;
break;
case 'L':
x[0] = x[0] - UNIT_SIZE;
break;
case 'R':
x[0] = x[0] + UNIT_SIZE;
break;
}
}
public void checkApple() {
if ((x[0] == appleX) && (y[0] == appleY)) {
bodyParts++;
applesEaten++;
newApple();
}
}
public void checkCollisions() {
//revisa si la cabeza se choca con el cuerpo
for (int i = bodyParts; i > 0; i--) {
if ((x[0] == x[i]) && (y[0] == y[i])) {
running = false;
}
}
//revisa si la cabeza toca borde izquierdo
if (x[0] < 0) {
x[0] = SCREEN_ANCHO-Math.abs(x[0]);
}
//revisa si la cabeza toca borde derecho
if (x[0] >= SCREEN_ANCHO) {
x[0] = x[0]%sCREEN_ANCHO;
}
//revisa si la cabeza toca borde superior
if (y[0] < 0) {
y[0]=SCREEN_ALTO-Math.abs(y[0]);
}
//revisa si la cabeza toca borde inferior
if (y[0] >= SCREEN_ALTO) {
y[0]=y[0]%sCREEN_ALTO;
}
}
public void gameOver(Graphics g) {
//GameOver text
g.setColor(Color.RED);
g.setFont(new Font("Ink Free",75));
FontMetrics metrics = getFontMetrics(g.getFont());
g.drawString("GAME OVER",(SCREEN_ANCHO - metrics.stringWidth("GAME OVER"))/2,SCREEN_ALTO/2-20);
//score
g.setFont(new Font("Ink Free",55));
metrics = getFontMetrics(g.getFont());
g.drawString("score:" + applesEaten,SCREEN_ALTO/2+40);
//PLAY AGAIN
g.setFont(new Font("Ink Free",35));
metrics = getFontMetrics(g.getFont());
g.drawString("Press ENTER to play again",(SCREEN_ANCHO - metrics.stringWidth("Press ENTER to playa again"))/2,SCREEN_ALTO/2+80);
}
@Override
public void actionPerformed(ActionEvent e) {
if (running) {
move();
checkApple();
checkCollisions();
}
repaint();
}
public class MyKeyAdapter extends KeyAdapter {
@Override
public void keypressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
if (direction != 'R') {
direction = 'L';
}
break;
case KeyEvent.VK_RIGHT:
if (direction != 'L') {
direction = 'R';
}
break;
case KeyEvent.VK_UP:
if (direction != 'D') {
direction = 'U';
}
break;
case KeyEvent.VK_DOWN:
if (direction != 'U') {
direction = 'D';
}
break;
}
if(e.getKeyCode() == KeyEvent.VK_SPACE){ //this is were i tried to close the old window
new GameFrame();
}
}
}
}
解决方法
我从 Bro Code 的视频中看到了那个代码,我以前也遇到过同样的问题。我确实修复了它,但使用不同的方法,我添加了一个新按钮,每当我单击该按钮时,游戏都会为我重置,这是代码:
按钮代码:
resetButton = new JButton();
resetButton.setText("Click to restart the Game!");
resetButton.setFont(F1);
resetButton.setForeground(Color.WHITE);
resetButton.setBackground(Color.BLACK);
resetButton.setSize(100,50);
resetButton.setLocation(0,200);
resetButton.addActionListener(this);
this.add(resetButton,BorderLayout.PAGE_END);
这是按钮的代码:
public void actionPerformed(ActionEvent e) {
if(e.getSource() == resetButton) {
this.remove(panel); // Closes the Game
panel = new GamePanel(); // Makes a new a Game
this.add(panel); // Opens the game
SwingUtilities.updateComponentTreeUI(this); // Automatically updates the game frame
panel.requestFocusInWindow(); // Brings back focus to the game from the reset button
}
}
将这些添加到 GameFrame 类中,它应该可以工作。