Name: Moving Enemies Animated
Description: In this pattern a static enemy is animated to make some limited movements around its home position, for example bobbing or bouncing.
Need for Pattern: Having a moving enemy is a way to increase the challenge of the player. It also gives a sense of movement and excitement to the game.
Related Game Patterns: Add Static Enemy [required], Add Moving Enemies Patrolling [related]
Links to other Computing Patterns: , Change Listener, Systems Dynamics
We add enemies like we add food to the game. Following this tutorial will add static enemies to your game. Click on the tilemap image for your first level. Create a totally Red tile in My Tiles. Add one or two red blocks to your first level.
For first line here reads for element value of array of all… This line contains a value and a list. The loop keeps running until it runs out of a values in the list. In this case create one item of Food for every yellow block.
Duplicate this loop section. Now we will change the for loop will turn the red squares in a tile map into a sprite of kind of Enemy.
Drop the copied loop back into the function after the original one.Change the values of content of this for loop. Change the yellow square to a red one. Change the name of food1 for the two blocks for it to mentioned. I’ll choose to create a new variable called staticEnemy and change the image too this time to snake.
Check your code with the example below.
We now code what happens when our player overlaps with the enemy our staticEnemy. Drag in an on player overlap with block from Sprites. Set the second value to be Enemy. Inside the block drag in from Game block of game over and keep it set to Lose.
We can add a bit more challenge to our game by making otherwise static enemies move around a fixed point using animation.
We alter our static enemies to animate them. To allow us to do this go to Advanced > + Extensions > Add Animation
We can now drag in an animate block which has an animation effect on it. Alter it so it matches the one below in our for each element loop so that it applies to all Enemies. I change the type of animate from the drop down menu to use the simple bobbing in place animation.
You can see here that for simplicity’s sake I’ve have not changed the variable name of staticEnemy. But you can change it or create a new variable if you want to.
Test your game to check that your changes have the desired behaviour and that there are no side effects. For example, one side effect that you might have if you already have more than one level is that enemies from a previous level may appear on your next level. See the Add Levels tutorial to fix that.
To check that you are making the most of this pattern you can ask yourself the following questions:
This Game Pattern is one of many allowing you to make improvements to your platform game and to learn coding and wider computing concepts. Find more on the Game Pattern page.
In this pattern we replace our static enemies with animated enemies. If you want to have both kinds of enemies then you can do this by following the patterns shown in the add a patrolling enemy tutorial.
Also this pattern may make your game much more challenging. To balance it out a potential next step may be to add the jump on enemies pattern to your game if you haven’t already.