Spaces:
Sleeping
Sleeping
Commit
·
2bd545f
1
Parent(s):
d0f1837
please work
Browse files
app.py
CHANGED
@@ -1,70 +1,71 @@
|
|
1 |
import gradio as gr
|
2 |
import random
|
3 |
|
4 |
-
def
|
5 |
# Initialize or reset the game state
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
def update_game(command,
|
9 |
PIPE_WIDTH = 3
|
10 |
GAME_HEIGHT = 5
|
11 |
GAP_SIZE = 2
|
12 |
|
13 |
-
if game_over or command == "start":
|
14 |
-
return
|
15 |
|
16 |
# Move bird based on command
|
17 |
-
bird_position += 1 if command.lower() != "flap" else -1
|
18 |
|
19 |
# Check bounds
|
20 |
-
if bird_position < 0 or bird_position >= GAME_HEIGHT:
|
21 |
-
|
|
|
22 |
|
23 |
# Update pipe position
|
24 |
-
height = (height + 1) % (PIPE_WIDTH + GAME_HEIGHT)
|
25 |
|
26 |
# Check for collision or passing through the gap
|
27 |
-
if height == PIPE_WIDTH:
|
28 |
-
if bird_position not in range(gap_position, gap_position + GAP_SIZE):
|
29 |
-
|
|
|
30 |
else:
|
31 |
-
score += 1
|
32 |
-
gap_position = random.randint(0, GAME_HEIGHT - GAP_SIZE)
|
33 |
|
34 |
-
return get_game_state(
|
35 |
|
36 |
-
def get_game_state(
|
37 |
PIPE_WIDTH = 3
|
38 |
GAME_HEIGHT = 5
|
39 |
GAP_SIZE = 2
|
40 |
-
|
41 |
for i in range(GAME_HEIGHT):
|
42 |
line = ""
|
43 |
-
if height < PIPE_WIDTH and (i < gap_position or i >= gap_position + GAP_SIZE):
|
44 |
line += "|"
|
45 |
else:
|
46 |
line += " "
|
47 |
-
line += "B" if i == bird_position else "_"
|
48 |
line += "\n"
|
49 |
-
|
50 |
-
return
|
51 |
|
52 |
iface = gr.Interface(
|
53 |
fn=update_game,
|
54 |
inputs=[
|
55 |
gr.Dropdown(choices=["flap", "start"], 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",
|
|
|
1 |
import gradio as gr
|
2 |
import random
|
3 |
|
4 |
+
def initialize_game():
|
5 |
# Initialize or reset the game state
|
6 |
+
game_state = {
|
7 |
+
'height': 0,
|
8 |
+
'gap_position': 2,
|
9 |
+
'bird_position': 2,
|
10 |
+
'score': 0,
|
11 |
+
'game_over': False
|
12 |
+
}
|
13 |
+
return get_game_state(game_state), game_state
|
14 |
|
15 |
+
def update_game(command, game_state):
|
16 |
PIPE_WIDTH = 3
|
17 |
GAME_HEIGHT = 5
|
18 |
GAP_SIZE = 2
|
19 |
|
20 |
+
if game_state['game_over'] or command == "start":
|
21 |
+
return initialize_game()
|
22 |
|
23 |
# Move bird based on command
|
24 |
+
game_state['bird_position'] += 1 if command.lower() != "flap" else -1
|
25 |
|
26 |
# Check bounds
|
27 |
+
if game_state['bird_position'] < 0 or game_state['bird_position'] >= GAME_HEIGHT:
|
28 |
+
game_state['game_over'] = True
|
29 |
+
return f"You hit the top/bottom! Game Over! Your score: {game_state['score']}", game_state
|
30 |
|
31 |
# Update pipe position
|
32 |
+
game_state['height'] = (game_state['height'] + 1) % (PIPE_WIDTH + GAME_HEIGHT)
|
33 |
|
34 |
# Check for collision or passing through the gap
|
35 |
+
if game_state['height'] == PIPE_WIDTH:
|
36 |
+
if game_state['bird_position'] not in range(game_state['gap_position'], game_state['gap_position'] + GAP_SIZE):
|
37 |
+
game_state['game_over'] = True
|
38 |
+
return f"You hit a pipe! Game Over! Your score: {game_state['score']}", game_state
|
39 |
else:
|
40 |
+
game_state['score'] += 1
|
41 |
+
game_state['gap_position'] = random.randint(0, GAME_HEIGHT - GAP_SIZE)
|
42 |
|
43 |
+
return get_game_state(game_state), game_state
|
44 |
|
45 |
+
def get_game_state(game_state):
|
46 |
PIPE_WIDTH = 3
|
47 |
GAME_HEIGHT = 5
|
48 |
GAP_SIZE = 2
|
49 |
+
display = "Score: {}\n".format(game_state['score'])
|
50 |
for i in range(GAME_HEIGHT):
|
51 |
line = ""
|
52 |
+
if game_state['height'] < PIPE_WIDTH and (i < game_state['gap_position'] or i >= game_state['gap_position'] + GAP_SIZE):
|
53 |
line += "|"
|
54 |
else:
|
55 |
line += " "
|
56 |
+
line += "B" if i == game_state['bird_position'] else "_"
|
57 |
line += "\n"
|
58 |
+
display += line
|
59 |
+
return display
|
60 |
|
61 |
iface = gr.Interface(
|
62 |
fn=update_game,
|
63 |
inputs=[
|
64 |
gr.Dropdown(choices=["flap", "start"], label="Control"),
|
|
|
|
|
|
|
|
|
65 |
gr.State()
|
66 |
],
|
67 |
outputs=[
|
68 |
gr.Textbox(label="Game State"),
|
|
|
|
|
|
|
|
|
69 |
gr.State()
|
70 |
],
|
71 |
title="Simple Text-based Flappy Bird Game",
|