Making Decisions

Your game code can make decisions while the game is running by use of if. if works just like it does in the English language.

*English*
If the character name is Bob then give him 100 hit points.

*Code*
If charName = "Bob" then hitPoints = 100
*English*
If the player has no lives then the game is over.

*Code*
If playerLives = 0 then gameOver = true

Comparisons

if playerLives = 0 is a comparison. You can also make other comparisons.

<> means does not equal.

*English*
If it isn't Bob then he gets 50 hit points.

*Code*
If cName <> "Bob" then hitpoints = 50

> means greater than.

*English*
If the player has more than 0 lives the game isn't over.

*Code*
if playerLives > 0 then gameOver = false

< means less than.

*English*
If Bob has less than 1 hit points then he is dead.

*Code*
If hitPoints < 1 then BobAlive = false

<= means less than or equal to.

*English*
   If the enemy building has less then 50 hitpoints bob can take control of it.
 *Code*
   If buildingHp <= 50 then buildingOwner = "bob"

>= means greater than or equal to.

*English* 
  Bob needs at least 50 mana points to cast fireball.
*Code*
  if manaPoints >= 50 then CastFireBall()
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution 2.5 License.