Pygame

 

Pygame is the best module so far, helping in game development. Pygame is a module used to build games that are usually 2D. Knowing Pygame proficiently can take you far in developing extreme ranges of games or big games. Pygame is a set of Python modules designed for writing video games. Pygame adds functionality on top of the excellent SDL library. This allows you to create fully featured games and multimedia programs in the python language.

Luckily, PyGame has a way to enumerate all the fonts available on the machine: allfonts = pygame.font.getfonts Additionally, there's a way to instantiate the default system font.

This tutorial is the first tutorial in a series of five Pygame tutorials:

Learning Objectives


These instructions will guide you through the first few steps required to set up the foundation for a Pygame project.

Now that you have learned the basics of Python you are most likely willing to start creating your own games. Pygame is one of the best libraries to create mainly 2D retro arcade games such as Tetris, PacMan or Space Invaders.

Pygame events

Let’s see what are the first few steps needed to create your first game in Python.

Installing Pygame


Make sure you have access to the Pygame library. If not you will need to download and install it following the instructions provided on the Pygame website: http://www.pygame.org/.

Step 1: Importing and initialising the Pygame library

Pygame blit
Your Python code will need to start with the following two lines of code:
17
19
21
23
25
27
29
31
33
35
37
39
41
43
45
47
# The loop will carry on until the user exit the game (e.g. clicks the close button).
# The clock will be used to control how fast the screen updates
whilecarryOn:
forevent inpygame.event.get():# User did something
carryOn=False# Flag that we are done so we exit this loop
# --- Game logic should go here
# --- Drawing code should go here
screen.fill(WHITE)
#The you can draw different shapes and lines or add text to your background stage.
pygame.draw.line(screen,GREEN,[0,0],[100,100],5)
pygame.draw.ellipse(screen,BLACK,[20,20,250,100],2)
# --- Go ahead and update the screen with what we've drawn.
clock.tick(60)
#Once we have exited the main program loop we can stop the game engine:

Your Challenge

Pygame
Pygame click eventUse the code above to create your first Pygame project.

Complete the code to draw the following background:

Next Step?


Your background is ready? Let’s add the first sprite to your project by completing the next tutorial:

Pygame Install



Other challenges you may enjoy...

Tagged with: Object-Oriented, OOP, PyGame