Menu
IDS - Basic Game Code
Basic Video Game Code
Good programs require good management of information. To manage this information, we need to use variables (think of a container that holds information that can change or vary over time) with good names.
Variable names should be a single word and be descriptive. If you need to use multiple words, make sure there are no spaces and that each word has the first letter capitalized.
Example: BankBalance would be a good name for a variable that holds the amount of money in a bank account. We can make the value of it equal to whatever we want by using SET.
We can add / subtract to that variable by using CHANGE.
Most video games follow the basic structure below...
Variable names should be a single word and be descriptive. If you need to use multiple words, make sure there are no spaces and that each word has the first letter capitalized.
Example: BankBalance would be a good name for a variable that holds the amount of money in a bank account. We can make the value of it equal to whatever we want by using SET.
We can add / subtract to that variable by using CHANGE.
Most video games follow the basic structure below...
- Set up the starting positions (and vectors) On Start.
- If a button is pressed, then set the vector accordingly - DO NOT MOVE ANYTHING YET !
- In the Forever loop:
- Slow down the clock
- Erase the moving objects
- Move each objects by the appropriate vector
- Check for collision
- Respond to collision (bounce, stop, remove, relocate)
- Display the objects in the new location
Game Boundary Collision |
After the object is moved by a vector, but BEFORE we display the new location, a collision check should be done.
Check collision by asking IF an object is located at a particular position. In the case on the right, we ask if the X value of the ball is > or = 5. If it is, THEN that means the object has gone off the right side of the screen (collision). Response - If a collision has taken place, a response happens inside of the IF. In this case, relocate the X position of the ball is set to zero (left side). Note: This example only shows collision with one side. Most games will require a collision check for each side of the screen. They will also require collision checks for penalties and rewards. |
Game Object Collision |
Collision of two objects will usually result in a penalty or reward. To determine if two objects collide, we need to determine if their coordinates are the same.
In the example on the right, we use the AND operation to determine if the bullet is in the same location as the alien. If it is, then we set the location of the bullet and alien off screen. |
Video Game: More Control |
There are only two buttons on the MicroBit, but you can add your own to pins 0, 1 and 2. You just need to follow these steps:
* note that there are pre-made circuits in TinkerCAD that have multiple buttons added to it already. |
Multiple Movement Example:
In the example on the right, we have a program to move 3 different objects in 3 different directions.
Notice that after the clock ticks (WAIT), then we erase the current position of our three objects (UNPLOT). Then we adjust the coordinates by the vector (CHANGE). After all objects are changed to the new location, we display them in the new locations (PLOT) At this point we have not yet added any collision checks. |