3
base_sprite - contains BaseSprite class.
2
8
import crashteroidsconfig
4
10
class BaseSprite(pygame.sprite.Sprite):
5
def __init__(self, spriteImage):
6
#default to center of screen pointing strait up
8
self.rotatingRight = False
9
self.rotatingLeft = False
12
self.accelerating = False
16
self.accelerationDivisor = 3 #accelerate at 1/3 speed
17
self.rotationRate = 15 #makes 360/14 = 24 orientations
19
pygame.sprite.Sprite.__init__(self)
20
#load the image and shave a few pixels from the rect
21
self.masterImage = pygame.image.load(spriteImage)
22
self.image = self.masterImage
23
self.rect = self.image.get_rect()
28
def startRotationRight(self):
29
self.rotatingRight = True
31
def startRotationLeft(self):
32
self.rotatingLeft = True
34
def stopRotationRight(self):
35
self.rotatingRight = False
37
def stopRotationLeft(self):
38
self.rotatingLeft = False
41
self.accelerating = True
43
def stopAcceleration(self):
44
self.accelerating = False
47
#update the orientation
49
self.orientation += self.rotationRate
50
if self.orientation >= 360:
53
if self.rotatingRight:
54
self.orientation -= self.rotationRate
55
if self.orientation < 0:
56
self.orientation = 345
58
#change the image if rotating
59
if self.rotatingLeft or self.rotatingRight:
62
#adjust the velocity based on orientation
64
num = (24 - self.orientation/15)
69
self.velocityY -= 6 - num
71
self.velocityX += 12 - num
72
self.velocityY += num - 6
74
self.velocityX -= num - 12
75
self.velocityY += 18 - num
77
self.velocityX -= 24 - num
78
self.velocityY -= num - 18
80
#set the velocity is within bounds
81
if self.velocityX > self.maxVelocity:
82
self.velocityX = self.maxVelocity
83
if self.velocityY > self.maxVelocity:
84
self.velocityY = self.maxVelocity
85
if self.velocityX < 0 - self.maxVelocity:
86
self.velocityX = 0 - self.maxVelocity
87
if self.velocityY < 0 - self.maxVelocity:
88
self.velocityY = 0 - self.maxVelocity
90
#adjust the location, raise accelrationDivisor to slow down
91
self.x += self.velocityX/self.accelerationDivisor
92
self.y += self.velocityY/self.accelerationDivisor
94
#wrap the sprite around the screen, maintain "hyper zone"
96
self.x = 0 - self.rect.height
98
self.y = 0 - self.rect.width
100
if self.x < 0 - self.rect.width:
102
if self.y < 0 - self.rect.height:
105
self.rect.topleft = [self.x,self.y]
107
def updateImage(self):
108
oldCenter = self.rect.center
109
self.image = pygame.transform.rotate(self.masterImage,self.orientation)
110
self.rect = self.image.get_rect()
111
#make a buffer so sprites can appear closer together
112
self.rect.width -= 20
113
self.rect.height -= 20
114
self.rect.center = oldCenter
117
self.kill()#remove self from any spritegroups by default
12
BaseSprite - handles all common sprite data and functionality
13
for sprites in crashteroids.
17
def __init__(self, sprite_image):
18
"""Creates a BaseSprite. Sets the sprite's initial screen
19
position, so this should be called by subclasses before they
20
make any changes positioning.
23
sprite_image - path to image file to use for the sprite.
27
self.__init_position()
29
#acceleration_divisor determines how fast a sprite
30
#accelerates. The hight the divisor, the slower the
32
self.acceleration_divisor = 3 #accelerate at 1/3 speed
34
#rotation_rate determines how many degrees the sprite will
35
#rotate witch each tick while rotating.
36
self.rotation_rate = 15 #makes 360/14 = 24 orientations
38
pygame.sprite.Sprite.__init__(self)
40
#load the image and shave a few pixels from the rect
41
#maintain a master image to avoid image corruption due to
42
#multiple transformations of an image
43
self.master_image = pygame.image.load(sprite_image)
44
self.image = self.master_image
45
self.rect = self.image.get_rect()
47
#make the rect for the sprite slightly smaller than the
48
#sprite image to accomedate non-rectangular images
55
def __init_position(self):
56
"""init_position - initializes values for positioning, and provides
57
a default position implemention, places the sprite in the center of
63
self._rotating_right = False
64
self._rotating_left = False
67
self._accelerating = False
70
self.max_velocity = 50
73
def start_rotation_right(self):
75
start_rotation_right - set sprite to rotate clockwise by
76
degrees set in rotation_rate on each call to update().
80
self._rotating_right = True
82
def start_rotation_left(self):
84
start_rotation_left - set sprite to rotate counter-clockwise by
85
degrees set in rotation_rate on each call to update().
88
self._rotating_left = True
90
def stop_rotating_right(self):
92
stop_rotating_right - set the sprite to stop rotating on each tick
96
self._rotating_right = False
98
def stop_rotating_left(self):
100
stop_rotating_right - set the sprite to stop rotating on each tick
104
self._rotating_left = False
106
def accelerate(self):
108
accelerate - set the sprite to accelerate up to the max_acceleration
112
self._accelerating = True
114
def stop_acceleration(self):
116
accelerate - set the sprite to stop accelateration
120
self._accelerating = False
123
"""update - update internal data and position
126
#update the orientation
127
if self._rotating_left:
128
self.orientation += self.rotation_rate
129
if self.orientation >= 360:
132
if self._rotating_right:
133
self.orientation -= self.rotation_rate
135
if self.orientation < 0:
136
self.orientation = 360 - self.rotation_rate
138
#change the image if rotating
139
if self._rotating_left or self._rotating_right:
142
#adjust the velocity based on orientation
143
up = 360/self.rotation_rate
148
if self._accelerating:
149
num = (up - self.orientation/self.rotation_rate)
153
self.velocity_x += num
154
self.velocity_y -= right - num
156
self.velocity_x += down - num
157
self.velocity_y += num - right
159
self.velocity_x -= num - down
160
self.velocity_y += left - num
162
self.velocity_x -= up - num
163
self.velocity_y -= num - left
165
#set the velocity is within bounds
166
if self.velocity_x > self.max_velocity:
167
self.velocity_x = self.max_velocity
168
if self.velocity_y > self.max_velocity:
169
self.velocity_y = self.max_velocity
170
if self.velocity_x < 0 - self.max_velocity:
171
self.velocity_x = 0 - self.max_velocity
172
if self.velocity_y < 0 - self.max_velocity:
173
self.velocity_y = 0 - self.max_velocity
175
#adjust the location, raise accelrationDivisor to slow down
176
self.x += self.velocity_x/self.acceleration_divisor
177
self.y += self.velocity_y/self.acceleration_divisor
179
#wrap the sprite around the screen, maintain "hyper zone"
181
self.x = 0 - self.rect.height
183
self.y = 0 - self.rect.width
185
if self.x < 0 - self.rect.width:
187
if self.y < 0 - self.rect.height:
190
self.rect.topleft = [self.x,self.y]
192
def update_image(self):
193
oldCenter = self.rect.center
194
self.image = pygame.transform.rotate(self.master_image,self.orientation)
195
self.rect = self.image.get_rect()
196
#make a buffer so sprites can appear closer together
198
self.rect.height -= 5
199
self.rect.center = oldCenter
202
self.kill()#remove self from any spritegroups by default