This repository has been archived by the owner on Mar 30, 2021. It is now read-only.
/
serveur.py
executable file
·291 lines (242 loc) · 10 KB
/
serveur.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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/usr/bin/python2
# -*- coding: utf-8 -*-
import pygame
import sys
import time
from pygame.locals import *
from PodSixNet.Channel import Channel
from PodSixNet.Server import Server
import outils
from Balle import Ball
from Bar import Bar, Bars
from Brique import Briques, Brique
from Tir import Tirs, Tir
class ClientChannel(Channel, pygame.sprite.Sprite):
"""
Cette classe gère un client, qui se connecte au serveur, et lui attribue un bar.
"""
def __init__(self, *args, **kwargs):
Channel.__init__(self, *args, **kwargs)
pygame.sprite.Sprite.__init__(self)
self.bar = Bar()
self.tirCompteurTmp = outils.FREQUENCE_TIR
self.shotAllowed = True
self.tir_sprites = Tirs()
self.joueur = outils.J1
def Close(self):
self._server.del_client(self)
def Network(self, data):
pass
def Network_keys(self, data):
"""
Cette fonction permet de récupérer les mouvements du client, et de les traiter.
:param data: Les données reçues du client.
"""
touches = data['keys']
if touches[K_RIGHT] or touches[K_d]:
self.bar.right()
if touches[K_LEFT] or touches[K_q]:
self.bar.left()
if touches[K_SPACE]:
if self.shotAllowed:
tir = Tir(self.bar)
self.tir_sprites.add(tir)
self.shotAllowed = False
self.tirCompteurTmp = outils.FREQUENCE_TIR
self.send_info("sound", "shot.wav")
else:
if self.tirCompteurTmp >= 0:
self.tirCompteurTmp -= 1
else:
self.shotAllowed = True
def update_bar(self):
self.bar.update()
self.tir_sprites.update(self.joueur)
def send_info(self, action, message):
print action + " - " + message
self.Send({"action": action, "message": message})
def get_bar(self):
return self.bar
class MyServer(Server):
channelClass = ClientChannel
def __init__(self, *args, **kwargs):
Server.__init__(self, *args, **kwargs)
pygame.display.set_caption("Server")
self.screen = pygame.display.set_mode(outils.SIZE_SERVEUR)
self.briques = Briques()
for i in range(10,outils.SCREEN_WIDTH,120):
self.brique = Brique((i, outils.SCREEN_HEIGHT/2))
self.briques.add(self.brique)
self.clients = Bars()
self.balle = Ball()
self.endGame = 0
self.spriteJ1 = pygame.sprite.RenderClear()
self.spriteJ2 = pygame.sprite.RenderClear()
# self.run = False
pygame.init()
def Connected(self, channel, addr):
self.clients.add(channel)
if len(self.clients) == 1:
clientTmp = self.clients.__getitem__(outils.J1)
clientTmp.bar.set_position(outils.POS_J1)
clientTmp.joueur = outils.J1
self.spriteJ1.add(clientTmp.bar)
elif len(self.clients) == 2: # Joueur 2
clientTmp = self.clients.__getitem__(outils.J2)
clientTmp.bar.set_position(outils.POS_J2)
clientTmp.joueur = outils.J2
self.spriteJ1.add(clientTmp.bar)
self.send_info("info", "C'est parti !")
else: # Partie pleine
channel.Send({"action": "error", "error": "La partie est pleine"})
# self.run = True
def get_shot(self):
liste = []
for client in self.clients:
for tir in client.tir_sprites:
liste.append(tir.rect.center)
return liste
def update_bar(self):
for client in self.clients:
client.update_bar()
def send_shot(self):
self.send_info("shot", self.clients.__getitem__(outils.J1).get_shot())
self.send_info("shot", self.clients.__getitem__(outils.J2).get_shot())
def remove_client(self, joueur):
if joueur == outils.KILL_J1:
self.clients.__getitem__(outils.J1).Send({"action":"info","message":"perdu"} )
self.clients.__getitem__(outils.J2).Send({"action":"info","message":"gagne"} )
self.clients.__getitem__(outils.J1).get_bar().kill()
self.endGame = outils.J1
if joueur == outils.KILL_J2:
self.clients.__getitem__(outils.J1).Send({"action":"info","message":"gagne"} )
self.clients.__getitem__(outils.J2).Send({"action":"info","message":"perdu"} )
self.clients.__getitem__(outils.J2).get_bar().kill()
self.endGame = outils.J2
def update_balle(self):
isBriqueHit = self.briques.gestion(self.balle)
if isBriqueHit:
self.send_info("sound", "break.wav")
if self.brique.__sizeof__() == 0 :
self.balle.speed += 1
if not(isBriqueHit):
isJoueurKill = self.balle.update(self.clients.__getitem__(outils.J1).get_bar(), self.clients.__getitem__(outils.J2).get_bar())
if isJoueurKill == outils.KILL_J1:
self.remove_client(outils.KILL_J1)
elif isJoueurKill == outils.KILL_J2:
self.remove_client(outils.KILL_J2)
elif isJoueurKill == outils.PLAY_SOUND_VAR:
self.send_info("sound", "bound.wav")
def get_positions_bars(self):
"""
Fonction qui retourne une liste de la position des joueurs.
"""
liste = []
for client in self.clients:
liste.append(client.bar.rect.center)
return liste
def get_positions_briques(self):
liste = []
for brique in self.briques:
liste.append(brique.rect.center)
return liste
def send_briques(self):
for client in self.clients:
print "Briques \n\t"
print self.get_positions_briques()
client.Send({"action" : "briques", "liste":self.get_positions_briques()})
def send_bar(self):
for client in self.clients:
print "Bar \n\t"
print self.get_positions_bars()
client.Send({"action": "bar", "liste": self.get_positions_bars()})
def send_balle(self):
for client in self.clients:
print "Balle : \n\t"
print self.balle.rect.center
client.Send({"action": "balle", "center": self.balle.rect.center})
print "Liste Tirs : \n\t"
print self.get_shot()
client.Send({"action": "shot", "liste":self.get_shot()})
def send_info(self, action, message):
"""
Cette méthode permet d'envoyer un message à tous les
joueurs, en précisant le type et le contenu du message
:param action: Type du message
:param message: Généralement une String, pour informer de qqch
:return:
"""
for client in self.clients:
print action + " - " +message
client.Send({"action": action, "message": message})
def get_all_clients(self):
liste = []
for client in self.clients:
liste.append(client.bar)
return liste
def collide_ball(self, balle, bar):
if balle.rect.colliderect(bar.rect) or balle.rect.colliderect(bar.rect):
self.send_info("sound", "bound.wav")
balle.deplacement()
def del_client(self, channel):
self.clients.remove(channel)
def check_collision_tir_player(self):
for tir in self.clients.__getitem__(outils.J1).tir_sprites:
if pygame.sprite.collide_rect(tir, self.clients.__getitem__(outils.J2).get_bar()):
# Collision joueur - tir 1
self.remove_client(outils.KILL_J1)
for tir in self.clients.__getitem__(outils.J2).tir_sprites:
if pygame.sprite.collide_rect(tir, self.clients.__getitem__(outils.J1).get_bar()):
# Collision joueur - tir 1
self.remove_client(outils.KILL_J2)
def launch_game(self):
screen = self.screen
background_image, background_rect = outils.Fonction.load_png('images/background.jpg')
background_load, background_load_rect = outils.Fonction.load_png("images/loading_mini.gif")
clock = pygame.time.Clock()
# Petit Timer pour eviter un début du jeu trop brutal
gameStart = False
while True:
self.Pump()
if self.endGame != 0:
if self.endGame == outils.J1:
self.clients.remove(self.clients.__getitem__(outils.J1))
if self.endGame == outils.J2:
self.clients.remove(self.clients.__getitem__(outils.J2))
clock.tick(60)
# Pour permettre de quitter le serveur
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit(0)
if len(self.clients) == 2:
if not gameStart:
gameStart = True
self.send_info("info", "debut du jeu dans 3 secondes !")
time.sleep(3)
self.update_bar()
pygame.sprite.groupcollide(self.briques, self.clients.__getitem__(outils.J1).tir_sprites, False, True, pygame.sprite.collide_circle_ratio(0.7))
pygame.sprite.groupcollide(self.briques, self.clients.__getitem__(outils.J2).tir_sprites, False, True, pygame.sprite.collide_circle_ratio(0.7))
self.update_balle()
self.check_collision_tir_player()
self.send_bar()
self.send_balle()
self.send_briques()
print "-----------------------------------------"
screen.blit(background_image, background_rect)
else:
screen.blit(background_load, background_load_rect)
# On dessine
pygame.display.flip()
def main_prog():
if len(sys.argv) == 3:
port = sys.argv[2]
ip = sys.argv[1]
else:
port = outils.PORT
ip = outils.IP
print "IP : " + ip + "\tPort : " + str(port)
my_server = MyServer(localaddr=(ip, int(port)))
my_server.launch_game()
if __name__ == '__main__':
main_prog()
sys.exit(0)