Spaces:
Runtime error
Runtime error
import pygame | |
import random | |
import time | |
import pandas as pd | |
# Inicializa o Pygame | |
pygame.init() | |
# Configurações da tela | |
screen = pygame.display.set_mode((640, 480)) | |
screen_width, screen_height = screen.get_size() | |
pygame.display.set_caption("A Jornada do Discípulo") | |
# Cores | |
white = (255, 255, 255) | |
black = (0, 0, 0) | |
green = (0, 255, 0) | |
red = (255, 0, 0) | |
yellow = (255, 255, 0) | |
# Configurações do jogo | |
high_scores = [] | |
score_file = "assets/high_scores.txt" | |
question_file = "assets/questions.xlsx" | |
# beep_sound = "assets/beep.wav" # Comentado, não usaremos som | |
congratulations_image_file = "assets/img0.png" # Caminho para a imagem de parabéns | |
score_decrement_interval = 5 # Intervalo de tempo em segundos para subtrair pontos do score | |
player_base_speed = 5 # Velocidade base do jogador | |
player_speed_increment = 0.5 # Incremento de velocidade do jogador | |
player_speed = player_base_speed # Velocidade atual do jogador | |
player_radius = 25 # Raio inicial do jogador | |
correct_answers_streak = 0 # Contador de perguntas corretas consecutivas | |
correct_answers = 0 # Contador total de perguntas corretas | |
player_velocity = pygame.math.Vector2(0, 0) # Velocidade inicial do jogador | |
game_start_time = 0 # Tempo inicial da partida | |
start_time = 0 # Tempo inicial para o cronômetro de score | |
questions = [] # Lista de perguntas | |
answered_questions = set() # Perguntas já respondidas corretamente | |
collision_count = 0 # Contador de colisões com obstáculos | |
# Carregar a imagem de parabéns | |
congratulations_image = pygame.image.load(congratulations_image_file) | |
congratulations_image = pygame.transform.scale(congratulations_image, (screen_width, screen_height)) | |
# Semente aleatória para garantir aleatoriedade | |
random.seed(time.time()) | |
def load_high_scores(): | |
try: | |
with open(score_file, "r", encoding="utf-8") as f: | |
return [line.strip().split(",") for line in f.readlines() if line.strip()] | |
except FileNotFoundError: | |
return [] | |
def save_high_scores(scores): | |
with open(score_file, "w", encoding="utf-8") as f: | |
for score in scores: | |
f.write(",".join(score) + "\n") | |
def load_questions(): | |
try: | |
df = pd.read_excel(question_file) | |
return df.to_dict(orient="records") | |
except FileNotFoundError: | |
return [] | |
# Desenha texto na tela | |
def draw_text(surface, text, size, x, y, align="center"): | |
font = pygame.font.Font(None, size) | |
text_surface = font.render(text, True, white) | |
text_rect = text_surface.get_rect() | |
if align == "center": | |
text_rect.midtop = (x, y) | |
elif align == "left": | |
text_rect.topleft = (x, y) | |
surface.blit(text_surface, text_rect) | |
# Exibe a pergunta na tela | |
def show_question(question): | |
draw_text(screen, question["pergunta"], 24, screen_width // 2, 10, align="center") | |
draw_text(screen, "1: " + question["opcao_1"], 24, screen_width // 2, 40, align="center") | |
draw_text(screen, "2: " + question["opcao_2"], 24, screen_width // 2, 70, align="center") | |
draw_text(screen, "3: " + question["opcao_3"], 24, screen_width // 2, 100, align="center") | |
draw_text(screen, "4: " + question["opcao_4"], 24, screen_width // 2, 130, align="center") | |
def run_game(): | |
global player_speed, correct_answers_streak, correct_answers, player_velocity, game_start_time, start_time, questions, answered_questions, collision_count | |
high_scores = load_high_scores() | |
questions = load_questions() | |
player_pos = pygame.math.Vector2(screen_width // 2, screen_height // 2) | |
player_velocity = pygame.math.Vector2(0, 0) | |
player_health = 3 | |
score = 0 | |
level_num = 1 | |
collision_count = 0 | |
obstacles = [] | |
health_items = [] | |
current_question = None | |
answer_start_time = None | |
def get_new_question(): | |
available_questions = [q for q in questions if q["pergunta"] not in answered_questions] | |
if available_questions: | |
return random.choice(available_questions) | |
return None | |
clock = pygame.time.Clock() | |
game_start_time = time.time() | |
start_time = game_start_time | |
running = True | |
while running: | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
running = False | |
elif event.type == pygame.KEYDOWN: | |
if event.key == pygame.K_ESCAPE: | |
running = False | |
elif current_question and event.key in [pygame.K_1, pygame.K_2, pygame.K_3, pygame.K_4]: | |
selected_option = int(event.unicode) | |
if selected_option == current_question["resposta"]: | |
correct_answers_streak += 1 | |
correct_answers += 1 | |
score += 10 * level_num | |
# beep.play() # Comentado, não usaremos som | |
answer_start_time = None | |
answered_questions.add(current_question["pergunta"]) | |
current_question = get_new_question() | |
else: | |
correct_answers_streak = 0 | |
player_health -= 1 | |
if player_health <= 0: | |
running = False | |
keys = pygame.key.get_pressed() | |
if keys[pygame.K_LEFT]: | |
player_velocity.x = -player_speed | |
elif keys[pygame.K_RIGHT]: | |
player_velocity.x = player_speed | |
else: | |
player_velocity.x = 0 | |
if keys[pygame.K_UP]: | |
player_velocity.y = -player_speed | |
elif keys[pygame.K_DOWN]: | |
player_velocity.y = player_speed | |
else: | |
player_velocity.y = 0 | |
player_pos += player_velocity | |
if player_pos.x < 0: | |
player_pos.x = 0 | |
elif player_pos.x > screen_width: | |
player_pos.x = screen_width | |
if player_pos.y < 0: | |
player_pos.y = 0 | |
elif player_pos.y > screen_height: | |
player_pos.y = screen_height | |
if time.time() - start_time >= score_decrement_interval: | |
score -= 1 | |
start_time = time.time() | |
if not current_question: | |
current_question = get_new_question() | |
answer_start_time = time.time() | |
if current_question and time.time() - answer_start_time > 10: | |
current_question = get_new_question() | |
answer_start_time = time.time() | |
screen.fill(black) | |
pygame.draw.circle(screen, green, (int(player_pos.x), int(player_pos.y)), player_radius) | |
for obstacle in obstacles: | |
pygame.draw.rect(screen, red, obstacle) | |
for health_item in health_items: | |
pygame.draw.rect(screen, yellow, health_item) | |
draw_text(screen, f"Pontos: {score}", 24, 10, 10, align="left") | |
draw_text(screen, f"Vidas: {player_health}", 24, 10, 40, align="left") | |
draw_text(screen, f"Nível: {level_num}", 24, 10, 70, align="left") | |
draw_text(screen, f"Colisões: {collision_count}", 24, 10, 100, align="left") | |
if current_question: | |
show_question(current_question) | |
pygame.display.flip() | |
clock.tick(30) | |
date_str = time.strftime("%Y-%m-%d %H:%M:%S") | |
high_scores.append([str(score), date_str]) | |
high_scores = sorted(high_scores, key=lambda x: int(x[0]), reverse=True)[:10] | |
save_high_scores(high_scores) | |
duration_seconds = int(time.time() - game_start_time) | |
duration_minutes = duration_seconds // 60 | |
duration_seconds = duration_seconds % 60 | |
duration_formatted = f"{duration_minutes:02}:{duration_seconds:02}" | |
screen.fill(black) | |
draw_text(screen, "Game Over", 48, screen_width // 2, screen_height // 2 - 100, align="center") | |
draw_text(screen, f"Pontuação Final: {score}", 36, screen_width // 2, screen_height // 2 - 50, align="center") | |
draw_text(screen, f"Nível Final: {level_num}", 36, screen_width // 2, screen_height // 2, align="center") | |
draw_text(screen, f"Duração da Partida: {duration_formatted}", 36, screen_width // 2, screen_height // 2 + 50, align="center") | |
draw_text(screen, f"Respostas Corretas: {correct_answers}", 36, screen_width // 2, screen_height // 2 + 100, align="center") | |
draw_text(screen, f"Colisões: {collision_count}", 36, screen_width // 2, screen_height // 2 + 150, align="center") | |
pygame.display.flip() | |
time.sleep(30) | |
pygame.quit() | |
print("Jogo encerrado.") # Mensagem de depuração | |