使用定时器和游戏循环 [英] Using timer and game loop

查看:104
本文介绍了使用定时器和游戏循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个简单的单机游戏,还有谁移动播放器的键时preSS下来的,有敌人而移动的自动,每种类型的敌人移动一个时间在X中毫秒为单位

I'm building a simple console game, there is the player who moves when key press down, and there are enemies which moves automatically, each type of enemy moves one time in X miliseconds.

我的理解,我应该使用定时,但我真的不知道该怎么做,在游戏圈,因为我不(不建尚未知道如何用定时器做的。但它应该是 while循环我认为)。游戏结束时,敌人'触摸'玩家(相同x和y)。

As I understood I should using the timer, but I don't really know how to do that in the game loop (isn't built yet because I don't know how to do with the timer. but it should be while loop I think). the game ends when the enemy 'touch' the player (same x and y).

一个重要的事情:我不能在此锻炼,但如果你有其他的建议,而不是使用定时欢迎您。

One important thing: I can't you in this exercise in Thread, but if you have other suggestions instead of using Timer you are welcome.

感谢您。

推荐答案

您通常不会在游戏中使用传统的计时器。游戏有自己的处理逻辑和所经过的时间非常不同的机制,他们通常不会在你所期望的方式与定时器工作或没有:

You normally don't use conventional timers in games. Games have a very different mechanism for handling their logic and the time that passed, they normally don't work with timers or not in the way you would expect:

游戏通常有一些所谓的游戏循环。一般来说它是被对方在一个循环后调用三种主要功能:

Games normally have something called a game loop. Generally speaking it's three main functions that are called one after the other in a loop:

while(running)
{
    HandleUserInput();
    ChangeWorld();
    Render();
}

您获取用户输入,则相应地改变游戏世界,你把它画到屏幕上。现在,更快您的计算机,速度越快这个循环运行。这是很好的图形(想想FPS),但糟糕的比赛。想象一下,俄罗斯方块,每一个帧块移动。现在,我不想买一个更快的计算机,游戏将变得更加困难的方式。

You get user input, you change the game world accordingly and you draw it to the screen. Now, the faster your computer is, the faster this loop runs. That's good for the graphics (think FPS), but bad for the game. Imagine Tetris where every frame the blocks move. Now I would not want to buy a faster computer, the game would get more difficult that way.

所以,保持计算机的电源的游戏速度恒定的独立,循环认为过去的时间:

So to keep the game speed constant independent of the power of the computer, the loop considers the time passed:

while(running)
{
    var timePassedSinceLastLoop = CalculateTimeDelta();

    HandleUserInput();
    ChangeWorld(timePassedSinceLastLoop);
    Render();
}

现在想象在游戏中的东西冷却时间。玩家pressed一,一些很酷的动作发生了,虽然他可能preSSA再次,什么都不会发生在接下来的5秒。但游戏仍然运行并执行所有可能发生的游戏内的其他东西。这不是一个常规的定时器。这是一个变量,让我们把它叫做ActionCooldown,而一旦玩家触发操作,它设置为5秒。每当世界发生变化,timePassed是从数量,直到它的零减去。所有的时间,在游戏运行和处理输入和渲染。但只有一次ActionCooldown降为零,另一preSSa将再次触发该动作。

Now imagine a cooldown for something in game. The player pressed "a", some cool action happened and although he may press "a" again, nothing will happen for the next 5 seconds. But the game still runs and does all the other things that may happen ingame. This is not a conventional timer. It's a variable, lets call it ActionCooldown, and once the player triggers the action, it's set to 5 seconds. Every time the world changes, the timePassed is subtracted from that number until it's zero. All the time, the game is running and handling input and rendering. But only once ActionCooldown hits zero, another press of "a" will trigger that action again.

该ChangeWorld方法包括对世界上所有的自动变化。敌人,导弹,没有任何玩家互动移动。而且它基于时间移动。如果敌人移动每秒一平方米,你需要让他的协调浮动,并添加一个方形的循环每次运行的一小部分。

The ChangeWorld method includes all automatic changes to the world. Enemies, missiles, whatever moves without player interaction. And It moves based on time. If the enemy moves one square per second, You need to make his coordinate a float and add a fraction of a square every time the loop is run.

假设你有30 FPS所以你的循环运行30次。你的敌人现在需要移动一个正方形每个循环的1/30。然后,它会在年底陆续每秒一个完整的正方形。

Lets say you have 30 fps so your loop runs 30 times a second. Your enemy now needs to move 1/30 of a square each loop. Then it will in the end have moved one full square per second.

这篇关于使用定时器和游戏循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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