File size: 2,177 Bytes
32d5783
 
 
9fcc15e
3bab061
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
pip install transformers
pip install datasets
pip install gradio

import turtle
import random
import gradio as gr

# ํ”Œ๋ ˆ์ด์–ด ํด๋ž˜์Šค
class Player(turtle.Turtle):
    def __init__(self):
        super().__init__()
        self.shape("circle")
        self.color("blue")
        self.penup()
        self.speed(0)
        self.direction = "stop"

    def move(self):
        if self.direction == "up":
            self.sety(self.ycor() + 10)
        elif self.direction == "down":
            self.sety(self.ycor() - 10)
        elif self.direction == "left":
            self.setx(self.xcor() - 10)
        elif self.direction == "right":
            self.setx(self.xcor() + 10)

# ์žฅ์• ๋ฌผ ํด๋ž˜์Šค
class Obstacle(turtle.Turtle):
    def __init__(self):
        super().__init__()
        self.shape("square")
        self.color("red")
        self.penup()
        self.speed(0)
        self.goto(random.randint(-200, 200), random.randint(-200, 200))

# ํ‚ค ์ด๋ฒคํŠธ ํ•จ์ˆ˜
def go_up():
    player.direction = "up"

def go_down():
    player.direction = "down"

def go_left():
    player.direction = "left"

def go_right():
    player.direction = "right"

# ๊ฒŒ์ž„ ์‹คํ–‰ ํ•จ์ˆ˜
def run_game():
    win = turtle.Screen()
    win.title("Keyboard Controlled Game")
    win.bgcolor("white")
    win.setup(width=500, height=500)

    # ํ”Œ๋ ˆ์ด์–ด ๋ฐ ์žฅ์• ๋ฌผ ์ƒ์„ฑ
    player = Player()
    obstacles = [Obstacle() for _ in range(5)]

    # ํ‚ค๋ณด๋“œ ์ด๋ฒคํŠธ ๋ฐ”์ธ๋”ฉ
    win.listen()
    win.onkeypress(go_up, "Up")
    win.onkeypress(go_down, "Down")
    win.onkeypress(go_left, "Left")
    win.onkeypress(go_right, "Right")

    while True:
        win.update()

        # ํ”Œ๋ ˆ์ด์–ด ์ด๋™
        player.move()

        # ์žฅ์• ๋ฌผ ์ด๋™ ๋ฐ ์ถฉ๋Œ ์ฒดํฌ
        for obstacle in obstacles:
            obstacle.setx(obstacle.xcor() - 2)
            if player.distance(obstacle) < 20:
                print("Game Over")
                return

# Gradio ์ธํ„ฐํŽ˜์ด์Šค ์‹คํ–‰
iface = gr.Interface(fn=run_game, inputs=None, outputs=None, title="Keyboard Controlled Game", description="Use arrow keys to move the player. Avoid red squares.")
iface.launch()