| 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 |
|
|
| |
| 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() |
|
|