-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.py
229 lines (194 loc) · 7.37 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
import math
import pygame
from animation import Animation
from entity import Entity
from newlevel import NewLevelException
from physics import PhysicsEngine
class Player(Entity):
HEIGHT_JUMP = 104
VEL_LEFTRIGHT = 200
VEL_KNOCKBACK = VEL_LEFTRIGHT
VEL_BULLET = 500
VEL_JUMP = math.sqrt(2 * HEIGHT_JUMP * PhysicsEngine.ACCEL_GRAVITY)
CD_TEETH = 250
CD_SHOT = 250
DIST_KNOCKBACK = VEL_KNOCKBACK / 10
ANIM_FRAMELEN_WALK = 100
ANIM_RIGHT = Animation([
pygame.image.load("mock/shark-right-1.png"),
pygame.image.load("mock/shark-right-2.png"),
pygame.image.load("mock/shark-right-3.png"),
pygame.image.load("mock/shark-right-4.png"),
pygame.image.load("mock/shark-right-5.png"),
pygame.image.load("mock/shark-right-6.png"),
],
ANIM_FRAMELEN_WALK)
ANIM_LEFT = Animation([
pygame.image.load("mock/shark-left-1.png"),
pygame.image.load("mock/shark-left-2.png"),
pygame.image.load("mock/shark-left-3.png"),
pygame.image.load("mock/shark-left-4.png"),
pygame.image.load("mock/shark-left-5.png"),
pygame.image.load("mock/shark-left-6.png")
],
ANIM_FRAMELEN_WALK)
ANIM_IDLE_RIGHT = Animation([
pygame.image.load("mock/shark-right-1.png")
],
0)
ANIM_IDLE_LEFT = Animation([
pygame.image.load("mock/shark-left-1.png")
],
0)
ANIM_DEAD = Animation([
pygame.image.load("mock/shark-dead.png")
],
0)
def __init__(self, image_path, position):
self.position = position
self.velocity = pygame.math.Vector2()
self.type = Entity.TYPE_PLAYER
self.health = 3
self.can_jump = False
self.jumping = False
self.dead = False
self.stunned = 10
self.animation = Player.ANIM_RIGHT
self.reset_collider()
self.shot_cooldown = 0
self.teeth_cooldown = 0
def collide(self, other, axis, dt):
if other.type == Entity.TYPE_ENEMY_STATIC \
or other.type == Entity.TYPE_ENEMY_DYNAMIC \
or other.type == Entity.TYPE_CHAMELEON:
self.health -= other.damage
print("collided with enemy")
if other.damage > 0:
if axis == 'y':
self.velocity.y = -self.velocity.y
else:
self.velocity.x = -self.velocity.x
self.stunned = 10
if self.health < 1:
self.die()
elif other.type == Entity.TYPE_POTION and other.active:
other.active = False
raise NewLevelException()
else:
self.resolve_collision(other, axis, dt)
self.reset_collider()
def take_damage(self, damage, source):
self.health -= damage
if self.health < 1:
self.die()
def die(self):
self.animation = Player.ANIM_DEAD
self.dead = True
def update_logic(self, input_m, entities, dt):
if self.dead:
return
if self.stunned > 0:
self.stunned -= 1
return
if self.jumping:
self.jumping = False
if input_m.left:
self.velocity.x = -Player.VEL_LEFTRIGHT
self.animation = Player.ANIM_LEFT
elif input_m.right:
self.velocity.x = Player.VEL_LEFTRIGHT
self.animation = Player.ANIM_RIGHT
else:
self.velocity.x = 0
if self.animation == Player.ANIM_LEFT \
or self.animation == Player.ANIM_IDLE_LEFT:
self.animation = Player.ANIM_IDLE_LEFT
else:
self.animation = Player.ANIM_IDLE_RIGHT
if input_m.up:
if self.can_jump:
self.can_jump = False
self.jumping = True
self.velocity.y = -self.VEL_JUMP
if input_m.z and self.shot_cooldown <= 0:
bullet_position = pygame.math.Vector2(self.position.x, self.position.y + 52)
bullet_direction = pygame.math.Vector2(0, 0)
if self.animation == Player.ANIM_RIGHT \
or self.animation == Player.ANIM_IDLE_RIGHT:
bullet_position.x += 2.5 * 26
bullet_direction.x = Player.VEL_BULLET
else:
bullet_direction.x = -Player.VEL_BULLET
entities.append(PlayerBullet(bullet_position, bullet_direction))
self.shot_cooldown = Player.CD_SHOT
if input_m.x and self.teeth_cooldown <= 0:
teeth_position = pygame.math.Vector2(self.position.x, self.position.y + 26)
if self.animation == Player.ANIM_RIGHT \
or self.animation == Player.ANIM_IDLE_RIGHT:
teeth_position.x += 3 * 26
else:
teeth_position.x -= 26
entities.append(PlayerTeeth(teeth_position))
self.teeth_cooldown = Player.CD_TEETH
if self.shot_cooldown > 0:
self.shot_cooldown -= dt
if self.teeth_cooldown > 0:
self.teeth_cooldown -= dt
def reset_collider(self):
dx = 19
dy = 26
self.collider = pygame.Rect(self.position.x + dx,
self.position.y + dy,
self.animation.current_frame().get_width() - 39,
self.animation.current_frame().get_height() - 26)
class PlayerBullet(Entity):
VEL_BULLET = 500
ANIM_BULLET = Animation([pygame.image.load("mock/bullet.png")], 0)
DMG_BULLET = 1
TIME_BULLET = 5000
def __init__(self, position, velocity):
self.animation = PlayerBullet.ANIM_BULLET
self.position = position
self.velocity = velocity
self.type = Entity.TYPE_PLAYER_RANGED
self.reset_collider()
self.dead = False
self.time = 0
def collide(self, other, axis, dt):
other.take_damage(PlayerBullet.DMG_BULLET, self)
print("bullet hit a " + str(type(other)))
self.dead = True
def update_logic(self, input_m, entities, dt):
self.time += dt
if self.time > PlayerBullet.TIME_BULLET:
self.dead = True
if self.dead:
entities.remove(self)
class PlayerTeeth(Entity):
DURATION_TEETH = 100
ANIM_TEETH = Animation([
pygame.image.load("mock/shark-bite-1.png"),
pygame.image.load("mock/shark-bite-2.png"),
pygame.image.load("mock/shark-bite-3.png"),
pygame.image.load("mock/shark-bite-4.png"),
], DURATION_TEETH / 4)
DMG_TEETH = 1
def __init__(self, position):
self.animation = PlayerTeeth.ANIM_TEETH
self.animation.frame_number = 0
self.animation.current_time = 0
self.position = position
self.velocity = pygame.math.Vector2()
self.type = Entity.TYPE_PLAYER_MELEE
self.reset_collider()
self.duration = 0
self.dead = False
def collide(self, other, axis, dt):
other.take_damage(PlayerTeeth.DMG_TEETH, self)
self.dead = True
def update_logic(self, input_m, entities, dt):
self.duration += dt
if self.duration > PlayerTeeth.DURATION_TEETH:
self.dead = True
if self.dead:
entities.remove(self)