Spaces:
Runtime error
Runtime error
Add game files and setup for Streamlit deployment
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ import streamlit as st
|
|
2 |
import pygame
|
3 |
import numpy as np
|
4 |
from pygame.locals import *
|
|
|
5 |
|
6 |
# Inicializa o Pygame
|
7 |
pygame.init()
|
@@ -45,6 +46,12 @@ def run_game():
|
|
45 |
screen.fill((0, 0, 0))
|
46 |
pygame.draw.rect(screen, player_color, (player_pos[0], player_pos[1], player_size, player_size))
|
47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
pygame.display.flip()
|
49 |
clock.tick(30)
|
50 |
|
|
|
2 |
import pygame
|
3 |
import numpy as np
|
4 |
from pygame.locals import *
|
5 |
+
from PIL import Image
|
6 |
|
7 |
# Inicializa o Pygame
|
8 |
pygame.init()
|
|
|
46 |
screen.fill((0, 0, 0))
|
47 |
pygame.draw.rect(screen, player_color, (player_pos[0], player_pos[1], player_size, player_size))
|
48 |
|
49 |
+
# Converter a tela do Pygame para uma imagem e exibir no Streamlit
|
50 |
+
image = pygame.surfarray.array3d(pygame.display.get_surface())
|
51 |
+
image = np.rot90(image, 3)
|
52 |
+
image = np.fliplr(image)
|
53 |
+
st.image(image, caption='Jogo em execução', use_column_width=True)
|
54 |
+
|
55 |
pygame.display.flip()
|
56 |
clock.tick(30)
|
57 |
|
game.py
CHANGED
@@ -7,7 +7,7 @@ import pandas as pd
|
|
7 |
pygame.init()
|
8 |
|
9 |
# Configurações da tela
|
10 |
-
screen = pygame.display.set_mode((
|
11 |
screen_width, screen_height = screen.get_size()
|
12 |
pygame.display.set_caption("A Jornada do Discípulo")
|
13 |
|
@@ -22,7 +22,7 @@ yellow = (255, 255, 0)
|
|
22 |
high_scores = []
|
23 |
score_file = "assets/high_scores.txt"
|
24 |
question_file = "assets/questions.xlsx"
|
25 |
-
beep_sound = "assets/beep.wav"
|
26 |
congratulations_image_file = "assets/img0.png" # Caminho para a imagem de parabéns
|
27 |
score_decrement_interval = 5 # Intervalo de tempo em segundos para subtrair pontos do score
|
28 |
player_base_speed = 5 # Velocidade base do jogador
|
@@ -38,10 +38,6 @@ questions = [] # Lista de perguntas
|
|
38 |
answered_questions = set() # Perguntas já respondidas corretamente
|
39 |
collision_count = 0 # Contador de colisões com obstáculos
|
40 |
|
41 |
-
# Carregar som de beep
|
42 |
-
pygame.mixer.init()
|
43 |
-
beep = pygame.mixer.Sound(beep_sound)
|
44 |
-
|
45 |
# Carregar a imagem de parabéns
|
46 |
congratulations_image = pygame.image.load(congratulations_image_file)
|
47 |
congratulations_image = pygame.transform.scale(congratulations_image, (screen_width, screen_height))
|
@@ -58,376 +54,165 @@ def load_high_scores():
|
|
58 |
|
59 |
def save_high_scores(scores):
|
60 |
with open(score_file, "w", encoding="utf-8") as f:
|
61 |
-
for score
|
62 |
-
f.write(
|
63 |
|
64 |
def load_questions():
|
65 |
-
global questions
|
66 |
try:
|
67 |
df = pd.read_excel(question_file)
|
68 |
-
|
69 |
except FileNotFoundError:
|
70 |
-
|
71 |
-
{"pergunta": "Qual é o maior mandamento?", "opcao_1": "Amar a Deus sobre todas as coisas", "opcao_2": "Não matar", "opcao_3": "Guardar o sábado", "opcao_4": "Não roubar", "resposta": "1"}
|
72 |
-
]
|
73 |
-
|
74 |
-
def draw_text(screen, text, size, x, y, align="center", max_width=None):
|
75 |
-
font = pygame.font.Font(pygame.font.match_font('arial'), size)
|
76 |
-
if max_width:
|
77 |
-
words = text.split(' ')
|
78 |
-
lines = []
|
79 |
-
current_line = words[0]
|
80 |
-
for word in words[1:]:
|
81 |
-
if font.size(current_line + ' ' + word)[0] <= max_width:
|
82 |
-
current_line += ' ' + word
|
83 |
-
else:
|
84 |
-
lines.append(current_line)
|
85 |
-
current_line = word
|
86 |
-
lines.append(current_line)
|
87 |
-
for i, line in enumerate(lines):
|
88 |
-
text_surface = font.render(line, True, white)
|
89 |
-
text_rect = text_surface.get_rect()
|
90 |
-
if align == "center":
|
91 |
-
text_rect.midtop = (x, y + i * size)
|
92 |
-
elif align == "left":
|
93 |
-
text_rect.topleft = (x, y + i * size)
|
94 |
-
elif align == "right":
|
95 |
-
text_rect.topright = (x, y + i * size)
|
96 |
-
screen.blit(text_surface, text_rect)
|
97 |
-
else:
|
98 |
-
text_surface = font.render(text, True, white)
|
99 |
-
text_rect = text_surface.get_rect()
|
100 |
-
if align == "center":
|
101 |
-
text_rect.midtop = (x, y)
|
102 |
-
elif align == "left":
|
103 |
-
text_rect.topleft = (x, y)
|
104 |
-
elif align == "right":
|
105 |
-
text_rect.topright = (x, y)
|
106 |
-
screen.blit(text_surface, text_rect)
|
107 |
-
|
108 |
-
def move_player(player, speed):
|
109 |
-
global player_velocity, game_start_time
|
110 |
-
keys = pygame.key.get_pressed()
|
111 |
-
if keys[pygame.K_LEFT]:
|
112 |
-
player_velocity.x = -speed
|
113 |
-
elif keys[pygame.K_RIGHT]:
|
114 |
-
player_velocity.x = speed
|
115 |
-
else:
|
116 |
-
player_velocity.x = 0 # Parar se não estiver pressionando esquerda ou direita
|
117 |
-
|
118 |
-
if keys[pygame.K_UP]:
|
119 |
-
player_velocity.y = -speed
|
120 |
-
elif keys[pygame.K_DOWN]:
|
121 |
-
player_velocity.y = speed
|
122 |
-
else:
|
123 |
-
player_velocity.y = 0 # Parar se não estiver pressionando cima ou baixo
|
124 |
-
|
125 |
-
# Inicia o cronômetro do jogo quando o jogador se move pela primeira vez
|
126 |
-
if player_velocity.x != 0 or player_velocity.y != 0:
|
127 |
-
if game_start_time == 0:
|
128 |
-
game_start_time = time.time()
|
129 |
-
|
130 |
-
# Atualiza a posição do jogador com base na velocidade
|
131 |
-
player.x += player_velocity.x
|
132 |
-
player.y += player_velocity.y
|
133 |
-
|
134 |
-
# Limita o jogador aos limites da tela
|
135 |
-
if player.left < 0:
|
136 |
-
player.left = 0
|
137 |
-
player_velocity.x = 0
|
138 |
-
if player.right > screen_width:
|
139 |
-
player.right = screen_width
|
140 |
-
player_velocity.x = 0
|
141 |
-
if player.top < 100: # Espaço reservado para o texto da pergunta
|
142 |
-
player.top = 100
|
143 |
-
player_velocity.y = 0
|
144 |
-
if player.bottom > screen_height - 100: # Espaço reservado para o texto captado
|
145 |
-
player.bottom = screen_height - 100
|
146 |
-
player_velocity.y = 0
|
147 |
-
|
148 |
-
def create_level(level_num):
|
149 |
-
num_obstacles = 5 + level_num # Incrementa a quantidade de obstáculos com o nível
|
150 |
-
items = [pygame.Rect(random.randint(0, screen_width - 20), random.randint(100, screen_height - 120), 20, 20) for _ in range(5)]
|
151 |
-
obstacles = [pygame.Rect(random.randint(0, screen_width - 20), random.randint(100, screen_height - 120), 20, 20) for _ in range(num_obstacles)]
|
152 |
-
item_speeds = [random.uniform(1.0, 2.0) + 0.5 * level_num for _ in range(len(items))] # Velocidade inicial mais aleatória e aumentada com o nível
|
153 |
-
obstacle_speeds = [random.uniform(1.0, 2.0) + 0.5 * level_num for _ in range(len(obstacles))] # Velocidade inicial mais aleatória e aumentada com o nível
|
154 |
-
item_directions = [pygame.math.Vector2(random.choice([-1, 1]), random.choice([-1, 1])) for _ in range(len(items))] # Direção inicial aleatória
|
155 |
-
obstacle_directions = [pygame.math.Vector2(random.choice([-1, 1]), random.choice([-1, 1])) for _ in range(len(obstacles))] # Direção inicial aleatória
|
156 |
-
health_items = [] # Inicialmente, sem itens de saúde
|
157 |
-
message = f"Bem-vindo ao Nível {level_num}!"
|
158 |
-
return items, obstacles, item_speeds, obstacle_speeds, item_directions, obstacle_directions, health_items, message
|
159 |
-
|
160 |
-
def move_objects(objects, speeds, directions):
|
161 |
-
for i, obj in enumerate(objects):
|
162 |
-
# Atualiza a posição com base na velocidade e direção
|
163 |
-
obj.x += speeds[i] * directions[i].x
|
164 |
-
obj.y += speeds[i] * directions[i].y
|
165 |
-
|
166 |
-
# Altera a direção se atingir as bordas
|
167 |
-
if obj.left < 0 or obj.right > screen_width:
|
168 |
-
directions[i].x *= -1
|
169 |
-
if obj.top < 100 or obj.bottom > screen_height - 100: # Limita o movimento na área de jogo
|
170 |
-
directions[i].y *= -1
|
171 |
-
|
172 |
-
# Verifica colisão com outros objetos
|
173 |
-
for j in range(i + 1, len(objects)):
|
174 |
-
if obj.colliderect(objects[j]):
|
175 |
-
directions[i].x *= -1
|
176 |
-
directions[i].y *= -1
|
177 |
-
directions[j].x *= -1
|
178 |
-
directions[j].y *= -1
|
179 |
-
|
180 |
-
return objects, speeds, directions
|
181 |
-
|
182 |
-
def handle_collisions(items, obstacles, item_speeds, obstacle_speeds, item_directions, obstacle_directions):
|
183 |
-
for i, item in enumerate(items):
|
184 |
-
for j, obstacle in enumerate(obstacles):
|
185 |
-
if item.colliderect(obstacle):
|
186 |
-
item_directions[i].x *= -1
|
187 |
-
item_directions[i].y *= -1
|
188 |
-
obstacle_directions[j].x *= -1
|
189 |
-
obstacle_directions[j].y *= -1
|
190 |
-
|
191 |
-
def show_start_screen():
|
192 |
-
start_time = time.time()
|
193 |
-
countdown = 5
|
194 |
-
font = pygame.font.Font(pygame.font.match_font('arial'), 36)
|
195 |
-
while True:
|
196 |
-
screen.fill(black)
|
197 |
-
current_time = time.time()
|
198 |
-
elapsed_time = current_time - start_time
|
199 |
-
remaining_time = max(0, countdown - int(elapsed_time))
|
200 |
-
|
201 |
-
if remaining_time == 0:
|
202 |
-
break
|
203 |
-
|
204 |
-
draw_text(screen, f"Preparando para começar! ({remaining_time})", 36, screen_width // 2, screen_height // 2 - 50, align="center")
|
205 |
-
|
206 |
-
pygame.display.flip()
|
207 |
-
pygame.time.Clock().tick(30)
|
208 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
209 |
def show_question(question):
|
210 |
-
draw_text(screen, question["pergunta"], 24, screen_width // 2,
|
211 |
-
draw_text(screen,
|
212 |
-
draw_text(screen,
|
213 |
-
draw_text(screen,
|
214 |
-
draw_text(screen,
|
215 |
-
|
216 |
-
def
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
228 |
return None
|
229 |
-
question = random.choice(available_questions)
|
230 |
-
return question
|
231 |
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
time.sleep(30)
|
236 |
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
# Carregar os melhores scores
|
250 |
-
high_scores = load_high_scores()
|
251 |
-
|
252 |
-
# Tela Inicial
|
253 |
-
show_start_screen()
|
254 |
-
|
255 |
-
# Loop principal do jogo
|
256 |
-
running = True
|
257 |
-
clock = pygame.time.Clock()
|
258 |
-
|
259 |
-
print("Iniciando o loop principal...") # Mensagem de depuração
|
260 |
-
|
261 |
-
while running:
|
262 |
-
for event in pygame.event.get():
|
263 |
-
if event.type == pygame.QUIT:
|
264 |
-
running = False
|
265 |
-
elif event.type == pygame.KEYDOWN:
|
266 |
-
if current_question:
|
267 |
-
if event.key == pygame.K_1:
|
268 |
-
answer = "1"
|
269 |
-
elif event.key == pygame.K_2:
|
270 |
-
answer = "2"
|
271 |
-
elif event.key == pygame.K_3:
|
272 |
-
answer = "3"
|
273 |
-
elif event.key == pygame.K_4:
|
274 |
-
answer = "4"
|
275 |
-
else:
|
276 |
-
answer = None
|
277 |
-
|
278 |
-
if answer:
|
279 |
-
if check_answer(current_question, answer):
|
280 |
-
score += 3
|
281 |
-
correct_answers += 1
|
282 |
correct_answers_streak += 1
|
|
|
|
|
|
|
|
|
283 |
answered_questions.add(current_question["pergunta"])
|
284 |
-
|
285 |
-
obstacles_to_remove = 1
|
286 |
-
if level_num > 5:
|
287 |
-
obstacles_to_remove = 2
|
288 |
-
if level_num > 8:
|
289 |
-
obstacles_to_remove = 3
|
290 |
-
if obstacles:
|
291 |
-
for _ in range(min(obstacles_to_remove, len(obstacles))):
|
292 |
-
idx = random.randint(0, len(obstacles) - 1)
|
293 |
-
obstacles.pop(idx)
|
294 |
-
obstacle_speeds.pop(idx)
|
295 |
-
obstacle_directions.pop(idx)
|
296 |
-
if correct_answers_streak >= 3:
|
297 |
-
player_health += 1
|
298 |
-
correct_answers_streak = 0 # Reinicia a contagem de respostas corretas
|
299 |
-
current_question = None
|
300 |
-
beep.play() # Beep sonoro
|
301 |
-
if len(answered_questions) == len(questions):
|
302 |
-
show_congratulations_screen()
|
303 |
-
running = False
|
304 |
else:
|
305 |
-
correct_answers_streak = 0
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
-
print(f"Colidiu com obstáculo. Vidas restantes: {player_health}") # Mensagem de depuração
|
339 |
-
if player_health <= 0 or score <= 0:
|
340 |
-
running = False
|
341 |
-
message = "Game Over"
|
342 |
-
print(message) # Mensagem de depuração
|
343 |
-
|
344 |
-
# Verifica colisão com itens de saúde
|
345 |
-
for health_item in health_items[:]:
|
346 |
-
if player.colliderect(health_item):
|
347 |
-
health_items.remove(health_item)
|
348 |
-
player_health += 1
|
349 |
-
|
350 |
-
# Verifica se o nível foi concluído
|
351 |
-
if not items:
|
352 |
-
level_num += 1
|
353 |
-
items, obstacles, item_speeds, obstacle_speeds, item_directions, obstacle_directions, health_items, message = create_level(level_num)
|
354 |
-
player_radius += 2 # Aumenta o raio do jogador a cada nível
|
355 |
-
player = pygame.Rect(player.x, player.y, player_radius * 2, player_radius * 2)
|
356 |
-
player_speed = player_base_speed + level_num * player_speed_increment # Incrementa a velocidade do jogador com o nível
|
357 |
-
print(f"Iniciando nível {level_num}") # Mensagem de depuração
|
358 |
-
|
359 |
-
# Movimento dos itens e obstáculos
|
360 |
-
items, item_speeds, item_directions = move_objects(items, item_speeds, item_directions)
|
361 |
-
obstacles, obstacle_speeds, obstacle_directions = move_objects(obstacles, obstacle_speeds, obstacle_directions)
|
362 |
-
|
363 |
-
# Tratamento de colisões entre itens e obstáculos
|
364 |
-
handle_collisions(items, obstacles, item_speeds, obstacle_speeds, item_directions, obstacle_directions)
|
365 |
-
|
366 |
-
# Aparição aleatória de itens de saúde
|
367 |
-
if level_num > 5 and time.time() - health_spawn_time > random.randint(10, 20):
|
368 |
-
health_items = [pygame.Rect(random.randint(0, screen_width - 20), random.randint(100, screen_height - 120), 20, 20)]
|
369 |
-
health_spawn_time = time.time()
|
370 |
-
|
371 |
-
# Remover itens de saúde após 5 segundos
|
372 |
-
if health_items and time.time() - health_spawn_time > 5:
|
373 |
-
health_items = []
|
374 |
-
|
375 |
-
# Cronômetro para subtrair pontos do score a cada intervalo de tempo
|
376 |
-
current_time = time.time()
|
377 |
-
if current_time - start_time >= score_decrement_interval:
|
378 |
-
if score > 0: # Apenas decrementar se o score for maior que 0
|
379 |
score -= 1
|
380 |
-
|
381 |
-
|
382 |
-
if
|
383 |
-
|
384 |
-
|
385 |
-
|
386 |
-
|
387 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
388 |
screen.fill(black)
|
389 |
-
|
390 |
-
|
391 |
-
|
392 |
-
|
393 |
-
|
394 |
-
|
395 |
-
pygame.draw.rect(screen, yellow, health_item)
|
396 |
-
|
397 |
-
# Exibe a pontuação, número de vidas, nível atual e contagem de colisões
|
398 |
-
draw_text(screen, f"Pontos: {score}", 24, 10, 10, align="left")
|
399 |
-
draw_text(screen, f"Vidas: {player_health}", 24, 10, 40, align="left")
|
400 |
-
draw_text(screen, f"Nível: {level_num}", 24, 10, 70, align="left")
|
401 |
-
draw_text(screen, f"Colisões: {collision_count}", 24, 10, 100, align="left")
|
402 |
-
|
403 |
-
# Exibe a pergunta na parte superior da tela
|
404 |
-
if current_question:
|
405 |
-
show_question(current_question)
|
406 |
-
|
407 |
pygame.display.flip()
|
408 |
-
|
409 |
-
|
410 |
-
|
411 |
-
|
412 |
-
high_scores.append([str(score), date_str])
|
413 |
-
high_scores = sorted(high_scores, key=lambda x: int(x[0]), reverse=True)[:10]
|
414 |
-
save_high_scores(high_scores)
|
415 |
-
|
416 |
-
# Exibe a tela de Game Over com os resultados
|
417 |
-
duration_seconds = int(time.time() - game_start_time)
|
418 |
-
duration_minutes = duration_seconds // 60
|
419 |
-
duration_seconds = duration_seconds % 60
|
420 |
-
duration_formatted = f"{duration_minutes:02}:{duration_seconds:02}"
|
421 |
-
|
422 |
-
screen.fill(black)
|
423 |
-
draw_text(screen, "Game Over", 48, screen_width // 2, screen_height // 2 - 100, align="center")
|
424 |
-
draw_text(screen, f"Pontuação Final: {score}", 36, screen_width // 2, screen_height // 2 - 50, align="center")
|
425 |
-
draw_text(screen, f"Nível Final: {level_num}", 36, screen_width // 2, screen_height // 2, align="center")
|
426 |
-
draw_text(screen, f"Duração da Partida: {duration_formatted}", 36, screen_width // 2, screen_height // 2 + 50, align="center")
|
427 |
-
draw_text(screen, f"Respostas Corretas: {correct_answers}", 36, screen_width // 2, screen_height // 2 + 100, align="center")
|
428 |
-
draw_text(screen, f"Colisões: {collision_count}", 36, screen_width // 2, screen_height // 2 + 150, align="center")
|
429 |
-
pygame.display.flip()
|
430 |
-
time.sleep(30)
|
431 |
-
|
432 |
-
pygame.quit()
|
433 |
-
print("Jogo encerrado.") # Mensagem de depuração
|
|
|
7 |
pygame.init()
|
8 |
|
9 |
# Configurações da tela
|
10 |
+
screen = pygame.display.set_mode((640, 480))
|
11 |
screen_width, screen_height = screen.get_size()
|
12 |
pygame.display.set_caption("A Jornada do Discípulo")
|
13 |
|
|
|
22 |
high_scores = []
|
23 |
score_file = "assets/high_scores.txt"
|
24 |
question_file = "assets/questions.xlsx"
|
25 |
+
# beep_sound = "assets/beep.wav" # Comentado, não usaremos som
|
26 |
congratulations_image_file = "assets/img0.png" # Caminho para a imagem de parabéns
|
27 |
score_decrement_interval = 5 # Intervalo de tempo em segundos para subtrair pontos do score
|
28 |
player_base_speed = 5 # Velocidade base do jogador
|
|
|
38 |
answered_questions = set() # Perguntas já respondidas corretamente
|
39 |
collision_count = 0 # Contador de colisões com obstáculos
|
40 |
|
|
|
|
|
|
|
|
|
41 |
# Carregar a imagem de parabéns
|
42 |
congratulations_image = pygame.image.load(congratulations_image_file)
|
43 |
congratulations_image = pygame.transform.scale(congratulations_image, (screen_width, screen_height))
|
|
|
54 |
|
55 |
def save_high_scores(scores):
|
56 |
with open(score_file, "w", encoding="utf-8") as f:
|
57 |
+
for score in scores:
|
58 |
+
f.write(",".join(score) + "\n")
|
59 |
|
60 |
def load_questions():
|
|
|
61 |
try:
|
62 |
df = pd.read_excel(question_file)
|
63 |
+
return df.to_dict(orient="records")
|
64 |
except FileNotFoundError:
|
65 |
+
return []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
+
# Desenha texto na tela
|
68 |
+
def draw_text(surface, text, size, x, y, align="center"):
|
69 |
+
font = pygame.font.Font(None, size)
|
70 |
+
text_surface = font.render(text, True, white)
|
71 |
+
text_rect = text_surface.get_rect()
|
72 |
+
if align == "center":
|
73 |
+
text_rect.midtop = (x, y)
|
74 |
+
elif align == "left":
|
75 |
+
text_rect.topleft = (x, y)
|
76 |
+
surface.blit(text_surface, text_rect)
|
77 |
+
|
78 |
+
# Exibe a pergunta na tela
|
79 |
def show_question(question):
|
80 |
+
draw_text(screen, question["pergunta"], 24, screen_width // 2, 10, align="center")
|
81 |
+
draw_text(screen, "1: " + question["opcao_1"], 24, screen_width // 2, 40, align="center")
|
82 |
+
draw_text(screen, "2: " + question["opcao_2"], 24, screen_width // 2, 70, align="center")
|
83 |
+
draw_text(screen, "3: " + question["opcao_3"], 24, screen_width // 2, 100, align="center")
|
84 |
+
draw_text(screen, "4: " + question["opcao_4"], 24, screen_width // 2, 130, align="center")
|
85 |
+
|
86 |
+
def run_game():
|
87 |
+
global player_speed, correct_answers_streak, correct_answers, player_velocity, game_start_time, start_time, questions, answered_questions, collision_count
|
88 |
+
|
89 |
+
high_scores = load_high_scores()
|
90 |
+
questions = load_questions()
|
91 |
+
|
92 |
+
player_pos = pygame.math.Vector2(screen_width // 2, screen_height // 2)
|
93 |
+
player_velocity = pygame.math.Vector2(0, 0)
|
94 |
+
player_health = 3
|
95 |
+
score = 0
|
96 |
+
level_num = 1
|
97 |
+
collision_count = 0
|
98 |
+
|
99 |
+
obstacles = []
|
100 |
+
health_items = []
|
101 |
+
current_question = None
|
102 |
+
answer_start_time = None
|
103 |
+
|
104 |
+
def get_new_question():
|
105 |
+
available_questions = [q for q in questions if q["pergunta"] not in answered_questions]
|
106 |
+
if available_questions:
|
107 |
+
return random.choice(available_questions)
|
108 |
return None
|
|
|
|
|
109 |
|
110 |
+
clock = pygame.time.Clock()
|
111 |
+
game_start_time = time.time()
|
112 |
+
start_time = game_start_time
|
|
|
113 |
|
114 |
+
running = True
|
115 |
+
while running:
|
116 |
+
for event in pygame.event.get():
|
117 |
+
if event.type == pygame.QUIT:
|
118 |
+
running = False
|
119 |
+
elif event.type == pygame.KEYDOWN:
|
120 |
+
if event.key == pygame.K_ESCAPE:
|
121 |
+
running = False
|
122 |
+
elif current_question and event.key in [pygame.K_1, pygame.K_2, pygame.K_3, pygame.K_4]:
|
123 |
+
selected_option = int(event.unicode)
|
124 |
+
if selected_option == current_question["resposta"]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
correct_answers_streak += 1
|
126 |
+
correct_answers += 1
|
127 |
+
score += 10 * level_num
|
128 |
+
# beep.play() # Comentado, não usaremos som
|
129 |
+
answer_start_time = None
|
130 |
answered_questions.add(current_question["pergunta"])
|
131 |
+
current_question = get_new_question()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
else:
|
133 |
+
correct_answers_streak = 0
|
134 |
+
player_health -= 1
|
135 |
+
if player_health <= 0:
|
136 |
+
running = False
|
137 |
+
|
138 |
+
keys = pygame.key.get_pressed()
|
139 |
+
if keys[pygame.K_LEFT]:
|
140 |
+
player_velocity.x = -player_speed
|
141 |
+
elif keys[pygame.K_RIGHT]:
|
142 |
+
player_velocity.x = player_speed
|
143 |
+
else:
|
144 |
+
player_velocity.x = 0
|
145 |
+
|
146 |
+
if keys[pygame.K_UP]:
|
147 |
+
player_velocity.y = -player_speed
|
148 |
+
elif keys[pygame.K_DOWN]:
|
149 |
+
player_velocity.y = player_speed
|
150 |
+
else:
|
151 |
+
player_velocity.y = 0
|
152 |
+
|
153 |
+
player_pos += player_velocity
|
154 |
+
|
155 |
+
if player_pos.x < 0:
|
156 |
+
player_pos.x = 0
|
157 |
+
elif player_pos.x > screen_width:
|
158 |
+
player_pos.x = screen_width
|
159 |
+
|
160 |
+
if player_pos.y < 0:
|
161 |
+
player_pos.y = 0
|
162 |
+
elif player_pos.y > screen_height:
|
163 |
+
player_pos.y = screen_height
|
164 |
+
|
165 |
+
if time.time() - start_time >= score_decrement_interval:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
166 |
score -= 1
|
167 |
+
start_time = time.time()
|
168 |
+
|
169 |
+
if not current_question:
|
170 |
+
current_question = get_new_question()
|
171 |
+
answer_start_time = time.time()
|
172 |
+
|
173 |
+
if current_question and time.time() - answer_start_time > 10:
|
174 |
+
current_question = get_new_question()
|
175 |
+
answer_start_time = time.time()
|
176 |
+
|
177 |
+
screen.fill(black)
|
178 |
+
|
179 |
+
pygame.draw.circle(screen, green, (int(player_pos.x), int(player_pos.y)), player_radius)
|
180 |
+
|
181 |
+
for obstacle in obstacles:
|
182 |
+
pygame.draw.rect(screen, red, obstacle)
|
183 |
+
for health_item in health_items:
|
184 |
+
pygame.draw.rect(screen, yellow, health_item)
|
185 |
+
|
186 |
+
draw_text(screen, f"Pontos: {score}", 24, 10, 10, align="left")
|
187 |
+
draw_text(screen, f"Vidas: {player_health}", 24, 10, 40, align="left")
|
188 |
+
draw_text(screen, f"Nível: {level_num}", 24, 10, 70, align="left")
|
189 |
+
draw_text(screen, f"Colisões: {collision_count}", 24, 10, 100, align="left")
|
190 |
+
|
191 |
+
if current_question:
|
192 |
+
show_question(current_question)
|
193 |
+
|
194 |
+
pygame.display.flip()
|
195 |
+
clock.tick(30)
|
196 |
+
|
197 |
+
date_str = time.strftime("%Y-%m-%d %H:%M:%S")
|
198 |
+
high_scores.append([str(score), date_str])
|
199 |
+
high_scores = sorted(high_scores, key=lambda x: int(x[0]), reverse=True)[:10]
|
200 |
+
save_high_scores(high_scores)
|
201 |
+
|
202 |
+
duration_seconds = int(time.time() - game_start_time)
|
203 |
+
duration_minutes = duration_seconds // 60
|
204 |
+
duration_seconds = duration_seconds % 60
|
205 |
+
duration_formatted = f"{duration_minutes:02}:{duration_seconds:02}"
|
206 |
+
|
207 |
screen.fill(black)
|
208 |
+
draw_text(screen, "Game Over", 48, screen_width // 2, screen_height // 2 - 100, align="center")
|
209 |
+
draw_text(screen, f"Pontuação Final: {score}", 36, screen_width // 2, screen_height // 2 - 50, align="center")
|
210 |
+
draw_text(screen, f"Nível Final: {level_num}", 36, screen_width // 2, screen_height // 2, align="center")
|
211 |
+
draw_text(screen, f"Duração da Partida: {duration_formatted}", 36, screen_width // 2, screen_height // 2 + 50, align="center")
|
212 |
+
draw_text(screen, f"Respostas Corretas: {correct_answers}", 36, screen_width // 2, screen_height // 2 + 100, align="center")
|
213 |
+
draw_text(screen, f"Colisões: {collision_count}", 36, screen_width // 2, screen_height // 2 + 150, align="center")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
214 |
pygame.display.flip()
|
215 |
+
time.sleep(30)
|
216 |
+
|
217 |
+
pygame.quit()
|
218 |
+
print("Jogo encerrado.") # Mensagem de depuração
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|