Need Help! Contact me.Send Feedback Thank You.

Game development in Python

Python Game Development

The scripting language Python is easy to use, and thus very convenient to program simple games. And to make it even simpler, there one Python module which is dedicated to games creation: pygame. It makes you life much easier when programming a game as it takes care of the user input, the graphics and the sound effects.

To install it, if you are using Ubuntu as I did for this tutorial, you can simply install it by typing this command in a terminal:

sudo apt-get install
pygame

If you are using another
operating system, please refer to the
downloads page
of the pygame website.<!>

 To initialize pygame, we need these 3 lines of code:

import os,sys

import pygame

from pygame.locals import *

Just try to run these 3 lines of code on your machine before going further, they are essential!

We can continue with a “Hello world” script that will simply initialize a window and wait for the user to close the window:

import os,sys

import pygame

from pygame.locals import *

# All pygame scripts start with the init() command

pygame.init()

# Initialize the window to 400*300 pixels with the “Hello world” title

screen = pygame.display.set_mode((400,300))

pygame.display.set_caption(“Hello world !”)

# Main loop of the game

while True:

for event in pygame.event.get():

if event.type == QUIT:

pygame.quit()

sys.exit()

pygame.display.update()

Next thing we have to do is actually display something on the screen. For this tutorial I will simply display a small car contained in a file called car.png. To load the image containing the car, just do:

car = pygame.image.load(“car.png”)

Yeah, it is that easy. Now we have to put the car on the display, for example at the position (100,100) and then update the display:

screen.blit(car,(100,100))

pygame.display.update()

Putting it together, this is what you should see on your display:

Python Game Development

 

 

Now we have to take care about the user input. Pygame offers a very convenient way to do this, using method we already saw: pygame.event.get(). Before, we detected the QUIT event to know when to exit the game, but we now want to detect when the user is pressing a direction key on the keyboard. This is done by this piece of code:

if (event.type ==
KEYDOWN):

if event.key ==
K_RIGHT: pos_x = pos_x + dx

elif event.key ==
K_LEFT: pos_x = pos_x – dx

elif event.key ==
K_UP: pos_y = pos_y – dy

elif event.key ==
K_DOWN: pos_y = pos_y + dy

So when a key is pressed, the software will update the position of our car by a small amount dx or dy, thus making the car move.

The last thing we have to care about before putting it all together is the speed of your game. Indeed, if we do not specify something at this point, the game will just run at the maximum speed possible. For this reason, we will create a clock with:

clock = pygame.time.Clock()

And then specify that the
game should run at 30 frames per second by inserting this command in
the main game loop:

clock.tick(30)

We can now put it all
together in a very basic game where you can move the picture of the
small car around:

import pygame, math, sys, os

from pygame.locals import *

# Init screen and car

pygame.init()

screen = pygame.display.set_mode((400,300))

car = pygame.image.load(“car.png”)

# Variables and constants for the game

pos_x = 100

pos_y = 100

dx = dy = 5

# Create clock

clock = pygame.time.Clock()

while True:

# Set clock to 30 fps

clock.tick(30)

# Catch user input

for event in pygame.event.get():

if event.type == QUIT:

pygame.quit()

sys.exit()

# Detect if a direction key is pressed

if (event.type == KEYUP) or (event.type == KEYDOWN):

if event.key == K_RIGHT: pos_x = pos_x + dx

elif event.key == K_LEFT: pos_x = pos_x – dx

elif event.key == K_UP: pos_y = pos_y – dy

elif event.key == K_DOWN: pos_y = pos_y + dy

# Clear the screen with a black background

screen.fill((0,0,0))

# Update display

screen.blit(car,(pos_x,pos_y))

pygame.display.update()

To conclude this introduction on game development with Python, I wanted to point out a nice cheat sheet where you will everything you need about pygame commands: http://inventwithpython.com/pygamecheatsheet.png.

The complete game is attached to the post.

Attachment Size
python-car-game.zip 6.98 KB
Subscribe to DeveloperFeedDid you like this article? Did it Help you Solve your Problem? You can get the all the latest articles published at DeveloperFeed in your email inbox by entering your email address below. Your address will only be used for mailing you the articles, and each one will include a link so you can unsubscribe at any time.