Place to find helpful snippets of code to use in making your own games.

Tuesday, October 19, 2010

Basic character movement

Moving a character is probably the first thing that you want to do when making a game. There are many different ways to move a character however. This time I will only cover a very basic way of moving a character, I will post more movement examples later. But its good to start from a simple example and it should be enough to get you started making games.

Movement Types
This snippet will show 4 directional movement. Which means the character is will only move in one of four directions. This is an extremely simple movement type but is still very applicable for a lot of games. Platformers for example only use 2 directional movement, right or left, with the added jumping and climbing mechanics in some games. Pacman is an example of a game that uses 4 directional movement even though you are limited to continue moving in that direction until you hit a wall. More complex example of movement are ones such as 2D shooters which either have 8 directional movement or 360 degree movement. Almost all utilize the arrow keys on the keyboard, or a D-pad/thumbstick on a gamepad. Some like the point-click movement only use the mouse. New styles of movement are possible with the emerging multi-touch screens and are especially applicable to iphone and android game developers.

So this snippet needs to handle events, for simplicity I will only use the basic KeyListener interface in java.awt.event. It works well enough for a demonstration of movement. If you want a more game oriented input system see my Keyboard Input Polling System In Java post which goes over how to turn the interrupt driven awt event system into a polling system.

Aside from character movement there are other types of movement some simple and some complex. For instance, click to move here types of movement, these can actually be a bit more complex and utilize path-finding algorithms (i.e. A* for shortest path), which I hope to cover later as well. There is also tactical movement or tile based movement, used mostly by turned based games, where the play field is a grid partitioned into tiles and each turn a character has a specific number of movement points or speed which allows them to move a certain number of tiles in certain directions or along a path around the play field.

Buggy Movement
I remember when I first started coding games my movement code was hideous compared to what I use now. One of the first implementations I used was to increment the characters (x,y) position every key pressed event. The problem with this approach is that it relies heavily on the speed and number of interrupts. I also used to do a lot more in the KeyListener interface, like doing collision detection and almost all the game logic using the key events instead of a timed update call. Those are just a couple examples of things that might create some buggy movement. A much more subtle cause of buggy movement for more advanced game implementations is due to tying the amount to increment (x,y) to a single update. So depending on how many updates you get per second the character will move that much. This can become a problem if the updates happen to take longer than expected or shorter than expected. So tying an objects movement, not just characters, to how much time passed between calls will fix most problems. Of course determining when an by how much to move an object each frame can get quite complex.

Example Details
The snippet for this post uses the KeyListener interface which many know is interrupt driven meaning the operating system your running under sends your program key events, through whatever it's underlying system may be. We don't want to do much at all when we get a key event and certainly no incrementing of anything that affects the game. We simply want to change the state of the keyboard and only the keys we care about. For instance the up-arrow key, we will store whether it's pressed or not. Right now for movement we only really care about doing something specific for the length of time it is being pressed. So we simply save a single boolean state (true/false) for each key we care about. So for 4-directional movement we just want to know if the up, down, left, right arrow keys are pressed (true) or not (false). This makes it simple to write code that only happens when a specific key is down (pressed).

Simple enough so what do we do while the key is down? Well since this is an example of player movement we move the player...in one of 4 directions either north, south, east, or west. Ok well here is the basic character movement snippet, it is based off of the BufferStrategy snippet.