Almost every game can be thought of as having one main function that contains all the game logic, and which is run either when the user does something, or after a certain amount of time. This cycle of running the same core function over and over again is called the game loop , and is crucial to understand for any sort of game development. Each player rolls the die or spins the spinner and moves forward the number of squares indicated. The square they land on may direct them to slide backwards or climb forwards several spaces.
The player wins the game when they get to the final square.
Subscribe to RSS
So, in the above image, landing on Square 6 makes you climb four squares to Square 10; landing on Square 19 makes you slide back 15 squares to Square 4; and landing on Square 64 means you win the game.
Now suppose you're playing single player, to practice. You do the same thing as above, over and over again, until you reach Square How would you represent this in code? You'd probably start by creating an array to store the values of the squares.
Most elements would contain zero, but a few would contain either a positive number indicating a ladder , or a negative number:. This function, takeATurn , encapsulates the core game logic for single-player Chutes and Ladders. But how do we actually get this function to run? There are several options:.
We could place a button on the screen, and have it call takeATurn each time it's clicked.
Actually, this option is a little more complicated than it may seem. In practice we never see any games without some player input; with no interaction, it can't really be considered a game. But still, some games have an element of "calendar time" involved.
Consider FarmVille: you, the player, plant your crops, and then every few minutes or hours they develop a little further, from seeds to shoots to fruit.
Loop the loop game logic pdf
And in some modes of Civilization, you're given a set amount of time say five minutes to make all of your moves for one "turn"; once those five minutes are up, the core game logic function is triggered. Some games use a mixture of these two options: for example, Mafia Wars has a resource called "energy" which refills one unit every five minutes; you take actions in the game by using this resource, so the core game logic function is still triggered by a user action, it's just restricted by time.
This is a pattern common to most games: one piece of code containing the core game logic is triggered repeatedly. We call this the game loop. There's a term for the action or period of time that triggers the core game logic code as well: a tick like the sound a clock makes.
So in Civilization, the tick is every five minutes. In Words With Friends, playing your turn causes a tick. In other words, the game loop runs once per tick. Super Mario Bros doesn't seem to fit into either of these categories.
Mario responds to the player's input Are there two game loops? There's just one, and it's triggered solely by time -- but with a tick of just a fraction of a second. In Civilization, you have a period of five minutes to input everything you want to do in the current turn, before the game "ticks" and runs the game loop again based on all your input.
So if you say, in Turn 23, that you want your warriors to attack a deer, then in Turn 24 everyone's getting venison for dinner. It's the same with Mario. If you press the Jump button during one tick, then in the next iteration of the game loop, Mario will start to jump. Note that you don't have to time your Jump press to occur just as the core game logic function is triggered -- all of your actions during a tick period are recorded and used during the next iteration of the game loop.
Incorporate our capabilities into your own solutions as an agency, reseller or software provider.
All of the game logic is handled in the game loop. But there's more to a game than its logic, graphics being the major example. Drawing graphics to the screen is hard work for the computer. In this scenario, we can use two separate loops: a game loop and a draw loop.
We could then run the draw loop at a much lower frequency than the game loop; let's say we refresh the screen half as often, i. The amount of processing power required by the game may vary from level to level.
Consider a shoot-'em-up: the first few levels will have very few ships on the screen, to ease the player in gently, while the last levels could have dozens of enemy ships and hundreds of bullets all flying around the same scene at once. It's generally simpler to slow down the draw loop than the game loop, if you have to choose.
For this reason, games often aim to keep the game loop's tick consistent, and slow down the draw loop if more power is needed.
satisfy your intellect
If you've ever turned on the FPS counter short for Frames Per Second, the number of times the draw loop is running per second in a first person shooter, you'll have seen it change depending on how much is on the screen; the draw loop's refresh rate gets adjusted automatically.
The game may look juddery -- like a live streaming video on a slow internet connection -- but unless it's being run on a computer with a lot less power than the game developers had in mind, all the objects in the game world will continue to move and interact at the correct speeds.
For a great article explaining how Flash deals with this, check out Sean Christmann's post on the 'Elastic Racetrack'.
Unlimited asset downloads! Subscribe Sign In. Web Design. Game Development. Computer Skills.
Learning Guides. Code Games. Understanding the Game Loop - Basix. Games Flash.
Understanding the Game Loop - Basix
Michael James Williams. Feed Like Follow Weekly email summary. Powered by. Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started. WordPress Plugins. PHP Scripts. Mobile App Templates.
Get access to over one million creative assets on Envato Elements. Over 9 Million Digital Assets. Everything you need for your next creative project. Join the Community.
Share ideas. Host meetups. Lead discussions. Quick Links Explore popular categories.