Learning Computer Science Through Games: Conditionals and If-Else Statements

Conditionals and Video Games

School is back in session, and there’s a lot of students who are taking their first programming classes.  Let’s make computer science a little more fun, and a bit easier to understand, by looking at how it’s used in your favorite games!  Today’s topic is conditionals.

A Basic IF statement

In programming, a conditional is a statement that performs a certain action only if a specific condition is true.  For example, think of a game like Call of Duty, or any video game that has a health system.  The health system is based on specific conditions.  At the most basic level:

IF the player’s health is at 0 or less, THEN the character is dead or disabled.

The pseudo-code for this test might look like:


If(player’s health <= 0)
Player is dead or disabled

In this case, the test or condition for the if statement is ‘player’s health <= 0’ (as a reminder, <= means “less than or equal to”).  The test condition is either true or false.  In computer science, the Boolean data type represents values that are true or false, so a test condition will result in a Boolean value.  It is either true that the player’s health is 0 or below, or it is false.

The resulting action, if our test example is true, is that the player’s status is somehow marked as disabled.  And of course, there can be further actions, depending on the game.  The player might lose a life, or even get a game over screen.

If the condition is not true, then the game will skip the actions for that condition, and go on to the next line of code after the if statement.  If the condition is true, the game will do all the actions for that condition, and then go on to the next line of code after the if statement.

Let’s look at how this might look in Java:

int playerHealth = 100;
if(playerHealth <= 0)
{
//Game over, perhaps!  The appropriate code would be placed in this block.
}

JavaScript would look similar:

var playerHealth = 100;
if(playerHealth <= 0)
{
//Game over, perhaps!  The appropriate code would be placed in this block.
}

You can also use conditionals to check if the player has met the conditions to complete a level.  For example, I am working on a puzzle game called Brain Bouncer, where players move geometric pieces on a board, and then bounce a ball off of the pieces to hit yellow stars on the board.  As the ball hits a star, that star is removed from the board.  It’s kind of like pool, but with a puzzle element.  The condition for completing a level is that no more stars are on the board, and the game checks that condition every time a star is hit.  The code might look like:

//This code is called after a star is removed from the board
if(starsLeft == 0)  //Assume we have previously created a variable called starsLeft.  == checks for equality
{
  finalScore += 1000; // Add 1000 points to the finalScore variable
  goToNextLevel();  // Assume we have a function that takes the player to the next level
}

Here’s what the board might look like:

If you want to play a demo of my Brain Bouncer game, you can check it out at https://cloudyheavengames.itch.io/brain-bouncer

ELSE and ELSE IF Statements: What If the First Condition Isn’t True?

We’ve looked at a very basic conditional statement, in which we were only checking for one condition and taking appropriate action.  What if we wanted to do a different action if the first condition is not true?  We can add an else statement.

Let’s look at Call of Duty again.  From a CoD fan wiki page:  “Whenever the player is below 33% of their maximum health, tunnel vision or a red tinge (depending on the game) will appear on screen, accompanied by a raised heartbeat and strained breathing.”  So if the player’s health is critically low, the screen might look like this (taken from the wiki page referenced below):

If you’re interested, you can read more about the health system and see more screenshots at http://callofduty.wikia.com/wiki/Health_system.  But in the meantime, let’s break this down into a set of conditional statements.

In pseudocode, the idea is:

if(the player’s health is below 33% of the maximum health)
{
- The player’s heartbeat should be raised
- The player’s breathing should be strained
- The screen should have a red tinge
}
else
{
- The player’s heart rate should be normal
- The player’s breathing should be normal
- The screen should have normal coloring
}

Notice that the if statement has a specific test condition, in between parentheses, while the else statement does not.  The else is optional, and it implies that the action should apply to any other case not caught by the if statement, almost like a default.

If we have multiple conditions to check, we can also use else-if statements.  Take Minecraft, for example.  When a player dies, the game displays a “death message” to other players.  According to the official Minecraft wiki page, “These messages communicate how the entity died, and some are intended to be humorous.”  So the exact message will depend on how the player died.  Let’s look at how that might look in Java:

if(causeOfDeath .equals(“drowning”))
{
  System.out.println(“Player drowned”);   //Prints out a statement to the screen
}
  else if(causeOfDeath.equals(“cactus”))
{
  System.out.println(“Player hugged a cactus”);
}
else if(causeOfDeath.equals(“magma block”)
{
  System.out.println(“Player discovered the floor was lava”);
}
else
{
  System.out.println(“Player died”);
}

If you want more information on death messages in Minecraft, you might want to check https://minecraft.gamepedia.com/Health#Death_messages.

Important notes:
• Else and else-if statements are optional
• Else must be the last statement, if you decide to include it, and it does NOT have a test statement
• The last else statement is like a catch-all, for all cases that didn’t match any of the previous tests

Conclusion and Challenge

So that’s it for our brief introduction to conditionals! This overview should give you a pretty good idea of what they are, and how you can apply them.

Can you think of other examples where conditionals are used in some of your favorite games? Feel free to leave your ideas, and any questions or concerns, in the comments!

If you want to play a demo of my Brain Bouncer game, you can check it out at https://cloudyheavengames.itch.io/brain-bouncer

JOIN OUR NEWSLETTER
Sign up to receive updates about new tutorials, game news, and educational opportunities.
We hate spam. Your email address will not be sold or shared with anyone else.

Share This:

Tagged , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published.