~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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
"""
base_sprite - contains BaseSprite class.

"""

import pygame
import crashteroidsconfig

class BaseSprite(pygame.sprite.Sprite):
    """
    BaseSprite - handles all common sprite data and functionality
    for sprites in crashteroids.

    """

    def __init__(self, sprite_image):
        """Creates a BaseSprite. Sets the sprite's initial screen
        position, so this should be called by subclasses before they
        make any changes positioning.

        arguments:
        sprite_image - path to image file to use for the sprite.

        """

        self.__init_position()

        #acceleration_divisor determines how fast a sprite
        #accelerates. The hight the divisor, the slower the
        #acceleration.
        self.acceleration_divisor = 3 #accelerate at 1/3 speed

        #rotation_rate determines how many degrees the sprite will
        #rotate witch each tick while rotating. 
        self.rotation_rate = 15 #makes 360/14 = 24 orientations

        pygame.sprite.Sprite.__init__(self)
  
        #load the image and shave a few pixels from the rect
        #maintain a master image to avoid image corruption due to
        #multiple transformations of an image
        self.master_image = pygame.image.load(sprite_image)
        self.image = self.master_image
        self.rect = self.image.get_rect()

        #make the rect for the sprite slightly smaller than the
        #sprite image to accomedate non-rectangular images
        self.rect.height -= 5
        self.rect.width -= 5

        self.update_image()


    def __init_position(self):
        """init_position - initializes values for positioning, and provides 
        a default position implemention, places the sprite in the center of
        the screen.

        """

        self.orientation = 0
        self._rotating_right = False
        self._rotating_left = False
        self.velocity_x = 0
        self.velocity_y = 0
        self._accelerating = False
        self.x = 400
        self.y = 240
        self.max_velocity = 50
  
  
    def start_rotation_right(self):
        """
        start_rotation_right - set sprite to rotate clockwise by
        degrees set in rotation_rate on each call to update().

        """

        self._rotating_right = True

    def start_rotation_left(self):
        """
        start_rotation_left - set sprite to rotate counter-clockwise by
        degrees set in rotation_rate on each call to update().

        """
        self._rotating_left = True
  
    def stop_rotating_right(self):
        """
        stop_rotating_right - set the sprite to stop rotating on each tick

        """

        self._rotating_right = False
  
    def stop_rotating_left(self):
        """
        stop_rotating_right - set the sprite to stop rotating on each tick

        """

        self._rotating_left = False
 
    def accelerate(self):
        """
        accelerate - set the sprite to accelerate up to the max_acceleration
        on each tick

        """
        self._accelerating = True
  
    def stop_acceleration(self):
        """
        accelerate - set the sprite to stop accelateration
        on each tick

        """
        self._accelerating = False
  
    def update(self):
        """update - update internal data and position

        """
        #update the orientation
        if self._rotating_left:
            self.orientation += self.rotation_rate
        if self.orientation >= 360:
            self.orientation = 0 

        if self._rotating_right:
            self.orientation -= self.rotation_rate
    
        if self.orientation < 0:
            self.orientation = 360 - self.rotation_rate
  
        #change the image if rotating
        if self._rotating_left or self._rotating_right:
            self.update_image()
  
        #adjust the velocity based on orientation
        up = 360/self.rotation_rate
        right = up/4
        down = up/2
        left = right * 3
        
        if self._accelerating:
            num = (up - self.orientation/self.rotation_rate) 
            if num == up:
                num = 0
            if num <= right:
                self.velocity_x += num
                self.velocity_y -= right - num
            elif num <= down:
                self.velocity_x += down - num
                self.velocity_y += num - right
            elif num <= left:
                self.velocity_x -= num - down
                self.velocity_y += left - num
            elif num < up:
                self.velocity_x -= up - num
                self.velocity_y -= num - left
  
        #set the velocity is within bounds
        if self.velocity_x > self.max_velocity:
            self.velocity_x = self.max_velocity
        if self.velocity_y > self.max_velocity:
            self.velocity_y = self.max_velocity
        if self.velocity_x < 0 - self.max_velocity:
            self.velocity_x = 0 - self.max_velocity
        if self.velocity_y < 0 - self.max_velocity:
            self.velocity_y = 0 - self.max_velocity
   
        #adjust the location, raise accelrationDivisor to slow down
        self.x += self.velocity_x/self.acceleration_divisor
        self.y += self.velocity_y/self.acceleration_divisor

        #wrap the sprite around the screen, maintain "hyper zone"
        if self.x > 800:
            self.x = 0 - self.rect.height
        if self.y > 480:
            self.y = 0 - self.rect.width
   
        if self.x < 0 - self.rect.width:
            self.x = 800
        if self.y < 0 - self.rect.height:
            self.y = 480
  
        self.rect.topleft = [self.x,self.y]
  
    def update_image(self):
        oldCenter = self.rect.center
        self.image = pygame.transform.rotate(self.master_image,self.orientation)
        self.rect = self.image.get_rect()
        #make a buffer so sprites can appear closer together
        self.rect.width -= 5
        self.rect.height -= 5
        self.rect.center = oldCenter
 
    def explode(self):
        self.kill()#remove self from any spritegroups by default