Repository navigation
Expand file tree
/
Copy pathgame.py
More file actions
101 lines (85 loc) · 3.48 KB
/
Copy pathgame.py
File metadata and controls
101 lines (85 loc) · 3.48 KB
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
import pygame
import json
import random
import os
import time
# Durée limite selon le niveau
TIME_LIMITS = {
"facile": 120,
"intermediaire": 90,
"difficile": 60,
}
def load_questions(theme, difficulty):
path = os.path.join("data", f"{theme.lower()}.json")
with open(path, encoding='utf-8') as f:
data = json.load(f)
# Gestion des différents formats dans les fichiers JSON
if difficulty.lower() in data:
return data[difficulty.lower()]
elif difficulty in data:
return data[difficulty]
else:
# Fallback: on utilise le premier niveau disponible
return list(data.values())[0]
def draw_text_center(screen, text, y, font, color=(255, 255, 255)):
rendered = font.render(text, True, color)
rect = rendered.get_rect(center=(400, y))
screen.blit(rendered, rect)
def run_quiz(screen, theme, difficulty):
questions = load_questions(theme, difficulty)
random.shuffle(questions)
font = pygame.font.SysFont(None, 32)
score = 0
current_q = 0
total_questions = min(10, len(questions)) # Pour éviter les erreurs si moins de 10 questions
start_time = time.time()
selected = None
answer_feedback = ""
feedback_color = (255, 255, 255)
while current_q < total_questions:
elapsed = time.time() - start_time
time_left = TIME_LIMITS[difficulty.lower()] - int(elapsed)
if time_left <= 0:
break
screen.fill((30, 30, 30))
question = questions[current_q]
# Afficher question
draw_text_center(screen, f"Question {current_q+1} : {question['question']}", 100, font)
# Afficher options
buttons = []
for i, option in enumerate(question['options']):
rect = pygame.Rect(150, 180 + i * 70, 500, 50)
buttons.append((rect, option))
color = (80, 80, 200)
pygame.draw.rect(screen, color, rect)
draw_text_center(screen, option, rect.centery, font)
# Afficher score et temps
draw_text_center(screen, f"Score : {score} Temps restant : {time_left}s", 40, font)
# Feedback visuel
if answer_feedback:
draw_text_center(screen, answer_feedback, 470, font, feedback_color)
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
elif event.type == pygame.MOUSEBUTTONDOWN and selected is None:
for rect, option in buttons:
if rect.collidepoint(event.pos):
selected = option
if selected == question['answer']:
score += 1
answer_feedback = "Bonne réponse !"
feedback_color = (0, 255, 0)
# pygame.mixer.Sound("correct.wav").play() # Ajout manuel plus tard
else:
answer_feedback = f"Mauvaise réponse ! (Réponse : {question['answer']})"
feedback_color = (255, 0, 0)
# pygame.mixer.Sound("wrong.wav").play()
pygame.display.flip()
pygame.time.wait(1000)
selected = None
answer_feedback = ""
current_q += 1
break
return score # Retour du score uniquement