soJaeyoon commited on
Commit
bfa85dc
Β·
verified Β·
1 Parent(s): 0b7b71d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -74
app.py CHANGED
@@ -1,86 +1,31 @@
1
- import pygame
2
  import random
3
  import gradio as gr
4
 
5
- # κ²Œμž„ μ„€μ •
6
- SCREEN_WIDTH = 600
7
- SCREEN_HEIGHT = 400
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
- class Player:
16
- def __init__(self, x, y):
17
- self.x = x
18
- self.y = y
19
 
20
- def move(self, dx, dy):
21
- self.x += dx
22
- self.y += dy
23
 
24
- def draw(self, screen):
25
- pygame.draw.circle(screen, PLAYER_COLOR, (self.x, self.y), PLAYER_SIZE)
 
26
 
27
- # μž₯μ• λ¬Ό 클래슀
28
- class Obstacle:
29
- def __init__(self, x, y):
30
- self.x = x
31
- self.y = y
32
 
33
- def move(self, speed):
34
- self.x -= speed
35
 
36
- def draw(self, screen):
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