-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
186 lines (139 loc) · 5.7 KB
/
game.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
from plateforme import *
from ennemy import *
import math
from datetime import datetime
import random
class Game():
def __init__(self, map: (), fond: (), objets: (), player, enemies):
self.fondecran = Sprite('Assets/Background/background_nuit.png', 1, 1, colorkey=False, scale=2.56)
self.map = map
self.fond = fond
self.objets = objets
self.player = player
self.enemies = enemies
self.platforms = Platforms()
self.background = Platforms()
self.startx = 0
self.starty = 0
self.endx = 0
self.endy = 0
self.score = 0
self.time = 0
self.test = pygame.font.Font('Font/Pixeled.ttf', 10)
self.load()
def load(self):
# Background
for y in range(len(self.fond)):
for i in range(len(self.fond[y])):
id = self.fond[y][i]
if id:
self.background.append(Fond(i * WIDTH_CELL, y * HEIGHT_CELL, id))
if id == 10:
self.startx = int(i * WIDTH_CELL + WIDTH_CELL / 2)
self.starty = int(y * HEIGHT_CELL + 50)
elif id == 11:
self.endx = int(i * WIDTH_CELL)
self.endy = int(y * HEIGHT_CELL)
# Objets
for y in range(len(self.objets)):
for i in range(len(self.objets[y])):
id = self.objets[y][i]
if id:
self.background.append(Objet(i * WIDTH_CELL, y * HEIGHT_CELL, id))
# Map
for y in range(len(self.map)):
for i in range(len(self.map[y])):
id = self.map[y][i]
if id:
if id in range(0, len(TUILES)+1):
self.platforms.append(Platform(i * WIDTH_CELL, y * HEIGHT_CELL, id))
elif id == -1:
self.platforms.append(Flag(i * WIDTH_CELL, y * HEIGHT_CELL))
elif id == -2:
# rond droite
self.platforms.append(Rambarde( i * WIDTH_CELL, y * HEIGHT_CELL))
elif id == -3:
# rond gauche
rambarde = Rambarde( i *WIDTH_CELL, y * HEIGHT_CELL)
rambarde.flip = True
self.platforms.append(rambarde)
self.playerLose()
def display(self, screen):
self.fondecran.draw(screen, 0, 0, handle=0)
self.background.draw(screen)
self.platforms.draw(screen)
self.player.falling = not self.platforms.collision(self.player)
for e in self.enemies:
e.draw(screen)
if isinstance(e, Sirene):
e.flip = self.player.x < e.x
distance = math.hypot(abs(self.player.x - e.x), abs(self.player.y - e.y))
if distance < e.attirance:
e.attire(self.player)
if e.hitbox and self.player.hitbox:
if e.hitbox.colliderect(self.player.hitbox):
self.playerLose()
if self.player.stream.dir != 0:
if self.time == 0:
self.time = self.get_time()
if abs(self.get_time() - self.time) <= 2:
self.player.stream.draw(screen, self.player.flip)
self.player.stream.fear(self.enemies)
if abs(self.get_time() - self.time) > 2:
self.player.setStream(0)
self.player.blow = False
self.time = 0
if (self.player.y >= HEIGHT and self.player.falling)\
or (self.player.x < 0 or self.player.x > WIDTH):
self.playerLose()
scores = self.test.render('REAPPARITIONS: ' + str(self.score), True, [255, 255, 255])
screen.blit(scores, (WIDTH / 2 - 60, 20))
self.player.draw(screen)
if self.player.hitbox and self.player.hitbox.colliderect(
pygame.rect.Rect(self.endx, self.endy, WIDTH_CELL, HEIGHT_CELL)):
return 1
else:
return 0
def playerLose(self):
self.player.x = self.startx
self.player.y = self.starty
self.score += 1
# print(self.score)
def initMixer(self):
pygame.init()
pygame.mixer.init()
pygame.mixer.music.load("Audio/1.mp3")
pygame.mixer.music.play(-1)
pygame.mixer.music.set_volume(0.3)
def playMixerEnnemy(self, channel, sounds):
channel.set_volume(0.5)
random_sound = random.choice(sounds)
if not channel.get_busy():
if random.randint(0, 30) == 10:
channel.play(random_sound)
def playMixerPlayer(self, channel, sound_jump, sound_flute, sound_step):
channel.set_volume(0.3)
if self.player.jumping:
if not channel.get_busy():
if not self.player.falling:
channel.play(sound_jump)
if self.player.blow:
if not channel.get_busy():
channel.set_volume(0.2)
channel.play(sound_flute)
if self.player.xVelocity != 0 and self.player.falling == False and self.player.jumping == False:
if not channel.get_busy():
channel.set_volume(0.4)
channel.play(sound_step)
def playMixerAmbiant(self, channel, sound):
channel.set_volume(0.8)
if not channel.get_busy():
channel.play(sound)
def get_time(self):
now = datetime.now()
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
now = int(dt_string[len(dt_string) - 2] + dt_string[len(dt_string) - 1])
if now == 0:
return 1
else:
return now