awais0300 commited on
Commit
e1784d7
·
verified ·
1 Parent(s): 4a36a01

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -0
app.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import random
3
+ import time
4
+
5
+ # Game settings
6
+ player_position = 5 # Player starts in the middle of the track
7
+ game_over = False
8
+ score = 0
9
+ game_speed = 0.1 # Speed of the game (lower value = faster)
10
+ track_length = 10 # Number of positions on the track
11
+
12
+ # Function to update game state
13
+ def update_game():
14
+ global score, player_position, game_over
15
+ move = st.session_state.get('move', None)
16
+
17
+ if move == 'left' and player_position > 0:
18
+ player_position -= 1
19
+ elif move == 'right' and player_position < track_length - 1:
20
+ player_position += 1
21
+
22
+ # Randomly generate obstacles (just for simulation)
23
+ obstacle_position = random.randint(0, track_length - 1)
24
+
25
+ # Check for collision
26
+ if player_position == obstacle_position:
27
+ game_over = True
28
+
29
+ score += 1 # Increment score over time
30
+
31
+ # Start page
32
+ def start_page():
33
+ st.title("Subway Surfer Game")
34
+ st.write("Welcome to the Subway Surfer Game!")
35
+ st.write("Use the left and right arrow keys to avoid obstacles.")
36
+ st.button("Start Game", on_click=start_game)
37
+
38
+ # Game over page
39
+ def game_over_page():
40
+ st.title("Game Over")
41
+ st.write(f"Your score: {score}")
42
+ st.button("Play Again", on_click=start_game)
43
+
44
+ # Main game loop
45
+ def main_game():
46
+ global game_over
47
+
48
+ st.title("Subway Surfer Game")
49
+ st.write(f"Score: {score}")
50
+ st.write(f"Player position: {player_position}")
51
+
52
+ if game_over:
53
+ game_over_page()
54
+ return
55
+
56
+ # Display available moves
57
+ st.text("Use the arrow keys (left, right) to move.")
58
+ st.text("Game speed: " + str(game_speed))
59
+
60
+ # Update the game every 100ms
61
+ time.sleep(game_speed)
62
+ update_game()
63
+
64
+ if not game_over:
65
+ main_game()
66
+
67
+ # Game start logic
68
+ def start_game():
69
+ global score, player_position, game_over
70
+ score = 0
71
+ player_position = 5 # Reset player to the center of the track
72
+ game_over = False
73
+ main_game()
74
+
75
+ # Initial game start page
76
+ start_page()