AbdullahYaser54456 commited on
Commit
643c8f1
·
verified ·
1 Parent(s): 579079c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -55
app.py CHANGED
@@ -3,74 +3,99 @@ import random
3
  import time
4
 
5
  # Streamlit Configurations
6
- st.set_page_config(page_title="Jumping Square Game", layout="wide")
7
- st.title("Jumping Square 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 "player_position" not in st.session_state:
15
- st.session_state.player_position = 2 # Player starts on the middle bridge (index 2)
16
- if "coins" not in st.session_state:
17
- st.session_state.coins = []
18
- if "falling_blocks" not in st.session_state:
19
- st.session_state.falling_blocks = []
20
 
21
- num_bridges = 5
22
  player_char = "🟥"
23
- bridge_char = "🟦"
24
- coin_char = "🟡"
25
- block_char = ""
 
 
 
26
 
27
- # Game Logic
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.coins = [random.randint(0, num_bridges - 1)]
33
- st.session_state.falling_blocks = []
34
 
35
- def update_game():
36
- # Move falling blocks
37
- new_falling_blocks = []
38
- for block in st.session_state.falling_blocks:
39
- if block[1] < 4: # Block is still falling
40
- new_falling_blocks.append((block[0], block[1] + 1))
41
- st.session_state.falling_blocks = new_falling_blocks
42
 
43
- # Check collisions
44
- for block in st.session_state.falling_blocks:
45
- if block[1] == 3 and block[0] == st.session_state.player_position:
46
- st.session_state.game_state = "game_over"
47
- return
 
 
 
 
 
 
 
 
48
 
49
- # Coin Collection
50
- if st.session_state.player_position in st.session_state.coins:
51
- st.session_state.coins.remove(st.session_state.player_position)
52
- st.session_state.score += 1
53
 
54
- # Add new coins and blocks
55
- if random.random() < 0.3: # 30% chance to spawn a coin
56
- st.session_state.coins.append(random.randint(0, num_bridges - 1))
57
- if random.random() < 0.2: # 20% chance to spawn a block
58
- st.session_state.falling_blocks.append((random.randint(0, num_bridges - 1), 0))
 
 
59
 
60
  def render_game():
61
- # Render bridges
62
- bridges = [" " * 10 for _ in range(num_bridges)]
63
- for i in range(num_bridges):
64
- if i == st.session_state.player_position:
65
- bridges[i] = player_char
66
- if i in st.session_state.coins:
67
- bridges[i] += coin_char
68
- for block in st.session_state.falling_blocks:
69
- bridges[block[0]] += block_char
 
 
 
 
 
 
 
70
 
71
- # Display game state
72
- for i in range(num_bridges):
73
- st.text(bridges[i])
 
 
 
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 and Render the Game
82
- update_game()
 
 
83
  render_game()
84
 
85
- st.write(f"**Score:** {st.session_state.score}")
86
- if st.button("Move Left") and st.session_state.player_position > 0:
87
- st.session_state.player_position -= 1
88
- if st.button("Move Right") and st.session_state.player_position < num_bridges - 1:
89
- st.session_state.player_position += 1
 
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("Retry"):
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()