File size: 2,196 Bytes
338ed73
0b7b71d
e432a57
 
338ed73
 
 
 
 
 
 
 
164a4d7
338ed73
 
 
 
 
164a4d7
338ed73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a71f36d
338ed73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a71f36d
 
 
e432a57
a71f36d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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()