-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.py
90 lines (78 loc) · 2.94 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
import pygame
from pygame.locals import *
from Box2D import *
from sprite import Sprite, Animation
from debug import draw_body
class Player(object):
def __init__(self, world, pos=(0, 0)):
self.body = world.b2world.CreateDynamicBody(
position=pos,
allowSleep=False
)
self.body.CreatePolygonFixture(
box=(0.8, 1.6),
density=1,
friction=0.0,
)
self.body.linearDamping = 0.0
self.body.angularDamping = 0.0
self.sensor = self.body.CreatePolygonFixture(
box=(0.6, 0.2, (0, -1.6), 0),
density=1,
friction=0.0,
isSensor = True
)
self.stand_sprite = Sprite("data/art/stand.png")
self.jump_sprite = Sprite("data/art/jump.png")
self.fall_sprite = Sprite("data/art/fall.png")
walksprite0 = self.stand_sprite
walksprite1 = Sprite("data/art/walk1.png")
walksprite2 = Sprite("data/art/walk2.png")
self.walk_sprite = Animation((
walksprite0, walksprite1, walksprite1,
walksprite0, walksprite2, walksprite2
))
self.sprite = self.stand_sprite
@property
def pos(self):
return tuple(self.body.position)
def update(self, delta):
keys = pygame.key.get_pressed()
can_jump = False
for ce in self.body.contacts:
if ce.contact.touching:
fixtureA = ce.contact.fixtureA
fixtureB = ce.contact.fixtureB
if self.sensor == fixtureA or self.sensor == fixtureB:
can_jump = True
break
# Vertical logic
if (keys[K_w] or keys[K_UP]) and can_jump:
self.body.linearVelocity[1] = 30.0
elif self.body.linearVelocity[1] > 0.0:
vel_delta = self.body.linearVelocity[1] * delta * 2.0
self.body.linearVelocity[1] -= vel_delta
# Horizontal logic
if keys[K_a] or keys[K_LEFT]:
self.body.linearVelocity[0] -= 30.0 * delta
if keys[K_d] or keys[K_RIGHT]:
self.body.linearVelocity[0] += 30.0 * delta
self.body.linearVelocity[0] -= self.body.linearVelocity[0] * delta * 2.0
self.body.angle = 0.0
self.body.angularVelocity = 0.0
# Sprite logic
if can_jump:
if abs(self.body.linearVelocity[0]) > 1.0:
self.sprite = self.walk_sprite
else:
self.sprite = self.stand_sprite
elif self.body.linearVelocity[1] > 0.0:
self.sprite = self.jump_sprite
else:
self.sprite = self.fall_sprite
sprite_delta = delta * abs(self.body.linearVelocity[0]) * 2.0
mirror = self.body.linearVelocity[0] < 0.0
self.sprite.update(sprite_delta, mirror)
def render(self, screen, camera):
self.sprite.render(screen, camera, self.pos)
#draw_body(self.body, screen, camera)