我有一个游戏,它是康威生命游戏的简单版本。
当我按下"运行"按钮时,我希望程序无限循环,玩游戏的回合。
我已经有以下两种方法,它们玩一轮游戏(game.playGameTick();
(,然后更新可见板(updateVisibleBoard();
(。
playForeverButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for (;;) {
try {
Thread.sleep(1000);
//to create a delay
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
game.playGameTick();
updateVisibleBoard();
}
}
});
当我按下按钮时,它什么也没做。棋盘未更新,游戏无法启动。
试试这个
您正在寻找的是游戏循环。在游戏仍在运行时true
运行的东西。也许这就是你要找的?
大多数引擎都有很多关于这些东西的文档。尝试浏览Java游戏引擎,甚至是JavaScript游戏引擎。
如果你真的喜欢冒险,Unity 提供了关于其所有代码和函数的精彩文档。最重要的是,如果你想在未来构建更多的游戏,OpenGl是目前要构建的事实库,但Vulkan也取得了一些很大的进展。
我的想法
游戏循环是跟踪游戏中时间范围的好方法。它们允许在其中进行多个循环,使您可以完全访问时间和物理。使用游戏循环时,请尝试仅保留其中的必需品,以便将来不会变得难以阅读和理解。
例
public game_loop() {
update_timer(); // Update the timer because everything else past this point will depend on the time this game loop started running
physics_update(); // Run calculations and setup events
update(); // Here would loop through each object
fixed_update(); // Would loop through each object again, but provide a more concise time frame based on screen updates, frame rate and physics
}