gpt4 book ai didi

java - 生命游戏未正确暂停

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

我正在尝试制作我的生活游戏版本,但我一直在尝试利用线程来让程序能够暂停和恢复。

我使用Thread来执行主Jpanel,我们可以在其中看到不同的代,当我点击“暂停”时,屏幕成功暂停,但当它恢复时5 秒后(因为我使用了 Thread.sleep(5000)),我意识到屏幕卡住了,但游戏实际上仍在运行,只是没有更新屏幕。

就像它在第 5 代暂停并在第 11 代恢复一样,显然我希望游戏在暂停的地方恢复,但我尝试了很多方法,但到目前为止没有任何效果。任何帮助都会很棒。

GameOfLife 类:

public class GameOfLife extends JFrame implements ActionListener {

static JLabel aliveLabel = new JLabel("Alive:");
static JLabel GenerationLabel = new JLabel("Generation #");
static SimpleCellGrid body = new SimpleCellGrid();
static JPanel header = new JPanel();
static int genNumber = 1;
static JButton PlayToggleButton = new JButton("pause");
static JButton ResetButton = new JButton("b");
static Thread t1 = new Thread(body, String.valueOf(header));



public GameOfLife() throws IOException {
super("Game of life");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(700, 660);
setLocationRelativeTo(null);
setLayout(new FlowLayout());

GenerationLabel.setName("GenerationLabel");
aliveLabel.setName("aliveLabel");
PlayToggleButton.setName("PlayToggleButton");
ResetButton.setName("ResetButton");

PlayToggleButton.addActionListener(this);
ResetButton.addActionListener(this);

PlayToggleButton.setIcon(new ImageIcon(play));
ResetButton.setIcon(new ImageIcon(reset));

PlayToggleButton.setPreferredSize(new Dimension(40,30));
ResetButton.setPreferredSize(new Dimension(40,30));

header.setLayout(new FlowLayout());
header.setPreferredSize(new Dimension(100, this.getHeight()));
header.add(PlayToggleButton);
header.add(ResetButton);
header.add(GenerationLabel);
header.add(aliveLabel);


body.setLayout(new BorderLayout());
body.setPreferredSize(new Dimension(500, this.getHeight()));

add(header, BorderLayout.WEST);
add(body, BorderLayout.CENTER);
setVisible(true);

}

public static void updateLabels(){

body.run();
GenerationLabel.setText("Generation #"+ genNumber++);
aliveLabel.setText("Alive: "+ body.totalAlive());

try {
Thread.sleep(100);
updateLabels();
} catch (InterruptedException ignore) { }
}

@Override
public void actionPerformed(ActionEvent e) {

if(e.getActionCommand().equals("pause")){
try {
t1.sleep(5000);
PlayToggleButton.setEnabled(true);

} catch (InterruptedException ex) {
t1.start();
}

}
}

public static void main(String[] args) throws IOException {
new GameOfLife();
updateLabels();
}
}

SimpleCellGrid 类:

public class SimpleCellGrid extends JPanel implements Runnable{
private static final int ROWS = 50;
private static final int COLS = 50;
private static final int CELL_WIDTH = 10;
private static SimpleCell[][] cellGrid = new SimpleCell[ROWS][COLS];

public SimpleCellGrid() {
for (int row = 0; row < cellGrid.length; row++) {
for (int col = 0; col < cellGrid[row].length; col++) {
int x = col * CELL_WIDTH;
int y = row * CELL_WIDTH;
cellGrid[row][col] = new SimpleCell(x, y, CELL_WIDTH);

if (new Random().nextBoolean()) {
cellGrid[row][col].setAlive(true);
} else {
cellGrid[row][col].setAlive(false);
}
}
}
}
public int totalAlive(){
int totalAlive = 0;
for (SimpleCell[] simpleCells : cellGrid) {
for (int j = 0; j < cellGrid.length; j++) {
if (simpleCells[j].isAlive())
totalAlive++;
}
}
return totalAlive;
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
for (SimpleCell[] cellRow : cellGrid) {
for (SimpleCell simpleCell : cellRow) {
simpleCell.draw(g2);
}
}
}

@Override
public void run() {
cellGrid = new GenerationMaker4().nextGeneration(cellGrid);
repaint();
}
}

最佳答案

这里不需要让线程 hibernate 。

要暂停游戏,请重构代码,以便您拥有一个核心游戏方法,可以在每次运行时按一个单位执行游戏进程。您可以使用 java.util.TimerTask 安排此核心游戏方法以常规速率运行。当您想暂停游戏时,只需停止 TimerTask 运行核心游戏方法,当您取消暂停时,再次开始运行它。

这样,您的程序将在 100% 的时间内保持响应。

关于java - 生命游戏未正确暂停,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60068430/

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