gpt4 book ai didi

java - 更新 JLabel 以显示贪吃蛇游戏的当前得分

转载 作者:行者123 更新时间:2023-12-01 10:02:10 25 4
gpt4 key购买 nike

still an error大家好:)我是一个java初学者,目前我正在尝试编写一个贪吃蛇游戏。贪吃蛇游戏是建立在一个JFrame上的,上面有一个GameGrid,西边的一个JPanel上有一些JButtons。在北方,我试图在 JLabel 上构建一个记分板,其中当前分数和当前“运行时间”应该可见。

问题:我坐在这里已经三天了,我仍然不明白如何每秒更新 JLabel 以显示新的分数和时间。如何在后台更新 JLabel,以使编译器仍然能够读取代码中的下一行?

screenshotofthegame

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.*;

import javax.swing.*;

public class menuGui {

JFrame frame;
JButton start;
JButton settings;
JButton info;
JButton exit;

GGSnake snake; // GameGrid wird hier definiert, damit man beim action listener unten zugreifen kann.

public static void main(String[] args) {

menuGui gui = new menuGui();
gui.menu();
}

public void menu() {

//Frame
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Panel1 mit Menu JButtons
JPanel panel1 = new JPanel(new BorderLayout());
panel1.setLayout(new GridLayout(0,1));
frame.add(panel1,BorderLayout.WEST);

//Spielwelt von GGSnake zu Frame hinzufügen
snake = new GGSnake();
frame.add(snake, BorderLayout.EAST);

//Scoreboard
JLabel scoreBoard = new JLabel("Score " + Snake.getScoreText());
scoreBoard.setForeground( Color.WHITE );
scoreBoard.setHorizontalAlignment(JLabel.CENTER);
frame.add(scoreBoard,BorderLayout.SOUTH);

//Buttons
JButton start = new JButton("Spiel starten");
start.setBounds(120, 40, 160, 40);
start.addActionListener(new startListener());
panel1.add(start);

JButton settings = new JButton("Einstellungen");
settings.setBounds(120, 120, 160, 40);
settings.addActionListener(new settingsListener());
panel1.add(settings);

JButton info = new JButton("Credits");
info.setBounds(120, 200, 160, 40);
info.addActionListener(new infoListener());
panel1.add(info);

JButton exit = new JButton("Beenden");
exit.setBounds(120, 280, 160, 40);
exit.addActionListener(new exitListener());
panel1.add(exit);

frame.setTitle("Snake");
frame.setSize(560,423);
frame.setBackground(Color.black);
frame.setResizable(false);
frame.setVisible(true);


snake.requestFocusInWindow(); // --> Tastatureingabe wird direkt auf das "GameGrid" geleitet.

}

//ActionListeners
class startListener implements ActionListener {

public void actionPerformed(ActionEvent event) {
// altes GameGrid entfernen
frame.remove(snake);
// neu erstellen und hinzufuegen.
snake = new GGSnake();
frame.add(snake, BorderLayout.EAST);

// frame aktualisieren (weil wir etwas entfernt und hinzugefügt haben).
frame.validate();
frame.repaint();
// input aus Tastatur anfordern
snake.requestFocusInWindow();

}
}
class settingsListener implements ActionListener {

public void actionPerformed(ActionEvent event) {

}
}
class infoListener implements ActionListener {

public void actionPerformed(ActionEvent event) {

}
}
class exitListener implements ActionListener {
//Anwendung wird geschlossen
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
}
}

import ch.aplu.jgamegrid.*;
import java.util.*;
import java.awt.event.KeyEvent;

class Snake extends Actor {

private ArrayList<Tail> tailList = new ArrayList<Tail>();
private static int tailSize = 0;
private static long startTime;

public Snake() {

super(true, "sprites/snakeHead.gif");
startTime = new Date().getTime();
}

public void act() {

if (gameGrid.kbhit())
{
switch (gameGrid.getKeyCode())
{
case KeyEvent.VK_UP:
setDirection(Location.NORTH);
break;
case KeyEvent.VK_LEFT:
setDirection(Location.WEST);
break;
case KeyEvent.VK_RIGHT:
setDirection(Location.EAST);
break;
case KeyEvent.VK_DOWN:
setDirection(Location.SOUTH);
break;
default:
return;
}
}

int lastIndex = tailList.size() - 1;
Location lastLocation = getLocation();
if (lastIndex > -1)
{
lastLocation = tailList.get(lastIndex).getLocation();
for (int i = lastIndex; i > 0; i--)
tailList.get(i).setLocation(tailList.get(i - 1).getLocation());
tailList.get(0).setLocation(getLocation());
}
move();
if (!isInGrid())
{
gameOver();
return;
}

Actor a = gameGrid.getOneActorAt(getLocation(), Tail.class);
if (a != null)
{
gameOver();
return;
}

tryToEat(lastLocation);
}

private void gameOver() {

gameGrid.removeAllActors();
gameGrid.addActor(new Actor("sprites/gameover.png"), new Location(10, 8));
}

private void tryToEat(Location lastLocation) {

Actor actor = gameGrid.getOneActorAt(getLocation(), Food.class);
if (actor != null)
{
Tail newTail = new Tail();
gameGrid.addActor(newTail, lastLocation);
tailList.add(newTail);
tailSize++;
actor.removeSelf();
gameGrid.addActor(new Food(), gameGrid.getRandomEmptyLocation());
}
}

public static String getScoreText() {

long currentTime = (new Date().getTime() - startTime) / 1000;
return tailSize + " tail segment(s) at " + currentTime + " s";
}
}

import ch.aplu.jgamegrid.*;

public class GGSnake extends GameGrid {
public int startingSpeed = 150;

public GGSnake() {
super(20, 20, 20, false);
this.setBgImagePath("sprites/maxresdefault.jpg");
this.reset();
this.doRun();
}

public void reset() {
this.removeAllActors();
this.setSimulationPeriod(this.startingSpeed);
Snake snake = new Snake();
this.addActor((Actor)snake, new Location(10, 10));
this.addActor((Actor)new Food(), this.getRandomEmptyLocation());
snake.setDirection(Location.NORTH);
}
}

最佳答案

下面写的方法每秒都会触发。您可以使用它来更新您在游戏中的得分。定时器设置为 1 秒(1000ms)。它将每 1 秒触发一次 ActionEvent,因此您可以使用它来定期更新分数。

            ActionListener l1 =new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
scoreboard.setText("Score " + Snake.getScoreText());
//Add here all your logic that contribute to update every second
}
};

Timer t1 = new Timer(1000,l1);
t1.start();

关于java - 更新 JLabel 以显示贪吃蛇游戏的当前得分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36716019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com