我有一个游戏,它是康威生命游戏的简单版本.
当我按下"运行"按钮时,我希望程序无限循环,播放游戏.
我已经有以下两种方法可以玩游戏(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();
}
}
});
Run Code Online (Sandbox Code Playgroud)
当我按下按钮时,它什么也没做.董事会未更新,游戏无法启动.
您正在寻找的是游戏循环.运行的东西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
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
159 次 |
| 最近记录: |