/
ap1k
/
PyGame_Practice
Обзор
Документация
Войти
/
ap1k
/
PyGame_Practice
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/core/game.py
135 строк
5 KB
Roman
The all game
27 май 2025, 15:26
27 май 2025, 15:26
4ea7de9
Код
Авторство
О чём код?
""" Game class - the main controller for the game Implements the Singleton pattern to ensure only one game instance exists """ import pygame import time from src.core.game_state import GameState from src.scenes.loading_scene import LoadingScene from src.scenes.game_scene import GameScene from src.scenes.leaderboard_scene import LeaderboardScene from src.utils.config import Config from src.utils.resource_loader import ResourceLoader class Game: """ Main game controller class implementing Singleton pattern """ _instance = None def __new__(cls): if cls._instance is None: cls._instance = super(Game, cls).__new__(cls) cls._instance._initialized = False return cls._instance def __init__(self): if self._initialized: return # Initialize game properties self.config = Config() self.width = self.config.screen_width self.height = self.config.screen_height self.screen = pygame.display.set_mode((self.width, self.height)) pygame.display.set_caption("Space Shooter") # Game clock and timing self.clock = pygame.time.Clock() self.fps = self.config.fps self.running = True self.start_time = 0 self.elapsed_time = 0 # Game state self.state = GameState() # Initialize scenes self.loading_scene = LoadingScene(self) self.game_scene = None # Will be initialized after loading self.leaderboard_scene = None # Will be initialized after loading # Current active scene self.current_scene = self.loading_scene self._initialized = True def change_scene(self, scene_name): """Change the current active scene""" if scene_name == "game": if not self.game_scene: self.game_scene = GameScene(self) self.current_scene = self.game_scene self.start_time = time.time() elif scene_name == "leaderboard": if not self.leaderboard_scene: self.leaderboard_scene = LeaderboardScene(self) self.current_scene = self.leaderboard_scene elif scene_name == "loading": self.current_scene = self.loading_scene def run(self): """Main game loop""" while self.running: # Handle events for event in pygame.event.get(): if event.type == pygame.QUIT: self.running = False # Handle ESC key for pause in any scene elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: if self.current_scene == self.game_scene: self.state.paused = not self.state.paused else: self.current_scene.handle_event(event) # Update elapsed time if in game scene and not paused if self.current_scene == self.game_scene and self.start_time > 0 and not self.state.paused: self.elapsed_time = time.time() - self.start_time # Only update if not paused if not self.state.paused or self.current_scene != self.game_scene: self.current_scene.update() # Always render the current scene self.current_scene.render(self.screen) # Draw pause overlay if paused if self.state.paused and self.current_scene == self.game_scene: self._render_pause_overlay() # Update the display pygame.display.flip() # Control the frame rate self.clock.tick(self.fps) def _render_pause_overlay(self): """Render the pause overlay""" # Create a semi-transparent overlay overlay = pygame.Surface((self.width, self.height), pygame.SRCALPHA) overlay.fill((0, 0, 0, 128)) # Semi-transparent black self.screen.blit(overlay, (0, 0)) # Draw the pause text font_large = ResourceLoader().get_font("large") font_medium = ResourceLoader().get_font("medium") # Draw "PAUSED" text pause_text = font_large.render("PAUSED", True, self.config.colors["white"]) text_rect = pause_text.get_rect(center=(self.width // 2, self.height // 2 - 50)) self.screen.blit(pause_text, text_rect) # Draw instructions instructions = [ "Press ESC to resume", "WASD/Arrows: Move", "Space: Shoot", "Q/E: Switch weapons" ] y_offset = 20 for instruction in instructions: inst_text = font_medium.render(instruction, True, self.config.colors["light_blue"]) inst_rect = inst_text.get_rect(center=(self.width // 2, self.height // 2 + y_offset)) self.screen.blit(inst_text, inst_rect) y_offset += 40