android游戏开发_Android游戏开发–游戏循环

android游戏开发

游戏循环是每个游戏的心跳。 到目前为止,我们仅使用了非常简单的一种(您可以在此处找到),无法控制我们更新游戏状态的速度或速度以及要渲染的帧。

概括地说,最基本的游戏循环是while循环,该循环不断执行一些指令,直到我们发出信号指示完成为止,通常是通过将一个称为running的变量设置为false来完成

boolean running = true;
while (!running)
{
   updateGameState();
   displayGameState();
}

上面的代码盲目运行,无需担心时间和资源。 如果您使用的设备较快,则其运行速度将非常快;如果设备较慢,则其运行速度将较慢。

updateGameState()更新游戏中每个对象的状态,而displayGameState()将对象渲染为图像,并显示在屏幕上。

我们在这里应该考虑两件事:FPS和UPS。

FPS每秒 帧数每秒调用displayGameState()的次数。
UPS每秒更新每秒调用updateGameState()的次数。

理想情况下,每秒调用更新渲染方法的次数相同(最好不低于每秒20-25次)。 25 FPS通常在电话上就足够了,因此我们人类不会注意到动画缓慢。

例如,如果我们以25 FPS为目标,则意味着我们必须每40毫秒( 1000/25 = 40毫秒,1000毫秒= 1 秒)调用displayGameState()方法。 我们需要记住,在显示方法之前也要调用updateGameState() ,并且要使我们达到25 FPS,我们必须确保更新– 显示序列恰好在40毫秒内执行。 如果所需时间少于40毫秒,则我们的FPS较高。 如果花费的时间更多,那么我们的游戏运行速度会更慢。

让我们看一些示例,以更好地了解FPS。

下图正好显示了1 FPS。 它需要更新 - 渲染周期恰好执行一秒钟。 这意味着您将看到屏幕上的图像每秒更改一次。

每秒1帧

下图显示了10FPS。 更新渲染周期需要100毫秒。 这意味着图像每十分之一秒发生变化。

10 FPS

但是上述情况意味着更新渲染周期每隔1秒就会执行一次。 这是一个假设,我们无法控制周期执行的实际时间,或者可以吗? 如果我们有200个敌人,而每个敌人都向我们开枪怎么办? 我们需要更新每个敌人的状态及其子弹的状态,并在一次更新中检查是否有碰撞。 当我们只有两个敌人时,情况就不同了。 时间显然会有所不同。 渲染方法也是如此。 显然,渲染200个发射机器人比仅渲染2个要花费更多的时间。

那是什么情况呢? 我们可以有一个更新-渲染周期,它可以在不到100毫秒(1/10秒)的时间内完成,可以在完全100毫秒内完成,也可以在更长的时间内完成。 在功能强大的硬件上,它会比功能较弱的硬件上更快。 让我们看一下图。

该周期在所需的时间范围之前完成,因此在运行下一个周期之前,我们有少量的空闲时间。

有时间的框架

下图显示了一个落后的周期。 这意味着完成更新渲染周期所花费的时间大于所需的时间。 如果花费12毫秒,则意味着我们落后2毫秒(仍考虑10FPS)。 这可能会加剧,并且每个周期我们都会失去时间,游戏将缓慢运行。

过期帧

第一种情况是所需的。 这使我们有一些空闲时间在开始下一个周期之前做某事。 我们不需要做任何事情,所以我们只是告诉游戏循环在剩余时间段内进入睡眠状态,并在下一个周期到期时醒来。 如果我们不这样做,游戏将比预期的运行得更快。 通过引入睡眠时间,我们获得了恒定的帧速率

当循环落后时,第二种情况(我跳过了理想情况,因为它几乎从未发生过),需要使用不同的方法。

为了在游戏中达到恒定的速度,我们需要在需要时更新对象的状态。 想象一下,机器人以恒定的速度接近您。 您知道它是否在一秒钟内通过了屏幕的一半,因此还需要一秒钟才能到达屏幕的另一侧。 为了准确地计算位置,我们需要知道自上一个位置以来的时间增量以及机器人的当前速度,或者我们以恒定的间隔更新机器人的位置(状态)。 我将选择第二个,因为在游戏更新中使用三角洲可能会很棘手。 为了达到恒定的游戏速度,我们将不得不跳过显示帧。 游戏速度不是FPS!

检查下图。 在这种情况下, 更新渲染周期花费的时间比所需时间长,因此我们必须赶上。 为此,我们将跳过此帧的渲染并进行另一次更新,以免影响游戏速度。 我们将在下一帧进行正常循环,甚至需要一些时间来让CPU休息。

可变FPS的恒定游戏速度

上面的场景有很多变体。 您可以想象游戏更新需要一个以上的全帧。 在这种情况下,我们无法采取任何措施来保持游戏速度恒定,并且游戏运行速度会变慢。 我们可能必须跳过多个渲染以保持速度恒定,但是我们必须确保设置允许跳过的最大帧数,因为它可能需要花费很多更新才能赶上,并且如果我们跳过15帧,则意味着我们从游戏中损失了很多东西,这将是无法玩的。

MainThread.javarun()如下所示:

// desired fps
private final static int 	MAX_FPS = 50;
// maximum number of frames to be skipped
private final static int	MAX_FRAME_SKIPS = 5;
// the frame period
private final static int	FRAME_PERIOD = 1000 / MAX_FPS;	

@Override
public void run() {
	Canvas canvas;
	Log.d(TAG, "Starting game loop");

	long beginTime;		// the time when the cycle begun
	long timeDiff;		// the time it took for the cycle to execute
	int sleepTime;		// ms to sleep (<0 if we're behind)
	int framesSkipped;	// number of frames being skipped 

	sleepTime = 0;

	while (running) {
		canvas = null;
		// try locking the canvas for exclusive pixel editing
		// in the surface
		try {
			canvas = this.surfaceHolder.lockCanvas();
			synchronized (surfaceHolder) {
				beginTime = System.currentTimeMillis();
				framesSkipped = 0;	// resetting the frames skipped
				// update game state
				this.gamePanel.update();
				// render state to the screen
				// draws the canvas on the panel
				this.gamePanel.render(canvas);
				// calculate how long did the cycle take
				timeDiff = System.currentTimeMillis() - beginTime;
				// calculate sleep time
				sleepTime = (int)(FRAME_PERIOD - timeDiff);

				if (sleepTime > 0) {
					// if sleepTime > 0 we're OK
					try {
						// send the thread to sleep for a short period
						// very useful for battery saving
						Thread.sleep(sleepTime);
					} catch (InterruptedException e) {}
				}

				while (sleepTime < 0 && framesSkipped < MAX_FRAME_SKIPS) {
					// we need to catch up
					// update without rendering
					this.gamePanel.update();
					// add frame period to check if in next frame
					sleepTime += FRAME_PERIOD;
					framesSkipped++;
				}
			}
		} finally {
			// in case of an exception the surface is not left in
			// an inconsistent state
			if (canvas != null) {
				surfaceHolder.unlockCanvasAndPost(canvas);
			}
		}	// end finally
	}
}

请仔细检查以上代码,因为它实现了图表背后的逻辑。 您可以在可下载的项目中找到完整的代码。

我喜欢另一种方法。 它是恒定的游戏速度,每秒最大帧数。 它使用插值法绘制状态,并且在下一次游戏更新之前还有剩余渲染时间的情况下,它会在快速硬件上发生。 这可以增强游戏的视觉效果,因为它可以使动画更加流畅,但是由于我们使用移动设备,因此给CPU休息可以节省大量电池。

有在游戏圈的真棒文章在这里 。 我个人理解游戏循环阅读本文,因此我强烈推荐它。 我能找到的最好的。

请注意,我还修改了Speed.java类中的默认值。 速度以单位/秒为单位。 因为我们将所需的FPS设置为50,这意味着速度将在每次更新时增加50 * speed.value。 若要具有40像素/秒的速度,则需要将每个刻度的速度增量设置为2(40 /(1000/50)= 2)。 换句话说,您需要机器人在每次游戏更新时(如果您每秒更新50个游戏)前进2个像素,以覆盖每秒40个像素。

此处下载代码并使用它。

检查它,您将获得具有恒定帧速率的恒定游戏速度。

参考:反对谷物 ”博客中我们JCG合作伙伴Tamas Jano的《 游戏循环 》。

不要忘记查看我们的新Android游戏 ArkDroid (以下屏幕截图) 。 您的反馈将大有帮助!
相关文章:

翻译自: https://www.javacodegeeks.com/2011/07/android-game-development-game-loop.html

android游戏开发

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
高清版 Book Description “Wolfenstein 3D”-like and ”Doom”-like game apps are some of the classic Android games presented in the original edition of this book. Since their release, Android has progressed with the debut of Android 4.0, adding better fonts, new User Interface and Experience (UI/UX) APIs, tablet considerations, multi-touch capabilities, multi-tasking, faster performance, and much more to the Android game app development repertoire. Multi-touch code gives these games and their players dynamic input and exchange ability, for a more realistic arcade game experience. Faster and better performance offers game players a more seamless, fun arcade experience like never before on Android. There is also improved native C/C++ integration with Android’s NDK as well, which makes coding, compiling, and converting both productive and efficient with gains in app performance. With actionable real-world source, Advanced Android 4 Games shows you how to build more sophisticated and addictive Android games, harnessing the power of these recent advancements. * Coverage of the new UI, UX, multi-touch and multi-tasking features available with Android 4.0. * Learn other techniques for improving the game playing experience including Wi-Fi tethering, better multi-tasking, new and better streaming Web video using WebM, and more. * By combining the elegant object-oriented features of Java and the raw power of C, there is no limit to the types of games that you can build for the platform, such as the “Quake 3D”-like game app case study in this book. You’ll definitely have fun, and perhaps you’ll even make some money. Enjoy! What you’ll learn * Key advanced Android gaming techniques using the new Android SDK 4 to help you earn more money in the app stores * How to compile native code (C) in Android using the NDK * How to add and integrate multi-touch * How to use Bluetooth controllers (Zeemote) * More gaming tricks and tips, such as hybrid 3D graphics with OpenGL and JNI * How to port and augment a 2D shooter game app similar to “Wolfenstein” for Android * How to port and augment another 2D shooter “Doom”-like game app for Android using OpenGL * How to build a 3D shooter game like “Quake” * How and where to best deploy these game apps Who this book is for This book is for savvy Android app developers who are looking for professional or advanced techniques for porting, augmenting and building 2D and 3D game apps that are complex, fun and lucrative. Table of Contents 1. Welcome to Android Gaming 2. Gaming Tricks: 3. More Gaming Tricks: Hybrid 3D Graphics with OpenGL and JNI 4. 2D Shooters: Wolfenstein-like App for Android 5. 2D Shooters with OpenGL: Doom-like App for Android 6. 3D Shooters Part I: Quake-like App 7. 3D Shooters Part II: Quake II 8. Appendix: Deployment and Compilation Tips
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值