Experimenting with Pygame #1: My First Program

Games fascinates all including me :). Playing games is something dear to me like python. This actually inspired me to learn Game Development with my favorite language. So is Game Development possible with Python?

Of Course. Even without the game libraries it is possible to make small games in python. I have made a small connect four game without using game libraries in python. But for now let’s leave that aside and focus on Game development using a game library in python called PYGAME.

Pygame is one of the famous game library in python with more than million downloads. It is designed to make cool games in python which is built on top of excellent SDL library. SDL or Simple DirectMediaLayer is a low level library on top of OpenGL and Direct3D to interface with audio, keyboard, mouse, joystick, and graphics hardware. Hence Pygame is highly portable and runs in any platform you name.

Now lets get into Pygame directly by creating some cool things.

Lets start in the conventional way by creating a Hello World program. For a difference let it be a “First Pygame experiment” Program.

__author__ = 'Krishnan'

import sys
import pygame

# This will initialize all the features needed in pygame
pygame.init()

# We are specifying the size of the window needed to display
window_size = (600, 400)

# We are initialising the screen with the resolution specified
screen = pygame.display.set_mode(window_size)

# Creating a SysFont font object for rendering text
font = pygame.font.SysFont("Arial", 32)

# Setting the text to italics before rendering
font.set_italic(1)

# Rendering the text needed with proper text color and background color
text_to_render = font.render("My First Pygame Experiment", 1, (255, 255, 255),
(0, 0, 255))

# Game loop
# Games always don't close on their own. They run an infinite loop until
# the user himself closes it.
while True:
    # Looping over multiple events happening in the game environment
    for event in pygame.event.get():
        # Checking if the event is to QUIT from the game.
        # ex: pressing the close button
        if event.type == pygame.QUIT:
            sys.exit()

    screen.blit(text_to_render, (0, 0))
    # Method to regularly update the display according to the user
    pygame.display.flip()

Now lets discuss how the above was built. As always to access all pygame resources we need to import the module.

import pygame

After importing all the pygame modules we must initialize them. Each and every modules have their own initialization, but it is always good to do a master intialization immediately after import.

pygame.init()

Now the game runs on a window. Hence we create window with resolution of our choice.

window_size = (600, 400)
screen = pygame.display.set_mode(window_size)

Now we have to show the screen to user. For that we are using the following code

pygame.display.flip()

Every game does not end on itself. The User interaction only ends the game. Hence it is always wise to have a infinite loop running with a user interaction mapped to exiting the game window. The while loops infinitely till the event happening is a pygame.QUIT event. That will be user closing the window. Again inside the loop we regularly update the window.

# Game loop
# Games always don't close on their own. They run an infinite loop until
# the user himself closes it.
while True:
    # Looping over multiple events happening in the game environment
    for event in pygame.event.get():
        # Checking if the event is to QUIT from the game.
        # ex: pressing the close button
        if event.type == pygame.QUIT:
            sys.exit()

    # Method to regularly update the display according to the user
    pygame.display.flip()

Now for our business logic in the game. We are going to show the text “First Experiment in Pygame” in the window. For that we are using below code.

screen.blit(text_to_render, (0, 0))

If you see clearly the text_to_render variable is not a text. We cannot directly give a text to the pygame display. It throws the below error.


Traceback (most recent call last):
File "/home/krishnan/Krish-Python-Venture/Game Development/pygame_startup/hello_world.py", line 28, in <module>
screen.blit("Hello World", (0, 0))
TypeError: argument 1 must be pygame.Surface, not str

This means that pygame display always needs a Surface class object and not plain string. So lets create one.

font = pygame.font.SysFont("Arial", 32)
text_to_render = font.render("First Pygame Experiment", 1, (255, 255, 255),
(0, 0, 255))

Here we are creating a SysFont object which loads a System font to the display. We choose Arial font with 32 size and create a SysFont object. Then using the render method we create a font “First Pygame Experiment” on the display. render method takes in the text to display, antialias arguement which decides the smoothness of characters, font color and font background color. Here we create a font with size 32, white foreground color and blue background color.

Now we have our first ever experiment in pygame. A window with our text on it.

pygame_hello_world

Watch out for cool stuffs in pygame following this. 🙂


References

http://www.pygame.org/docs

Leave a comment