soJaeyoon/GameSpace
2
1import pygame2import random3import gradio as gr4 5# 게임 설정6SCREEN_WIDTH = 6007SCREEN_HEIGHT = 4008PLAYER_SIZE = 209OBSTACLE_SIZE = 2010PLAYER_COLOR = (0, 0, 255)11OBSTACLE_COLOR = (255, 0, 0)12BACKGROUND_COLOR = (255, 255, 255)13 14# 플레이어 클래스15class Player:16 def __init__(self, x, y):17 self.x = x18 self.y = y19 20 def move(self, dx, dy):21 self.x += dx22 self.y += dy23 24 def draw(self, screen):25 pygame.draw.circle(screen, PLAYER_COLOR, (self.x, self.y), PLAYER_SIZE)26 27# 장애물 클래스28class Obstacle:29 def __init__(self, x, y):30 self.x = x31 self.y = y32 33 def move(self, speed):34 self.x -= speed35 36 def draw(self, screen):37 pygame.draw.rect(screen, OBSTACLE_COLOR, (self.x, self.y, OBSTACLE_SIZE, OBSTACLE_SIZE))38 39# 게임 실행 함수40def run_game():41 pygame.init()42 screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))43 pygame.display.set_caption("Pygame & Gradio Game")44 clock = pygame.time.Clock()45 46 player = Player(50, SCREEN_HEIGHT // 2)47 obstacles = []48 49 running = True50 while running:51 screen.fill(BACKGROUND_COLOR)52 53 for event in pygame.event.get():54 if event.type == pygame.QUIT:55 running = False56 57 keys = pygame.key.get_pressed()58 if keys[pygame.K_UP]:59 player.move(0, -5)60 if keys[pygame.K_DOWN]:61 player.move(0, 5)62 if keys[pygame.K_LEFT]:63 player.move(-5, 0)64 if keys[pygame.K_RIGHT]:65 player.move(5, 0)66 67 if random.randint(0, 100) < 5:68 obstacles.append(Obstacle(SCREEN_WIDTH, random.randint(0, SCREEN_HEIGHT - OBSTACLE_SIZE)))69 70 for obstacle in obstacles:71 obstacle.move(5)72 obstacle.draw(screen)73 if obstacle.x < -OBSTACLE_SIZE:74 obstacles.remove(obstacle)75 76 player.draw(screen)77 78 pygame.display.flip()79 clock.tick(60)80 81 pygame.quit()82 83 return "Game Over"84 85iface = gr.Interface(fn=run_game, inputs=None, outputs="text", title="Pygame & Gradio Game", description="Use arrow keys to move the player. Avoid obstacles.")86iface.launch()87 88 