The
|
Method |
Description |
|---|---|
|
get_pos() |
Returns the object's x-coordinate and y-coordinate. |
|
get_xpos() |
Returns the object's x-coordinate. |
|
get_ypos() |
Returns the object's y-coordinate. |
|
get_left() |
Returns the x-coordinate of the object's left edge. |
|
get_right() |
Returns the x-coordinate of the object's right edge. |
|
get_top() |
Returns the y-coordinate of the object's top edge. |
|
get_bottom() |
Returns the y-coordinate of the object's bottom edge. |
|
get_velocity() |
Returns the object's x and y velocity
|
|
move_to( x , y ) |
Moves the object to the new coordinates ( x , y ). |
|
set_left( x ) |
Moves the object horizontally so that its left edge is at the new coordinate x . |
|
set_right( x ) |
Moves the object horizontally so that its right edge is at the new coordinate x . |
|
set_top( y ) |
Moves the object vertically so that its top edge is at the new coordinate y . |
|
set_bottom( y ) |
Moves the object vertically so that its bottom edge is at the new coordinate y . |
|
set_velocity( dx , dy ) |
Sets the object's x velocity to dx and its y velocity to dy . |
|
overlapping_objects() |
Returns a list of objects that overlap the object. |
|
destroy() |
Removes all of the associated Screen object's references to an object. |
| HINT |
In the course of writing a game, you'll never directly instantiate an object of
Games_Object
. The class is
|
Again, there's no need to try to remember all of the methods described in Table 11.3. Just know they're available through all Text , Message , and Sprite objects.
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
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.
|
|
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
|
|
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.
Figure 11.8:
The pizza image is not part of the background, but an independent object based on the
Sprite
class.
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
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
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.
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
If an image is loaded with transparency on, then the color of the point at the
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.
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. |
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.
Finally, I end the program by invoking mainloop() :
my_screen.mainloop()
In addition to keeping the graphics window
| 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
|