Loops

closed account (30X1hbRD)
Hi, I'm currently trying to learn game development and I'm having lots of trouble wrapping my head around loops, is there anywhere to go to get good practice or a more in-depth understanding on how to execute them properly?
You cannot learn game development if you don't know the basis of c++ yet.
Here are the loops :
http://www.cplusplus.com/doc/tutorial/control/
Have fun reading!
closed account (30X1hbRD)
I've actually already read that, see I know the proper syntax and how to use them I just want to figure out a way to understand how the loops should go together in game development. like how to know which loop to use for the main game loop and including the other loops... I suppose what I mean is I want to know how to get the loops to work together, like if I had multiple loops because I have trouble thinking and considering all the possibilities and getting them to coagulate is all.
You cannot learn game development if you don't know the basis of c++ yet.


I disagree with this statement.

You have to learn the basics of C++ through coding... so you have to code something.

And that something might as well be games if that is what you're interested it. And in fact, when you're coding something you're interested in, you learn better and faster because it's more engaging.


I suppose what I mean is I want to know how to get the loops to work together, like if I had multiple loops because I have trouble thinking and considering all the possibilities and getting them to coagulate is all.



A general guideline is:

-) If you will be looping a known, fixed number of times, use a for loop
-) If you will be looping an undetermined number of times, use a while loop


Things like a main game loop is generally a while loop because you don't know how many times the loop will run (it runs for as long as the player wants to keep playing).

Things like iterating over groups of enemies to update/move them typically go in a for loop because you know up front how many enemies there are.



If this isn't what you're asking, you might have to be more specific and/or give an example of what you mean (perhaps in the form of a situation which confuses you).
closed account (30X1hbRD)
Well, I tried doing a guess the number game program as practice, and I couldn't figure it out so I looked up a tutorial and the person that had posted it used this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
int main() {
int gumballs;
int choices = 5;
int try1;
srand(time(0));
gumballs = rand() % 20 *3;
cout << "try and guess how many gumballs in " << choices  << " Tries!!!" << endl;
for (int i = 0; i < choices; i++){
cout << "Guess # "<< i+1 << ": "<< endl;
cin >> try1;
if (try1 != gumballs)
{ 
if (try1 > gumballs) {cout << "Too big. Go smaller" << endl;
}
else {
cout << "Too small. Go bigger." << endl; }}
else {
cout << "You got it! congratulations!!! The amount was " << gumballs << endl; system("pause"); return 0;
}
	}
cout << "You LOSE!" << endl;
cout << "The Answer was " << gumballs << endl;
system("pause");
return 0;}
it works great but I couldn't understand the for loop because I don't understand where the i variable came from. That's mostly what confused me. (P.S. sorry for the long code there)
in for loop you can declare variables inside the for()
This code
1
2
3
4
int i = 0;
for ( i ; i < 10 ; i++)
{ // do something 
}


EDIT "Thanks for the noticing MikeeyBoy" :
You can use the variable i in the whole main function.

is same as
1
2
3
for (int i = 0 ; i < 10 ; i++)
{ // do something 
}


EDIT "Thanks for the noticing MikeeyBoy" :
You can use the variable i only inside the for loop.





this is the structure of for :
1
2
3
4
5
for ( // first what is checked in for // the declaration ; // second what is checked is the comparison ; fourth is the increment, decrements )
{
 // The body
 // Third step is that the compiler executes the body before the increment // decrement counter //
}


Hope I am right & the explanation is will.
Last edited on
You cannot learn game development if you don't know the basis of c++ yet.


I disagree with this statement.


Will Disch, before I learn how to make games, I just started by making simple programs, then day after day, making programs,
then when I reached the array lesson, exactly 2d arrays, I thought why not use 2d arrays as x and y, and make a character such as O or @ as a character, then after each movement use the system("cls"); function which I got it from using CMD.

closed account (30X1hbRD)
Well, I appreciate the explanation, but I already know how to use for loops, I'm just trying to figure out how to loop efficiently, without overlapping other loops and such. I think that I might be starting to understand a little better though, basically when they created the I variable in the example I listed, they were using the loop to calculate how many guesses could be made before ending the game, correct?
when they created the I variable in the example I listed, they were using the loop to calculate how many guesses could be made before ending the game, correct?

Yes. A "for" statement has the following structure:

1
2
3
4
for (statement 1;  statement 2; statement 3)
{
  /* Code that is executed each time the loop iterates */
}

where:

statement 1 is executed before the first iteration of the loop

statement 2 is evaluated before each iteration of the loop, and the loop only continues iterating if the statement evaluates to true

statement 3 is executed at the end of each iteration of the loop.

So in that example, before the loop begins, the variable i is declared and initialised to 0.

Before each time the loop iterated, the value of i is checked, and the loop will keep looping only if i < choices (i.e. i < 5)

At the end of each iteration of the loop, i is incremented.

So, at the end of the 5th iteration, i is incremented to 5. This means it is no longer less than 5, which means that the loop stops looping.

Net result: the loop runs 5 times.

Edit: in a simple for loop like this, the variable i is often known as a loop counter or loop index.
Last edited on
If you want to make games, you probably want to look for some library that will allow you to use graphics, sounds, handle keyboard events, etc.

I am currently using SDL.

Direct answer to your question(how to game loop) is:

1
2
3
4
5
6
7
8
9
10
11
12
bool gameRunning;

while( gameRunning) 
{

  //Handle events

  // Logic of game

  // Drawing on screen

}


Everything should look clear to you.

There are few things that you should know when it comes to creating games.

First is: start simple. You will never make Quake 4 if you start it at the beggining. You simply don't have proper skills/knowledge/experience, and you will fail quickly.

Second is: don't try to make Best Game Ever. Many times you think that your game will be super-best if you add this and this and that and eventually you just lose yourself in many details. Sometimes these are hard to do, so you get annoyed and resign. Start simple - if you want to make tic-tac-toe game, make tic-tac-toe game. Want to add simple bot? Okay, but mind that it may not help you too much in learning how to create games.

Which brings us to third advice: Always try to learn. If you make tic-tac-toe, make sure you learned something, or gained some experience that will help you in future games that you will make.

If you make some game and it's not perfect, it's okay. You are learning. It can be bugged. It can be imperfect. But you must learn from it. Otherwise you just lost your time.

So, before learning how to make 'games' like this(with many if statements in main code, and so on), I encourage you to:

- work hard and look for resources by yourself( really! People tend to be polite if they see that you made research on topic but didn't find anything).
- try to learn by yourself(if you want, for example, to check some pointers behavior, you can simply make small program that will check this thing, instead of asking it on forum).
- If you want to write games with GUI, you may try SDL. Google SDL and Lazy Foo's SDL lessons. These can help you understand writing games, and other useful stuff.

Good luck!

PS. Before you even start writing games, please, go and humbly complete tutorial for C++ on this site. If you want to create games, you need to know C++ basics. This tutorial got it. If you don't, you will struggle, and later will have to come back and learn it anyway.
Last edited on
Yemeni Cpluspluser wrote:
in for loop you can declare variables inside the for()
This code

1
2
3
4
int i = 0;
for ( i ; i < 10 ; i++) { 
     // do something 
}


is same as

1
2
3
for (int i = 0 ; i < 10 ; i++) { 
     // do something 
}


Not quite. In the first version, the variable i remains in scope after the for loop. In the second version, it does not:

1
2
3
4
5
for (int i = 0 ; i < 10 ; i++) { 
     // do something 
}

std::cout << i; // invalid i is out of scope 
My approach to game dev has been to build a game from the center out. To start with one feature and then add the next layer of game play on top.

In other words... play with simple loops. This is not yet a game, but can become one as you gradually develope them to pertain as such.

I would suggest practice with rock paper scissors as a starter game.

Many examples of that can be found on this site somewhere already.
Not quite. In the first version, the variable i remains in scope after the for loop. In the second version, it does not:


AKA local variable to the for loop, and the first one was a global variable for the main() function, is it legal/correct to say that?
Last edited on
the first one was a global variable for the main() function

No - the first one is a local variable in the main() function. A global variable would be:

1
2
3
4
5
6
7
8
int i = 0;

int main()
{
   for ( i ; i < 10 ; i++) { 
      // do something 
   }
}
Last edited on
closed account (30X1hbRD)
@Mikeyboy Thanks for the clarification, I think I've gotten a basic view on how to use loops. I guess the best way to master them is just to practice a bit more.

@MatthewRock I am currently learning how to use SDL, and I have a knowledge of the basics of C++ rest assured, I have no intentions of jumping into game development, I am just trying to get a little practice into the basic concepts, and then intend to start small with things like pong, space invaders, and slowly move my way up as I get better.

@Manga I'll be sure to look for some rock paper scissors examples, thanks for the advice.

Thanks all! I'll try and practice with loops a little more, just to be sure I get the hang of them.


Global variables can be used in side any function including main().
Local variables can be used between the curly brackets they are declared in, such as in mian() or specific function.

So what we call the variable inside the "for loop"
private variable? or not necessary that every variable has a name.
So what we call the variable inside the "for loop"

It's still a local variable, because it's only in scope within a certain code block. It's just that the "for" loop is a special case - declaring a variable within the "for" statement like that means that it has that specific scope.

Since I'm too lazy busy to go and read the standard, are there any other constructs which have similar rules for specifying the scope of a local variable. For example, could I do:

1
2
3
4
5
while (int i = 0, i < SOME_VALUE) // Using the comma operator
{
  // Do some stuff
  ++i;
}


What scope would i have?
Last edited on
local to the "while()"
Thanks.
Topic archived. No new replies allowed.