-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsprites.py
247 lines (221 loc) · 8.9 KB
/
sprites.py
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import pygame
from values import *
'''
class: controllable player
image: path to the image for the sprite
speed: speed of the player
init_x: initial x coordinate of the player
init_y: initial y coordinate of the player
'''
class Player(pygame.sprite.Sprite):
def __init__(self,image,speed,init_x,init_y):
super().__init__()
self.speed = speed
self.image = pygame.image.load(image)
self.rect = self.image.get_rect()
self.width = self.image.get_width()
self.height = self.image.get_height()
self.rect.x = init_x
self.rect.y = init_y
self.x_change = 0
self.y_change = 0
'''
function: behave by adjusting the values
speed: float speed of the game
'''
def behave(self,speed):
self.rect.x += self.x_change*speed
self.rect.y += self.y_change*speed
if self.rect.x >= SCREEN_WIDTH-20:
self.rect.x = SCREEN_WIDTH-20
if self.rect.x <= 0:
self.rect.x = 0
if self.rect.y >= SCREEN_HEIGHT-20:
self.rect.y = SCREEN_HEIGHT-20
if self.rect.y <= 0:
self.rect.y = 0
'''
function: adjust x_change or y_change
direction: integer 0 through 3 representing different directions
'''
def accelerate(self,direction):
if direction == 0:
self.y_change = -self.speed
if direction == 1:
self.y_change = self.speed
if direction == 2:
self.x_change = -self.speed
if direction == 3:
self.x_change = self.speed
'''
function: set x_change or y_change to 0
direction: integer 0 through 3 representing different directions
'''
def deccelerate(self,direction):
if direction == 0:
self.y_change = 0
if direction == 1:
self.y_change = 0
if direction == 2:
self.x_change = 0
if direction == 3:
self.x_change = 0
'''
class: enemy to be spawned in rooms
room: room object that the enemy will occupy
image: string path to the image used
speed: integer speed of the enemy
displacement:
init_x: integer initial x location of the enemy
init_y: integer initial y location of the enemy
'''
class Enemy(pygame.sprite.Sprite):
def __init__(self,room,image,speed,displacement,init_x,init_y):
super().__init__()
self.room = room
self.x_speed = speed
self.y_speed = speed
self.image = pygame.image.load(image)
self.rect = self.image.get_rect()
self.max_displacement = displacement
self.current_displacement = 0
self.rect.x = init_x
self.rect.y = init_y
self.loc_init = (self.rect.x,self.rect.y)
'''
function: behave by adjusting the objects values
speed: float speed of the game
'''
def behave(self,speed):
self.current_displacement = abs(self.loc_init[0]-self.rect.x)
if self.current_displacement>=self.max_displacement:
self.x_speed = -self.x_speed
if self.rect.y >= SCREEN_HEIGHT - self.image.get_height() or self.rect.y <= 0:
self.y_speed = -self.y_speed
self.rect.x += self.x_speed*speed
self.rect.y += self.y_speed*speed
if self.rect.x >= (self.loc_init[0]+self.max_displacement):
self.rect.x = self.loc_init[0]+self.max_displacement
elif self.rect.x <= (self.loc_init[0]-self.max_displacement):
self.rect.x = self.loc_init[0]-self.max_displacement
'''
class: laser projectile from the player
room: room object that the laser will occupy
image: pygame image of the laser
speed: integer speed of the laser
x: integer x coordinate of the spawn location
y: integer y coordinate of the spawn location
'''
class Player_Laser(pygame.sprite.Sprite):
def __init__(self,room,image,speed,x,y):
super().__init__()
self.room = room
self.speed = speed
self.image = pygame.image.load(image)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.width = self.image.get_width()
self.height = self.image.get_height()
self.time_spawned = 0
'''
function: behave by adjusting the values of the object
speed: float speed of the game
dt: float time between cycles of the main loop
'''
def behave(self,speed,dt):
self.rect.y += -self.speed*speed
self.time_spawned += dt
if self.time_spawned >= 1000:
self.room.laser_sprite_group.remove(self)
self.room.lasers.remove(self)
'''
function: what to do when the object collides with an enemy
enemy: enemy object that has been collided with
'''
def on_collision(self,enemy):
if self.rect.colliderect(enemy.rect):
self.room.laser_sprite_group.remove(self)
self.room.enemy_sprite_group.remove(enemy)
self.room.lasers.remove(self)
self.room.enemies.remove(enemy)
self.room.enemy_count -= 1
def __repr__(self):
return "X coor: " + str(self.rect.x) + " Y coor: " + str(self.rect.y) + " Time: " + str(self.time_spawned)
'''
class: portal to transport player to different rooms
room: room object that the portal occupies
level: level object that the room occupies
frames: list of strings of paths to
ani_time: float time time between frames in the animation
x: integer initial x location of the portal
y: integer initial y location of the portal
'''
class Portal(pygame.sprite.Sprite):
def __init__(self,room,level,frames,ani_time,x,y):
super().__init__()
self.room = room
self.frames = frames
self.current_frame = frames[0]
self.image = pygame.image.load(self.current_frame)
self.frame_idx = 0
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.ani_time = ani_time
self.current_ani_time = ani_time
self.level = level
'''
function: animate the portal
dt: float time passed in a mainloop cycle
'''
def animate(self,dt):
if self.current_ani_time >= self.ani_time:
self.current_ani_time = 0
self.image = pygame.image.load(self.current_frame)
self.frame_idx += 1
if self.frame_idx >= len(self.frames):
self.frame_idx = 0
self.current_frame = self.frames[self.frame_idx]
else:
self.current_ani_time += dt
'''
function: what to do when a player collides with a portal
player: player object that is collided with
screen: pygame display object that is displayed on
'''
def on_collision(self,player,screen):
if self.rect.colliderect(player.rect):
#print("portal touching player")
if self.rect.x == (SCREEN_WIDTH/2)-20 and self.rect.y == 0:
#print("top portal touching player")
self.level.current_room_coor[1] -= 1
self.level.current_room = self.level.level_map[self.level.current_room_coor[1]][self.level.current_room_coor[0]]
self.level.current_room.generate(screen)
self.level.current_room.ally_sprite_group.add(player)
player.rect.x = (SCREEN_WIDTH/2)-10
player.rect.y = SCREEN_HEIGHT - 60
if self.rect.x == SCREEN_WIDTH-40 and self.rect.y == (SCREEN_HEIGHT/2)-20:
#print("right portal touching player")
self.level.current_room_coor[0] += 1
self.level.current_room = self.level.level_map[self.level.current_room_coor[1]][self.level.current_room_coor[0]]
self.level.current_room.generate(screen)
self.level.current_room.ally_sprite_group.add(player)
player.rect.x = 60
player.rect.y = (SCREEN_HEIGHT/2)-10
if self.rect.x == (SCREEN_WIDTH/2)-20 and self.rect.y == SCREEN_HEIGHT-40:
#print("bottom portal touching player")
self.level.current_room_coor[1] += 1
self.level.current_room = self.level.level_map[self.level.current_room_coor[1]][self.level.current_room_coor[0]]
self.level.current_room.generate(screen)
self.level.current_room.ally_sprite_group.add(player)
player.rect.x = (SCREEN_WIDTH/2)-10
player.rect.y = 60
if self.rect.x == 0 and self.rect.y == (SCREEN_HEIGHT/2)-20:
#print("left portal touching player")
self.level.current_room_coor[0] -= 1
self.level.current_room = self.level.level_map[self.level.current_room_coor[1]][self.level.current_room_coor[0]]
self.level.current_room.generate(screen)
self.level.current_room.ally_sprite_group.add(player)
player.rect.x = SCREEN_WIDTH - 60
player.rect.y = (SCREEN_HEIGHT/2)-10