~rick-rickspencer3/pygame-template/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
"""
guy module - contains the Guy class which represents the sprite
controlled by the player. Managers bullets in addition to 
BaseSprite functionality.

"""

import pygame, random
from baseSprite import BaseSprite
from bullet import Bullet
import crashteroidsconfig

class Guy(BaseSprite):
    """
    Guy - The sprite controlled by the player. managers 

    """

    def __init__(self, bullets_group):
        """
        Creates a Guy

        arguments:

        bullets_group - A pygame.SpriteGroup managed by the games
        collision detection system.

        """

        BaseSprite.__init__(self, crashteroidsconfig.guy_img)
        self.bullets = bullets_group
        self.hum = pygame.mixer.Sound(crashteroidsconfig.guy_eng)
        self.explosionSound = pygame.mixer.Sound(crashteroidsconfig.guy_explode)
        self.explosionSound.set_volume(.2)
        self.alive = True
        self.exploding = False
        self.explodestage = 0
        self.visible = True
        self.max_bullets = 4
        self.bigbullets = False
 
    def init_position(self):
        """init_position - resets the Guy's position on screen
        Will reset the Sprite's image to the starting image.
        
        """

        #start off in the center of screen, still, facing up
        self.stop()
        self.x = 400
        self.y = 240
        self.master_image = pygame.image.load(crashteroidsconfig.guy_img)
        self.update_image()
        self.visible = True
        self.alive = True
  
    def accelerate(self):
        """
        accelerate - increase the sprites speed along it's current
        trajectory and plays the sound for the sprite's "moto".

        """

        #only accelerate if the Guy is alive
        if self.alive and not self.exploding:
            BaseSprite.accelerate(self)
            self.hum.play(-1)#loop sound endlessly
  
    def stop_acceleration(self):
        """
        stop_acceleration - tells the sprite to stop accelerating.
        Depending on the game physics that may or my not cause
        the sprite to accually stop.
 
        """

        BaseSprite.stop_acceleration(self)
        self.hum.stop()
 
    def explode(self):
        """explode - starts the Guy's animated exploding sequence.
        Sets the alive to False so the the exploding sprite does
        not collide with sprits in future ticks. The sprite will 
        be removed from the screen after the exploding sequence is 
        complete. To remove the sprite immediately, call kill().

        """

        self.stop()
        if self.alive:
            self.hum.stop()
            self.explosionSound.play()
            self.alive = False
            self.exploding = True
   
    def shoot(self):
        """shoot - fire a bullet. Adds the bullet to the bullet sprite group.
        If the maximum number of bullets premitted would be exceeded, 
        the bullet will not fire. If the guy is exploding, the guy will no
        fire.

        """

        if self.alive:
            #only allow max numbe of  bullets on the screen at a time
            if len(self.bullets.sprites()) < self.max_bullets:
                imgName = crashteroidsconfig.guy_bullet
                b = Bullet(self.x,self.y,self.orientation, imgName)
                self.bullets.add(b)

    def hyperspace(self):
        """
        hyperspace - causes the Guy to disappear and reappear in a 
        random screen location. The Guy will stop. The Guy will
        randomly explode 20% of the time that this funciton is used.
 
        """

        self.stop()
        self.x = random.randint(0,800)
        self.y = random.randint(0,480)
        #every now and then, blow up on hyperspace for no reason
        if random.randint(0,5) == 2:
            self.explode()
   
    def stop(self):
        """
        stop - stops all velocity and totation, and sets the Sprite's
        orientation to 0

        """

        self.orientation = 0
        self._rotating_right = False
        self._rotating_left = False
        self.velocity_x = 0
        self.velocity_y = 0
        self._accelerating = False
  
    def update(self):
        """update - Update internal data for a game tick""" 
    
        BaseSprite.update(self)

        #this stops the guy completely
        #for frictionless physics like in a space game
        #you can decrement the velocity or even
        #delete these lines for asteroids-like physics
        if not self._accelerating:
            self.velocity_x = 0
            self.velocity_y = 0
  
        #manage the exploding animation
        if self.exploding: 
            #do an explosion image for each tick
            self.explodestage += 1
            e = self.explodestage
            if e < 8:#there are 7 explosion images
                e = str(e)
                img_name = crashteroidsconfig.guy_explode_stage + e  + ".png"
                self.master_image = pygame.image.load(img_name)
                self.update_image()

            else:#explosion is done
                self.visible = False
                self.exploding = False