Escape the men's room(Java版密室逃脫)

1.概述
老外用Java做的2D密室逃脫遊戲。個人認爲挺不錯的。
來源[url]http://meatfighter.com/escape/index.html[/url]
效果如圖
[img]http://dl2.iteye.com/upload/attachment/0105/2427/a742cd21-e376-3ab4-8723-1e96e56970c7.png[/img]

2.源碼賞析
[img]http://dl2.iteye.com/upload/attachment/0105/2430/714432c5-559e-302b-98d7-3bc888de61e7.png[/img]
還是一如既往,定義了一個IMode,這樣不同模式可以切換,避免一堆if else,而且擴展也容易。
遊戲主要核心就是Main以及GameMode這兩個類。其他都是場景。當然裏面有很多小遊戲,如fifteen,漢諾塔,記憶題。
Main是遊戲的一個框架。

public void run() {

Images.inventoryTile.getWidth(); // load images
addMouseListener();

BufferedImage image = new BufferedImage(CANVAS_WIDTH, CANVAS_HEIGHT,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D)image.getGraphics();
Graphics2D g2 = null;
int frameSkips = 0;

long nextFrameStartTime = System.nanoTime();
while(alive) {

//1.跳幀,防止電腦太慢
do {
nextFrameStartTime += 16666667;
frameSkips++;

//2.更新模型
mode.update();

} while(nextFrameStartTime < System.nanoTime()
&& frameSkips < MAX_FRAME_SKIPS);

if (frameSkips >= MAX_FRAME_SKIPS) {
nextFrameStartTime = System.nanoTime();
}
frameSkips = 0;

//3.如果targetMode存在,則進入該模式,並進入下一次循環(continue)
if (targetMode != null) {
mode = targetMode;
mode.enter();
targetMode = null;
nextFrameStartTime = System.nanoTime();
continue;
}

//4.渲染(在內存中畫)(double buffer)
mode.render(g);

//5.一次性畫到屏幕上
// show the hidden buffer
if (g2 == null) {
g2 = (Graphics2D)getGraphics();
requestFocus();
} else {
g2.drawImage(image, 0, 0, null);
}

//6.睡一會兒,節省cpu
// burn off extra cycles
while(nextFrameStartTime - System.nanoTime() > 0) {
Thread.yield();
}

// do not eat up CPU if user leaves browser
if (sleeping) {
synchronized(this) {
while (sleeping && alive) {
try {
wait();
} catch(Throwable t) {
}
}
}
nextFrameStartTime = System.nanoTime();
}
}
}


GameMode稍微複雜一點,處理了鼠標點擊,鼠標拖放核心功能。
具體不多說了,附件可下載我加了中文註釋的源碼包,在JDK7上可以運行。

如果覺得難度太高的話,可以修改Main.DEBUG=true,可打開調試模式,降低遊戲難度,一方面也方便大家學習源碼。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章