Part 1: Pygame building blocks

Pygame is daunting to look at understand initially. Especially to those uninitiated to programming or just initiated to block based languages like Scratch.

As a part of my work for a summer camp at Curiositygym, Mumbai, I dived into it and here is my walkthrough for all levels from super easy to hard.

Introduction

Pygame is a library over SDL for creating 2-D games in Python. In layman terms, it is some ready made code you can make calls to and it would do the job for you and make it easy to create games.

For this, you will need Python and IDLE installed and pygame installed on top of it.

Here are the links:

Download and install python (Please check your processor is a 32 bit one or 64 bit one). A 32-bit software will run on a 64-it one but not vice versa.

Install pygame (Here we shall use the pip tool that python uses to install packages or libraries like pygame.

python3 -m pip install pygame

If that does not work use further tips for installing pygame.

Game

We shall write a game to draw circles and rectangles and create walls and things.

The circle can be drawn on key and mouse movements and the circle can be drawn when the mouse moves and also change the colour of the cirlce when space is pressed. Basic event recognition and management will be covered here.

The circles are so powerful that when it comes in contact with the walls/things, it has the special power to destroy it.

Reach till here and you have covered all aspects of pygame and learn why Object-oriented programming is required.

Post this we shall write a full-fledged tile based game. First an easy memory game and then a game “Just 10”.

Super easy

    • Open IDLE
    • Create a new file and save it as “1_buildingBlocks.py” or any name that you would like.
    • The lines starting with “#” and in red are comments. Comments describe the code for the person writing the code and for those who read it. For you!

Initialize pygame using

#initialize pygame
pygame.init()

Create a window of size 500*500(length*width)

SCREENWIDTH = 500
SCREENHEIGHT = 500
SCREENSIZE = [SCREENWIDTH, SCREENHEIGHT]
SCREEN = pygame.display.set_mode(SCREENSIZE)

The while “True” is a loop, in the sense, it runs the commands in the loop again and again and checks what events (key presses/mouse presses) need to be handled and how?

while True:
    for events in pygame.event.get():
        if events.type == pygame.QUIT:
            pygame.quit()
            #till sys.exit() is not mentioned, it will not end the program
            #but pygame has quit, hence the for loop to get events from pygame
            #will fail and hence will get an error if program does not exit
            sys.exit()

The whole code is here with loads of comments:

#Import statements are to enable the code to use the functions from the library
import pygame
import sys
import os

#instructions to windows to center the game window in the center of
#the screen, which it might ignore
os.environ["SDL_VIDEO_CENTERED"] = "1"

#initialize pygame
pygame.init()

#500, 500 = width and height of the square shaped window on which the game can be played
#putting [500,500] in square brackets means that it is a list.
#A list is a data structure. In layman terms, it is a bag in which you can
#store the width and height of the rectangle together.

#"screen", is the handle that you always use to draw on the window. Hence remember the name

#The width and height ideally should be variables and assigned values in the
#beginning of the code. If later you need to use the same [500,500] again or
#want to change it to [300, 550], you can do it by changing the value of the
#variable at one location, instead of changing the number 500 to 300 at multiple
#places.
#And ideally these are constants, whose values you dont change during in the code
#Hence the variables are written in capital letters
#There is no "const" keyword in python (keywords are special words recognised by the
#programming language like "for", "if", "while" etc)

#Not the right way
#screen = pygame.display.set_mode([500, 500])

#Right way
SCREENWIDTH = 500
SCREENHEIGHT = 500
SCREENSIZE = [SCREENWIDTH, SCREENHEIGHT]
SCREEN = pygame.display.set_mode(SCREENSIZE)

#caption for the game
pygame.display.set_caption("My first game in pygame")

#The while loop, which keeps running for ever (becaue we have "while True")
#In computer systems, there is always an event loop always checking for user inputs
#(It keeps checking for key presses on the key board and whenever for example, "caps lock"
#is preseed all the letters you trype are printed in upper case.

#Similarly, an event loop which runs the code over and over again for ever, till
#you choose to quit is required in pygame.
#In that loop, you check for all events in the bag of events, like key presses,quit etc
#and take the right action

#here we shall first handle the "quit" event, when the "X" button is pressed on the
#pygame screen
#when it is pressed we want to exit pygame and close the window (sys.exit())
while True:
    for events in pygame.event.get():
        if events.type == pygame.QUIT:
            pygame.quit()
            #till sys.exit() is not mentioned, it will not end the program
            #but pygame has quit, hence the for loop to get events from pygame
            #will fail and hence will get an error if program does not exit
            sys.exit()

Set the caption for the game:

#caption for the game
pygame.display.set_caption("My first game in pygame")

The result:

1_buildingBlocks

Yay! We are done with the basic building blocks for the game. What next? Draw shapes? What say? Are you ready?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s