Introduction to C++ with Game Development: Part 5, Conditions
This tutorial is about the condition commands C/C++ has which allow you to make important decisions that can help you to control the way your program flows. You already did some of these conditions when you did loops without realizing it, so this is just a cool way to make those decisions more explicit.
Getting the stuff you need
As with the last couple of tutorials, we'll use the template again. But you will need to know how to use your sprites from lesson 3 so if you have not done lesson 3 please go back and do so now. Extract a fresh version of the template, remove the hello world code, and let's begin.
To Draw or not to Draw
In your Game::Tick loop from the last lesson you put a sprite on screen. Lets do that again.
Add a Sprite variable above the tick function like this:
Sprite theSprite( new Surface("assets/ctankbase.tga"), 16 );
Remember your ctankbase.tga image is located in the assests folder...
Now make your Tick function like this:
void Game::Tick( float a_DT ) { // render a single frame here m_Screen->Clear( 0 ); theSprite.Draw( 0, 0, m_Screen ); }
Try and run this and make sure you get an image on screen at the top left corner.
Ok?
Good lets continue. Let's move our sprite down a little bit and then move it around using your variable skills. Above the tick routine and below the Sprite definition add a couple of "Global" variables.
int SpriteX = 0; int SpriteY = 100; bool Visible = true;
Hopefully these variable names will explain their function. Notice also that I initialsed them when I created them, so that they will have initial values . Of course you should make sure these variables are also set up in any initialise routines you may write.
Now lets alter our game tick a little.
void Game::Tick( float a_DT ) { // render a single frame here m_Screen->Clear( 0 ); theSprite.Draw( SpriteX, SpriteY, m_Screen ); }
Run this and you should see your sprite is now a little down the screen.
You are now drawing your sprite based on variable values which we set as (0,100).
But remember that other variable, Visible. We need to use that.
We are going to make a simple decision, and decide whether we want to draw our sprite. The simplest way to do this is to use an IF test the easiest of all types of test, which works like this.
if some value we want to test is true, then do something, else do something else.
This is a golden rule, try to remember this.
So lets say that we want to draw the sprite IF the Visible variable is set to true.
Change your code to this:
void Game::Tick( float a_DT ) { // render a single frame here m_Screen->Clear( 0 ); if (Visible == true) theSprite.Draw( SpriteX, SpriteY, m_Screen ); }
Run that…..it won't really seem any different!
But go and change your Visible variable to false.
Run it again….what happened?
Has your program crashed?
Why can't you see your tank?
Change it back to true and retry…phew!
This looks a little different from our rule of IF – THEN – ELSE because in C/C++ the THEN part is assumed and the else is also assumed in a simple line like this.
But if you read it...
if (Visible == true)
Is the same as "if some value we want to test is true"
There is no THEN because it is assumed
But the next part is:
theSprite.Draw( SpriteX, SpriteY, m_Screen );
Which corresponds to
"then do something"
So where's the "else do something else."? Well it is there….only in this case it does nothing…or more correctly it simple does the next line
Do you know now why your tank did not appear when you set Visible to false?
This kind of condition check can be very powerful lets try a more useful version.
Add another variable after the Visible, which you should have reset to true.
bool Direction = true;
Now lets alter our tick a little more to allow our tank to move left and right across the screen, we will make a coding assumption before than that if Direction is true that we are going right, and if Direction is false we are moving left.
void Game::Tick( float a_DT ) { // render a single frame here m_Screen->Clear( 0 ); if (Visible == true) theSprite.Draw( SpriteX, SpriteY, m_Screen ); //alter our coords if (Direction == true) { // he is moving right so add to his x SpriteX += 1; } else { // he is moving left so subtract from his x SpriteX -= 1; } }
This time we are being quite specific and using the keyword else because there is a very specific need to do one thing or the other.
Run this and you should see your tank moving right across the screen.
But why does it not go Left? Do you know? Try to answer this yourself without looking at the answer in the next section.
The Answer? We have not made any chances to the Direction value. Somehow our code has to do that, since its not practical to recompile the program every time we want to chance that value.
We need to add some code now to make a decision to change the Direction at a certain point
Lets change the movement direction when the Sprite gets to a particular point,
The best place to do this is just after we have changed the screen coord. Since there are 2 places we change the screen coord we need to do this twice.
void Game::Tick( float a_DT ) { // render a single frame here m_Screen->Clear( 0 ); if (Visible == true) theSprite.Draw( SpriteX, SpriteY, m_Screen ); // alter our coords if (Direction == true) { // he is moving right so add to his x SpriteX += 1; if (SpriteX > 500) Direction = false; } else { // he is moving left so sub tract from his x SpriteX -= 1; if (SpriteX < 100) Direction = true; } }
Now compile this and you should see a tank moving left and right across the screen?
Notice that we used a slightly different condition check > and < instead of ==.
Our rule states that "if some value we want to test is true" means that we can use lots of different types of test, we only care if they work out to be true or false.
Assignment
Here's your tasks for today:
- Make your tank move up and down as well as left and right. You will need to take account of the up and down Direction so a new variable will be needed. If your tank is moving down….make it invisible.
Complete this assignment, and then you may continue with the next part. Ask for help on the forums if you get stuck!
Tagged:
Recently Featured Posts
- Accessing Microsoft Windows 8 Desktop Sensors
- Shader Effects: Glow and Bloom
- Shader Effects: Screen Space Ambient Occlusion
- Shader Effects: Refraction
- Shader Effects: Blend Modes
- Shader Effects: Gamma Correction
- Follow Devmaster.net on Facebook, Twitter, and Google+
- Shader Effects: Depth of Field
- Shader Effects: Shadow Mapping
- Shader Effects: Old Film
Recent Forum Discussions
- PS3 System is not Reading Games
- HIERARCHICAL TEMPORAL MEMORY CRITICISM
- Shadow techniques
- 3D Alien Buildings for Game Design
- Animating simple player sprite (Platform)
- some thoughts on artificial intelligence
- Yildiz-Online: MMORTS (Alpha)
- advice on the best schools in either bc...
- Giant rubber duck meets sorry end
- MinGW64 and GLES2 emulator?
Comments