soJaeyoon commited on
Commit
338ed73
Β·
verified Β·
1 Parent(s): 5596ad8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -12
app.py CHANGED
@@ -1,18 +1,84 @@
 
1
  import random
2
  import gradio as gr
3
 
4
- def guess_number(user_input):
5
- # 컴퓨터가 μ„ νƒν•œ 랜덀 숫자 생성
6
- computer_number = random.randint(1, 100)
 
 
 
 
 
7
 
8
- # μ‚¬μš©μž μž…λ ₯κ³Ό 컴퓨터 숫자 비ꡐ
9
- user_number = int(user_input)
10
- if user_number == computer_number:
11
- return f"μ •λ‹΅μž…λ‹ˆλ‹€! 컴퓨터가 μ„ νƒν•œ μˆ«μžλŠ” {computer_number}μž…λ‹ˆλ‹€."
12
- elif user_number < computer_number:
13
- return "더 큰 숫자λ₯Ό μž…λ ₯ν•˜μ„Έμš”."
14
- else:
15
- return "더 μž‘μ€ 숫자λ₯Ό μž…λ ₯ν•˜μ„Έμš”."
16
 
17
- iface = gr.Interface(fn=guess_number, inputs="text", outputs="text", title="숫자 λ§žμΆ”κΈ° κ²Œμž„", description="1μ—μ„œ 100 μ‚¬μ΄μ˜ 숫자λ₯Ό μž…λ ₯ν•˜μ„Έμš”.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  iface.launch()
 
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(direction):
41
+ pygame.init()
42
+ screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
43
+ pygame.display.set_caption("Pygame & Gradio 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="Pygame & Gradio Game", description="Use arrow keys to move the player. Avoid obstacles.")
84
  iface.launch()