-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
275 lines (222 loc) · 9.3 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import streamlit as st
from st_keyup import st_keyup as keyup
import random
import time
def init_game_state():
"""Inicializa o estado do jogo se ainda não existir"""
if 'game_state' not in st.session_state:
st.session_state.game_state = {
'player_position': 0, # Posição horizontal do jogador
# Pista atual (0: superior, 1: meio, 2: inferior)
'player_lane': 1,
'track_length': 40, # Comprimento da pista
'game_over': False,
'obstacles': [], # Lista de obstáculos
'score': 0, # Pontuação atual
'high_score': 0, # Recorde
'player_speed': 2, # Velocidade atual
'max_speed': 5, # Velocidade máxima
'lives': 3, # Vidas restantes
'boost_available': True, # Se o turbo está disponível
'last_key': None # Última tecla pressionada
}
# Gera obstáculos iniciais
st.session_state.game_state['obstacles'] = generate_obstacles()
def handle_input():
"""Processa input do teclado"""
key = keyup(label="game", key="keyboard")
if key:
# Evita processamento duplicado da mesma tecla
if key != st.session_state.game_state['last_key']:
st.session_state.game_state['last_key'] = key
# Controles
if key == "ArrowUp":
move_up()
elif key == "ArrowDown":
move_down()
elif key == "ArrowRight":
accelerate()
elif key == "ArrowLeft":
brake()
elif key == " ": # Tecla espaço
use_boost()
elif key == "r": # Tecla R
reset_game()
def generate_obstacles():
"""Gera novos obstáculos para a pista"""
track_length = st.session_state.game_state['track_length']
return [
{'lane': 0, 'position': random.randint(25, track_length - 5)},
{'lane': 1, 'position': random.randint(25, track_length - 5)},
{'lane': 2, 'position': random.randint(25, track_length - 5)}
]
def move_up():
"""Move o jogador para a pista superior"""
if st.session_state.game_state['player_lane'] > 0:
st.session_state.game_state['player_lane'] -= 1
st.session_state.game_state['player_speed'] = max(
1, st.session_state.game_state['player_speed'] - 0.5)
def move_down():
"""Move o jogador para a pista inferior"""
if st.session_state.game_state['player_lane'] < 2:
st.session_state.game_state['player_lane'] += 1
st.session_state.game_state['player_speed'] = max(
1, st.session_state.game_state['player_speed'] - 0.5)
def accelerate():
"""Aumenta a velocidade do jogador"""
if st.session_state.game_state['player_speed'] < st.session_state.game_state['max_speed']:
st.session_state.game_state['player_speed'] += 0.5
def brake():
"""Reduz a velocidade do jogador"""
if st.session_state.game_state['player_speed'] > 1:
st.session_state.game_state['player_speed'] -= 0.5
def use_boost():
"""Ativa o turbo"""
if st.session_state.game_state['boost_available']:
st.session_state.game_state['player_speed'] = min(
st.session_state.game_state['player_speed'] + 2,
st.session_state.game_state['max_speed']
)
st.session_state.game_state['boost_available'] = False
def check_collision():
"""Verifica se houve colisão com obstáculos"""
player_lane = st.session_state.game_state['player_lane']
player_pos = st.session_state.game_state['player_position']
for obstacle in st.session_state.game_state['obstacles']:
if (obstacle['lane'] == player_lane and
abs(player_pos - obstacle['position']) < 2):
return True
return False
def move_player():
"""Atualiza a posição do jogador e verifica colisões"""
if not st.session_state.game_state['game_over']:
# Move o jogador
st.session_state.game_state['player_position'] += st.session_state.game_state['player_speed']
# Verifica colisão
if check_collision():
st.session_state.game_state['lives'] -= 1
st.session_state.game_state['player_speed'] = 2
if st.session_state.game_state['lives'] <= 0:
st.session_state.game_state['game_over'] = True
if st.session_state.game_state['score'] > st.session_state.game_state['high_score']:
st.session_state.game_state['high_score'] = st.session_state.game_state['score']
else:
# Recua o jogador após colisão
st.session_state.game_state['player_position'] = max(
0, st.session_state.game_state['player_position'] - 10)
# Verifica se completou a pista
if st.session_state.game_state['player_position'] >= st.session_state.game_state['track_length']:
st.session_state.game_state['player_position'] = 0
st.session_state.game_state['score'] += int(
100 * st.session_state.game_state['player_speed'])
st.session_state.game_state['obstacles'] = generate_obstacles()
st.session_state.game_state['boost_available'] = True
def render_track():
"""Renderiza o estado atual da pista"""
tracks = []
player_pos = int(st.session_state.game_state['player_position'])
# Renderiza cada pista
for lane in range(3):
track = ['═'] * st.session_state.game_state['track_length']
# Adiciona obstáculo
for obstacle in st.session_state.game_state['obstacles']:
if obstacle['lane'] == lane:
pos = int(obstacle['position'])
if 0 <= pos < len(track):
track[pos] = '🌲'
# Adiciona jogador
if lane == st.session_state.game_state['player_lane'] and 0 <= player_pos < len(track):
track[player_pos] = '🏎️'
# Adiciona bordas e número da pista
track_str = f"{lane + 1} ║{''.join(track)}║"
tracks.append(track_str)
# Adiciona uma linha de meta
finish_line = " " + "╠" + "═" * \
st.session_state.game_state['track_length'] + "╣"
return "\n".join(tracks + [finish_line])
def reset_game():
"""Reinicia o estado do jogo"""
st.session_state.game_state['player_position'] = 0
st.session_state.game_state['player_lane'] = 1
st.session_state.game_state['game_over'] = False
st.session_state.game_state['obstacles'] = generate_obstacles()
st.session_state.game_state['score'] = 0
st.session_state.game_state['player_speed'] = 2
st.session_state.game_state['lives'] = 3
st.session_state.game_state['boost_available'] = True
st.session_state.game_state['last_key'] = None
def main():
st.set_page_config(page_title="Corrida Maluca", layout="centered")
# Inicializa o estado do jogo
init_game_state()
st.title('🏁 Corrida Maluca 🏁')
# Instruções do jogo
with st.expander("📖 Como Jogar"):
st.write("""
Controles do Teclado:
- ⬆️ Seta para cima: Subir de pista
- ⬇️ Seta para baixo: Descer de pista
- ➡️ Seta para direita: Acelerar
- ⬅️ Seta para esquerda: Frear
- Barra de Espaço: Usar Turbo
- Tecla R: Reiniciar jogo
Objetivo:
1. Desvie dos obstáculos (🌲)
2. Complete voltas para ganhar pontos
3. Use o turbo (🚀) para ganhar velocidade extra
4. Cuidado: você perde uma vida ao bater!
""")
# Processa input do teclado
handle_input()
# Status do jogo
st.subheader("📊 Status")
col1, col2, col3, col4 = st.columns(4)
with col1:
st.metric("❤️ Vidas", st.session_state.game_state['lives'])
with col2:
st.metric("🏃 Velocidade", f"{
st.session_state.game_state['player_speed']:.1f}")
with col3:
st.metric("🎯 Pontuação", st.session_state.game_state['score'])
with col4:
st.metric("🏆 Recorde", st.session_state.game_state['high_score'])
# Pista
st.subheader("🛣️ Pista")
st.code(render_track(), language=None)
# Controles alternativos (botões)
st.subheader("🎮 Controles Alternativos")
# Movimento
col1, col2 = st.columns(2)
with col1:
if st.button('⬆️ Subir'):
move_up()
with col2:
if st.button('⬇️ Descer'):
move_down()
# Velocidade
col1, col2 = st.columns(2)
with col1:
if st.button('➡️ Acelerar'):
accelerate()
with col2:
if st.button('⬅️ Frear'):
brake()
# Turbo e Reset
col1, col2 = st.columns(2)
with col1:
if st.button('🚀 Turbo', disabled=not st.session_state.game_state['boost_available']):
use_boost()
with col2:
if st.button('🔄 Reiniciar'):
reset_game()
# Atualiza estado do jogo
move_player()
# Game Over
if st.session_state.game_state['game_over']:
st.error('🔥 Game Over! 🔥')
st.button('🎮 Jogar Novamente', on_click=reset_game)
# Atualiza a página
time.sleep(0.1)
st.rerun()
if __name__ == '__main__':
main()