Game in Python
Yes, this time we are going to be looking at making games. No, I'm not that good a Pygame, before you all start hounding me in the comments. That's because I prefer the more classical approach of using the CLI for graphics in Python. However, I can show you the basics, so let's do that.
Pygame is one of the most, if not, the most well known Python module for games (although, I prefer Pyxel for it's retro feel). So, why not start with that. First, like always, we need to import our modules:
pygame is not part of the standard library, so make sure that you use pip or Repl's Package Manager to install it before running the code.
If you are using Repl, you should use the Pygame found in the language choices, this way
pygameis installed, and you can test the game out in Repl.
Make The Game Already!
Okay, okay. So, first we must initialise
pygame, like so:
# initialise pygame pygame.init()
pygame ready to use, so don't forget this step. Now, let's get our
# Create our screen with a width and height screen = pygame.display.set_mode((32*8, 32*8)) # Make our pygame clock clock = pygame.time.Clock()
First of all, we make our screen by using
pygame.display.set_mode(), this takes in a tuple containing the width and height of the screen. We will be using
32*8 as our character is going to be
32 x 32. This will make it so that there is going to be 8 spaces for it to move in. The
clock will later be used to set the games FPS.
Let's now make some colours:
# Let's predefine some colors for clarity, these are easy on the eyes, and look good DARK = (50, 50, 50) LIGHT = (210, 210, 210)
These are stored in tuples, where the first value is red, then green, the blue. This matches the colour system which uses the good old RGB system. The reasoning behind the two chose colours is that:
- I use them all the time.
- They look good.
- They are easy on the eyes.
- They are easily distinguishable.
That's it, you can make them whatever you want them to be, but I like these two. Now we will make a rectangle and surface for that rectangle:
# Let's make a rectangle, with x and y and width an height rect = pygame.Rect((0, 0), (32, 32)) # Then make a surface with a width and height surface = pygame.Surface((32, 32)) # And fill the surface with a color surface.fill(LIGHT)
rect has a tuple with its x and y, and one for its width and height. This is so we know where to put it, and its dimensions. Then we also have the
surface which has the same width and height as the
rect. We will then
surface with the
Now let's start our game loop:
# Start our game loop while True: # Let's set the clock's FPS to 60 clock.tick(60)
This will loop forever, setting the
clock.tick() to 60, which is our FPS. Now let's add some events in!
The first one will check to see if the user closes the window:
# Now we can start the event loop for event in pygame.event.get(): # If the event type is QUIT if event.type == pygame.QUIT: # Then we end pygame pygame.quit() # And quit the program quit()
We get each
event that has collected in
pygame.event.get(). We then check if the
event's type is
pygame.QUIT, if so then we end the
pygame instance and
quit() the program
. This checks to see if the user has pressed thex` button to close the window.
Now for player movement:
# Otherwise, if a key is down elif event.type == pygame.KEYDOWN: # And that key is w if event.key == pygame.K_w: # Move the rectangle up rect.move_ip(0, -32) # And that key is s elif event.key == pygame.K_s: # Move the rectangle down rect.move_ip(0, 32) # And that key is a elif event.key == pygame.K_a: # Move the rectangle left rect.move_ip(-32, 0) # And that key is d elif event.key == pygame.K_d: # Move the rectangle right rect.move_ip(32, 0)
Once again, we check to see if the
event type is
pygame.KEYDOWN. If so, we then check if the key that is down is either
d. If it is one of these, then we move the
rect.ip_move(), using a tuple to specify how much to move in the 2D window. I'm using
32 as the speed here, as it snaps the player over by one, if you imagine it as a grid.
pygame, like most other game modules, uses a Cartesian Coordinate System, from top left, to bottom right. this means that the top left most point is
0, 0, and the bottom right most point is
width, height. So we have to use negatives to go up/left, or positive to go down/right.
Using the Cartesian Coordinate System, the horizontal and vertical are instead called x and y. So, if we wanted to move left, we would move
-32 on the x, and
0 on the y`.
If you don't understand this system still, then have a look at this reference. I hope it helps.
So this is our player movement all done. Phew.
Now, all we have to do is the rest of the update:
# Clear the screen by filling it screen.fill(DARK) # Then blit (add to the buffer) our surface with our rectangle on screen.blit(surface, rect) # Then update the display to show the buffer pygame.display.update()
Which consists of us clearing the screen by filling it in with the
DARK colour using
screen.fill(). The we blit
rect and add it to the buffer. And then call
pygame.disply.update() to show what we have.
When you run this, you should see an off-white square, which you can move with
wasd. It's simple, but you can make it into so much more.
You should finally be able to make games using Pygame. This was only a simple example, so try to expand it into something more interesting. Hope you enjoyed the tutorial.
Have a great day!
This tutorial is the last in a series of tutorials suggested by @enigma_dev .
If you have any suggestions for tutorials, leave them in the comments and I'll be sure to have a look. If you like one in the comments, then give it an up vote to show that you want to see it. It makes my life so much more easier. Thanks in advance!
This is super helpful, thanks. Note that if you're building this project in repl.it, you need to create the repl using the "Pygame" template and not Python. Using IDLE I had no issues.