Balloon Ninja: Creating a screen

Back to Tutorial Contents

In this first section of the Balloon Ninja tutorial, we will install Pygame and create an empty screen.

Installing Pygame

If you are using ubuntu, you can install pygame in one line:

$ sudo apt-get install python-pygame

Creating a screen

Here is the bare-bones code to make a game window appear on your screen. Create a directory wherever you save your programs, and call it “balloon_ninja”. Save this code as “balloon_ninja.py”, in your new balloon_ninja directory:

import pygame

def run_game():
    # screen and game parameters
    screen_width, screen_height = 800, 600

    # initialize game
    pygame.init()
    screen = pygame.display.set_mode( (screen_width, screen_height), 0, 32)

run_game()

We start by importing pygame.  Then we define a function called run_game that will manage the overall game play.  This function starts by defining our screen parameters, and then initializes our game and builds the screen.  Finally, we run this function.

The pygame documentation is quite good.  If you are interested, check out the documentation for the display.set_mode function we just used.  You might also want to take a look at the pygame.Surface documentation if you are already writing your own game.  The call to pygame.display.set_mode returns a pygame Surface, which  is used for drawing our game.  Our screen is a pygame Surface.

If you run this code, you will not see much.  A black window will appear, and then disappear after a moment.  Let’s make the window stick around until we choose to close it.  Add a call to “import sys” at the beginning of your file, and then add a main event loop to the run_game function:

import pygame
import sys

def run_game():
    # screen and game parameters
    screen_width, screen_height = 800, 600

    # initialize game
    pygame.init()
    screen = pygame.display.set_mode( (screen_width, screen_height), 0, 32)

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

run_game()
The simplest empty window created by Pygame.

The simplest empty window created by Pygame.

This sets up an infinite loop once the screen is displayed.  Within this loop, we are constantly watching for pygame “events”.  These events are actions such as moving a mouse, clicking a mouse button, pressing a key, or any other action a user might make.  The only way to break this loop is to initiate a pygame.QUIT action, which for now is just pressing the orange x in the top left corner of the game window.  When that button is pressed, we call sys.exit to quit our game.

Let’s do one simple thing to our screen before we start playing with balloons: let’s set a background color for our window.  We need to modify our game and screen parameters to include a background color.  Then we need to add some code to the event loop, which will redraw the background on every pass through the loop:

import pygame
import sys

def run_game():
    # screen and game parameters
    screen_width, screen_height = 800, 600
    bg_color = 150, 150, 150

    # initialize game
    pygame.init()
    screen = pygame.display.set_mode( (screen_width, screen_height), 0, 32)

    # main event loop
    while True:
        # Check for game events
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

        # redraw the screen, every pass through the event loop
        screen.fill(bg_color)
        pygame.display.flip()

run_game()

Simple game animation works by drawing on one screen, and then displaying that screen. When something changes, we draw on a hidden screen, and then display that second screen when we are finished with our new drawing. Pygame manages much of this work for us, and we see that in the screen.fill and pygame.display.flip lines. When you run this code, you should see an empty gray window:

An empty screen, which we will use to create our game.

An empty screen, which we will use to create our game.

In the next section, we will add a balloon to the screen, and make it rise to the top of the game window.

Next:  Releasing Balloons

Back to Tutorial Contents

Advertisements

About ehmatthes

Teacher, hacker, new dad, outdoor guy
This entry was posted in programming and tagged , , , , . Bookmark the permalink.

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