Project 1: Space Shooter
Using pseudocode to solve coding problems
This time to apply some logic to the player’s movement
It doesn’t matter what sort of game I or anyone else is making, at some point we have to decide how to limit the movement of our player. In my prototype’s case I don’t want my player to be able to go over the sides. To prevent that I have to apply some sort of logic to my movement. This is where thinking this problem through with a pseudocode comes in handy.
In the example I have two types of versions of pseudocode, a more verbose one and slightly more concise one. Both of these work, because they give an idea of what I want to happen when some condition is met. Of course I could give them even more code like look, but since I’m only figuring out how to implement the idea that I have, both of these are fine. Doing it this way, and not just writing the code out, makes you think about what it is that you really want to happen and what are its conditions.
It is true that the example I gave is quite simplistic, all I’m doing is bounding the movement of the player inside the screen area. So I could have written the code straight away, but would I have spotted that one tiny detail if I would have done it without the pseudocode. If I wouldn’t have spotted it, my player would have snapped back to the middle of either direction when colliding with the side.
One might argue that even this would have been easy to spot and correct, and they would be right about that. But if you don’t start using pseudocode in these simple tasks and build a routine to it, you will most certainly not use it when you are constructing that few lines long logical argument of yours. Debugging those kinds of arguments sure is fun, wish there was a way to figure out the logic beforehand…
Anyway after applying the logic, adding some variables and removing the now unnecessary pseudocode I’m left with this code, which produces the movement depicted in the gif under the title. However hard I push, the cube won’t go over the edge. And all I had to do for it was a minute or two of thinking about my problem and what it meant before doing the actual implementation.