Displaying a Sprite


Background images and text can spruce up a plain graphics window. But even a stunning background is still just a static image. A graphics screen with only a background image is like an empty stage. What you need are some actors. Enter the sprite.

A sprite is a special, graphics object with a graphics image that can make programs really come alive. Sprites are used in games, entertainment software, presentations, and all over the Web. In fact, you've already seen examples of sprites in the Pizza Panic game. The crazy chef, pan, and pizzas are all sprites.

While it would be cool to see a bunch of sprites flying around and crashing into each other, I start with the first step: displaying a single, nonmoving sprite.

start sidebar
IN THE REAL WORLD

Sprites aren't just for games. There are plenty of places in non-entertainment software where they're used . . . or misused. In fact, you probably know the most infamous sprite in application software history, Clippy the Office Assistant, the animated paperclip meant to give helpful suggestions in Microsoft Office. However, many people found Clippy obtrusive and irritating. One major online publication even ran an article entitled "Kill Clippy!" Well, Microsoft finally saw the light. Starting in Office XP, Clippy is no longer installed by default. A user must request him (and if a user requests him, he or she deserves him). So, while graphics can make a program more interesting, remember: Use your sprite powers for good instead of evil.

end sidebar

Introducing the Pizza Sprite Program

In the Pizza Sprite program, I create a graphics window and set a background image. This time, I use the background image from the Pizza Panic game. Then I create a new class based on Sprite and instantiate an object of this new class using the image of a pizza. Figure 11.8 shows the results of the program.

click to expand
Figure 11.8: The pizza image is not part of the background, but an independent object based on the Sprite class.

Setting Up the Program

I start the program just as before, by importing the games module and setting global constants for the graphics screen's height and width:

 # Pizza Sprite # Demonstrates creating a sprite # Michael Dawson 5/9/03 from livewires import games SCREEN_WIDTH = 640 SCREEN_HEIGHT = 480 

Creating the Pizza Class

Next, I create a new class, Pizza, based on Sprite:

 class Pizza(games.Sprite):     """ A pizza sprite. """     def __init__(self, screen, x, y, image):         """ Initialize pizza object. """         self.init_sprite(screen = screen, x = x, y = y, image = image) 

The only thing I do in the constructor method is invoke the Pizza object's init_sprite() method to initialize the sprite. You must invoke a sprite object's init_sprite() method every time you create one. You must supply init_sprite() with the Screen object the sprite will be associated with, x- and y-coordinates, and an image object for the graphics image of the sprite.

Setting Up the Screen

Next, I set up the graphics screen, just as before:

 # main my_screen = games.Screen(SCREEN_WIDTH, SCREEN_HEIGHT) wall_image = games.load_image("wall.jpg", transparent = False) my_screen.set_background(wall_image) 

First, I create a Screen object. Then I load the image of the brick wall and set it as the background.

Loading an Image for a Sprite

In order to create a sprite, you first need to load an image into memory to create an image object, like so:

 pizza_image = games.load_image("pizza.bmp") 

However, you'll notice one small difference from the way I load a background image. This time, when loading an image for a sprite, I did not include a value for transparent. The default value is True, so the image is loaded with transparency on.

When an image is loaded with transparency on, it's displayed on a graphics screen so that the background image shows through its transparent parts. This is great for irregular sprites that aren't perfect rectangles and sprites with "holes" in them, like, say, a Swiss cheese sprite. The parts of an image that are transparent are defined by their color.

If an image is loaded with transparency on, then the color of the point at the upper-left corner of the image, its (0,0) coordinate, is set as its transparent color. What this means is that all parts of the image that are this transparent color will allow the background image of the screen to show through. Figure 11.9 shows a Swiss cheese sprite on a solid, white background, ready to take advantage of transparency.

click to expand
Figure 11.9: A cheesy sprite, drawn on a solid-color background to take advantage of transparency.

If I load this Swiss cheese image with transparency on, every part that is pure white (the color taken from the pixel at the sprite's (0,0) coordinate) will be transparent when the sprite is displayed on a graphics window. The background image will show through these transparent parts. Figure 11.10 shows how the image looks when loaded with transparency on and off.

click to expand
Figure 11.10: On the left, the image is loaded with transparency on. On the right, the same image is loaded with transparency off.

As a general rule, you'll want to create your sprite image files on a solid color that is not used in any other part of the image. This transparent color must of course appear at the upper-left corner of the image, its (0,0) coordinate. Then, when the image is loaded with transparency on, it will allow the background to show through in all the right places.

TRAP

Make sure your sprite image doesn't also contain the color you're using for transparency. Otherwise, those parts of the sprite will become transparent too, making your sprite look like it has small holes or tears in it as the background image of the graphics window shows through.

Creating a Sprite

Next, I create a pizza sprite:

 Pizza(screen = my_screen,        x = SCREEN_WIDTH/2, y = SCREEN_HEIGHT/2,        image = pizza_image) 

A new instance of the Pizza class is created, with x- and y-coordinates that put the sprite right in the middle of the screen. That's all it takes.

Wrapping Up

Finally, I end the program by invoking mainloop():

 my_screen.mainloop() 

In addition to keeping the graphics window open, mainloop() now draws the sprite onto the background.

TRICK

You don't need to be an artist to create graphics for your games. As you see in this chapter, I make up for my utter lack of artistic ability with a modern piece of technology: my digital camera. If you have access to a digital camera, you can create some great images for your projects. In fact, that's how I created all of the graphics for the Pizza Panic game. The brick wall is the back of a friend's house. For the pizza, I ordered delivery one night. And the chef is my brave, brave friend Dave.

While this is a great technique, an important thing to remember is that if you take a picture of a person or object, you don't necessarily own the image. Obviously some things are trademarked or copyrighted. But using a digital camera is a great way to capture generic images while infusing your programs with a unique, photorealistic style.




Python Programming for the Absolute Beginner
Python Programming for the Absolute Beginner, 3rd Edition
ISBN: 1435455002
EAN: 2147483647
Year: 2003
Pages: 194

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net