-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsprites.py
More file actions
executable file
·211 lines (172 loc) · 6.09 KB
/
sprites.py
File metadata and controls
executable file
·211 lines (172 loc) · 6.09 KB
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
203
204
205
206
207
208
209
210
211
import pygame
from math import sqrt
from random import randint
from pygame.locals import *
from globals import *
'''
Base class for all sprites. Handles simple collision and updating position.
One weird nuance is that the pygame Rect can only store ints. So external
x and y coordinates are defined to handle smooth subpixel movement. But the
coordinates should be accessed from the rect field.
'''
class Sprite(pygame.sprite.Sprite):
def __init__(self, position, dimensions, color):
pygame.sprite.Sprite.__init__(self)
self.x, self.y = position
self.color = color
self.y_velocity, self.x_velocity = 0, 0
self.rect = pygame.Rect(position, dimensions)
'''
Called once per frame. Clients can override to manage passing time
'''
def update(self, elapsed_time, map):
self._update_x(elapsed_time)
collisions = [wall for wall in map.nearby_walls(self.rect)]
for wall in collisions:
if wall.collide(self.rect):
self._collide_x(wall)
self.interact_with(wall)
self._update_y(elapsed_time)
collisions = [wall for wall in map.nearby_walls(self.rect)]
for wall in collisions:
if wall.collide(self.rect):
self._collide_y(wall)
self.interact_with(wall)
'''
Interaction with the given wall. Override to define special functionality
'''
def interact_with(self, wall):
pass
def _update_x(self, elapsed_time):
self.x += self.x_velocity * elapsed_time
self.rect.x = self.x
def _update_y(self, elapsed_time):
self.y += self.y_velocity * elapsed_time
self.rect.y = self.y
def _collide_x(self, wall):
# Wall is normal ground
if wall.type != LEDGE:
collision = wall.rect
if self.x_velocity < 0: # Moving left
self.x = collision.right
elif self.x_velocity > 0: # Moving right
self.x = collision.left - self.rect.width
self.rect.x = self.x
def _collide_y(self, wall):
if wall.type == LEDGE:
if self.y_velocity > 0 and self.rect.bottom - wall.rect.top <= LEDGE_HEIGHT:
self.y = wall.rect.y - self.rect.height
self.y_velocity = 0
else:
collision = wall.rect
if self.y_velocity > 0:
self.y = collision.y - self.rect.height
self.y_velocity = 0
elif self.y_velocity < 0:
# Moving up
self.y = collision.bottom
self.y_velocity = 0
self.rect.y = self.y
'''
Simple bullet. Shoots in the given direction, until it hits a wall
'''
class Bullet(Sprite):
MAX_SPEED = 0.5
LIFESPAN = 5000
DIMENSIONS = (5, 2)
type = "Bullet"
def __init__(self, position, velocity):
Sprite.__init__(self, position, Bullet.DIMENSIONS, Bullet.type)
self.x_velocity, self.y_velocity = velocity
self.time = 0
def update(self, elapsed_time, map):
self.time += elapsed_time
if self.time > Bullet.LIFESPAN:
self.kill()
Sprite.update(self, elapsed_time, map)
def interact_with(self, wall):
self.y_velocity, self.x_velocity = 0, 0
class Creature(Sprite):
def __init__(self, position, dimensions, material):
material = pygame.image.load("img/"+material+".png")
Sprite.__init__(self, position, dimensions, material)
self.jumping = False
self.health = 100
def damage(self, damage):
self.health -= damage
def update(self, elapsed_time, map):
self.y_velocity += GRAVITY * elapsed_time
Sprite.update(self, elapsed_time, map)
def jump(self, speed):
if not self.jumping:
self.y_velocity = -speed
self.jumping = True
def _collide_y(self, wall):
if self.y_velocity > 0:
self.jumping = False
Sprite._collide_y(self, wall)
'''
The player! Our hero. We love him so
'''
class Player(Creature):
DIMENSIONS = (30, 40)
MAXHEALTH = 100
SPEED = 1
ATTACK = 1
DEFENSE = 1
MAX_SPEED = 0.3+0.005*(SPEED-1)
type = "Player"
def __init__(self, position):
Creature.__init__(self, position, Player.DIMENSIONS, Player.type)
self.health = 100
def jump(self):
Creature.jump(self, 0.3+0.002*(self.SPEED-1))
def shrink(self):
self.color = (0, 0, 0)
self.rect.height, self.rect.width = (10, 5)
def grow(self):
self.color = (255, 255, 0)
self.rect.height, self.rect.width = (100, 50)
def interact_with(self, wall):
if wall.type == SHRINK:
self.shrink()
elif wall.type == GROW:
self.grow()
def move_left(self):
self.x_velocity = -Player.MAX_SPEED
def move_right(self):
self.x_velocity = Player.MAX_SPEED
def stop_x(self):
self.x_velocity = 0
def shoot(self, position):
diff_x = position[0] - self.rect.centerx
diff_y = position[1] - self.rect.centery
magnitude = sqrt(diff_x * diff_x + diff_y * diff_y)
return Bullet(self.rect.center, (diff_x / magnitude, diff_y / magnitude))
def alive(self):
return self.health > 0
class Goomba(Creature):
MAX_SPEED = 0.1
DIMENSIONS = (BLOCK_SIZE, BLOCK_SIZE)
type = "Goomba"
def __init__(self, position):
Sprite.__init__(self, position, Goomba.DIMENSIONS, Goomba.type)
self.move_left()
self.time = randint(0, 3000)
def update(self, elapsed_time, map):
self.time -= elapsed_time
if self.time <= 0:
self.time = randint(0, 3000)
self.jump()
Creature.update(self, elapsed_time, map)
def stop_x(self):
self.x_velocity = -self.x_velocity
def move_left(self):
self.x_velocity = -Goomba.MAX_SPEED
def move_right(self):
self.x_velocity = Goomba.MAX_SPEED
def jump(self):
Creature.jump(self, Goomba.MAX_SPEED)
def interact_with(self, wall):
if self.y_velocity != 0:
self.x_velocity = -self.x_velocity