Python Programming for the Absolute Beginner, 3rd Edition

Before you can display any graphics, you have to first create a graphics window. Once you've created the window, you have your blank canvas on which to display text and images.

Introducing the New Graphics Window Program

Creating a graphics window with the livewires package is a snap. The New Graphics Window program creates an empty graphics window in just a few lines of code. Figure 11.3 shows the results of the program.

Figure 11.3: My first graphics window. Not much, but it's mine.

Importing the games Module

The livewires package contains several important modules, including games, which contains a group of key classes for game programming. You can import a specific module of a package by using the from statement. To import a module, use from, followed by a package name, followed by import, followed by a module name (or a list of module names separated by commas).

The first thing I do in the program is import the games module of the livewires package:

# New Graphics Window # Demonstrates creating a graphics window # Michael Dawson 5/9/03 from livewires import games

As a result of this code, I can use games like any other module I import. To get an overview of what the games module is all about, check out Table 11.1, which lists the most commonly used games classes.

Table 11.1: COMMONLY USED games MODULE CLASSES

Class

Description of Class Object

Screen

Provides a region on which graphics objects may exist, move, and interact.

Text

A graphics object for text displayed on a Screen object.

Message

A graphics object that is a special kind of Text object that disappears after a set period of time.

Sprite

A graphics object for images that can be displayed on a Screen object.

Defining Global Constants

Next, I define two global constants:

SCREEN_WIDTH = 640 SCREEN_HEIGHT = 480

These represent the width and height of the new graphics window in pixels—a single point in a graphics area.

Creating a Screen Object

Next, I start a main section and create the graphics screen:

# main my_screen = games.Screen(SCREEN_WIDTH, SCREEN_HEIGHT)

This causes a graphics window, 640 pixels wide by 480 pixels high, to spring into existence and be displayed. The new Screen object is assigned to my_screen.

When you create a Screen object, you may pass values of width and height; otherwise, their respective default values of 640 and 480 are used. In this case, I could have written my_screen = games.screen() and achieved exactly the same results. It's also important to note that you can have only one active Screen object at a time. If you try to create a second, you'll raise an exception.

Though not an exhaustive list, Table 11.2 describes some of the most useful Screen methods.

Table 11.2: USEFUL Screen METHODS

Method

Description

set_background(image)

Sets the background of a Screen object to image object image.

mouse_pos()

Returns the position of the mouse pointer on a Screen object.

mouse_visible(on)

Sets the mouse pointer to visible or invisible. on can be True or False.

mainlooop([fps])

Starts a loop that draws all of the graphics objects associated with the Screen object. Takes an optional argument, fps, the number of frames per second to update the Screen object. The default value is 50.

all_objects()

Returns a list of all objects associated with the Screen object.

clear()

Destroys all objects associated with the Screen object.

quit()

Stops mainloop() and destroys the Screen object and all objects associated with it.

HINT

All of these methods are important, but don't bother trying to memorize them. The table is just meant to give you an overview of what you can do with a Screen object.

Invoking a Screen Object's mainloop() Method

The final line in the program is

my_screen.mainloop()

This kicks off the Screen object's event loop, which updates the graphics window, redrawing every graphics object 50 times per second.

TRAP

Just as with a program that use Tkinter to create a new window, you shouldn't run a livewires program from IDLE. Instead, run the program directly, by double-clicking the program's icon in Windows, for example.

Категории