Slick2D游戏速度在变化 [英] Slick2D game speed changing

查看:158
本文介绍了Slick2D游戏速度在变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Swing创建了一个游戏,它有点不可靠,所以我开始使用Slick2D游戏引擎重新制作它,我遇到了问题。

I created a game using Swing, and it was a bit unreliable, so I started remaking it using Slick2D game engine and I have encountered issues.

背景每次调用更新方法时,游戏以一定的像素滚动屏幕。这样可以保持加速和减速,因此背景会非常快速地移动,然后非常慢,并且会一直波动。

The background of the game rolls across the screen at a certain about of pixels each time the update method is called. This keeps speeding up and slowing down, so the background will move very fast, and then very slow, and keeps fluctuating.

我尝试过* delta(监视器刷新率,我想!)在我的值上移动背景,但由于这不会给我一个确切的值我可以用来将背景重置到左侧(2个背景从右向左移动。左手一个去右边是-800像素。)

I have tried * by delta (which monitors the refresh rate, I think!) on my value which moves the background, but as this wont give me an exact value I can use to reset the background to the left hand side (2 background move from right to left. left hand one goes to the right at -800 pixels).

造成这种情况的原因是什么?如何克服它?

What is causing this and how do I overcome it?

谢谢

推荐答案

这里有一些阅读材料(有一个) gamedev特定的StackExchange网站,BTW):

Here's some reading for you (there's a gamedev-specific StackExchange site, BTW):

  • https://gamedev.stackexchange.com/questions/6825/time-based-movement-vs-frame-rate-based-movement
  • https://gamedev.stackexchange.com/questions/1589/fixed-time-step-vs-variable-time-step

这些文章中最重要的一点是,事物以一定的速度移动超过一定时间,而不是超过一定数量的帧。由于帧速率可能无法预测地改变,因此基于时间和基于帧的移动不会相互影响。

One of the most important points in these articles is that things move at a certain rate OVER TIME, not over a certain number of frames. Since frame rates can unpredictably change, time-based and frame-based movement don't wind up being equivalent to one another.

这里有一些解释..

因此,您的计算机和操作系统是多线程的,因此,您永远无法知道应用程序外发生了什么,以及整体负载是什么机。因此,即使您处于全屏模式,也无法独占访问CPU。因此,这是导致事物加速和减速的一个因素。

So, your computer and OS are multithreaded, and thus, you can never know what's happening outside your app, and what the overall load is on the machine. Because of this, even when you're in full-screen mode you aren't getting exclusive access to the CPU. So, that's one factor to why things speed up and slow down.

Slick2D中delta的目的是让你处理这种加速/减速,并允许您的应用会动态更改其帧速率,以便屏幕上的感知移动不会因您的计算机负载而发生变化。 delta 不是监视器刷新率(不变); delta是自上次调用 update 以来经过的毫秒数。

The delta's purpose in Slick2D is to allow you to deal with this speed up/slow down, and allow your app to change its frame rate dynamically so that the perceived movement on the screen doesn't change due to the load on your machine. The delta is not the monitor the refresh rate (which is constant); the delta is the number of milliseconds that have passed since the last call to update.

那么你如何使用这个三角洲适当?假设你的背景应该以100px / sec的速度移动。如果delta(在给定的 update 的调用中)为33毫秒,那么您应该在此更新中移动背景的金额为 100 *(33 /1000.0)= 0.033 - 这样你就可以将背景移动0.033像素。这可能看起来很奇怪,你可能想知道移动< 1像素是什么意思,但坚持我。

So how do you use this delta properly? Let's say your background is supposed to move at a rate of 100px/sec. If the delta (on a given call to update) is 33 milliseconds, then the amount you should move your background on this update is 100*(33/1000.0) = 0.033 - so you would move your background by 0.033 pixels. This might seem weird, and you may wonder what the point is of moving <1 pixel, but stick with me.

首先,你必须将它除以1000.0而不是1000的原因是因为你希望三角洲的移动给你一个浮点数。

First, the reason you have to divide it by 1000.0 instead of 1000, is because you want the movement of the delta to give you a floating point number.

您会注意到Slick2D中的2D图形内容使用浮点值来跟踪事物的位置。那是因为如果delta告诉你移动0.033像素的东西,你需要移动0.033:不是0,而不是1像素。子像素移动对于平滑帧速率的增加/减少也是至关重要的,因为对几个子像素移动的累积效应是,当时刻正确时,所有这些小移动加起来整个像素,并且它非常流畅,从而产生正确的整体移动速度。

You'll notice that the 2D graphics stuff in Slick2D uses float values to track the placement of things. That's because if the delta tells you to move something by 0.033 pixels, you need to move it by 0.033: not 0, and not 1 pixels. Sub-pixel movement is critical to smoothing out the increase/decrease in frame rates as well, because the cumulative effect over several sub-pixel movements is that, when the moment is right, all those little movements add up to a whole pixel, and it's perfectly smooth, resulting in the correct overall movement rate.

您可能会认为,由于您的屏幕将图像解析为给定像素,而不是子像素元素,如果您进行子像素移动并不重要,但如果将所有移动跟踪转换为浮动,您将看到您观察到的效果很大程度上消失了。

You may think that, since your screen resolves images to a given pixel, and not sub-pixel elements, that it doesn't matter if you do sub-pixel movement, but if you convert all your movement tracking to floats, you'll see that the effect you're observing largely goes away.

这篇关于Slick2D游戏速度在变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆