elif difficulty == 1: colors = ('red', 'yellow', 'green', 'blue', 'purple') elif difficulty == 2: colors = ('red', 'orange', 'yellow', 'green', 'blue', 'light_blue', 'purple') else: colors = ('red', 'yellow', 'green', 'blue', 'purple') medium_font = pygame.font.SysFont('Comic Sans MS', 20) player_name_text = medium_font.render('Имя: ' + player_name, False, (0, 0, 0)) big_font = pygame.font.SysFont('Comic Sans MS', 50) win = False win_text = big_font.render('Победа!', False, (0, 0, 0)) game_over_text = big_font.render("Игра окончена :(", False, (0, 0, 0)) close_button = pyg_button.PygButton((38*17+30, 38*16, 60, 30), 'Выход') if _game_field is None: _game_field = game_field.GameField((17, 17), 7, colors) if curr_ball is None: curr_ball = ball.Ball(random.choice(colors)) if next_ball is None: next_ball = ball.Ball(random.choice(colors)) # is_ball_flying = False game_over = False statistics_checked = False # Проверка статистики сразу после завершения игры if misses_remaining == 0: misses_remaining = 5 start_ticks = pygame.time.get_ticks() run = True while run:
class StatisticsMenuScene: """Статистика игроков""" def update_statistics(self): self.best_statistics = statistics.get_first_statistics(10) self.labels_username = [] self.labels_max_score = [] self.labels_win_count = [] self.labels_min_win_time = [] self.medium_font = pygame.font.SysFont('Comic Sans MS', 22) for line in self.best_statistics: self.labels_username.append( self.medium_font.render(line[0], False, (0, 0, 0))) self.labels_max_score.append( self.medium_font.render(str(line[1]), False, (0, 0, 0))) self.labels_win_count.append( self.medium_font.render(str(line[2]), False, (0, 0, 0))) self.labels_min_win_time.append( self.medium_font.render( str(line[3] // 60) + ':' + str(line[3] % 60), False, (0, 0, 0))) # Кнопки close_button = pyg_button.PygButton((377, 519, 100, 30), 'Выход') button_max_score = pyg_button.PygButton((242 + 130, 60, 80, 40), 'Макс. счет') button_win_count = pyg_button.PygButton((242 + 130 + 80, 60, 80, 40), 'Победы') button_min_win_time = pyg_button.PygButton( (242 + 130 + 80 * 2, 60, 90, 40), 'Мин. время') # Статистика statistics.sort('max_score', True) best_statistics = statistics.get_first_statistics(10) # Текст medium_font = pygame.font.SysFont('Comic Sans MS', 22) label_username = medium_font.render('Имя', False, (0, 0, 0)) labels_username = [] labels_max_score = [] labels_win_count = [] labels_min_win_time = [] for line in best_statistics: labels_username.append(medium_font.render(line[0], False, (0, 0, 0))) labels_max_score.append( medium_font.render(str(line[1]), False, (0, 0, 0))) labels_win_count.append( medium_font.render(str(line[2]), False, (0, 0, 0))) labels_min_win_time.append( medium_font.render( str(line[3] // 60) + ':' + str(line[3] % 60), False, (0, 0, 0))) def handle_event(self, event): global scene if event == pygame.QUIT: scene = scenes['LoginMenu'] if 'click' in self.close_button.handleEvent(event): # Выход из игры scene = scenes['LoginMenu'] if 'click' in self.button_max_score.handleEvent(event): statistics.sort('max_score', True) self.update_statistics() if 'click' in self.button_win_count.handleEvent(event): statistics.sort('win_count', True) self.update_statistics() if 'click' in self.button_min_win_time.handleEvent(event): statistics.sort('min_win_time', False) self.update_statistics() def update(self): pass def draw(self, surface): surface.fill(BG_COLOR) # Фон surface.blit(self.label_username, (242, 60)) for i in range(len(self.labels_username)): surface.blit(self.labels_username[i], (242, 100 + 40 * i)) surface.blit(self.labels_max_score[i], (242 + 130, 100 + 40 * i)) surface.blit(self.labels_win_count[i], (242 + 130 + 80, 100 + 40 * i)) surface.blit(self.labels_min_win_time[i], (242 + 130 + 80 * 2, 100 + 40 * i)) self.close_button.draw(surface) self.button_max_score.draw(surface) self.button_win_count.draw(surface) self.button_min_win_time.draw(surface) pygame.display.update()
class AdminMenu: """Меню администратора""" # Кнопки delete_player_button = pyg_button.PygButton((377, 469, 100, 30), 'Удалить') close_button = pyg_button.PygButton((377, 519, 100, 30), 'Выход') # Ввод имени name_input = text_input.TextInput((367, 319), 180, 25) medium_font = pygame.font.SysFont('Comic Sans MS', 14) name_text = medium_font.render('Имя:', False, (0, 0, 0)) delete_error = False delete_error_text = medium_font.render('Игрока не существует', False, (255, 0, 0)) delete_success = False delete_success_text = medium_font.render('Игрок удален', False, (0, 255, 0)) def handle_event(self, event): global scene if event == pygame.QUIT: scene = scenes['LoginMenu'] self.clear() if 'click' in self.close_button.handleEvent(event): # Выход из игры self.clear() scene = scenes['LoginMenu'] if 'click' in self.delete_player_button.handleEvent( event): # Удаление игрока if len(self.name_input.get_text()) > 0: if authorization.delete_player(self.name_input.get_text()): self.delete_error = False self.delete_success = True else: self.delete_error = True self.delete_success = False self.name_input.handle_event(event) def update(self): self.name_input.update() if len(self.name_input.input_string) > 20: self.name_input.input_string = self.name_input.input_string[:21] def draw(self, surface): surface.fill(BG_COLOR) # Фон surface.blit(logo, (277, 19)) # Логотип surface.blit(self.name_text, (307, 319)) # Ввод имени self.name_input.draw(surface) if self.delete_error: surface.blit(self.delete_error_text, (307, 394)) elif self.delete_success: surface.blit(self.delete_success_text, (307, 394)) self.delete_player_button.draw(surface) self.close_button.draw(surface) pygame.display.update() def clear(self): self.name_input.clear_text() self.delete_error = False self.delete_success = False
class MainMenuScene: """Главное меню""" # Кнопки new_game_button = pyg_button.PygButton((377, 369, 100, 30), 'Новая игра') load_game_button = pyg_button.PygButton((377, 419, 100, 30), 'Продолжить') statistics_button = pyg_button.PygButton((377, 469, 100, 30), 'Статистика') close_button = pyg_button.PygButton((377, 519, 100, 30), 'Выход') is_new_game_pressed = False difficulty_easy_button = pyg_button.PygButton((317, 369, 60, 30), 'Легко') difficulty_medium_button = pyg_button.PygButton((397, 369, 60, 30), 'Средне') difficulty_hard_button = pyg_button.PygButton((477, 369, 60, 30), 'Сложно') def handle_event(self, event): global run global scene if event == pygame.QUIT: scene = scenes['LoginMenu'] if 'click' in self.close_button.handleEvent( event): # Выход в меню входа в игру scene = scenes['LoginMenu'] if not self.is_new_game_pressed: if 'click' in self.new_game_button.handleEvent( event): # Начало новой игры self.is_new_game_pressed = True else: if 'click' in self.difficulty_easy_button.handleEvent(event): settings.update_setting('difficulty', '0') player_name = settings.get_settings().get('player_name') savegame.save(player_name ) # Создание пустого файла сохранения для игрока scene = scenes['GameProcess'] if 'click' in self.difficulty_medium_button.handleEvent(event): settings.update_setting('difficulty', '1') player_name = settings.get_settings().get('player_name') savegame.save(player_name ) # Создание пустого файла сохранения для игрока scene = scenes['GameProcess'] if 'click' in self.difficulty_hard_button.handleEvent(event): settings.update_setting('difficulty', '2') player_name = settings.get_settings().get('player_name') savegame.save(player_name ) # Создание пустого файла сохранения для игрока scene = scenes['GameProcess'] if 'click' in self.load_game_button.handleEvent( event): # Загрузка игры scene = scenes['GameProcess'] def update(self): pass def draw(self, surface): surface.fill(BG_COLOR) # Фон surface.blit(logo, (277, 19)) # Логотип if not self.is_new_game_pressed: self.new_game_button.draw(surface) else: self.difficulty_easy_button.draw(surface) self.difficulty_medium_button.draw(surface) self.difficulty_hard_button.draw(surface) self.load_game_button.draw(surface) self.statistics_button.draw(surface) self.close_button.draw(surface) pygame.display.update()
class LoginMenuScene: """Меню входа в игру""" # Кнопки login_button = pyg_button.PygButton((307, 419, 100, 30), 'Войти') signup_button = pyg_button.PygButton((447, 419, 100, 30), 'Регистрация') statistics_button = pyg_button.PygButton((377, 469, 100, 30), 'Статистика') close_button = pyg_button.PygButton((377, 519, 100, 30), 'Выход') # Ввод имени name_input = text_input.TextInput((367, 319), 180, 25) # Ввод пароля password_input = text_input.TextInput((367, 369), 180, 25) medium_font = pygame.font.SysFont('Comic Sans MS', 14) name_text = medium_font.render('Имя:', False, (0, 0, 0)) password_text = medium_font.render('Пароль:', False, (0, 0, 0)) auth_error = False auth_error_text = medium_font.render('Неверное имя или пароль', False, (255, 0, 0)) signup_error = False signup_error_text = medium_font.render('Пользователь уже существует', False, (255, 0, 0)) signup_success = False signup_success_text = medium_font.render('Вы зарегистрированы', False, (0, 255, 0)) def handle_event(self, event): global run global scene if event == pygame.QUIT: run = False if 'click' in self.close_button.handleEvent(event): # Выход из игры pygame.quit() if 'click' in self.login_button.handleEvent(event): # Авторизация if len(self.name_input.get_text()) > 0 and len( self.password_input.get_text()) > 0: if authorization.auth(self.name_input.get_text(), self.password_input.get_text()): self.auth_error = False self.signup_error = False self.signup_success = False player_name = self.name_input.get_text() if player_name == 'admin': # Авторизация администратора self.clear() scene = scenes['AdminMenu'] else: self.clear() settings.update_setting('player_name', player_name) scene = scenes['MainMenu'] else: self.auth_error = True self.signup_error = False self.signup_success = False if 'click' in self.signup_button.handleEvent( event): # Регистраиця нового игрока if len(self.name_input.get_text()) > 0 and len( self.password_input.get_text()) > 0: if authorization.add_player(self.name_input.get_text(), self.password_input.get_text()): self.auth_error = False self.signup_error = False self.signup_success = True else: # Если игрок уже существует self.auth_error = False self.signup_error = True self.signup_success = False if 'click' in self.statistics_button.handleEvent( event): # Открытие статистики self.clear() scene = scenes['StatisticsMenu'] if event.type == pygame.MOUSEBUTTONDOWN: pos = pygame.mouse.get_pos() self.name_input.handle_event(event) self.password_input.handle_event(event) def update(self): self.name_input.update() self.password_input.update() if len(self.name_input.input_string) > 20: self.name_input.input_string = self.name_input.input_string[:21] if len(self.password_input.input_string) > 20: self.password_input.input_string = self.password_input.input_string[: 21] def draw(self, surface): surface.fill(BG_COLOR) # Фон surface.blit(logo, (277, 19)) # Логотип surface.blit(self.name_text, (307, 319)) # Ввод имени self.name_input.draw(surface) surface.blit(self.password_text, (307, 369)) # Ввод пароля self.password_input.draw(surface) if self.auth_error: # Ошибка входа surface.blit(self.auth_error_text, (307, 394)) elif self.signup_error: # Ошибка регистрации surface.blit(self.signup_error_text, (307, 394)) elif self.signup_success: # Регистрация успешна surface.blit(self.signup_success_text, (307, 394)) self.login_button.draw(surface) self.signup_button.draw(surface) self.statistics_button.draw(surface) self.close_button.draw(surface) pygame.display.update() def clear(self): self.name_input.clear_text() self.password_input.clear_text() self.auth_error = False self.signup_error = False self.signup_success = False