forked from matheusmaldaner/JasonJourney
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharacter.py
226 lines (188 loc) · 8.68 KB
/
character.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
import pygame
import weapon
import constants
import math
class Character:
def __init__(self, x, y, health, max_health, mob_animations, mob_type, boss_enemy, size, static=False):
self.mob_type = mob_type
self.boss_enemy = boss_enemy
self.flip = False
self.animation_list = mob_animations[mob_type]
self.frame_index = 0 # index to change sprite states
self.move_state = 0 # idle = 0, run = 1, ...
self.update_time = pygame.time.get_ticks() # time since frame updated
self.health = health
self.max_health = max_health
self.alive = True
self.hit = False
self.last_hit = pygame.time.get_ticks()
self.last_attack = pygame.time.get_ticks()
self.money = 0
self.stunned = False
# image stuff :)
self.image = self.animation_list[self.move_state][self.frame_index]
self.rect = pygame.Rect(0, 0, constants.TILE_SIZE * size, constants.TILE_SIZE * size) # hit box
self.rect.center = (x, y) # center of hit box
# used so the NPCs stay still
self.static = static
def move(self, dx, dy, wall_tiles):
screen_scroll = [0, 0]
# makes it so player does not move twice as fast in diagonal
if dx != 0 and dy != 0:
dx = dx * math.sqrt(2)/2
dy = dy * math.sqrt(2)/2
# faces the main player where the cursor is
if self.mob_type == 0:
pos = pygame.mouse.get_pos()
x_dist = pos[0] - self.rect.centerx
if x_dist > 0:
self.flip = False
if x_dist < 0:
self.flip = True
else:
if dx > 0:
self.flip = False
if dx < 0:
self.flip = True
self.rect.x += dx
# check if player is touching a wall
for wall in wall_tiles:
# verify wall and player collision
if wall[1].colliderect(self.rect):
# check which side collision occurred
if dx > 0:
self.rect.right = wall[1].left
if dx < 0:
self.rect.left = wall[1].right
self.rect.y += dy
# check if player is touching a wall
for wall in wall_tiles:
# verify wall and player collision
if wall[1].colliderect(self.rect):
# check which side collision occurred
if dy > 0:
self.rect.bottom = wall[1].top
elif dy < 0:
self.rect.top = wall[1].bottom
# if player is moving, display running animation
if dx == 0 and dy == 0:
self.move_state = 0
else:
self.move_state = 1
# screen only scrolls around player
if self.mob_type == 0:
# scrolls camera left/right
if self.rect.right > (constants.SCREEN_WIDTH - constants.SCROLL_THRESH):
screen_scroll[0] = (constants.SCREEN_WIDTH - constants.SCROLL_THRESH) - self.rect.right
self.rect.right = constants.SCREEN_WIDTH - constants.SCROLL_THRESH
if self.rect.left < constants.SCROLL_THRESH:
screen_scroll[0] = constants.SCROLL_THRESH - self.rect.left
self.rect.left = constants.SCROLL_THRESH
# scrolls camera up/down
if self.rect.bottom > (constants.SCREEN_HEIGHT - constants.SCROLL_THRESH):
screen_scroll[1] = (constants.SCREEN_HEIGHT - constants.SCROLL_THRESH) - self.rect.bottom
self.rect.bottom = constants.SCREEN_HEIGHT - constants.SCROLL_THRESH
if self.rect.top < constants.SCROLL_THRESH:
screen_scroll[1] = constants.SCROLL_THRESH - self.rect.top
self.rect.top = constants.SCROLL_THRESH
return screen_scroll
def ai(self, player, wall_tiles, screen_scroll, fireball_image):
# npcs get aggressive if they take damage
if self.static and self.health != 50:
self.static = False
clipped_line = ()
stun_cooldown = 100
ai_dx = 0
ai_dy = 0
fireball = None
# moves the enemies based on the screen scrolling
self.rect.x += screen_scroll[0]
self.rect.y += screen_scroll[1]
line_of_sight = ((self.rect.centerx, self.rect.centery), (player.rect.centerx, player.rect.centery))
for wall in wall_tiles:
if wall[1].clipline(line_of_sight):
clipped_line = wall[1].clipline(line_of_sight)
# checks distance to player
dist = math.sqrt(((self.rect.centerx - player.rect.centerx) ** 2) + (
(self.rect.centery - player.rect.centery) ** 2))
if not clipped_line and dist > constants.RANGE:
if self.rect.centerx > player.rect.centerx: # right hand side
ai_dx = -constants.ENEMY_SPEED
if self.rect.centerx < player.rect.centerx: # left hand side
ai_dx = constants.ENEMY_SPEED
if self.rect.centery > player.rect.centery: # below
ai_dy = -constants.ENEMY_SPEED
if self.rect.centery < player.rect.centery: # above
ai_dy = constants.ENEMY_SPEED
if self.alive and not self.static:
if not self.stunned:
self.move(ai_dx, ai_dy, wall_tiles)
if dist < constants.ATTACK_RANGE and player.hit is False:
player.health -= 10
player.hit = True
player.last_hit = pygame.time.get_ticks()
# boss enemies shoot fireballs
fireball_cooldown = 700
if self.boss_enemy:
if dist < 500:
if pygame.time.get_ticks() - self.last_attack >= fireball_cooldown:
fireball = weapon.Fireball(fireball_image, self.rect.centerx, self.rect.centery, player.rect.centerx, player.rect.centery)
self.last_attack = pygame.time.get_ticks()
# when boss gets hit
if self.hit:
self.hit = False
self.last_hit = pygame.time.get_ticks()
self.stunned = True
self.move_state = 0
# idle animation not implemented yet
if pygame.time.get_ticks() - self.last_hit > stun_cooldown:
self.stunned = False
return fireball
def update(self):
# checks if player is dead
if self.health <= 0:
self.health = 0
self.alive = False
# timer to reset hit
hit_cooldown = 500
if self.mob_type == 0:
if self.hit:
if pygame.time.get_ticks() - self.last_hit > hit_cooldown:
self.hit = False
# sets current character frame state -- idle version
self.image = self.animation_list[self.move_state][self.frame_index]
# check if enough time passed since last update
if pygame.time.get_ticks() - self.update_time > constants.ANIMATION_COOLDOWN:
self.frame_index += 1
self.update_time = pygame.time.get_ticks()
# addresses out of bounds error
if self.frame_index >= len(self.animation_list[self.move_state]):
self.frame_index = 0
def draw_health_bar(self, surface, pos, size, border_c, back_c, health_c, progress):
pygame.draw.rect(surface, back_c, (*pos, *size))
pygame.draw.rect(surface, border_c, (*pos, *size), 1)
inner_pos = (pos[0] + 1, pos[1] + 1)
inner_size = ((size[0] - 2) * progress, size[1] - 2)
rect = (round(inner_pos[0]), round(inner_pos[1]), round(inner_size[0]), round(inner_size[1]))
pygame.draw.rect(surface, health_c, rect)
def draw_health(self, surface):
if self.health < self.max_health:
health_rect = pygame.Rect(0, 0, 30, 8)
health_rect.midbottom = self.rect.centerx, self.rect.top
health_rect.midbottom = self.rect.centerx, self.rect.top
self.draw_health_bar(surface, health_rect.topleft, health_rect.size,
(1, 0, 0), (255, 0, 0), (0, 255, 0), self.health / self.max_health)
def draw(self, surface):
flipped_image = pygame.transform.flip(self.image, self.flip, False)
if self.mob_type == 0:
surface.blit(flipped_image, (self.rect.x, self.rect.y - constants.PLAYER_SCALE * constants.OFFSET))
else:
if self.alive:
self.draw_health(surface)
surface.blit(flipped_image, self.rect)
def get_rect(self):
return self.rect
def get_pos(self):
return self.rect.x, self.rect.y
def set_pos(self, pos):
self.rect.x, self.rect.y = pos