java制作密室脱逃,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,可打开调试模式,降低游戏难度,一方面也方便大家学习源码。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值