Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,74 +3,99 @@ import random
|
|
| 3 |
import time
|
| 4 |
|
| 5 |
# Streamlit Configurations
|
| 6 |
-
st.set_page_config(page_title="Jumping
|
| 7 |
-
st.title("Jumping
|
| 8 |
|
| 9 |
# Game Variables
|
| 10 |
if "game_state" not in st.session_state:
|
| 11 |
st.session_state.game_state = "start"
|
| 12 |
if "score" not in st.session_state:
|
| 13 |
st.session_state.score = 0
|
|
|
|
|
|
|
| 14 |
if "player_position" not in st.session_state:
|
| 15 |
-
st.session_state.player_position = 2 # Player starts on the middle
|
| 16 |
-
if "
|
| 17 |
-
st.session_state.
|
| 18 |
-
if "
|
| 19 |
-
st.session_state.
|
| 20 |
|
| 21 |
-
|
| 22 |
player_char = "🟥"
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
#
|
| 28 |
def reset_game():
|
| 29 |
st.session_state.game_state = "running"
|
| 30 |
st.session_state.score = 0
|
|
|
|
| 31 |
st.session_state.player_position = 2
|
| 32 |
-
st.session_state.
|
| 33 |
-
st.session_state.
|
| 34 |
|
| 35 |
-
def
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
if
|
| 40 |
-
|
| 41 |
-
st.session_state.falling_blocks = new_falling_blocks
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
st.session_state.
|
| 53 |
|
| 54 |
-
|
| 55 |
-
if
|
| 56 |
-
|
| 57 |
-
if
|
| 58 |
-
st.session_state.
|
|
|
|
|
|
|
| 59 |
|
| 60 |
def render_game():
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
# Game Control
|
| 76 |
if st.session_state.game_state == "start":
|
|
@@ -78,19 +103,22 @@ if st.session_state.game_state == "start":
|
|
| 78 |
if st.button("Start Game"):
|
| 79 |
reset_game()
|
| 80 |
elif st.session_state.game_state == "running":
|
| 81 |
-
# Update
|
| 82 |
-
|
|
|
|
|
|
|
| 83 |
render_game()
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
| 90 |
|
| 91 |
# Simulate real-time updates
|
| 92 |
time.sleep(0.5)
|
| 93 |
elif st.session_state.game_state == "game_over":
|
| 94 |
st.write("**Game Over!** Your final score is:", st.session_state.score)
|
| 95 |
-
if st.button("
|
| 96 |
reset_game()
|
|
|
|
| 3 |
import time
|
| 4 |
|
| 5 |
# Streamlit Configurations
|
| 6 |
+
st.set_page_config(page_title="Jumping Logs Game", layout="wide")
|
| 7 |
+
st.title("Jumping Logs Game")
|
| 8 |
|
| 9 |
# Game Variables
|
| 10 |
if "game_state" not in st.session_state:
|
| 11 |
st.session_state.game_state = "start"
|
| 12 |
if "score" not in st.session_state:
|
| 13 |
st.session_state.score = 0
|
| 14 |
+
if "lives" not in st.session_state:
|
| 15 |
+
st.session_state.lives = 3
|
| 16 |
if "player_position" not in st.session_state:
|
| 17 |
+
st.session_state.player_position = 2 # Player starts on the middle log (index 2)
|
| 18 |
+
if "logs" not in st.session_state:
|
| 19 |
+
st.session_state.logs = [{"position": 2, "direction": 1} for _ in range(5)] # Logs move up and down
|
| 20 |
+
if "shooter_blocks" not in st.session_state:
|
| 21 |
+
st.session_state.shooter_blocks = []
|
| 22 |
|
| 23 |
+
num_logs = 5
|
| 24 |
player_char = "🟥"
|
| 25 |
+
log_char = "🟫"
|
| 26 |
+
shooter_char = "⬛"
|
| 27 |
+
river_char = "🟦"
|
| 28 |
+
jump_duration = 2 # Time allowed to jump
|
| 29 |
+
block_speed = 1
|
| 30 |
+
shoot_interval = 2 # Interval between shooter attacks (seconds)
|
| 31 |
|
| 32 |
+
# Helper functions
|
| 33 |
def reset_game():
|
| 34 |
st.session_state.game_state = "running"
|
| 35 |
st.session_state.score = 0
|
| 36 |
+
st.session_state.lives = 3
|
| 37 |
st.session_state.player_position = 2
|
| 38 |
+
st.session_state.logs = [{"position": 2, "direction": 1} for _ in range(num_logs)]
|
| 39 |
+
st.session_state.shooter_blocks = []
|
| 40 |
|
| 41 |
+
def update_logs():
|
| 42 |
+
"""Move logs up and down."""
|
| 43 |
+
for log in st.session_state.logs:
|
| 44 |
+
log["position"] += log["direction"]
|
| 45 |
+
if log["position"] <= 0 or log["position"] >= 4: # Reverse direction
|
| 46 |
+
log["direction"] *= -1
|
|
|
|
| 47 |
|
| 48 |
+
def update_shooter():
|
| 49 |
+
"""Move shooter blocks and check for collisions."""
|
| 50 |
+
new_shooter_blocks = []
|
| 51 |
+
for block in st.session_state.shooter_blocks:
|
| 52 |
+
block["y"] += block_speed
|
| 53 |
+
if block["y"] < 5: # Block still in range
|
| 54 |
+
new_shooter_blocks.append(block)
|
| 55 |
+
if block["y"] == 4 and block["x"] == st.session_state.player_position:
|
| 56 |
+
st.session_state.lives -= 1
|
| 57 |
+
if st.session_state.lives == 0:
|
| 58 |
+
st.session_state.game_state = "game_over"
|
| 59 |
+
return
|
| 60 |
+
st.session_state.shooter_blocks = new_shooter_blocks
|
| 61 |
|
| 62 |
+
def spawn_shooter():
|
| 63 |
+
"""Randomly spawn shooter blocks targeting the player."""
|
| 64 |
+
if random.random() < 0.3: # 30% chance to spawn a shooter
|
| 65 |
+
st.session_state.shooter_blocks.append({"x": random.randint(0, num_logs - 1), "y": 0})
|
| 66 |
|
| 67 |
+
def check_jump():
|
| 68 |
+
"""Check if the player is on a log after a jump."""
|
| 69 |
+
current_log = st.session_state.logs[st.session_state.player_position]
|
| 70 |
+
if current_log["position"] != 4: # If the log isn't at the bottom
|
| 71 |
+
st.session_state.lives -= 1
|
| 72 |
+
if st.session_state.lives == 0:
|
| 73 |
+
st.session_state.game_state = "game_over"
|
| 74 |
|
| 75 |
def render_game():
|
| 76 |
+
"""Render the game logs, player, and river."""
|
| 77 |
+
for i in range(5): # River height
|
| 78 |
+
row = ""
|
| 79 |
+
for j in range(num_logs):
|
| 80 |
+
if i == st.session_state.logs[j]["position"] and i == 4:
|
| 81 |
+
if j == st.session_state.player_position:
|
| 82 |
+
row += player_char
|
| 83 |
+
else:
|
| 84 |
+
row += log_char
|
| 85 |
+
elif i == 4:
|
| 86 |
+
row += river_char
|
| 87 |
+
elif any(block["x"] == j and block["y"] == i for block in st.session_state.shooter_blocks):
|
| 88 |
+
row += shooter_char
|
| 89 |
+
else:
|
| 90 |
+
row += " "
|
| 91 |
+
st.text(row)
|
| 92 |
|
| 93 |
+
def jump():
|
| 94 |
+
"""Handle jump mechanic."""
|
| 95 |
+
st.session_state.player_position += 1
|
| 96 |
+
if st.session_state.player_position >= num_logs:
|
| 97 |
+
st.session_state.player_position = 0
|
| 98 |
+
check_jump()
|
| 99 |
|
| 100 |
# Game Control
|
| 101 |
if st.session_state.game_state == "start":
|
|
|
|
| 103 |
if st.button("Start Game"):
|
| 104 |
reset_game()
|
| 105 |
elif st.session_state.game_state == "running":
|
| 106 |
+
# Update game state
|
| 107 |
+
update_logs()
|
| 108 |
+
update_shooter()
|
| 109 |
+
spawn_shooter()
|
| 110 |
render_game()
|
| 111 |
|
| 112 |
+
# Display score and lives
|
| 113 |
+
st.write(f"**Score:** {st.session_state.score} | **Lives:** {st.session_state.lives}")
|
| 114 |
+
|
| 115 |
+
# Jump control
|
| 116 |
+
if st.button("Jump (SPACE)"):
|
| 117 |
+
jump()
|
| 118 |
|
| 119 |
# Simulate real-time updates
|
| 120 |
time.sleep(0.5)
|
| 121 |
elif st.session_state.game_state == "game_over":
|
| 122 |
st.write("**Game Over!** Your final score is:", st.session_state.score)
|
| 123 |
+
if st.button("Restart"):
|
| 124 |
reset_game()
|