121

There's a long history of how we arrived at this common convention, with lots of fascinating challenges along the way, so I'll try to motivate it in stages: 1. Problem: Devices run at different speeds Ever try to play an old DOS game on a modern PC, and it runs unplayably fast - just a blur? A lot of old games had a very naive update loop - they'd collect ...


42

tl;dr don't mix your event loop with your game loop. When you move your mouse, the game receives a load of pygame.MOUSEMOTION events. You don't actually use these events to update your mouse position though, you are getting the current state of the mouse using pygame.mouse.get_pos(). That's inefficient, but it's not the problem. The problem is you are ...


34

It will cause one CPU core to always run on 100%. This usually doesn't cause any harm to the system. CPUs are designed to run on 100% for hours. But on a mobile device it will drain the battery quickly and heat up the device, which will likely cost you about a stars in your store ratings. On a desktop computer this is less of a problem, but it will consume ...


32

"Delta", "d" or "Δ", means "difference" in a mathematical context. Whenever there's a difference difference between two numbers with similar meanings, that difference may be called a "delta", or a "d". Deltas are very common in game development. For example, the difference between a character's X-coordinate one second ago and its X-coordinate now can be ...


31

Some games do indeed extrapolate. An advantage of extrapolation is that you can do it with just the single most recent state and a rate of change (like velocity, angular velocity, etc.) rather than two complete states. So it is simpler to implement and compute. The main disadvantage of extrapolation is that it's a prediction about the future, and like most ...


27

This is the "time delta." It's how much time has elapsed since the previous update. It's necessary to ensure that animations, physics, and so on are running at the right speed. The code is running once per frame update. However, there's no guarantee that frames are drawn at a constant speed. One frame might take 1/60th of a second and the next might ...


21

Don't actually stop the game loop when pausing the game. Instead, you have to add bool variable, that is changed to true/false depending if game is paused. If game is paused, you only have to stop updating the game, but you can still keep rendering the current frame including getting updates from input. if(gameIsPaused == false) { // run updates } ...


19

Your main problem is likely this: final int skipTicks = 1000 / ticksPerSecond; Dividing an int by an int returns an int (the value is rounded down if needed), so skipTicks evaluates to 16. 1000 / 16 is 62.5 so you get around this many ticks per second. To solve this the easiest solution would be to use a millisecondsPerTick variable (what you are calling ...


17

Your GPU can probably render even 100k sprites without issues, but you need to do it smart. Sprites and other geometry must be supplied to a GPU in batches grouped by the same texture, shader and blend mode. Big AAA games are minimizing draw calls issued to the GPU. Draw calls are usually expensive, so many similar drawing operations are grouped together ...


13

Like so much of gamedev, the answer to how city sim games accomplish this seemingly-impossible feat is: they probably don't. They're just faking it well. ;) Sims like these will typically operate on a "chunked" level, modelling groups of people, neighbourhoods, traffic corridors, or other city dynamics as a whole, rather than processing AI for ...


10

I have not seen an else being used in this instance (the Rastertek tutorial you mentioned does not use an else). My guess would be if that you tried to resize your window the game rendering would freeze proving that the else part is never executed, as the resize message would be constantly sent. A common loop would have this structure. while (true) { ...


10

FixedUpdate can run faster or slower than Update depending on your settings. That'll depend on load (how fast Update is running) and speed you have set for FixedUpdate (found in Edit->Project Settings->Time). Ideally, since Update is run once per frame, this is where you want to capture input. If you need to act on input in the FixedUpdate method (like ...


10

Typically, a frame is drawn every time through the game loop. Thus, the FPS tells you how fast the game is looping. That said, more sophisticated rendering architectures often decouple the rendering from the main game loop. In that case, the two are only vaguely related.


10

Yes, it does make sense. As you said it will make less load on the system, which is good for thermals, and other applications. However.... Your games logic should NOT depend on the updates per second. Therefore I recommebd you to take a look at deltatime, which will make your game independent of the updates per second. I recommend you to take a look at ...


9

In a word, synchronization. If you have some data (your world) which you're reading from one thread (the rendering thread), and writing to in another thread (the logic thread), it is possible that your data will be read in an inconsistent state (for example, half the world has been updated, but not the other half). To maintain consistency, you will probably ...


8

The other answers are good and talk about why the game loop exists and should be seperate from the render loop. However, as for the specific example of "Why render a frame when there hasn't been any changes?" It really just comes down to hardware and complexity. Video cards are state machines and they're really good at doing the same thing over and over ...


8

Store your birthday and calculate your age on demand. When I buy alcohol the shopkeeper sometimes checks my ID to make sure I am not underage. However, my ID card doesn't have my actual age, and I don't have to update it every year. Instead, it shows my birthday, and the shopkeeper compares that date to the current date to calculate my actual age. This is ...


7

You seem to have a handle on this already. My current 2D platformer has objects that implement different concerns, including the following: Notifiable - can be event-driven and scripted Collidable - can interact with solid tiles (eg NPCs) Intersectable - exists in 2D space and can intersect with the player (eg doors) Intersectable - exists ...


7

First, Fix Your Timestep. The component update should always have a fixed time interval. This is critical for stable physics and can avoid bugs in other systems as well. You may have cases where the time interval becomes huge, too. This can happen if you set a breakpoint while debugging. You'll want to cap the update time used for the time accumulator (...


7

Alex's answer seems sufficient, but you say you want more detail, so here goes. First, why does the gameloop from http://www.koonsolo.com/news/dewitters-gameloop/ deliver its desired frame rate while your version doesn't? Because their integer division works out exactly: const int FRAMES_PER_SECOND = 25; const int SKIP_TICKS = 1000 / FRAMES_PER_SECOND; ...


7

There is no specific game loop in libGDX because it is event-driven. And still, you can see the render method as the main loop. You've got it right - usually, this is where you update the game logic before you do the rendering. What you can do is to create a class responsible for the logic and a class responsible for the rendering. Then in your render ...


7

As congusbongus notes in his answer, many games benefit from dividing content into "scenes". A scene might represent a particular level, a room, a minigame, or lots of other possibilities, depending on the game. In such a system, menus can be implemented as scenes as well, and this works well for simple games with only a few menus. Personally, I like to ...


7

You need to understand what each one does. Update() gets called as often as possible (not sure, maybe it can be capped), either way - each frame. FixedUpdate() gets called every constant amount of time (hence "fixed"). Input goes into Update(), as simple as that (because as you noticed FixedUpdate() might not catch the input event). Game logic however might ...


7

From what I see, the game loops you are trying to use are somewhat specialized. I don't know why you chose those ones, but in my opinion you are trying to solve problems that don't exist in the first place. Instead of copy-pasting a game loop from some site, I suggest you think about how your game is made and what your priorities are, and then create a game ...


7

You can't. Whatever code runs on the computer owned by the user is under the users control. Anything you program can be circumvented or patched by a determined enough hacker. The only solution to avoid cheating in an online game is by calculating all game mechanics on the server. When the game is offline, then you have no chance at all.


Only top voted, non community-wiki answers of a minimum length are eligible