def __initializeTopLeftPanel(self, topLeftPanel): textLabel = tk.Label(topLeftPanel, text='Texte', font=self.__labelsFont) textLabel.pack(side=tk.TOP, fill=tk.X) self.textPanel = TextPanel(topLeftPanel) self.textPanel.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=1)
def show_stats(self, w, h, win): text1 = self.left_player_name + ': ' + str(self.left_player_points) text2 = self.right_player_name + ': ' + str(self.right_player_points) if self.last_win_player == 1: text1 += '!' else: text2 += '!' left_player_score = TextPanel(w // 4, 20, "comicsans", 32, text1, (0, 0, 0)) right_player_score = TextPanel(w // 2 + w // 4, 20, "comicsans", 32, text2, (0, 0, 0)) left_player_score.draw(win) right_player_score.draw(win)
def confirmation_screen(message): run = True clock = pygame.time.Clock() text = TextPanel(w/2, h/2 - 50, "comicsans", 40, message, (0, 0, 0)) yes = TextButton(w/2 - 60, h/2, "comicsans", 40, "Yes", (0, 0, 0)) no = TextButton(w/2 + 50, h/2, "comicsans", 40, "No", (0, 0, 0)) while run: clock.tick(60) yes_hover = yes.cursor_hover() no_hover = no.cursor_hover() if yes_hover: yes.update("Yes", (200, 0, 0)) else: yes.update("Yes", (0, 0, 0)) if no_hover: no.update("No", (0, 0, 200)) else: no.update("No", (0, 0, 0)) text.draw(win) yes.draw(win) no.draw(win) pygame.display.update() for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() run = False if event.type == pygame.MOUSEBUTTONDOWN: if yes_hover or no_hover: run = False if yes_hover: return True else: return False
def game_over_screen(game): if game.left_player_points > game.right_player_points: text = TextPanel(w / 2, h / 3, "comicsans", 40, game.left_player_name + " has won the game", (0, 0, 0)) else: text = TextPanel(w / 2, h / 3, "comicsans", 40, game.right_player_name + " has won the game", (0, 0, 0)) text.draw(win) pygame.display.update() pygame.time.delay(3000)
def waiting_screen(): text = TextPanel(w // 2, h // 3.4, "comicsans", 34, "Waiting for opponent...", (0, 0, 0)) text.draw(win) pygame.display.update() pygame.time.delay(50)
class MainPanel(FluidPanel): __labelsFont = ("Helvetica", 12) def __init__(self, parent, **options): super(FluidPanel, self).__init__(parent, options) mainPane = tk.PanedWindow(self, orient=tk.HORIZONTAL, relief=tk.RIDGE) leftPane = tk.PanedWindow(mainPane, orient=tk.VERTICAL, relief=tk.SUNKEN) topLeftPanel = tk.Frame(leftPane) self.__initializeTopLeftPanel(topLeftPanel) leftPane.add(topLeftPanel, stretch="always") bottomLeftPanel = tk.Frame(leftPane) self.__initializeBottomLeftPanel(bottomLeftPanel) leftPane.add(bottomLeftPanel, stretch="always") mainPane.add(leftPane, stretch="always") rightPane = tk.PanedWindow(mainPane, orient=tk.VERTICAL, relief=tk.SUNKEN) topRightPanel = tk.Frame(rightPane) self.__initializeTopRightPanel(topRightPanel) rightPane.add(topRightPanel, stretch="always") bottomRightPanel = tk.Frame(rightPane) self.__initializeBottomRightPanel(bottomRightPanel) rightPane.add(bottomRightPanel, stretch="always") mainPane.add(rightPane, stretch="always") mainPane.pack(fill=tk.BOTH, expand=1) def __initializeTopLeftPanel(self, topLeftPanel): textLabel = tk.Label(topLeftPanel, text='Texte', font=self.__labelsFont) textLabel.pack(side=tk.TOP, fill=tk.X) self.textPanel = TextPanel(topLeftPanel) self.textPanel.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=1) def __initializeBottomLeftPanel(self, bottomLeftPanel): videoLabel = tk.Label(bottomLeftPanel, text='Vidéo', font=self.__labelsFont) videoLabel.pack(side=tk.TOP, fill=tk.X) self.videoPanel = VideoPanel(bottomLeftPanel) self.videoPanel.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=1) def __initializeTopRightPanel(self, topRightPanel): speechLabel = tk.Label(topRightPanel, text='Voix', font=self.__labelsFont) speechLabel.pack(side=tk.TOP, fill=tk.X) self.speechPanel = SpeechPanel(topRightPanel) self.speechPanel.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=1) def __initializeBottomRightPanel(self, bottomRightPanel): musicLabel = tk.Label(bottomRightPanel, text='Musique', font=self.__labelsFont) musicLabel.pack(side=tk.TOP, fill=tk.X) self.musicPanel = MusicPanel(bottomRightPanel) self.musicPanel.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=1)
def LAN_game(server_address): n = Network(server_address) try: player1, player2, ball, game, guest_connected = n.getP() except: win.blit(bg, (0, 0)) if confirmation_screen("Cannot connect. Do you want try again?"): LAN_game(server_address) else: menu_screen() run = True while run: clock.tick(75) try: # n.send([player1, flag_pause, flat_resume]) player2, ball, game, guest_connected = n.send([player1, False, False]) except: win.blit(bg, (0,0)) text1 = TextPanel(w // 2, h // 4.5, "comicsans", 30, "Lost connection with server " + str(get_server_address()), (0, 0, 0)) text2 = TextPanel(w // 2, h // 3.4, "comicsans", 34, "Back to menu...", (0, 0, 0)) text1.draw(win) text2.draw(win) pygame.display.update() pygame.time.delay(2000) break redraw_game_window(win, bg, player1, player2, ball, game) if game.pause or guest_connected == False: text1 = TextPanel(w//2, h//4.5, "comicsans", 30, "Server " + str(get_server_address()), (0, 0, 0)) if guest_connected == False: text2 = TextPanel(w // 2, h//3.4, "comicsans", 34, "Waiting for opponent...", (0, 0, 0)) else: text2 = TextPanel(w // 2, h // 3.4, "comicsans", 34, "Game paused...", (0, 0, 0)) text1.draw(win) text2.draw(win) pygame.display.update() pygame.time.delay(50) else: player1.move() if game.is_game_over(): n.disconnect() run = False game_over_screen(game) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False pygame.quit() keys = pygame.key.get_pressed() if keys[pygame.K_ESCAPE]: n.send([player1, True, False]) if confirmation_screen("Are you sure you want to quit?"): n.disconnect() run = False else: n.send([player1, False, True]) menu_screen()
def online_game(server_address): n = Network(server_address) global buffer_player1, buffer_player1_recv, buffer_player2, buffer_ball, game, ball, opponent_connected, pause_game try: player1, player2, ball, game, player = n.getP() player1_recv = copy.copy(player1) except: win.blit(bg, (0, 0)) if confirmation_screen("Cannot connect. Do you want try again?"): online_game(server_address) else: menu_screen() print("connected") start_new_thread(thread_updating_data, (n,0)) run = True while run: clock.tick(75) keys = pygame.key.get_pressed() if opponent_connected == False: game = Game() ball = Ball(200, 250, 0, 0, 20) text = TextPanel(w // 2, h // 3.4, "comicsans", 34, "Waiting for opponent...", (0, 0, 0)) text.draw(win) pygame.display.update() pygame.time.delay(50) elif pause_game: text = TextPanel(w // 2, h // 3.4, "comicsans", 34, "Game paused...", (0, 0, 0)) text.draw(win) pygame.display.update() pygame.time.delay(50) else: player1.move() buffer_player1.buf.append([player1.x, player1.y]) if len(buffer_player1_recv.buf) > 0: player1_recv.x, player1_recv.y = buffer_player1_recv.buf[0][0], buffer_player1_recv.buf[0][1] buffer_player1_recv.buf = buffer_player1_recv.buf[1:] if len(buffer_player2.buf) > 0: player2.x, player2.y = buffer_player2.buf[0][0], buffer_player2.buf[0][1] buffer_player2.buf = buffer_player2.buf[1:] if len(buffer_ball.buf) > 0: ball.x, ball.y = buffer_ball.buf[0][0], buffer_ball.buf[0][1] buffer_ball.buf = buffer_ball.buf[1:] redraw_game_window(win, bg, player1_recv, player2, ball, game) if game.is_game_over(): n.send([Buffer(), False, False]) n.disconnect() run = False game_over_screen(game) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False pygame.quit() if keys[pygame.K_ESCAPE]: n.send([Buffer(), True, False]) if confirmation_screen("Are you sure you want to quit?"): n.disconnect() run = False else: n.send([Buffer(), False, True]) menu_screen()