< Prev  |  Home  |  Next >

Animation

In this section, we’re going to add a cloud that floats above the map. First, let’s create a cloud image:

Drawing a semi-transparent cloud

I’ve lowered the opacity of the paintbrush, so that as the cloud floats across the screen we can still see that map beneath it. Let’s add some cloud stuff to our program:

We’ve set ‘cloudx’ to -200, so that it starts off the screen, to the left of the map. In our game loop, we just need to draw the cloud, and then move it slightly to the right each time. If the cloud x-position is a lot greater than the map width, then we just reset the x-position to -200 and use a random y-position.

Here’s how the cloud will look in your game:

A cloud on the map

The cloud moves a little too fast though, so let’s use a clock to slow it down a little. We’ll create a clock at the top of our game:

Then, at the very bottom of our game loop, we need to wait for the clock to tick a certain amount of milliseconds:

You might have noticed that we sometimes have a problem. If the cloud happens to have been drawn away from the map, then the cloud is ‘left’ on the game surface:

Problem with the cloud

This happens because nothing is being drawn over the top of where the cloud has been. To stop this from happening, we just need to clear the screen at the very top of the game loop, using this line:

Now all we need to do is add an icon and a title to our game:

Here’s a world that I managed to create:

My 2D Minecraft game

Challenge

Create multiple clouds that move independently, at different speeds. You could also add a bird that sometimes flies across the screen!

< Prev  |  Home  |  Next >