|
1 | 1 | import random
|
| 2 | +import pygame |
| 3 | +import sys |
2 | 4 |
|
3 |
| -import simplegui |
4 |
| - |
5 |
| - |
6 |
| -def new_game(): |
7 |
| - global card3, po, state, exposed, card1 |
8 |
| - |
9 |
| - def create(card): |
10 |
| - while len(card) != 8: |
11 |
| - num = random.randrange(0, 8) |
12 |
| - if num not in card: |
13 |
| - card.append(num) |
14 |
| - return card |
15 |
| - |
16 |
| - card3 = [] |
17 |
| - card1 = [] |
18 |
| - card2 = [] |
19 |
| - po = [] |
20 |
| - card1 = create(card1) |
21 |
| - card2 = create(card2) |
22 |
| - card1.extend(card2) |
23 |
| - random.shuffle(card1) |
24 |
| - state = 0 |
25 |
| - exposed = [] |
26 |
| - for i in range(0, 16, 1): |
27 |
| - exposed.insert(i, False) |
28 |
| - |
29 |
| - |
30 |
| -def mouseclick(pos): |
31 |
| - global card3, po, state, exposed, card1 |
32 |
| - if state == 2: |
33 |
| - if card3[0] != card3[1]: |
34 |
| - exposed[po[0]] = False |
35 |
| - exposed[po[1]] = False |
36 |
| - card3 = [] |
37 |
| - state = 0 |
38 |
| - po = [] |
39 |
| - ind = pos[0] // 50 |
40 |
| - card3.append(card1[ind]) |
41 |
| - po.append(ind) |
42 |
| - if exposed[ind] == False and state < 2: |
43 |
| - exposed[ind] = True |
44 |
| - state += 1 |
45 |
| - |
46 |
| - |
47 |
| -def draw(canvas): |
48 |
| - global card1 |
49 |
| - gap = 0 |
50 |
| - for i in range(0, 16, 1): |
51 |
| - if exposed[i] == False: |
52 |
| - canvas.draw_polygon( |
53 |
| - [[0 + gap, 0], [0 + gap, 100], [50 + gap, 100], [50 + gap, 0]], |
54 |
| - 1, |
55 |
| - "Black", |
56 |
| - "Green", |
57 |
| - ) |
58 |
| - elif exposed[i] == True: |
59 |
| - canvas.draw_text(str(card1[i]), [15 + gap, 65], 50, "White") |
60 |
| - gap += 50 |
61 |
| - |
62 |
| - |
63 |
| -frame = simplegui.create_frame("Memory", 800, 100) |
64 |
| -frame.add_button("Reset", new_game) |
65 |
| -label = frame.add_label("Turns = 0") |
66 |
| - |
67 |
| -frame.set_mouseclick_handler(mouseclick) |
68 |
| -frame.set_draw_handler(draw) |
69 |
| - |
70 |
| -new_game() |
71 |
| -frame.start() |
| 5 | +# Initialisation de pygame |
| 6 | +pygame.init() |
| 7 | + |
| 8 | +# Définir les couleurs |
| 9 | +WHITE = (255, 255, 255) |
| 10 | +PASTEL_PINK = (255, 182, 193) |
| 11 | +PINK = (255, 105, 180) |
| 12 | +LIGHT_PINK = (255, 182, 193) |
| 13 | +GREY = (169, 169, 169) |
| 14 | + |
| 15 | +# Définir les dimensions de la fenêtre |
| 16 | +WIDTH = 600 |
| 17 | +HEIGHT = 600 |
| 18 | +FPS = 30 |
| 19 | +CARD_SIZE = 100 |
| 20 | + |
| 21 | +# Créer la fenêtre |
| 22 | +screen = pygame.display.set_mode((WIDTH, HEIGHT)) |
| 23 | +pygame.display.set_caption("Memory Game : Les Préférences de Malak") |
| 24 | + |
| 25 | +# Charger les polices |
| 26 | +font = pygame.font.Font(None, 40) |
| 27 | +font_small = pygame.font.Font(None, 30) |
| 28 | + |
| 29 | +# Liste des questions et réponses (préférences) |
| 30 | +questions = [ |
| 31 | + {"question": "Quelle est sa couleur préférée ?", "réponse": "Rose", "image": "rose.jpg"}, |
| 32 | + {"question": "Quel est son plat préféré ?", "réponse": "Pizza", "image": "pizza.jpg"}, |
| 33 | + {"question": "Quel est son animal préféré ?", "réponse": "Chat", "image": "chat.jpg"}, |
| 34 | + {"question": "Quel est son film préféré ?", "réponse": "La La Land", "image": "lalaland.jpg"} |
| 35 | +] |
| 36 | + |
| 37 | +# Créer les cartes avec des questions et réponses |
| 38 | +cards = [] |
| 39 | +for q in questions: |
| 40 | + cards.append(q["réponse"]) |
| 41 | + cards.append(q["réponse"]) |
| 42 | + |
| 43 | +# Mélanger les cartes |
| 44 | +random.shuffle(cards) |
| 45 | + |
| 46 | +# Créer un dictionnaire pour les positions des cartes |
| 47 | +card_positions = [(x * CARD_SIZE, y * CARD_SIZE) for x in range(4) for y in range(4)] |
| 48 | + |
| 49 | +# Fonction pour afficher le texte |
| 50 | +def display_text(text, font, color, x, y): |
| 51 | + text_surface = font.render(text, True, color) |
| 52 | + screen.blit(text_surface, (x, y)) |
| 53 | + |
| 54 | +# Fonction pour dessiner les cartes |
| 55 | +def draw_cards(): |
| 56 | + for idx, pos in enumerate(card_positions): |
| 57 | + x, y = pos |
| 58 | + if visible[idx]: |
| 59 | + pygame.draw.rect(screen, WHITE, pygame.Rect(x, y, CARD_SIZE, CARD_SIZE)) |
| 60 | + display_text(cards[idx], font, PINK, x + 10, y + 30) |
| 61 | + else: |
| 62 | + pygame.draw.rect(screen, LIGHT_PINK, pygame.Rect(x, y, CARD_SIZE, CARD_SIZE)) |
| 63 | + pygame.draw.rect(screen, GREY, pygame.Rect(x, y, CARD_SIZE, CARD_SIZE), 5) |
| 64 | + |
| 65 | +# Variables du jeu |
| 66 | +visible = [False] * len(cards) |
| 67 | +flipped_cards = [] |
| 68 | +score = 0 |
| 69 | + |
| 70 | +# Boucle principale du jeu |
| 71 | +running = True |
| 72 | +while running: |
| 73 | + screen.fill(PASTEL_PINK) |
| 74 | + draw_cards() |
| 75 | + display_text("Score: " + str(score), font_small, PINK, 20, 20) |
| 76 | + |
| 77 | + for event in pygame.event.get(): |
| 78 | + if event.type == pygame.QUIT: |
| 79 | + running = False |
| 80 | + if event.type == pygame.MOUSEBUTTONDOWN: |
| 81 | + x, y = pygame.mouse.get_pos() |
| 82 | + col = x // CARD_SIZE |
| 83 | + row = y // CARD_SIZE |
| 84 | + card_idx = row * 4 + col |
| 85 | + |
| 86 | + if not visible[card_idx]: |
| 87 | + visible[card_idx] = True |
| 88 | + flipped_cards.append(card_idx) |
| 89 | + |
| 90 | + if len(flipped_cards) == 2: |
| 91 | + if cards[flipped_cards[0]] == cards[flipped_cards[1]]: |
| 92 | + score += 1 |
| 93 | + else: |
| 94 | + pygame.time.delay(1000) |
| 95 | + visible[flipped_cards[0]] = visible[flipped_cards[1]] = False |
| 96 | + flipped_cards.clear() |
| 97 | + |
| 98 | + if score == len(questions): |
| 99 | + display_text("Félicitations ! Vous êtes officiellement le plus grand fan de Malak.", font, PINK, 100, HEIGHT // 2) |
| 100 | + display_text("Mais… Pour accéder au prix ultime (photo ultra exclusive + certificat de starlette n°1),", font_small, PINK, 30, HEIGHT // 2 + 40) |
| 101 | + display_text("veuillez envoyer 1000$ à Malak Inc.", font_small, PINK, 150, HEIGHT // 2 + 70) |
| 102 | + display_text("(paiement accepté en chocolat, câlins ou virement bancaire immédiat)", font_small, PINK, 100, HEIGHT // 2 + 100) |
| 103 | + pygame.display.update() |
| 104 | + pygame.time.delay(3000) |
| 105 | + running = False |
| 106 | + |
| 107 | + pygame.display.update() |
| 108 | + pygame.time.Clock().tick(FPS) |
| 109 | + |
| 110 | +# Quitter pygame |
| 111 | +pygame.quit() |
| 112 | +sys.exit() |
0 commit comments