-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.py
170 lines (137 loc) · 5.45 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
import pygame
from sprite import Sprite
from plateforme import Platform
from stream import Stream
class Player():
def __init__(self, x, y):
# Listes des sprites pour l'animation
self.sprites = {
'run': Sprite('Assets/Player/run.png', 8, 1),
'blow': Sprite('Assets/Player/joueur_souffle.png', 20, 1),
'idle': Sprite('Assets/Player/idle.png', 4, 1),
'chute': Sprite('Assets/Player/player_chute.png', 4, 1)
}
# Initialisation position
self.setLocation(x, y)
# Vitesse du joueur
self.velocity = 20
self.jumpRange = [-10, -6, -5, -4, 1, 4, 5, 6, 10]
# Flip
self.flip = False
# Current plateforme
self.currentPlatorm = None
self.hitbox = None
self.stream = Stream(self.x,self.y,0)
self.glissement = False
self.valeurTampon = 0
# Définition du joueur
def setLocation(self, x, y):
self.x, self.y = x, y
self.xVelocity = 0
self.jumpCounter = 0
self.jumping = False
self.falling = True
self.blow = False
def setStream(self,dir):
if self.flip==True:
self.stream= Stream(self.x,self.y,dir)
else:
self.stream= Stream(self.x, self.y,dir)
# Actions du joueur
def events(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_q]:
if self.y == self.stream.y and self.stream.dir==1 :
self.xVelocity = -self.velocity -5
self.blow = False
self.flip = True
elif self.y == self.stream.y and self.stream.dir==2 :
self.xVelocity = -self.velocity +10
self.blow = False
self.flip = True
else:
self.xVelocity = -self.velocity
self.blow = False
self.flip = True
elif keys[pygame.K_d]:
if self.y == self.stream.y and self.stream.dir==2 :
self.xVelocity = self.velocity +5
self.blow = False
self.flip = False
elif self.y == self.stream.y and self.stream.dir==1 :
self.xVelocity = self.velocity - 10
self.blow = False
self.flip = False
else:
self.xVelocity = self.velocity
self.blow = False
self.flip = False
else: self.xVelocity = 0
if keys[pygame.K_z] and not self.jumping and not self.falling:
self.jumping = True
self.jumpCounter = 0
self.blow = False
elif keys[pygame.K_DOWN] and self.xVelocity == 0 and not self.jumping and not self.falling:
self.setStream(0)
self.blow= True
elif keys[pygame.K_UP] and self.xVelocity == 0 and not self.jumping and not self.falling:
self.setStream(3)
self.blow = True
elif keys[pygame.K_RIGHT] and self.xVelocity == 0 and not self.jumping and not self.falling:
self.setStream(2)
self.blow = True
self.flip = False
elif keys[pygame.K_LEFT] and self.xVelocity == 0 and not self.jumping and not self.falling:
self.setStream(1)
self.blow = True
self.flip = True
# Déplacement du joueur
def move(self):
if self.xVelocity == 0 and self.stream.dir in [1, 2] and self.stream.y == self.y:
self.x += -1 if self.stream.dir == 1 else 1
else:
self.x += self.xVelocity
self.glissement = False
if self.currentPlatorm:
if not self.currentPlatorm.test(self) :
self.falling = True
self.currentPlatorm = None
if self.jumping and self.stream.dir==3 and (self.stream.x-self.x<45 and self.stream.x-self.x>-45):
self.y += self.jumpRange[self.jumpCounter] * 7
self.jumpCounter += 1
if self.jumpCounter >= len(self.jumpRange) / 2:
self.jumping = False
self.falling = True
elif self.jumping:
self.y += self.jumpRange[self.jumpCounter] * 5
self.jumpCounter += 1
if self.jumpCounter >= len(self.jumpRange) /2:
self.jumping = False
self.falling = True
elif self.falling and not self.currentPlatorm:
self.y += 20
def draw(self, fenetre):
self.events()
self.move()
s = ''
if self.blow:
self.sprites['blow'].draw(fenetre, self.x, self.y, self.flip)
s = 'blow'
elif self.falling:
self.sprites['chute'].draw(fenetre, self.x, self.y, self.flip)
s = 'chute'
elif self.xVelocity == 0:
self.sprites['idle'].draw(fenetre, self.x, self.y, self.flip)
s = 'idle'
elif self.jumping or self.flip:
self.sprites['run'].draw(fenetre, self.x, self.y, self.flip)
s = 'run'
else:
self.sprites['run'].draw(fenetre, self.x, self.y, self.flip)
s='run'
self.hitbox = pygame.rect.Rect(self.x + self.sprites[s].handle[7][0],
self.y + self.sprites[s].handle[7][1],
self.sprites[s].cells[0][2],
self.sprites[s].cells[0][3]
)
# pygame.draw.rect(fenetre, [255, 0, 0], self.hitbox)