Spaces:
Sleeping
Sleeping
Commit
·
63495b1
1
Parent(s):
0c9ab08
new
Browse files- app.py +74 -0
- requirements.txt +1 -0
app.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
17 |
+
|
18 |
+
# Check bounds
|
19 |
+
if bird_position < 0 or bird_position >= GAME_HEIGHT:
|
20 |
+
return "You hit the top/bottom! Game Over! Your score: {}".format(score), 0, 2, 2, 0, True
|
21 |
+
|
22 |
+
# Update pipe position
|
23 |
+
height = (height + 1) % (PIPE_WIDTH + GAME_HEIGHT)
|
24 |
+
|
25 |
+
# Check for collision or passing through the gap
|
26 |
+
if height == PIPE_WIDTH:
|
27 |
+
if bird_position not in range(gap_position, gap_position + GAP_SIZE):
|
28 |
+
return "You hit a pipe! Game Over! Your score: {}".format(score), 0, 2, 2, 0, True
|
29 |
+
else:
|
30 |
+
score += 1
|
31 |
+
gap_position = random.randint(0, GAME_HEIGHT - GAP_SIZE)
|
32 |
+
|
33 |
+
return get_game_state(height, gap_position, bird_position, score), height, gap_position, bird_position, score, False
|
34 |
+
|
35 |
+
def get_game_state(height, gap_position, bird_position, score):
|
36 |
+
PIPE_WIDTH = 3
|
37 |
+
GAME_HEIGHT = 5
|
38 |
+
GAP_SIZE = 2
|
39 |
+
game_state = "Score: {}\n".format(score)
|
40 |
+
for i in range(GAME_HEIGHT):
|
41 |
+
line = ""
|
42 |
+
if height < PIPE_WIDTH and (i < gap_position or i >= gap_position + GAP_SIZE):
|
43 |
+
line += "|"
|
44 |
+
else:
|
45 |
+
line += " "
|
46 |
+
line += "B" if i == bird_position else "_"
|
47 |
+
line += "\n"
|
48 |
+
game_state += line
|
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()
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
gradio
|