< Prev  |  Home  |  Next >

Introduction to pygame

In these tutorials, we’ll be learning how to use the python ‘pygame’ module to make a 2-dimensional version of Minecraft.

My 2D Minecraft game

In this game, you’ll be able to move around a map, and pick up various ‘resources’ (for example grass, water, etc.) You will then be able to place these collected resources around the map, allowing you to create your own world!

This first section is just to make sure that pygame is running properly on your computer, and to demonstrate a very simple ‘game’. I say ‘game’, because it probably won’t win any awards.

To install pygame, simply go to the pygame website, and download the correct version of pygame for your version of Python / operating system.

The best way to see if pygame has installed correctly, is to run a program that is simply:

If there are no error messages displayed, then it means that the pygame module was installed successfully.

Here’s probably the simplest pygame program you can run:

Run the program above, and you should see a window:

First pygame

Not very exciting yet, but it’s a start. To understand what is happening, read the comments in the program above. That’s what comments are for!

I’ll explain a couple of things in the program we created. Firstly, all the graphics that we draw will be drawn to a drawing surface. In this case, we’ve called it DISPLAYSURF (but you can use another name if you prefer). There’s also an infinite ‘while‘ loop (the ‘while True’ loop), which can be considered our game loop. At the bottom of our game loop is a line to update the display, which means that our game loop will continually check whether the user wants to quit, and if they don’t want to, it will update the display.

Let’s add something to the drawing surface; a green square. Add the following line to your program, just before the line that updates the display:

When drawing a rectangle, there are 3 things (parameters) that you need to provide. You need to give the name of the drawing surface you want to draw on, the colour of the rectangle, and the coordinates/sizes.

Drawing a rectangle

tuple is what we call a list that cannot be modified. So [0,255,0] is a list of 3 numbers, but (0,255,0) is a tuple. It’s a good idea to use tuples instead of lists when you know that the contents aren’t going to change.

The second parameter when creating a rectangle is a tuple containing the amount of red, green and blue (known as RGB) that should be combined to make a colour. See the ‘Colour Converter‘ challenge for more information on RGB colours.

The last parameter is another tuple, containing the x-coordinate, y-coordinate, width and height of the rectangle. In graphics programming, the x-coordinate is the distance from the left, and the y-coordinate is the distance from the top of the window. The top-left of the rectangle is drawn at the coordinates given:

Pygame coordinates

There’s a special name for a rectangle with equal width and height: a square! This is how your program should look:

Pygame rectangle

Challenge

Put lots of rectangles (of different colours) together to make a picture. You can even try drawing other shapes.

< Prev  |  Home  |  Next >