To answer that question you should first know that a lot of math and math concepts are used in creating games. That is true even in such simple games as Space Traffic which is shown and can be played below. Just click on the big Start button in the middle of the screen.
If you are thinking about writing games, plan on getting at the very least, comfortable with basic math. And really the more math you learn the better because the logical thinking that you learn along with the math will help you in your programming career.
If you have a chance, play the game below and then look at the text below it detailing some of the math used in the game. Don't be alarmed at the complexity of the math/code used in the game as once you have a good math and logic foundation you'll be able to write such code yourself!
So if you played the game you can see that it is a simple game with only one level. Now check out the stats below on just some of the math operations used in Space Traffic. This doesn't include the math used in the high score API which is a different program altogether.
- 139 variables defined
- 94 Comparison statements ( >, <, =, >=, <= )
- 4 equations using Pi
- 4 equations using cosine
- 4 equations using sine
- 2 boolean operations
- 75 variables defined with values
- 39 boolean variables defined
- 2 negative integers used
- 22 equations using addition
- 3 equations using subtraction
- 15 equations using multiplication
- 4 equations using division
- 4 arrays defined
- 47 statements using arrays
What does some of the math look like? Below are just a few samples of code from the game to give you an idea. Included are lines to give you an idea of some of the simple as well as complex math that is used in this game.
Variables defined for the cars:
- public var _car1:Car1;
- public var _carFrame:CarFrame;
- public var _carExplos:CarExplos;
- public var _cars:Cars;
- public var carStartersArray:Array;
- public var carsArray:Array;
- public var carStarterPosArray:Array;
- public var carTimer:Timer;
Some geometry and even Pi is used in the game to calculate each car’s x and y position on the screen:
- carsArray[m].x += carsArray[m]._speed * (Math.cos(carsArray[m]._angle * (Math.PI/180)));
- carsArray[m].y += carsArray[m]._speed * (Math.sin(carsArray[m]._angle * (Math.PI/180)));
Yes, the statements above are fairly complex but there are lots of tutorials and sample code on the web and in books on how to move objects across the screen. Of course, the code would have been simpler if the cars were only moving vertically or horizontally and not at an angle.
The comparison statement below checks to see if a car’s x or y location puts it off of the screen. Does usage of the x and y variables ring any bells? It should, as the x and y variables used below are basically coordinate grid points like those taught in math class.
- if (x < -100 || x > 700 || y < -100 || y > 700)
At the start of each game, the line below sets the game score variable to zero
Below are just two of the many Boolean operations used. The first statement checks to see if the animation for the explosion that occurs when two cars collide has completed. If so, then the gameOverBool variable is set to true which will cause the game to end.
- if (ExplosionOverBool == true)
- gameOverBool = true;
Note that the “==” in the statement above is not a typo. The first statement is doing a comparison to see if the ExplosionOverBool variable is true. It isn’t assigning a value to the ExplosionOverBool variable. The second statement with a single “=” sign is actually setting the gameOverBool variable to “true”.
Questions? Feel free to contact us at [email protected]. If you found this page or our site helpful or fun please like us on Facebook. Your support is appreciated by all of us here at MathNook.