扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:马岩编译 来源:天极网 2007年12月28日
关键字:
package net.frog_parrot.jump; /** * This class contains the loop that keeps the game running. * * @author Carol Hamer */ public class GameThread extends Thread { //--------------------------------------------------------- // Fields /** * Whether the main thread would like this thread * to pause. */ private boolean myShouldPause; /** * Whether the main thread would like this thread * to stop. */ private boolean myShouldStop; /** * A handle back to the graphical components. */ private JumpCanvas myJumpCanvas; //---------------------------------------------------------- // Initialization /** * Standard constructor. */ GameThread(JumpCanvas canvas) { myJumpCanvas = canvas; } //---------------------------------------------------------- // Actions /** * Pause the game. */ void pauseGame() { myShouldPause = true; } /** * Restart the game after a pause. */ synchronized void resumeGame() { myShouldPause = false; notify(); } /** * Stops the game. */ synchronized void requestStop() { myShouldStop = true; notify(); } /** * Start the game. */ public void run() { // Flush any keystrokes that occurred before the // game started: myJumpCanvas.flushKeys(); myShouldStop = false; myShouldPause = false; while(true) { if(myShouldStop) { break; } synchronized(this) { while(myShouldPause) { try { wait(); } catch(Exception e) {} } } myJumpCanvas.checkKeys(); myJumpCanvas.advance(); // You do a short pause to allow the other thread // to update the information about which keys are pressed: synchronized(this) { try { wait(1); } catch(Exception e) {} } } } } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者