def main(chat_box, screen, window, menu, player_name, host, port): global PLAYER_LIST msg_queue = [] menu.destroy() server = socket(AF_INET, SOCK_STREAM) udp_server = socket(AF_INET, SOCK_DGRAM) try: server.connect((host, port)) except error as msg: print("Erreure de connexion au serveur", msg) sys.exit(1) ''' envoie des info joueur par le serveur ''' res = json.loads(server.recv(const.RECV_BUFF).decode()) player_id = res[0]["id"] player_color = res[0]["color"] data = b'ack;\r\n' server.sendall(data) print("nouveau jouer chargé depuis le serveur avec l'id:", player_id) def command(txt): print(txt) msg = 'sentMessage;' + player_name + ';' + txt + ';\r\n' server.sendall(msg.encode()) running = True clock = pygame.time.Clock() pong = Pong( const.SCREENSIZE, res[1]["id"], res[1]["x"], res[1]["y"], res[1]["lscore"], res[1]["rscore"], ) player_paddle1 = PlayerPaddle(const.SCREENSIZE, player_id, player_color) PLAYER_LIST.append(player_paddle1) print("joueur:", player_id, "créé et ajouté") pygame.display.set_caption('Multi Pong') win = pygame.mixer.Sound(os.path.join('data/win.wav')) lose = pygame.mixer.Sound(os.path.join('data/lose.wav')) start_new_thread(handle_serveur, (msg_queue, server, pong, player_name)) chat_box.set_nick("SERVER") chat_box.set_command(command) chat_box.send("Bienvenu " + player_name + "!") chat_box.send("En attente de connexion...") chat_box.set_nick(player_name) while running: if len(PLAYER_LIST) <= 0: continue else: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() if event.type == KEYDOWN: if event.key == K_UP: player_paddle1.direction = -1 elif event.key == K_DOWN: player_paddle1.direction = 1 if event.type == KEYUP: if event.key == K_UP and player_paddle1.direction == -1: player_paddle1.direction = 0 elif event.key == K_DOWN and player_paddle1.direction == 1: player_paddle1.direction = 0 player_paddle1.update(udp_server) screen.fill(const.BLACK) # pygame.draw.line(screen, const.WHITE, (const.SCREEN_WIDTH / 2, 0), (const.SCREEN_WIDTH / 2, const.SCREEN_LENGTH), 5) # Deterimine win if pong.lwin == True: running = False elif pong.rwin == True: running = False for player in PLAYER_LIST: player.render(screen) pong.render(screen) default_font = pygame.font.get_default_font() font = pygame.font.Font(default_font, 30) left_score = font.render("West Side " + str(pong.lscore), True, const.WHITE) screen.blit(left_score, (20, 0)) right_score = font.render("East Side " + str(pong.rscore), True, const.WHITE) screen.blit(right_score, ((const.SCREEN_WIDTH / 2) + 20, 0)) user_font = pygame.font.Font(default_font, 20) user_msg = user_font.render(player_name, True, player_color) screen.blit( user_msg, ((const.SCREEN_WIDTH / 2) + 5, const.SCREEN_LENGTH - 20)) for msg in msg_queue: chat_box.user_message(msg[0], msg[1]) msg_queue.remove(msg) chat_box.interior.pack(expand=True, fill=BOTH) window.update() clock.tick(const.FPS) pygame.display.flip() if pong.lwin == True and player_paddle1.side == 1: txt = font.render(" Victoire!!!!", True, const.WHITE) screen.blit(txt, (100, 200)) win.play() elif pong.rwin == True and player_paddle1.side == 0: txt = font.render(" Victoire!!!!", True, const.WHITE) screen.blit(txt, (100, 200)) win.play() print("left won") else: txt2 = font.render("Défaite!", True, const.WHITE) screen.blit(txt2, (100, 200)) lose.play() pygame.display.flip() server.close() pygame.time.delay(5000) pygame.quit()
def main(chat_box, screen, window, menu, player_name, host, port): global PLAYER_LIST msg_queue = [] menu.destroy() server = socket(AF_INET, SOCK_STREAM) udp_server = socket(AF_INET, SOCK_DGRAM) try: server.connect((host, port)) except error as msg: print("Could not connect to server", msg) sys.exit(1) ''' Grabs a player from server instead of creating individually Upon initial connection the server creates a player with an id and sends this to the server. it's the first response and lets the new client know their client id ''' res = json.loads(server.recv(const.RECV_BUFF).decode()) player_id = res[0]["id"] player_color = res[0]["color"] data = b'ack;\r\n' server.sendall(data) print("Loaded new player from server with id:", player_id) def command(txt): print(txt) msg = 'sentMessage;' + player_name + ';' + txt + ';\r\n' server.sendall(msg.encode()) running = True clock = pygame.time.Clock() pong = Pong( const.SCREENSIZE, res[1]["id"], res[1]["x"], res[1]["y"], res[1]["lscore"], res[1]["rscore"], ) player_paddle1 = PlayerPaddle(const.SCREENSIZE, player_id, player_color) PLAYER_LIST.append(player_paddle1) print("player:", player_id, "created and added") pygame.display.set_caption('Multi Pong') win = pygame.mixer.Sound(os.path.join('data/win.wav')) lose = pygame.mixer.Sound(os.path.join('data/lose.wav')) start_new_thread(handle_server, (msg_queue, server, pong, player_name)) chat_box.set_nick("SERVER") chat_box.set_command(command) chat_box.send("Welcome to Multi Pong " + player_name + "!") chat_box.send("Waiting for players to connect...") chat_box.set_nick(player_name) while running: if len(PLAYER_LIST) <= 0: continue else: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() if event.type == KEYDOWN: if event.key == K_UP: player_paddle1.direction = -1 elif event.key == K_DOWN: player_paddle1.direction = 1 if event.type == KEYUP: if event.key == K_UP and player_paddle1.direction == -1: player_paddle1.direction = 0 elif event.key == K_DOWN and player_paddle1.direction == 1: player_paddle1.direction = 0 player_paddle1.update(udp_server) screen.fill(const.BLACK) #pygame.draw.line(screen, const.WHITE, (const.SCREEN_WIDTH / 2, 0), (const.SCREEN_WIDTH / 2, const.SCREEN_LENGTH), 5) #Deterimine win if pong.lwin== True: running = False elif pong.rwin == True: running = False for player in PLAYER_LIST: player.render(screen) pong.render(screen) default_font = pygame.font.get_default_font() font = pygame.font.Font(default_font, 30) left_score = font.render("West Side " + str(pong.lscore), True, const.WHITE) screen.blit(left_score, (20, 0)) right_score = font.render("East Side " + str(pong.rscore), True, const.WHITE) screen.blit(right_score, ((const.SCREEN_WIDTH/2)+20, 0)) user_font = pygame.font.Font(default_font, 20) user_msg = user_font.render(player_name, True, player_color) screen.blit(user_msg, ((const.SCREEN_WIDTH/2)+5, const.SCREEN_LENGTH-20)) for msg in msg_queue: chat_box.user_message(msg[0], msg[1]) msg_queue.remove(msg) chat_box.interior.pack(expand=True, fill=BOTH) window.update() clock.tick(const.FPS) pygame.display.flip() if pong.lwin == True and player_paddle1.side == 1: txt = font.render(" You Won!!!!", True, const.WHITE) screen.blit(txt, (100, 200)) win.play() elif pong.rwin == True and player_paddle1.side == 0: txt = font.render(" You Won!!!!", True, const.WHITE) screen.blit(txt, (100, 200)) win.play() print("left won") else: txt2 = font.render("Sorry, You Lost!", True, const.WHITE) screen.blit(txt2, (100, 200)) lose.play() pygame.display.flip() server.close() pygame.time.delay(5000) pygame.quit()