Space Shooter 2D — Part 2
Player Movement, Pseudocode, & An Brief Explanation of Variables
I was excited to get home today and begin working on the first steps of this game. I am using simple cubes and shapes of different colors to present the player ship, enemies, and laser fire for now in order to focus on getting the code to work just right.
In the code, I have defined a variable for the speed. This is set to ‘private’ so that it cannot be accidentally changed by another script and set to show in the inspector so that I can change it during run-time without having to edit the script. Note the underscore in front of the variable name and the ‘f’ at the end. The underscore lets anyone reading the code know that the variable, speed in this case, is a private variable. The ‘f’ tacked onto the end of the initial value stands for float, letting the software know to expect decimals.
There are several different types of variables: int, float, bool, & string are just a few of the most common.
- “int” stands for integer data type and is used to store positive or negative whole numbers such as 5, -32, or 123.
- “float” as mentioned earlier, stores floating point values, or decimals such as 2.34 or -12.5.
- “bool” is short for boolean. These are simple true/false values. I like naming these with names like isThePlayerDead to make them very easy to remember!
- “string” can be used to store , well, a string of alpha-numeric characters such as a name or other text. boatname = “Boaty McBoatface”
Anyway, the player movement code is complete. And, as you can see, it seems to work well! Using the arrow keys, I can move the player mockup around the screen and even go from one side to the other. I posted the code below along with some explanations, assuming you are still reading….
— — — — — — It’s not magic but it is FUN!! — — — — —
Here is what is going on…
Line 7 — initialize the variable _speed. I could have named this anything, but it helps to keep your sanity of you make the variable names sensible.
Line 9–12 — This section is run as soon as the object this script is part of gets loaded into the game. I want the player to ALWAYS start in the bottom center of the playable area, so this line of code moves (or translates) the game object from what ever position it starts in to a specific point in 3D space (x, y, & z). I am not using the Z-axis, so it will always be zero. How did I get the other values? I placed the player game object in the scene view and copied the coordinates from the inspector. :-) Sneaky, huh?
The next section, lines 14–17, contain the Update method. This section is called every frame while the game is running. This is usually 60 times per second but it can vary. Inside this method is a method called CalculateMovement. That is a method that I created (and named) to handle the calculate movement based on the user inputs. I could have put all the code in the Update method instead of making a separate method for it but later, when I add more functionality to the player, like lasers (pew! pew!) it could get very messy!
Now the last section, lines 19 to 46, is where some strategic thinking came into play. Time to talk quickly about pseudocode.
To create a section like this, a programmer thinks it through logically. Pseudocode helps without having to worry about syntax when a stroke of genius strikes. While not verbatim what I had before writing the actual code, here is a very close approximation…
// ←these back slashes mean this line is a comment and ignored by the complier. ←this text was meant as an explanation and I didn’t actually put them in my pseudocode.
Pseudocode is just plain english logic for what you want to have happen in your code. From this you can flesh out your script with the correct commands and correct syntax and just like that you are Buck Rogers.
Come back tomorrow for the next installment. I plan to add laser pellets.