Repository navigation
Expand file tree
/
Copy pathMemory_LinxConcept.py
More file actions
126 lines (101 loc) · 4.51 KB
/
Copy pathMemory_LinxConcept.py
File metadata and controls
126 lines (101 loc) · 4.51 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import pygame
import random
import time
# Initialisation
pygame.init()
WIDTH, HEIGHT = 800, 700
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Jeu Memory")
# Configuration
CARD_WIDTH, CARD_HEIGHT = 100, 150
GRID_COLS, GRID_ROWS = 4, 4
MARGIN = 20
BACKGROUND_COLOR = (50, 50, 50)
CARD_BACK_COLOR = (70, 130, 180)
#definition de la classe carte
class Card:
def __init__(self, x, y, value):
self.x = x
self.y = y
self.value = value
self.rect = pygame.Rect(x, y, CARD_WIDTH, CARD_HEIGHT)
self.face_up = False
self.solved = False
def draw(self):
if self.solved:
pygame.draw.rect(screen, (0, 200, 0), self.rect, border_radius=10)
elif self.face_up:
pygame.draw.rect(screen, (255, 255, 255), self.rect, border_radius=10)
font = pygame.font.SysFont('Arial', 60)
text = font.render(str(self.value), True, (0, 0, 0))
screen.blit(text, (self.x + CARD_WIDTH//2 - text.get_width()//2, self.y + CARD_HEIGHT//2 - text.get_height()//2))
else:
pygame.draw.rect(screen, CARD_BACK_COLOR, self.rect, border_radius=10)
def is_clicked(self, pos):
return self.rect.collidepoint(pos) and not self.solved
def created_board():
values = [i for i in range(1, (GRID_COLS * GRID_ROWS)//2 + 1)] * 2
random.shuffle(values)
cards = []
for row in range(GRID_ROWS):
for col in range(GRID_COLS):
x = (WIDTH - (GRID_COLS * (CARD_WIDTH + MARGIN))) // 2 + col * (CARD_WIDTH + MARGIN)
y = (HEIGHT - (GRID_ROWS * (CARD_HEIGHT + MARGIN))) // 2 + row * (CARD_HEIGHT + MARGIN)
cards.append(Card(x, y, values[row * GRID_COLS + col]))
return cards
def main():
cards = created_board()
selected_cards = []
moves = 0
font = pygame.font.SysFont('Arial', 32)
game_over = False
running = True
while running:
screen.fill(BACKGROUND_COLOR)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN and not game_over:
for card in cards:
if card.is_clicked(event.pos) and card not in selected_cards:
card.face_up = True
selected_cards.append(card)
if len(selected_cards) == 2:
moves += 1
if selected_cards[0].value == selected_cards[1].value:
selected_cards[0].solved = True
selected_cards[1].solved = True
selected_cards = []
else:
pygame.display.flip()
time.sleep(0.5)
selected_cards[0].face_up = False
selected_cards[1].face_up = False
selected_cards = []
# Dessin des cartes
for card in cards:
card.draw()
#Affichage du score
moves_text = font.render("Coups : {}".format(moves), True, (255, 255, 255))
screen.blit(moves_text, (20, 20))
#Ecran de victoire
if all(card.solved for card in cards) and not game_over:
game_over = True
win_font = pygame.font.SysFont('Arial', 72)
win_text = win_font.render("Gagne :)", True, (255, 255, 0))
# Dessiner par dessus les cartes
overlay = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA)
overlay.fill((0, 0, 0, 128))
screen.blit(overlay, (0, 0))
screen.blit(win_text, (WIDTH//2 - win_text.get_width()//2, HEIGHT//2 - win_text.get_height()//2))
# Afficher le score final
score_text = font.render("Score final : {} coups".format(moves), True, (255, 255, 255))
screen.blit(score_text, (WIDTH//2 - score_text.get_width()//2, HEIGHT//2 + 50))
pygame.display.flip()
time.sleep(3)
running = False
pygame.display.flip()
pygame.time.Clock().tick(60)
if __name__ == "__main__":
main()
pygame.quit()