Game Making with Python and Pygame Part 2

in #python5 years ago

Creating a Blank Window

gmwithpp-p2.png

See Part 1 of this guide for how to get set up with an up-to-date version of Pygame and how to check that it works: Game Making with Python and Pygame Part 1

Creating a window using Pygame involves various steps before we can actually run the code and see an empty window.

Open your Python editor of choice, make sure it's set up to run the newest Python version you've installed, create a new file and then we can start getting things to a point where code can be run and a window will appear. A minimal Pygame program which doesn't actually do anything other than set up Pygame before closing it down nicely and tidying up after itself would look like this:

import pygame

pygame.init()

pygame.quit()

Initially, we import the pygame module, then we have to call init() on pygame to initialise all of its systems so that we can then use them. Finally, we call quit() on it to clean up after it.

To make a program that does slightly more, we need to create a window onto which we will then put all of our images. We set this up as a window so that we have the normal title bar, close button and borders etc. We set the size of the window as follows, but when we run it, unfortunately, it will display briefly and then exit.

import pygame

pygame.init()

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

pygame.quit()

Run the code, make sure you see a window. Try changing the size of the window. Note it's a tuple of values being passed as an argument rather than just the values themselves.

To make the window display until we close it we need to add an event loop, that will listen for an exit signal and then exit pygame, properly. Adding a while True loop, that then contains a for loop to check for pygame events, will let us keep the window open until the window is closed. We also need to move the pygame.quit() method call, into the loop after the QUIT event.

import pygame

pygame.init()

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

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()

If you run this the window will stay open until you close it, but it then gives an error: pygame error: video system not initialized although everything seems to close without any problems otherwise. If we add code to do a sys.exit when we close the window this seems to fix things (I'm not actually sure why). To do this we have to import the sys module first and then add the line for sys.exit.

import pygame
import sys

pygame.init()

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

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

If we then add in a line after we create the DISPLAYSURF to set the caption then we are getting towards a working minimum window setup.

import pygame
import sys

pygame.init()

DISPLAYSURF = pygame.display.set_mode((400, 300))
pygame.display.set_caption('Game Making Part 2')

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

Finally, to fill the whole window with a nice turquoise-y blue colour, we add in a single line to fill the display with the colour defined by (100, 200, 255), and then a line in the main loop to update the display, which is what is required to make our colour visible.

import pygame
import sys

pygame.init()

DISPLAYSURF = pygame.display.set_mode((400, 300))
pygame.display.set_caption('Game Making Part 2')
DISPLAYSURF.fill((100, 200, 255))

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    pygame.display.update()

This should give us a window that looks like this:

window.png

We are going to leave the second tutorial there.

In Part 3 we'll turn the code we have into a MainWindow class which will hold our game, and experiment with changing some values, before using an image as our background.

Coin Marketplace

STEEM 0.30
TRX 0.11
JST 0.034
BTC 66499.54
ETH 3203.31
USDT 1.00
SBD 4.14