-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplayer.py
More file actions
105 lines (92 loc) · 3.42 KB
/
player.py
File metadata and controls
105 lines (92 loc) · 3.42 KB
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
import pyxel
from pygame import mixer
mixer.init()
class Player:
def __init__(self, x, y, d):
self.x = x
self.y = y
self.dir = d
self.moving = 0
self.jumping = 0
self.grav = 2
self.on_key = -1
self.alive = 1
self.win = 0
self.deadzone = 2000
self.jump_sound = mixer.Sound("ressources/jump.wav")
def move(self, room, current_screen, difficulty):
if (
(pyxel.btn(pyxel.KEY_RIGHT) or pyxel.btn(pyxel.GAMEPAD1_BUTTON_DPAD_RIGHT) or pyxel.btnv(pyxel.GAMEPAD1_AXIS_LEFTX) > self.deadzone)
and room.collision(self.x + 8, self.y) != 1
and room.collision(self.x + 8, self.y + 7) != 1
):
self.x += 1
self.dir = 0
self.moving = 1
elif (
(pyxel.btn(pyxel.KEY_LEFT) or pyxel.btn(pyxel.GAMEPAD1_BUTTON_DPAD_LEFT) or pyxel.btnv(pyxel.GAMEPAD1_AXIS_LEFTX) < -self.deadzone)
and ( (room.collision(self.x - 1, self.y) != 1
and room.collision(self.x - 1, self.y + 7) != 1
and self.x > 0)
or (self.x <= 0 and current_screen > 0) )
):
self.x -= 1
self.dir = 1
self.moving = 1
else:
self.moving = 0
if (
(pyxel.btn(pyxel.KEY_SPACE) or pyxel.btn(pyxel.KEY_UP) or pyxel.btn(pyxel.GAMEPAD1_BUTTON_A))
and (room.collision(self.x, self.y + 8) == 1
or room.collision(self.x + 7, self.y + 8) == 1)
):
self.jumping = 12
self.jump_sound.play()
for i in range(self.grav):
if (
self.jumping == 0
and room.collision(self.x, self.y + 8) != 1
and room.collision(self.x + 7, self.y + 8) != 1
):
self.y += 1
if (
room.collision(self.x, self.y - 2) == 1
or room.collision(self.x + 7, self.y - 2) == 1
):
self.jumping = 0
if self.jumping > 0:
self.y -= 2
self.jumping -= 1
if difficulty <= 1:
if (room.collision(self.x + 1, self.y + 1) == 5
or room.collision(self.x + 6, self.y + 1) == 5
or room.collision(self.x + 1, self.y + 6) == 5
or room.collision(self.x + 6, self.y + 6) == 5
):
self.alive = 0
else:
if (room.collision(self.x, self.y) == 5
or room.collision(self.x + 7, self.y) == 5
or room.collision(self.x, self.y + 7) == 5
or room.collision(self.x + 7, self.y + 7) == 5
):
self.alive = 0
if (room.collision(self.x, self.y) == 6
or room.collision(self.x + 7, self.y) == 6
or room.collision(self.x, self.y + 7) == 6
or room.collision(self.x + 7, self.y + 7) == 6
):
self.win = 1
def reset(self, x, y):
self.x = x
self.y = y
self.dir = 0
self.moving = 0
self.jumping = 0
def draw_player(self):
if self.jumping > 0:
pyxel.blt(self.x, self.y, 0, 24, 8*self.dir, 8, 8, 0)
elif self.moving == 1:
pyxel.blt(self.x, self.y, 0, 8*(pyxel.frame_count % 2 + 1), 8*self.dir, 8, 8, 0)
else:
pyxel.blt(self.x, self.y, 0, 8 + 8*self.dir, 8*self.dir, 8, 8, 0)