Creating Asteroids


Since the game involves deadly asteroids, I thought I'd start with them. Although this seems like the best first step to me, it may not to another programmer—and that's fine. You could certainly start with a different first step, such as getting the player's ship on the screen. There's no one right first step. The important thing to do is define and complete "bite-sized" programs that build on each other, working your way toward the completed project.

The Astrocrash01 Program

The Astrocrash01 program creates a graphics window, sets the nebula back-ground, and spawns eight randomly located asteroids. The velocity of each asteroid is also randomly calculated, but smaller asteroids have the potential to move faster than larger ones. Figure 12.8 shows the program in action.

click to expand
Figure 12.8: A field of moving asteroids is the foundation of the game.

Setting Up the Program

The program starts like most others:

 # Astrocrash01 # Get asteroids moving on the screen # Michael Dawson 5/20/03 import random from livewires import games # global constants SCREEN_WIDTH = 640 SCREEN_HEIGHT = 480 THE_SCREEN = games.Screen(SCREEN_WIDTH, SCREEN_HEIGHT) 

The Asteroid Class

The Asteroid class is used for creating moving asteroids. The first thing I do in the class is load three images—one for each size of asteroid—and assign them to class variables:

 class Asteroid(games.Sprite):     """ An asteroid which floats across the screen. """     image_big = games.load_image("asteroid_big.bmp")     image_med = games.load_image("asteroid_med.bmp")     image_small = games.load_image("asteroid_small.bmp") 

Next, I tackle the constructor method:

    def __init__(self, screen, x, y, size):        """ Initialize asteroid sprite. """        if size == 1:            image = Asteroid.image_small        elif size == 2:            image = Asteroid.image_med        elif size == 3:            image = Asteroid.image_big        else:            print "Asteroid size must be 1, 2, or 3."            sys.exit()        # set velocity based on asteroid size        dx = random.choice([2, -2]) * random.random() / size        dy = random.choice([2, -2]) * random.random() / size        self.init_sprite(screen = screen, x = x, y = y,                           dx = dx, dy = dy, image = image)        self.size = size 

The method's screen, x, and y parameter values determine where the new asteroid will start life. The value of the parameter size represents the size of the asteroid and can be 1 for small, 2 for medium, or 3 for large. Based on size, the appropriate image is used for the sprite. If size isn't passed either a 1, 2, or 3, the program displays an error message and exits.

Next, the constructor generates random values for the new object's velocity components based partly on its size attribute. Smaller asteroids have the potential to move faster than larger ones. Finally, the constructor initializes the sprite and sets the object's size attribute.

The moved() method keeps an asteroid in play by wrapping it around the screen:

     def moved(self):         """ Wrap the asteroid around screen. """         if self.get_top() > SCREEN_HEIGHT:             self.set_bottom(0)         if self.get_bottom() < 0:             self.set_top(SCREEN_HEIGHT)         if self.get_left() > SCREEN_WIDTH:             self.set_right(0)         if self.get_right() < 0:             self.set_left(SCREEN_WIDTH) 

The Main Section

Finally, the main section of code sets the nebula background and creates eight randomly sized asteroids at random screen locations:

 # main my_screen = THE_SCREEN nebula_image = games.load_image("nebula.jpg") my_screen.set_background(nebula_image) # create 8 asteroids for i in range(8):     x = random.randrange(SCREEN_WIDTH)     y = random.randrange(SCREEN_HEIGHT)     size = random.randrange (1, 4)     Asteroid(screen = my_screen, x = x, y = y, size = size) my_screen.mainloop () 




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