Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › How is looping and branching useful for programming games
New Posts  All Forums:Forum Nav:

How is looping and branching useful for programming games

post #1 of 5
Thread Starter 
I'm not really sure how looping and branching is useful when programming for games and it's really just the start of my semester. Could anyone shed some light for me? Thanks.
Parvum
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 6700k Gigabyte Z170X-Gaming 6 2x Gigabyte GTX 1080 G1 16GB DDR4 
Hard DriveOSMonitorKeyboard
Samsung 512GB M.2 950 Pro NVMe Windows 10 Dell 1440p Pok3r 
PowerCaseMouseMouse Pad
EVGA 750GQ Parvum M1.0 Logitech G9X Corsair 
  hide details  
Reply
Parvum
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 6700k Gigabyte Z170X-Gaming 6 2x Gigabyte GTX 1080 G1 16GB DDR4 
Hard DriveOSMonitorKeyboard
Samsung 512GB M.2 950 Pro NVMe Windows 10 Dell 1440p Pok3r 
PowerCaseMouseMouse Pad
EVGA 750GQ Parvum M1.0 Logitech G9X Corsair 
  hide details  
Reply
post #2 of 5
I'm not sure I follow your question. Can you be more specific?
Death Star
(21 items)
 
Darksaber
(11 items)
 
 
CPUMotherboardGraphicsRAM
Athlon II x2 245 Asus M3A78 Radeon HD6570 1GB Mushkin Silverline 2GB DDR2  
Hard DriveOptical DriveOSMonitor
OCZ Vertex 2 120GB Samsung Blu-Ray Windows 7 Samsung 46" DLP 
PowerCaseOther
Silverstone Strider Essentials 400W Silverstone Milo ML03B Hauppage WinTV 1250 
  hide details  
Reply
Death Star
(21 items)
 
Darksaber
(11 items)
 
 
CPUMotherboardGraphicsRAM
Athlon II x2 245 Asus M3A78 Radeon HD6570 1GB Mushkin Silverline 2GB DDR2  
Hard DriveOptical DriveOSMonitor
OCZ Vertex 2 120GB Samsung Blu-Ray Windows 7 Samsung 46" DLP 
PowerCaseOther
Silverstone Strider Essentials 400W Silverstone Milo ML03B Hauppage WinTV 1250 
  hide details  
Reply
post #3 of 5
At it's most basic, almost all games are an infinite loop. Aside from that, any time you need to do something more than once in a row, a loop is probably what you need.

As to branching, any time you need to ask a question, the answer is a branch. Anything from "is your guess correct?" to "what did that bullet hit?" all require branching.

note: it's a low-level idea, but some things are handled by interrupts rather than normal branches, but this doesn't matter unless you are programming something like assembly.
post #4 of 5
As mentioned almost the whole game runs inside what is called a game loop. Game loop consists all the code that needs to be run again and again every frame. For example you'll want to check for the user input and react to it, issue AI commands, track the movement of objects in the game world and finally render everything to the screen. For example, using some pseudocode:
Code:
while (gameIsRunning)
{
    checkUserInput();
    runAI();
    moveObjects();
    renderScene();
    drawUserInterface();
}

Each of this steps usually requires a lot of code which forms different parts of a typical game engine, like physics engine, rendering engine etc. Within these steps there are different scenarios that can happen, which introduces branching. Branching means that different parts of the code get executed, depending on certain conditions.

For example you may have a counter which displays ammo on the screen in fps game. When user presses "fire" button, the very next frame, when the loops runs again, this will get picked up by checkUserInput() function and will result in changing some variables and, for example, calling shootBullet() function which creates a bullet object. This object is then moved, based on it's speed etc. inside moveObjects() part of the game loop and then rendered at the correct position on the screen inside rederScene() method. Finally drawUserInterface() function updates the ammo on your screen which has now decreased by 1. Few frames later, the bullet may hit a target, which again will result in calling a different part of the code, for example displaying a death of the enemy.
buka
(17 items)
 
  
Reply
buka
(17 items)
 
  
Reply
post #5 of 5
Quote:
Originally Posted by poroboszcz View Post

As mentioned almost the whole game runs inside what is called a game loop. Game loop consists all the code that needs to be run again and again every frame. For example you'll want to check for the user input and react to it, issue AI commands, track the movement of objects in the game world and finally render everything to the screen. For example, using some pseudocode:
Code:
while (gameIsRunning)
{
    checkUserInput();
    runAI();
    moveObjects();
    renderScene();
    drawUserInterface();
}

Each of this steps usually requires a lot of code which forms different parts of a typical game engine, like physics engine, rendering engine etc. Within these steps there are different scenarios that can happen, which introduces branching. Branching means that different parts of the code get executed, depending on certain conditions.

For example you may have a counter which displays ammo on the screen in fps game. When user presses "fire" button, the very next frame, when the loops runs again, this will get picked up by checkUserInput() function and will result in changing some variables and, for example, calling shootBullet() function which creates a bullet object. This object is then moved, based on it's speed etc. inside moveObjects() part of the game loop and then rendered at the correct position on the screen inside rederScene() method. Finally drawUserInterface() function updates the ammo on your screen which has now decreased by 1. Few frames later, the bullet may hit a target, which again will result in calling a different part of the code, for example displaying a death of the enemy.

pretty much nailed it...to the OP, I would recommend coding a very simple animation in Javascript using either setInterval() or requestAnimationFrame() which are essentially the "game loops"; tons of information on how to use these through google. After understanding the game loop you can throw in a spritesheet and you now have a fully controllable character on screen with animation.
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › How is looping and branching useful for programming games