Astronaut / app.py
soJaeyoon's picture
Update app.py
3bab061 verified
Raw
History Blame Contribute Delete
2.18 kB
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()