GameSpace / app.py
soJaeyoon's picture
Update app.py
a71f36d verified
Raw
History Blame Contribute Delete
2.2 kB
import pygame
import random
import gradio as gr
# κ²Œμž„ μ„€μ •
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 400
PLAYER_SIZE = 20
OBSTACLE_SIZE = 20
PLAYER_COLOR = (0, 0, 255)
OBSTACLE_COLOR = (255, 0, 0)
BACKGROUND_COLOR = (255, 255, 255)
# ν”Œλ ˆμ΄μ–΄ 클래슀
class Player:
def __init__(self, x, y):
self.x = x
self.y = y
def move(self, dx, dy):
self.x += dx
self.y += dy
def draw(self, screen):
pygame.draw.circle(screen, PLAYER_COLOR, (self.x, self.y), PLAYER_SIZE)
# μž₯μ• λ¬Ό 클래슀
class Obstacle:
def __init__(self, x, y):
self.x = x
self.y = y
def move(self, speed):
self.x -= speed
def draw(self, screen):
pygame.draw.rect(screen, OBSTACLE_COLOR, (self.x, self.y, OBSTACLE_SIZE, OBSTACLE_SIZE))
# κ²Œμž„ μ‹€ν–‰ ν•¨μˆ˜
def run_game():
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Pygame & Gradio Game")
clock = pygame.time.Clock()
player = Player(50, SCREEN_HEIGHT // 2)
obstacles = []
running = True
while running:
screen.fill(BACKGROUND_COLOR)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
player.move(0, -5)
if keys[pygame.K_DOWN]:
player.move(0, 5)
if keys[pygame.K_LEFT]:
player.move(-5, 0)
if keys[pygame.K_RIGHT]:
player.move(5, 0)
if random.randint(0, 100) < 5:
obstacles.append(Obstacle(SCREEN_WIDTH, random.randint(0, SCREEN_HEIGHT - OBSTACLE_SIZE)))
for obstacle in obstacles:
obstacle.move(5)
obstacle.draw(screen)
if obstacle.x < -OBSTACLE_SIZE:
obstacles.remove(obstacle)
player.draw(screen)
pygame.display.flip()
clock.tick(60)
pygame.quit()
return "Game Over"
iface = gr.Interface(fn=run_game, inputs=None, outputs="text", title="Pygame & Gradio Game", description="Use arrow keys to move the player. Avoid obstacles.")
iface.launch()