diff --git a/image/common.jpeg b/image/common.jpeg new file mode 100644 index 0000000000000..e3a8e6f3fe65d Binary files /dev/null and b/image/common.jpeg differ diff --git a/main.py b/main.py index bb4491856b3db..051d31f1ee6f1 100644 --- a/main.py +++ b/main.py @@ -1,76 +1,36 @@ -# main_game.py import pygame import sys +import subprocess +import os from pygame.locals import * -def G_snake(): - pygame.init() - - screen = pygame.display.set_mode((400, 300)) - pygame.display.set_caption("G_snake") - - font = pygame.font.Font(None, 36) - clock = pygame.time.Clock() - while True: - screen.fill((255, 255, 255)) - text = font.render("G_snake", True, (0, 0, 0)) - screen.blit(text, (150, 150)) - pygame.display.flip() - - for event in pygame.event.get(): - if event.type == QUIT: - pygame.quit() - sys.exit() +def G_snake(): + game_path = os.path.abspath("G_snake.py") + subprocess.run([sys.executable, game_path]) + print("Selected Game: G_snake") - clock.tick(30) def G_avoid(): - pygame.init() - - screen = pygame.display.set_mode((400, 300)) - pygame.display.set_caption("G_avoid") - - font = pygame.font.Font(None, 36) - clock = pygame.time.Clock() - - while True: - screen.fill((255, 255, 255)) - text = font.render("G_avoid", True, (0, 0, 0)) - screen.blit(text, (150, 150)) - pygame.display.flip() + game_path = os.path.abspath("G_avoid.py") + subprocess.run([sys.executable, game_path]) + print("Selected Game: G_avoid") - for event in pygame.event.get(): - if event.type == QUIT: - pygame.quit() - sys.exit() - - clock.tick(30) def G_memoryGame(): - pygame.init() - - screen = pygame.display.set_mode((400, 300)) - pygame.display.set_caption("G_memoryGame") - - font = pygame.font.Font(None, 36) - clock = pygame.time.Clock() - - while True: - screen.fill((255, 255, 255)) - text = font.render("G_memoryGame", True, (0, 0, 0)) - screen.blit(text, (150, 150)) - pygame.display.flip() - - for event in pygame.event.get(): - if event.type == QUIT: - pygame.quit() - sys.exit() - - clock.tick(30) + game_path = os.path.abspath("G_memoryGame.py") + try: + subprocess.run([sys.executable, game_path], check=True) + print("Selected Game: G_memoryGame") + except subprocess.CalledProcessError as e: + print(f"Error while running G_memoryGame: {e}") +def display_text(screen, font, text, x, y): + text_surface = font.render(text, True, (0, 0, 0)) + text_rect = text_surface.get_rect(center=(x, y)) + screen.blit(text_surface, text_rect) def main(): pygame.init() @@ -79,18 +39,26 @@ def main(): screen = pygame.display.set_mode((width, height)) pygame.display.set_caption("Main Game") + # 배경 이미지 로드 + background_image = pygame.image.load("image/common.jpeg") + background_rect = background_image.get_rect() + font = pygame.font.Font(None, 36) clock = pygame.time.Clock() games = [G_snake, G_avoid, G_memoryGame] - selected_game = 0 + selected_game = None while True: - screen.fill((255, 255, 255)) + screen.blit(background_image, background_rect) + + game_names = ["Snake Game", "Avoid Game", "Memory Game"] + for i, game_name in enumerate(game_names): + text = font.render(game_name, True, (0, 0, 0)) + text_rect = text.get_rect(center=(width // 2, 50 + i * 50)) + screen.blit(text, text_rect) - for i, game in enumerate(games): - text = font.render(f"Game {i + 1}", True, (0, 0, 0)) - screen.blit(text, (width // 2 - 50, 50 + i * 50)) + display_text(screen, font, f"Selected Game: {selected_game}", width // 2, height - 50) pygame.display.flip() @@ -98,14 +66,18 @@ def main(): if event.type == QUIT: pygame.quit() sys.exit() + elif event.type == MOUSEBUTTONDOWN: + x, y = event.pos + for i, game in enumerate(games): + text_rect = font.render(game_names[i], True, (0, 0, 0)).get_rect(center=(width // 2, 50 + i * 50)) + if text_rect.collidepoint(x, y): + selected_game = game_names[i] + games[i]() + break elif event.type == KEYDOWN: - if event.key == K_UP: - selected_game = (selected_game - 1) % len(games) - elif event.key == K_DOWN: - selected_game = (selected_game + 1) % len(games) - elif event.key == K_RETURN: - # 선택된 게임 실행 - games[selected_game]() + if event.key == K_q: + pygame.quit() + sys.exit() clock.tick(30)