Update app.py
Browse files
app.py
CHANGED
|
@@ -1,86 +1,31 @@
|
|
| 1 |
-
import pygame
|
| 2 |
import random
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
PLAYER_SIZE = 20
|
| 9 |
-
OBSTACLE_SIZE = 20
|
| 10 |
-
PLAYER_COLOR = (0, 0, 255)
|
| 11 |
-
OBSTACLE_COLOR = (255, 0, 0)
|
| 12 |
-
BACKGROUND_COLOR = (255, 255, 255)
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
self.y = y
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
|
|
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
self.y = y
|
| 32 |
|
| 33 |
-
|
| 34 |
-
self.x -= speed
|
| 35 |
|
| 36 |
-
|
| 37 |
-
pygame.draw.rect(screen, OBSTACLE_COLOR, (self.x, self.y, OBSTACLE_SIZE, OBSTACLE_SIZE))
|
| 38 |
-
|
| 39 |
-
# κ²μ μ€ν ν¨μ
|
| 40 |
-
def run_game():
|
| 41 |
-
pygame.init()
|
| 42 |
-
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
|
| 43 |
-
pygame.display.set_caption("Keyboard Controlled Game")
|
| 44 |
-
clock = pygame.time.Clock()
|
| 45 |
-
|
| 46 |
-
player = Player(50, SCREEN_HEIGHT // 2)
|
| 47 |
-
obstacles = []
|
| 48 |
-
|
| 49 |
-
running = True
|
| 50 |
-
while running:
|
| 51 |
-
screen.fill(BACKGROUND_COLOR)
|
| 52 |
-
|
| 53 |
-
for event in pygame.event.get():
|
| 54 |
-
if event.type == pygame.QUIT:
|
| 55 |
-
running = False
|
| 56 |
-
|
| 57 |
-
keys = pygame.key.get_pressed()
|
| 58 |
-
if keys[pygame.K_UP]:
|
| 59 |
-
player.move(0, -5)
|
| 60 |
-
if keys[pygame.K_DOWN]:
|
| 61 |
-
player.move(0, 5)
|
| 62 |
-
if keys[pygame.K_LEFT]:
|
| 63 |
-
player.move(-5, 0)
|
| 64 |
-
if keys[pygame.K_RIGHT]:
|
| 65 |
-
player.move(5, 0)
|
| 66 |
-
|
| 67 |
-
if random.randint(0, 100) < 5:
|
| 68 |
-
obstacles.append(Obstacle(SCREEN_WIDTH, random.randint(0, SCREEN_HEIGHT - OBSTACLE_SIZE)))
|
| 69 |
-
|
| 70 |
-
for obstacle in obstacles:
|
| 71 |
-
obstacle.move(5)
|
| 72 |
-
obstacle.draw(screen)
|
| 73 |
-
if obstacle.x < -OBSTACLE_SIZE:
|
| 74 |
-
obstacles.remove(obstacle)
|
| 75 |
-
|
| 76 |
-
player.draw(screen)
|
| 77 |
-
|
| 78 |
-
pygame.display.flip()
|
| 79 |
-
clock.tick(60)
|
| 80 |
-
|
| 81 |
-
pygame.quit()
|
| 82 |
-
|
| 83 |
-
iface = gr.Interface(fn=run_game, inputs=None, outputs=None, title="Keyboard Controlled Game", description="Use arrow keys to move the player. Avoid obstacles.")
|
| 84 |
iface.launch()
|
| 85 |
|
| 86 |
|
|
|
|
|
|
|
| 1 |
import random
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
+
def run_game(direction):
|
| 5 |
+
player_position = 0
|
| 6 |
+
obstacle_position = 5
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
if direction == "Left":
|
| 9 |
+
player_position -= 1
|
| 10 |
+
elif direction == "Right":
|
| 11 |
+
player_position += 1
|
|
|
|
| 12 |
|
| 13 |
+
# μ₯μ λ¬Όκ³Ό μΆ©λ 체ν¬
|
| 14 |
+
if player_position == obstacle_position:
|
| 15 |
+
return "Game Over"
|
| 16 |
|
| 17 |
+
# μμμ λμ°©ν κ²½μ°
|
| 18 |
+
if player_position == 10:
|
| 19 |
+
return "You Win!"
|
| 20 |
|
| 21 |
+
# νμ¬ μμΉ νμ
|
| 22 |
+
game_map = [" "] * 10
|
| 23 |
+
game_map[player_position] = "P"
|
| 24 |
+
game_map[obstacle_position] = "O"
|
|
|
|
| 25 |
|
| 26 |
+
return " | ".join(game_map)
|
|
|
|
| 27 |
|
| 28 |
+
iface = gr.Interface(fn=run_game, inputs="text", outputs="text", title="Keyboard Controlled Game", description="Use 'Left' and 'Right' keys to move the player. Avoid obstacle 'O' and reach the box 'P'.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
iface.launch()
|
| 30 |
|
| 31 |
|