-
Notifications
You must be signed in to change notification settings - Fork 1
/
player.py
249 lines (209 loc) · 9.24 KB
/
player.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
248
249
import pygame
from config import *
class Player(pygame.sprite.Sprite):
def __init__(self, game):
self.delta_x = 0
self.delta_y = 0
self.shadowForm = False
self.game = game
self._layer = PLAYER_LAYER
self.groups = self.game.all_sprites
pygame.sprite.Sprite.__init__(self, self.groups)
self.screen_width = 13 * TILESIZE * 0.5 - TILESIZE/2
self.screen_height = 7.3 * TILESIZE * 0.5 - TILESIZE/2
self.x = self.screen_width#WIN_WIDTH * TILESIZE * 0
self.y = self.screen_height#WIN_HEIGHT * TILESIZE * 0
self.width = TILESIZE/2
self.height = TILESIZE/2
self.x_change = 0
self.y_change = 0
self.facing = 'down'
self.animation_loop = 0
self.image = self.game.character_spritesheet.get_sprite(0, 0, self.width, self.height)
# self.image = pygame.Surface([self.width, self.height])
# self.image.fill((255, 0, 0))
self.rect = self.image.get_rect()
self.rect.x = self.x
self.rect.y = self.y
self.animations = [
self.game.character_spritesheet.get_sprite(0, 0, self.width, self.height),
self.game.character_spritesheet.get_sprite(51, 0, self.width, self.height),
self.game.character_spritesheet.get_sprite(0, 0, self.width, self.height),
self.game.character_spritesheet.get_sprite(51, 0, self.width, self.height)
]
self.animations_shadow = [
self.game.character_spritesheet.get_sprite(0, 52, self.width, self.height),
self.game.character_spritesheet.get_sprite(51, 52, self.width, self.height),
self.game.character_spritesheet.get_sprite(0, 52, self.width, self.height),
self.game.character_spritesheet.get_sprite(51, 52, self.width, self.height)
]
def update(self): # pygame sprite manditory function
self.movement()
self.animate()
# keys = pygame.key.get_pressed()
if self.shadow_condition(): #and keys[pygame.K_h]:
# Turn into shadow on H key hold
# need to decide between toggle or hold
self.shadowForm = True
# Red for now for testing purposes
# self.image.fill((255, 0, 0))
else:
self.shadowForm = False
# self.image = self.game.character_spritesheet.get_sprite(0, 0, self.width, self.height)
self.collide_enemy()
self.rect.x += self.x_change
self.collide_blocks('x')
self.rect.y += self.y_change
self.collide_blocks('y')
self.collide_door()
self.collide_item()
self.x_change = 0
self.y_change = 0
def movement(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
for sprite in self.game.all_sprites:
sprite.rect.x += PLAYER_SPEED
self.x_change -= PLAYER_SPEED
self.facing = 'left'
if keys[pygame.K_RIGHT]:
for sprite in self.game.all_sprites:
sprite.rect.x -= PLAYER_SPEED
self.x_change += PLAYER_SPEED
self.facing = 'right'
if keys[pygame.K_UP]:
for sprite in self.game.all_sprites:
sprite.rect.y += PLAYER_SPEED
self.y_change -= PLAYER_SPEED
self.facing = 'up'
if keys[pygame.K_DOWN]:
for sprite in self.game.all_sprites:
sprite.rect.y -= PLAYER_SPEED
self.y_change += PLAYER_SPEED
self.facing = 'down'
#used for enemy return to position
self.delta_x += self.x_change
self.delta_y += self.y_change
# print(self.x_change, self.y_change, self.delta_x, self.delta_y)
def shadow_condition(self):
callable = pygame.sprite.collide_rect_ratio(1)
# Get the sprites that the player sprite is touching
touchingShadowSprites = pygame.sprite.spritecollide(self, self.game.shadow, False, callable)
touchingGoatSprites = pygame.sprite.spritecollide(self, self.game.goats, False, callable)
newShadowRect = pygame.Rect(0,0,0,0)
newGoatsRect = pygame.Rect(0,0,0,0)
if touchingShadowSprites:
# make the bigger shadow rect
for shadows in touchingShadowSprites:
if not newShadowRect:
newShadowRect = shadows.rect
else:
newShadowRect = pygame.Rect.union(newShadowRect, shadows.rect)
if touchingGoatSprites:
# make the bigger goats rect
for goats in touchingGoatSprites:
if not newGoatsRect:
newGoatsRect = goats.rect
else:
newGoatsRect = pygame.Rect.union(newGoatsRect, goats.rect)
newShadowRect = newShadowRect.scale_by(1.2)
newGoatsRect = newGoatsRect.scale_by(1.2)
if newShadowRect and newGoatsRect:
return pygame.Rect.contains(newShadowRect, self.rect) or pygame.Rect.contains(newGoatsRect, self.rect)
elif newShadowRect:
return pygame.Rect.contains(newShadowRect, self.rect)
elif newGoatsRect:
return pygame.Rect.contains(newGoatsRect, self.rect)
else:
return False
def collide_enemy(self):
hits = pygame.sprite.spritecollide(self, self.game.enemies, False)
if hits:
self.game.state = 'lose'
self.kill() #remove player from all sprites
# self.game.running = False
# self.game.playing = False #exit game
def collide_item(self):
hits = pygame.sprite.spritecollide(self, self.game.item, False)
if hits:
if not hits[0].gdp:
self.game.item_aquired = True
else:
hits[0].item_popup()
# text screen
hits[0].kill() #remove item from all sprites
def collide_door(self):
hits = pygame.sprite.spritecollide(self, self.game.door, False)
if hits and self.game.item_aquired:
# print(self.game.playing)
# self.game.playing = False
self.game.state = 'cutscene'
# self.game.level_clear = True
# self.game.item_aquired = False
# self.game.levelUpdate()
#pygame.sprite.spritecollide(self, self.game.door, True)
# self.game.running = False #next level
else:
self.collide_blocks('x', self.game.door)
self.collide_blocks('y', self.game.door)
def collide_blocks(self, direction, group = None):
if not group:
group = self.game.blocks
hits = pygame.sprite.spritecollide(self, group, False) #check player rect and every block in the game
#False is dont want to delete sprite when collide
if direction == "x":
if hits:
if self.x_change > 0: #moving right
self.rect.x = hits[0].rect.left - self.rect.width
for sprite in self.game.all_sprites:
sprite.rect.x += PLAYER_SPEED
if self.x_change < 0:
self.rect.x = hits[0].rect.right
for sprite in self.game.all_sprites:
sprite.rect.x -= PLAYER_SPEED
if direction == "y":
if hits:
if self.y_change > 0: #moving down
self.rect.y = hits[0].rect.top - self.rect.height #hits is the block rect
for sprite in self.game.all_sprites:
sprite.rect.y += PLAYER_SPEED
if self.y_change < 0:
self.rect.y = hits[0].rect.bottom
for sprite in self.game.all_sprites:
sprite.rect.y -= PLAYER_SPEED
def animate(self):
keys = pygame.key.get_pressed()
if self.shadowForm:
self.image = self.animations_shadow[int(self.animation_loop)]
else:
self.image = self.animations[int(self.animation_loop)]
if keys[pygame.K_LEFT] or keys[pygame.K_RIGHT] or keys[pygame.K_UP] or keys[pygame.K_DOWN]:
self.animation_loop += 0.1
if self.animation_loop > len(self.animations):
self.animation_loop = 0
class PlayerAOE(pygame.sprite.Sprite):
def __init__(self, game, x, y):
self.game = game
self._layer = GROUND_LAYER
self.groups = self.game.all_sprites
pygame.sprite.Sprite.__init__(self, self.groups)
self.x = x
self.y = y
self.width = TILESIZE * 6
self.height = TILESIZE * 6
self.image = pygame.Surface([self.width, self.height])
# self.image.fill((255, 0, 0))
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect()
self.rect.center = self.game.player.rect.center
def update(self): # pygame sprite manditory function
self.movement()
# self.collide_enemy()
def movement(self):
self.rect.center = self.game.player.rect.center
def collide_enemy(self):
hits = pygame.sprite.spritecollide(self, self.game.enemies, False)
if hits:
print("aoe")
self.kill() #remove player from all sprites
# self.game.playing = False #exit game