forked from nlguillemot/GGJ2012-Poncho
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharacter.py
297 lines (247 loc) · 10.5 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import pygame
import vector2
import bullet
import collision as cl
from math import fabs
import enemy
gravity = 9.8
gravity_modifier = 3
#creates a class for characters in the game
class character:
#set run speed
max_movement_speed = 200 #px/s
min_movement_speed = 2
movement_acceleration = 400 #ps/s^2
friction_constant = 0.05 #u
bullet_reload_max = 2 #milliseconds
bullet_reload_ammo = 4
position_x_start = 200
position_y_start = 300
jump_speed = 10 #px/s
def __init__(self):
self.set_acceleration(0,0)
self.set_position(0,0)
self.position.x = self.position_x_start
self.position.y = self.position_y_start
self.set_speed(0,0)
self.direction = 1
self.jumps_allowed = 2
self.affected_by_gravity = True
self.bullet_ammo = 4
self.bullet_reload_time = 0
self.__is_key_down_right = False
self.__is_key_down_left = False
#images
self.idle = pygame.image.load("kid_idle.png").convert_alpha()
self.run = pygame.image.load("kid_run.png").convert_alpha()
self.jump = pygame.image.load("kid_jump.png").convert_alpha()
self.skid = pygame.image.load("kid_skid.png").convert_alpha()
self.image = self.idle
self.in_air = True
#sets the x and y of the character
def set_position(self, xpos, ypos):
self.position = vector2.vector2(xpos, ypos)
#retrieves the x and y of the character
def get_position(self):
return self.position
#set the acceleration of the character
def set_acceleration(self, xaccel, yaccel):
self.acceleration = vector2.vector2(xaccel, yaccel)
#get the acceleration of the character
def get_acceleration(self):
return self.acceleration
#defines the horizontal and vertical speeds of the character
def set_speed(self, xspeed, yspeed):
self.speed = vector2.vector2(xspeed, yspeed)
#retrieves the vector of the character's speed
def get_speed(self):
return self.speed
#define whether or not the character is affected by gravity
def set_affected_by_gravity(self, state):
self.affected_by_gravity = state
#applies gravity when necessary
def __apply_gravity(self,dtime):
self.speed.y += (gravity * gravity_modifier) * dtime
#returns whether or not the character is on the ground
def __on_hit_floor(self):
self.in_air = False
self.jumps_allowed = 2
#returns whether or not the character is in the air
def __is_in_air(self):
return self.in_air
#returns whether or not the character is moving horizontally at the maximum speed
def __is_at_max_speed(self):
return fabs(self.speed.x) >= self.max_movement_speed
#def applies acceleration and friction depending on keys currently pressed and current speed and all that stuff
def __update_x_acceleration(self):
self.acceleration.x = 0
if (self.__is_key_down_left and not self.speed.x <= -self.max_movement_speed):
self.acceleration.x += -self.movement_acceleration
if (self.__is_key_down_right and not self.speed.x >= self.max_movement_speed):
self.acceleration.x += self.movement_acceleration
if(not self.__is_key_down_left and not self.__is_key_down_right):
self.speed.x *= (1.0-self.friction_constant)
#if you're moving at a negligeable speed, just stop him
if(fabs(self.speed.x) <= (self.min_movement_speed)):
self.speed.x = 0
#updates the speed and position depending on the acceleration
def __update_x_pos_and_speed(self,dtime):
if(fabs(self.speed.x) > self.max_movement_speed):
self.speed.x = self.max_movement_speed * fabs(self.speed.x)/self.speed.x
self.speed.x += (self.acceleration.x * dtime)
self.position.x += self.speed.x *dtime
#updates the y position, speed, acceleration depending on dtime
def __update_y_pos_speed_acceleration(self,dtime):
self.__apply_gravity(dtime)
self.position.y += self.speed.y
#decides what the animation played should be based on the physics and the direction facing and all that
def __update_animation(self):
if(self.__is_in_air()):
self.image = self.jump
#on floor
else:
if(fabs(self.speed.x) <= self.min_movement_speed):
self.image = self.idle
elif((self.__is_key_down_left == False) and (self.__is_key_down_right == False)):
self.image = self.skid
else:
self.image = self.run
#updating bullet properties
def __update_bullet(self,dtime):
if (self.bullet_reload_time > 0):
self.bullet_reload_time -= dtime
if(self.bullet_reload_time <= 0):
self.bullet_ammo = self.bullet_reload_ammo
self.bullet_reload_time = 0
if ((self.bullet_ammo == 0) and (self.bullet_reload_time == 0)):
self.bullet_reload_time = self.bullet_reload_max
def set_tile_map_for_collisions(self,target):
self.tile_map_for_collisions = target
def set_tile_map_for_data(self,target):
self.tile_map_for_data = target
#get list of tiles currently colliding with the player
def __get_colliding_tiles(self):
colliding_tiles = list()
for row in range(len(self.tile_map_for_collisions)):
for column in range(len(self.tile_map_for_collisions[row])):
current_tile = self.tile_map_for_collisions[row][column]
if(cl.AABB_2d_collide(self.position.x,self.position.y,self.image.get_width(),self.image.get_height(),
current_tile.x,current_tile.y,current_tile.image.get_width(),current_tile.image.get_height())):
colliding_tiles.append(current_tile)
return colliding_tiles
#resolves collisions with tiles
def __resolve_collisions(self):
old_pos = vector2.vector2(self.position.x,self.position.y)
old_speed = vector2.vector2(self.speed.x,self.speed.y)
for tiles in self.__get_colliding_tiles():
if tiles.collidable:
depth = cl.AABB_intersection_depth(self.position.x,self.position.y,self.image.get_width(),self.image.get_height(),
tiles.x,tiles.y,tiles.image.get_width(),tiles.image.get_height())
if(fabs(depth.x) > 0.05 or fabs(depth.y) > 0.05):
absDepthX = fabs(depth.x)
absDepthY = fabs(depth.y)
if(absDepthY < absDepthX):
self.position = vector2.vector2(self.position.x,self.position.y + depth.y)
else:
#if under your feet
if fabs(tiles.y - (self.position.y + self.image.get_height() - self.speed.y)) < 0.05:
#if depth.x > 0.05 or tiles.y > self.position.y + self.image.get_height():
None
#not under your feet
else:
self.position = vector2.vector2(self.position.x + depth.x, self.position.y)
if(fabs(old_pos.x - self.position.x) > 0.05):
#print "stopped x because resolved %f" % (old_pos.x - self.position.x)
self.speed.x = 0
self.acceleration.x = 0
if(fabs(old_pos.y - self.position.y) > 0.05):
self.speed.y = 0
if(old_speed.y > 0 and fabs(self.speed.y) < 0.05 and self.in_air):
self.__on_hit_floor()
#restarts the level
def restart_level(self):
print "respawning at %f %f" % (self.tile_map_for_data.spawnpos.x,self.tile_map_for_data.spawnpos.y)
self.position = vector2.vector2(self.tile_map_for_data.spawnpos.x,self.tile_map_for_data.spawnpos.y)
bullet.bullet_list = list()
self.bullet_ammo = self.bullet_reload_ammo
self.bullet_reload_time = 0
enemy.spawn_enemies(self.tile_map_for_data)
#detects character collision with bullets
def is_hit_by_bullet(self):
for bullets in bullet.bullet_list:
if cl.AABB_2d_collide(self.position.x, self.position.y, self.image.get_width(), self.image.get_height(),
bullets.position.x, bullets.position.y, bullets.image.get_width(), bullets.image.get_height()):
self.restart_level()
break
def handle_enemy_collisions(self):
for enemies in enemy.enemy_list:
if(cl.AABB_2d_collide(self.position.x,self.position.y,self.image.get_width(),self.image.get_height(),
enemies.position.x,enemies.position.y,enemies.image.get_width(),enemies.image.get_height())):
self.restart_level()
break
#dtime is in seconds, this updates the character's position based on time passed
def update(self, dtime):
self.__update_x_acceleration()
self.__update_x_pos_and_speed(dtime)
self.__update_y_pos_speed_acceleration(dtime)
self.__resolve_collisions()
self.__update_animation()
self.__update_bullet(dtime)
self.is_hit_by_bullet()
self.handle_enemy_collisions()
#draws the character
def draw(self, screen):
needs_to_be_flipped = (self.direction == -1)
transformed_image = pygame.transform.flip(self.image, needs_to_be_flipped, 0)
screen.blit(transformed_image, pygame.Rect(self.position.x,self.position.y,0,0))
#handles keys pressed down
def handle_key_down(self, key):
if (key == pygame.K_RIGHT):
self.__is_key_down_right = True
if (not self.__is_in_air()):
self.image = self.run
self.direction = 1
self.acceleration.x = self.movement_acceleration
elif (key == pygame.K_LEFT):
self.__is_key_down_left = True
if (not self.__is_in_air()):
self.image = self.run
self.direction = -1
self.acceleration.x = -self.movement_acceleration
elif (key == pygame.K_z):
if (self.jumps_allowed > 0):
self.in_air = True
self.image = self.jump
self.speed.y = -self.jump_speed
self.jumps_allowed -= 1
elif key == pygame.K_r:
self.restart_level()
elif (key == pygame.K_x):
self.__shoot_bullet()
elif (key == pygame.K_c):
self.bullet_reload_time = self.bullet_reload_max
print self.bullet_reload_time
#handles key release
def handle_key_up(self, key):
if (key == pygame.K_RIGHT):
self.__is_key_down_right = False
if (self.direction is 1):
if (not self.__is_in_air()):
self.image = self.idle
elif (key == pygame.K_LEFT):
self.__is_key_down_left = False
if (self.direction is -1):
if (not self.__is_in_air()):
self.image = self.idle
#shoots a bullet from the player
def __shoot_bullet(self):
bullet_offset = 10
if ((self.bullet_ammo > 0) and (self.bullet_reload_time is 0)):
new_bullet_position = vector2.vector2(self.position.x - bullet_offset,self.position.y + self.image.get_height()/2)
print ("spawned bullet at %d %d") % (new_bullet_position.x, new_bullet_position.y)
#if player facing right, offset bullet spawn position to his right side
if(self.direction == 1):
new_bullet_position.x += self.image.get_width() + 2*bullet_offset
self.bullet_ammo -= 1
#spawn bullet at new_bullet_position with speed left or right depending on facing direction
bullet.bullet_list.append(bullet.bullet(new_bullet_position.x,new_bullet_position.y,bullet.bullet_speed * self.direction,0))