-
Notifications
You must be signed in to change notification settings - Fork 12.4k
/
Copy pathMemory_game.py
112 lines (93 loc) · 3.71 KB
/
Memory_game.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
import random
import pygame
import sys
# Initialisation de pygame
pygame.init()
# Définir les couleurs
WHITE = (255, 255, 255)
PASTEL_PINK = (255, 182, 193)
PINK = (255, 105, 180)
LIGHT_PINK = (255, 182, 193)
GREY = (169, 169, 169)
# Définir les dimensions de la fenêtre
WIDTH = 600
HEIGHT = 600
FPS = 30
CARD_SIZE = 100
# Créer la fenêtre
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Memory Game : Les Préférences de Malak")
# Charger les polices
font = pygame.font.Font(None, 40)
font_small = pygame.font.Font(None, 30)
# Liste des questions et réponses (préférences)
questions = [
{"question": "Quelle est sa couleur préférée ?", "réponse": "Rose", "image": "rose.jpg"},
{"question": "Quel est son plat préféré ?", "réponse": "Pizza", "image": "pizza.jpg"},
{"question": "Quel est son animal préféré ?", "réponse": "Chat", "image": "chat.jpg"},
{"question": "Quel est son film préféré ?", "réponse": "La La Land", "image": "lalaland.jpg"}
]
# Créer les cartes avec des questions et réponses
cards = []
for q in questions:
cards.append(q["réponse"])
cards.append(q["réponse"])
# Mélanger les cartes
random.shuffle(cards)
# Créer un dictionnaire pour les positions des cartes
card_positions = [(x * CARD_SIZE, y * CARD_SIZE) for x in range(4) for y in range(4)]
# Fonction pour afficher le texte
def display_text(text, font, color, x, y):
text_surface = font.render(text, True, color)
screen.blit(text_surface, (x, y))
# Fonction pour dessiner les cartes
def draw_cards():
for idx, pos in enumerate(card_positions):
x, y = pos
if visible[idx]:
pygame.draw.rect(screen, WHITE, pygame.Rect(x, y, CARD_SIZE, CARD_SIZE))
display_text(cards[idx], font, PINK, x + 10, y + 30)
else:
pygame.draw.rect(screen, LIGHT_PINK, pygame.Rect(x, y, CARD_SIZE, CARD_SIZE))
pygame.draw.rect(screen, GREY, pygame.Rect(x, y, CARD_SIZE, CARD_SIZE), 5)
# Variables du jeu
visible = [False] * len(cards)
flipped_cards = []
score = 0
# Boucle principale du jeu
running = True
while running:
screen.fill(PASTEL_PINK)
draw_cards()
display_text("Score: " + str(score), font_small, PINK, 20, 20)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
x, y = pygame.mouse.get_pos()
col = x // CARD_SIZE
row = y // CARD_SIZE
card_idx = row * 4 + col
if not visible[card_idx]:
visible[card_idx] = True
flipped_cards.append(card_idx)
if len(flipped_cards) == 2:
if cards[flipped_cards[0]] == cards[flipped_cards[1]]:
score += 1
else:
pygame.time.delay(1000)
visible[flipped_cards[0]] = visible[flipped_cards[1]] = False
flipped_cards.clear()
if score == len(questions):
display_text("Félicitations ! Vous êtes officiellement le plus grand fan de Malak.", font, PINK, 100, HEIGHT // 2)
display_text("Mais… Pour accéder au prix ultime (photo ultra exclusive + certificat de starlette n°1),", font_small, PINK, 30, HEIGHT // 2 + 40)
display_text("veuillez envoyer 1000$ à Malak Inc.", font_small, PINK, 150, HEIGHT // 2 + 70)
display_text("(paiement accepté en chocolat, câlins ou virement bancaire immédiat)", font_small, PINK, 100, HEIGHT // 2 + 100)
pygame.display.update()
pygame.time.delay(3000)
running = False
pygame.display.update()
pygame.time.Clock().tick(FPS)
# Quitter pygame
pygame.quit()
sys.exit()