-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
267 lines (225 loc) · 9.13 KB
/
main.py
File metadata and controls
267 lines (225 loc) · 9.13 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
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
import pygame
from math import sin, cos, radians
from random import randint
class robot:
def __init__(self, nom: str):
self.nom = nom
self.pos = [960, 360]
self.direction = 0
self.vitesseBase = 2
self.endurance = 300
self.isSprinte = False
self.vitesse = self.vitesseBase
def avance(self,sens):
avanceX=sin(radians(self.direction))*self.vitesse
avanceY=cos(radians(self.direction))*self.vitesse
if sens=='devant':
if mursInvisible[0]<=self.pos[0]+avanceX<=mursInvisible[2]-50:
self.pos[0]+=avanceX
if mursInvisible[1]<=self.pos[1]+avanceY<=mursInvisible[3]-50:
self.pos[1]+=avanceY
if sens=='derriere':
if mursInvisible[0]<=self.pos[0]-avanceX<=mursInvisible[2]-50:
self.pos[0]-=avanceX
if mursInvisible[1]<=self.pos[1]-avanceY<=mursInvisible[3]-50:
self.pos[1]-=avanceY
def tourner(self, sens):
if sens == "gauche":
self.direction = (self.direction+10)%360
elif sens == 'droite':
if self.direction==0:
self.direction=350
else:
self.direction = (self.direction-10)
def update(self):
if self.endurance<=0 and self.isSprinte:
self.vitesse = self.vitesseBase
self.isSprinte=False
elif self.endurance>0 and self.isSprinte:
self.vitesse=self.vitesseBase*2
self.endurance-=2
self.isSprinte=False
elif self.isSprinte==False:
self.vitesse = self.vitesseBase
if self.endurance<300:
self.endurance += 1
def sprinter(self):
if self.endurance > 1:
self.isSprinte=True
def affiche_etat(self):
return {"nom": self.nom, "position": self.pos, "direction": self.direction}
class robotNG:
def __init__(self, nom: str):
self.nom = nom
self.pos = [320, 360]
self.direction = 0
self.vitesseBase = 5
self.endurance = 300
self.isSprinte = False
self.vitesse = self.vitesseBase
def avance(self,sens):
avanceX=sin(radians(self.direction))*self.vitesse
avanceY=cos(radians(self.direction))*self.vitesse
if sens=='devant':
if mursInvisible[0]<=self.pos[0]+avanceX<=mursInvisible[2]-50:
self.pos[0]+=avanceX
if mursInvisible[1]<=self.pos[1]+avanceY<=mursInvisible[3]-50:
self.pos[1]+=avanceY
if sens=='derriere':
if mursInvisible[0]<=self.pos[0]-avanceX<=mursInvisible[2]-50:
self.pos[0]-=avanceX
if mursInvisible[1]<=self.pos[1]-avanceY<=mursInvisible[3]-50:
self.pos[1]-=avanceY
def demiTour(self):
self.direction = (self.direction+180)%360
def tourner(self, sens):
if sens == "gauche":
self.direction = (self.direction+10)%360
elif sens == 'droite':
if self.direction==0:
self.direction=350
else:
self.direction = (self.direction-10)
def update(self):
if self.endurance<=0 and self.isSprinte:
self.vitesse = self.vitesseBase
self.isSprinte=False
elif self.endurance>0 and self.isSprinte:
self.vitesse=self.vitesseBase*2
self.endurance-=2
self.isSprinte=False
elif self.isSprinte==False:
self.vitesse = self.vitesseBase
if self.endurance<300:
self.endurance += 1
def sprinter(self):
if self.endurance > 1:
self.isSprinte=True
def affiche_etat(self):
return {"nom": self.nom, "position": self.pos, "direction": self.direction}
class boulet:
def __init__(self):
self.pos = [0, 0]
self.direction = 0
self.visible = False
self.vitesse = 10
def tir(self, robot):
self.direction = robot.direction
self.pos = [robot.pos[0], robot.pos[1]]
self.visible = True
sonTir[randint(0,len(sonTir)-1)].play()
def update(self):
avanceX=sin(radians(self.direction))*self.vitesse
avanceY=cos(radians(self.direction))*self.vitesse
if mursInvisible[0]<=self.pos[0]-avanceX<=mursInvisible[2] and mursInvisible[1]<=self.pos[1]-avanceY<=mursInvisible[3]:
self.pos[0]+=avanceX
self.pos[1]+=avanceY
else:
self.visible = False
pygame.init()
screen_size = [1280, 720]
screen = pygame.display.set_caption("Mini Jeux")
screen = pygame.display.set_mode(screen_size)
CLOCK = pygame.time.Clock()
mursInvisible=[0,0,1280,720]
fond = pygame.image.load("assets/fond_jeux.png").convert()
sonImpacte = (pygame.mixer.Sound('assets/Impacte_1.mp3'),pygame.mixer.Sound('assets/Impacte_2.mp3'),pygame.mixer.Sound('assets/Impacte_3.mp3'))
sonTir = (pygame.mixer.Sound('assets/Tir_1.mp3'),pygame.mixer.Sound('assets/Tir_2.mp3'),pygame.mixer.Sound('assets/Tir_3.mp3'),pygame.mixer.Sound('assets/Tir_4.mp3'))
# Robot_1
robot_1 = robot("Bilbo")
surfFixe_Robot_1 = pygame.image.load("assets/TankRouge.png").convert()
surfFixe_Robot_1.set_colorkey((0, 0, 0))
surf_robot_1 = surfFixe_Robot_1
# Boulet_1
boulet_1 = boulet()
surf_Boulet_1 = pygame.image.load("assets/boulet_1.png").convert()
surf_Boulet_1.set_colorkey((255,255,255))
surf_Boulet_1 = pygame.transform.scale(surf_Boulet_1, (50,50))
nb_kill_1 = 0
# Robot_2
robot_2 = robotNG("Obit")
surfFixe_Robot_2 = pygame.image.load("assets/TankVert.png").convert()
surfFixe_Robot_2.set_colorkey((0,0,0))
surf_robot_2 = surfFixe_Robot_2
# Boulet_2
boulet_2 = boulet()
surf_Boulet_2 = pygame.image.load("assets/boulet_2.png").convert()
surf_Boulet_2.set_colorkey((0,0,0))
surf_Boulet_2 = pygame.transform.scale(surf_Boulet_2, (50,50))
nb_kill_2 = 0
police = pygame.font.Font(None, 50)
surf_texte_1 = pygame.Surface((200, 30))
surf_texte_2 = pygame.Surface((200, 30))
def rotate_surf(surf, angle):
return pygame.transform.rotate(surf, angle)
running = True
while running:
screen.blit(fond, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_s:
robot_2.demiTour()
surf_robot_2 = rotate_surf(surfFixe_Robot_2, robot_2.direction)
if event.key == pygame.K_m:
boulet_1.tir(robot_1)
if event.key == pygame.K_q:
boulet_2.tir(robot_2)
# Robot 1 Controls
if pygame.key.get_pressed()[pygame.K_UP]:
robot_1.avance("devant")
#if pygame.key.get_pressed()[pygame.K_DOWN]:
# robot_1.avance("derriere")
if pygame.key.get_pressed()[pygame.K_RIGHT]:
robot_1.tourner("droite")
surf_robot_1 = rotate_surf(surfFixe_Robot_1, robot_1.direction)
#if pygame.key.get_pressed()[pygame.K_LEFT]:
# robot_1.tourner("gauche")
# surf_robot_1 = rotate_surf(surfFixe_Robot_1, robot_1.direction)
#if pygame.key.get_pressed()[pygame.K_RCTRL]:
# robot_1.sprinter()
# Robot 2 Controls
if pygame.key.get_pressed()[pygame.K_w]:
robot_2.avance("devant")
#if pygame.key.get_pressed()[pygame.K_s]:
# robot_2.avance("derriere")
if pygame.key.get_pressed()[pygame.K_d]:
robot_2.tourner("droite")
surf_robot_2 = rotate_surf(surfFixe_Robot_2, robot_2.direction)
if pygame.key.get_pressed()[pygame.K_a]:
robot_2.tourner("gauche")
surf_robot_2 = rotate_surf(surfFixe_Robot_2, robot_2.direction)
if pygame.key.get_pressed()[pygame.K_LSHIFT]:
robot_2.sprinter()
if boulet_1.visible == True:
boulet_1.update()
screen.blit(surf_Boulet_1, boulet_1.pos)
if surf_Boulet_1.get_rect(center=boulet_1.pos).colliderect(surf_robot_2.get_rect(center=robot_2.pos)):
nb_kill_1 += 1
sonImpacte[randint(0,len(sonImpacte)-1)].play()
boulet_1.visible = False
if boulet_2.visible == True:
boulet_2.update()
screen.blit(surf_Boulet_2, boulet_2.pos)
if surf_Boulet_2.get_rect(center=boulet_2.pos).colliderect(surf_robot_1.get_rect(center=robot_1.pos)):
nb_kill_2 += 1
sonImpacte[randint(0,len(sonImpacte)-1)].play()
boulet_2.visible = False
# Textes
texte_1 = police.render(str(nb_kill_1), True, (255, 0, 0))
texte_2 = police.render(str(nb_kill_2), True, (0, 255, 0))
surf_texte_1.fill((255, 255, 255))
surf_texte_2.fill((255, 255, 255))
surf_texte_1.blit(texte_1, (0, 0))
surf_texte_2.blit(texte_2, (0, 0))
# Update robot a mettre a la fin
robot_1.update()
robot_2.update()
screen.blit(surf_robot_1, robot_1.pos)
screen.blit(surf_texte_1, (100, 100))
screen.blit(surf_robot_2, robot_2.pos)
screen.blit(surf_texte_2, (500, 100))
pygame.display.flip()
CLOCK.tick(30)
pygame.quit()