rioanggara commited on
Commit
b8a1a18
·
1 Parent(s): 63495b1
Files changed (1) hide show
  1. app.py +21 -18
app.py CHANGED
@@ -1,16 +1,17 @@
1
  import gradio as gr
2
  import random
3
 
4
- def flappy_bird(command, height=0, gap_position=2, bird_position=2, score=0, game_over=False):
 
 
 
 
5
  PIPE_WIDTH = 3
6
  GAME_HEIGHT = 5
7
  GAP_SIZE = 2
8
 
9
  if game_over:
10
- return "Game Over! Your score: {}\nType 'start' to play again.".format(score), 0, 2, 2, 0, False
11
-
12
- if command.lower() == "start":
13
- return get_game_state(2, 2, 0, 0), 0, 2, 2, 0, False
14
 
15
  # Move bird based on command
16
  bird_position += 1 if command.lower() != "flap" else -1
@@ -49,26 +50,28 @@ def get_game_state(height, gap_position, bird_position, score):
49
  return game_state
50
 
51
  iface = gr.Interface(
52
- flappy_bird,
53
  inputs=[
54
- gr.Textbox(label="Enter 'flap' to move up or 'start' to start/restart the game"),
55
- gr.Hidden(value=0),
56
- gr.Hidden(value=2),
57
- gr.Hidden(value=2),
58
- gr.Hidden(value=0),
59
- gr.Hidden(value=False)
60
  ],
61
  outputs=[
62
  gr.Textbox(label="Game State"),
63
- gr.Hidden(),
64
- gr.Hidden(),
65
- gr.Hidden(),
66
- gr.Hidden(),
67
- gr.Hidden()
68
  ],
69
  title="Simple Text-based Flappy Bird Game",
70
- description="Type 'flap' to move the bird up and avoid the pipes. Start the game by typing 'start'."
71
  )
72
 
 
 
73
  if __name__ == "__main__":
74
  iface.launch()
 
1
  import gradio as gr
2
  import random
3
 
4
+ def flappy_bird(_):
5
+ # Initialize or reset the game state
6
+ return get_game_state(2, 2, 0, 0), 0, 2, 2, 0, False
7
+
8
+ def update_game(command, height, gap_position, bird_position, score, game_over):
9
  PIPE_WIDTH = 3
10
  GAME_HEIGHT = 5
11
  GAP_SIZE = 2
12
 
13
  if game_over:
14
+ return "Game Over! Your score: {}\nClick 'Start' to play again.".format(score), 0, 2, 2, 0, False
 
 
 
15
 
16
  # Move bird based on command
17
  bird_position += 1 if command.lower() != "flap" else -1
 
50
  return game_state
51
 
52
  iface = gr.Interface(
53
+ fn=update_game,
54
  inputs=[
55
+ gr.Buttons(choices=["flap"], label="Control"),
56
+ gr.State(),
57
+ gr.State(),
58
+ gr.State(),
59
+ gr.State(),
60
+ gr.State()
61
  ],
62
  outputs=[
63
  gr.Textbox(label="Game State"),
64
+ gr.State(),
65
+ gr.State(),
66
+ gr.State(),
67
+ gr.State(),
68
+ gr.State()
69
  ],
70
  title="Simple Text-based Flappy Bird Game",
71
+ description="Click 'Start' to begin. Then click 'Flap' to move the bird up and avoid the pipes."
72
  )
73
 
74
+ iface.add_component(gr.Button("Start").click(flappy_bird, [], ["game_state", "height", "gap_position", "bird_position", "score", "game_over"]), "above")
75
+
76
  if __name__ == "__main__":
77
  iface.launch()