Spaces:
Runtime error
Runtime error
Download src/streamlit_app.py from Math3ws/detran-championship: direct link, hf CLI and curl.
- Browser
- Download file 5.95 kB
-
https://huggingface.co/spaces/Math3ws/detran-championship/resolve/main/src/streamlit_app.py
- Command line
-
hf download hf://spaces/Math3ws/detran-championship/src/streamlit_app.py
-
curl -L -o streamlit_app.py https://huggingface.co/spaces/Math3ws/detran-championship/resolve/main/src/streamlit_app.py
5.95 kB
| import streamlit as st | |
| import json | |
| import time | |
| import random | |
| import os | |
| # ---------------- CONFIG ---------------- | |
| st.set_page_config( | |
| page_title="DETRAN Pro Simulator", | |
| layout="wide" | |
| ) | |
| # ---------------- CONSTANTES ---------------- | |
| PISTA_URL = "https://images.unsplash.com/photo-1505852679233-d9fd70aff56d?auto=format&fit=crop&w=1200&q=60" | |
| RANKING_FILE = "ranking.json" | |
| # ---------------- BANCO DE QUESTÕES ---------------- | |
| QUESTOES_BASE = [ | |
| { | |
| "pergunta": "Preferência em cruzamento sem sinalização?", | |
| "opcoes": ["Vem pela direita", "Vem pela esquerda", "Maior velocidade"], | |
| "correta": "Vem pela direita" | |
| }, | |
| { | |
| "pergunta": "Placa R-1 (PARE) exige:", | |
| "opcoes": ["Reduzir", "Parar totalmente", "Ceder se vier carro"], | |
| "correta": "Parar totalmente" | |
| }, | |
| { | |
| "pergunta": "Distância mínima para ultrapassar ciclista:", | |
| "opcoes": ["0,5m", "1,5m", "2,0m"], | |
| "correta": "1,5m" | |
| }, | |
| { | |
| "pergunta": "Uso de celular dirigindo gera infração:", | |
| "opcoes": ["Média", "Grave", "Gravíssima"], | |
| "correta": "Gravíssima" | |
| } | |
| ] | |
| # ---------------- FUNÇÕES ---------------- | |
| def load_ranking(): | |
| if not os.path.exists(RANKING_FILE): | |
| return [] | |
| try: | |
| with open(RANKING_FILE, "r") as f: | |
| return json.load(f) | |
| except: | |
| return [] | |
| def save_ranking(data): | |
| with open(RANKING_FILE, "w") as f: | |
| json.dump(data, f, indent=4) | |
| def reset_game(): | |
| st.session_state.pos = 0 | |
| st.session_state.vidas = 3 | |
| st.session_state.erros = 0 | |
| st.session_state.start_time = time.time() | |
| st.session_state.questoes = random.sample(QUESTOES_BASE, len(QUESTOES_BASE)) | |
| # ---------------- LOGIN ---------------- | |
| if "user" not in st.session_state: | |
| st.session_state.user = None | |
| if st.session_state.user is None: | |
| st.title("🔐 Login - DETRAN Pro Simulator") | |
| username = st.text_input("Digite seu nome para começar") | |
| if st.button("Entrar"): | |
| if username.strip(): | |
| st.session_state.user = username.strip() | |
| reset_game() | |
| st.rerun() | |
| st.stop() | |
| # ---------------- ESTADO INICIAL ---------------- | |
| if "pos" not in st.session_state: | |
| reset_game() | |
| # ---------------- CSS DINÂMICO ---------------- | |
| velocidade = max(0.8, 3 - (st.session_state.pos * 0.4)) | |
| st.markdown(f""" | |
| <style> | |
| .main-game-container {{ | |
| border: 4px solid #FFD700; | |
| border-radius: 25px; | |
| overflow: hidden; | |
| height: 420px; | |
| background-image: url("{PISTA_URL}"); | |
| background-size: cover; | |
| background-repeat: repeat-y; | |
| animation: moveTrack {velocidade}s linear infinite; | |
| display: flex; | |
| justify-content: center; | |
| align-items: center; | |
| }} | |
| @keyframes moveTrack {{ | |
| from {{ background-position: center 0px; }} | |
| to {{ background-position: center 800px; }} | |
| }} | |
| .formula-car {{ | |
| width: 220px; | |
| }} | |
| .quiz-card {{ | |
| background: white; | |
| padding: 25px; | |
| border-radius: 20px; | |
| box-shadow: 0 5px 15px rgba(0,0,0,0.1); | |
| }} | |
| </style> | |
| """, unsafe_allow_html=True) | |
| # ---------------- LAYOUT ---------------- | |
| st.title(f"🏎️ DETRAN Pro Simulator") | |
| st.caption(f"Jogador: {st.session_state.user}") | |
| col1, col2 = st.columns([2,1]) | |
| # ---------------- AREA VISUAL ---------------- | |
| with col1: | |
| st.markdown(""" | |
| <div class="main-game-container"> | |
| <img src="https://i.postimg.cc/mD8zM9f8/top-view-formula-1-racing-car-isolated-on-white-background-3d-rendering-removebg-preview.png" class="formula-car"> | |
| </div> | |
| """, unsafe_allow_html=True) | |
| # ---------------- QUIZ ---------------- | |
| with col2: | |
| st.markdown("<div class='quiz-card'>", unsafe_allow_html=True) | |
| if st.session_state.vidas <= 0: | |
| st.error("💥 GAME OVER") | |
| if st.button("🔁 Reiniciar"): | |
| reset_game() | |
| st.rerun() | |
| elif st.session_state.pos < len(st.session_state.questoes): | |
| q = st.session_state.questoes[st.session_state.pos] | |
| st.subheader(f"Checkpoint {st.session_state.pos + 1}/{len(st.session_state.questoes)}") | |
| st.write(q["pergunta"]) | |
| for opcao in q["opcoes"]: | |
| if st.button(opcao, use_container_width=True): | |
| if opcao == q["correta"]: | |
| st.toast("🔥 ACERTOU!", icon="🏎️") | |
| st.session_state.pos += 1 | |
| time.sleep(0.3) | |
| st.rerun() | |
| else: | |
| st.session_state.vidas -= 1 | |
| st.session_state.erros += 1 | |
| st.error("❌ Resposta incorreta! -1 vida") | |
| else: | |
| tempo_final = round(time.time() - st.session_state.start_time, 2) | |
| ranking = load_ranking() | |
| ranking.append({ | |
| "usuario": st.session_state.user, | |
| "tempo": tempo_final, | |
| "erros": st.session_state.erros | |
| }) | |
| ranking = sorted(ranking, key=lambda x: (x["tempo"], x["erros"])) | |
| save_ranking(ranking) | |
| st.success("🏆 Você completou o trajeto!") | |
| st.info(f"⏱️ Tempo Final: {tempo_final}s | ❌ Erros: {st.session_state.erros}") | |
| if st.button("🏁 Jogar novamente"): | |
| reset_game() | |
| st.rerun() | |
| # Telemetria | |
| st.divider() | |
| st.progress(st.session_state.pos / len(st.session_state.questoes)) | |
| st.write(f"❤️ Vidas: {st.session_state.vidas}") | |
| st.write(f"❌ Erros: {st.session_state.erros}") | |
| st.markdown("</div>", unsafe_allow_html=True) | |
| # ---------------- RANKING ---------------- | |
| st.divider() | |
| st.subheader("🏆 Ranking Global") | |
| ranking = load_ranking() | |
| if ranking: | |
| for i, r in enumerate(ranking[:10]): | |
| st.write(f"{i+1}º - {r['usuario']} | {r['tempo']}s | {r['erros']} erros") | |
| else: | |
| st.write("Nenhum resultado ainda.") | |
| # ---------------- RODAPÉ ---------------- | |
| tempo_atual = round(time.time() - st.session_state.start_time, 1) | |
| st.caption(f"⏱️ Tempo atual: {tempo_atual}s") |