Project 1: Space Shooter

Using pseudocode to solve coding problems

This time to apply some logic to the player’s movement

The movement of the player bound by simple logic.

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.

Two different forms of pseudocode.

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.

When the 2D nature of the movement is forgotten for a moment.

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…

New private variables for bounding the movement.
The logic to bound the player’s movement with Mathf.Clamp.

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.

The new Movement method to handle the player’s movement.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store