Python Game 🎮 v3.0 for Numworks, all models.
By Hugo.C & Siméon F. décembre 2024.
Un projet libre réalisé dans la cadre de l’enseignement de spécialité NSI,
participation au Concours Python 2024-2025 de tiplanet
Learn more about Memory-Countries on: nsi.xyz/memorycountries (FR)
#Memory pays/capitales v1 05/01/2025 from ion import * from kandinsky import * from random import * from math import * dico = { "France": "Paris", "Angleterre": "Londres", "Italie": "Rome", "Espagne": "Madrid", "Portugal": "Lisbonne", "Allemagne": "Berlin", "Pologne": "Varsovie", "Autriche": "Vienne", "Serbie": "Belgrade", "Croatie": "Zagreb", "Bielorussie": "Minsk", "Lettonie": "Riga"} nb_tour = 0 pays, pays2 = 0,0 carte_trouve= [] # Extraire les pays et capitales liste_pays = list(dico.keys()) liste_capitale = list(dico.values()) def recherche_liste(liste,k): for i in liste: if k == i: return True def paire(pays,pays2): for i in dico.items(): if (pays,pays2) == i or (pays2,pays) == i: return True return False def melange(lst): for i in range(len(lst) - 1, 0, -1): j = randint(0, i) lst[i], lst[j] = lst[j], lst[i] def draw(pays,x,y): L = 42 H = 28 if recherche_liste(liste_capitale,pays): Z = L // 7 C = H//3 fill_rect(x,y,L,C,"#056f1a") fill_rect(x,y+C,L,C,"white") fill_rect(x,y+2*C,L,C,"#056f1a") draw_string(pays[:3],x + Z,y +5,"black" ,"white") if pays == "France": C = L // 3 fill_rect(x,y,C,H,"blue") fill_rect(x+C,y,C,H,"white") fill_rect(x+2*C,y,C,H,"red") if pays == "Italie": C = L // 3 fill_rect(x,y,C,H,"#175501") fill_rect(x+C,y,C,H,"white") fill_rect(x+2*C,y,C,H,"red") if pays == "Pologne": C = H//2 fill_rect(x,y,L,C,"white") fill_rect(x,y+C,L,C,"red") if pays == "Allemagne": C = H//3 fill_rect(x,y,L,C,"black") fill_rect(x,y+C,L,C,"red") fill_rect(x,y+2*C,L,C,"yellow") if pays == "Autriche": C = H//3 fill_rect(x,y,L,C,"red") fill_rect(x,y+C,L,C,"white") fill_rect(x,y+2*C,L,C,"red") if pays == "Angleterre": #BUG # Dessiner le fond bleu fill_rect(x, y, L, H, "white") # Couleur bleue # Dessiner la croix blanche fill_rect(x, y + (H // 2) - 3, L, 6, "red") # Ligne blanche horizontale fill_rect(x + (L // 2) - 3, y, 6, H, "red") # Ligne blanche verticale if pays == "Lettonie": C = H//2 Z = C//2 fill_rect(x,y,L,H,"#7A0125") C=H//5 fill_rect(x,y+12,L,C,"white") if pays == "Espagne": C = H//3 Z = C//2 fill_rect(x,y,L,C,"red") fill_rect(x,y+C,L,C,"yellow") fill_rect(x,y+2*C,L,C,"red") fill_rect(x+Z,y+C,Z,10,"red") if pays == "Portugal": C = L // 3 Z = C // 2 fill_rect(x,y,C,H,"#2B4601") fill_rect(x+C,y,C,H,"red") fill_rect(x+2*C,y,C,H,"red") fill_rect(x+Z,y+Z+2,C,10,"yellow") if pays == "Bielorussie": C = H//2 fill_rect(x,y,L,C,"red") fill_rect(x,y+C,L,C,"#00532E") fill_rect(x,y,4,H,"white") if pays == "Serbie": C = H//3 Z = C//2 fill_rect(x,y,L,C,"red") fill_rect(x,y+C,L,C,"blue") fill_rect(x,y+2*C,L,C,"white") fill_rect(x+Z,y+C,Z,10,"white") if pays == "Croatie": C = H//3 Z = C//2 fill_rect(x,y,L,C,"red") fill_rect(x,y+C,L,C,"white") fill_rect(x,y+2*C,L,C,"blue") fill_rect(x+C+Z+3,y+C,C,12,"red") def aleatoir(): global l l = liste_pays + liste_capitale melange(l) #print(l) aleatoir() liste_coord = [] def carte(): for x in range(0,6,1): for y in range(0,4,1): fill_rect(6+x*53,5+y*50,42,28,"#ee0042") for i in carte_trouve: draw(i[0],i[1],i[2]) # Créer le dictionnaire clé-valeur def creer_dictionnaire(): global dico_final dico_final = {l[i]: liste_coord[i] for i in range(len(l))} print(dico_final) def wait_key(keys=(0,1,2,3,4,52)): # input keyboard while True: for k in keys: if keydown(k): while keydown(k): True # Fonction anti-rebond return k def recherche(dico,k):#recherche la veleure et retourne la clé for i,j in dico.items() : if j == k: return i def clique(x, y): global pays global pays2 global dico_final global xp, yp global xp2,yp2 if pays == 0: xp, yp = x, y elif pays != 0: xp2,yp2 = x,y if pays != 0 : pays2 = recherche(dico_final,[x,y]) else : pays = recherche(dico_final,[x,y]) #print(pays, pays2) if pays != 0 and pays2 != 0: paire(pays,pays2) def move(): global x,y global nb_tour global pays global pays2 global xp, yp x = 0 # Position initiale du curseur en x y = 0 # Position initiale du curseur en y cell_width = 28 cell_height = 42 grid_spacing = 10 # Espace entre les cellules while True: i = 0 pays = 0 pays2 = 0 deplacement = False while i != 3: # Efface l'écran avant de dessiner le curseur fill_rect(0, 0, 320, 240, "grey") # Fond gris carte() #remet les cartes en place # Dessine le curseur à la position actuelle if not deplacement: fill_rect(6+x*53,5+y*50,42,28,"#056f1a") #Dessin pays if pays != 0: if pays2 !=0: draw(pays,6+xp*53,5+yp*50) draw(pays2,6+xp2*53,5+yp2*50) else: draw(pays,6+xp*53,5+yp*50) draw_string(str(nb_tour), 300, 200,"black","grey") draw_string("Carte 1 : " + str(pays), 5, 186,"black","grey") draw_string("Carte 2 : " + str(pays2), 5, 205,"black","grey") # Attend une touche k = wait_key() if not deplacement: # Gère les déplacements if k == 3: # Flèche droite x = min(x + 1, 5) # Limite à 6 pour éviter de sortir de l'écran elif k == 0: # Flèche gauche x = max(x - 1, 0) # Limite à 1 pour éviter de sortir de l'écran elif k == 1: # Flèche haut y = max(y - 1, 0) # Limite à 0 elif k == 2: # Flèche bas y = min(y + 1, 3) # Limite à 3 pour éviter de sortir de l'écran if k == 4 or k == 52: clique(x,y) i +=1 if pays != 0 and pays2 != 0: deplacement = True if paire(pays,pays2) == True: if ([pays,6+xp*53,5+yp*50]) not in carte_trouve and ([pays2,6+xp2*53,5+yp2*50]) not in carte_trouve: carte_trouve.append([pays,6+xp*53,5+yp*50]) carte_trouve.append([pays2,6+xp2*53,5+yp2*50]) nb_tour += 1 print(carte_trouve) if len(carte_trouve) == 24: break def fin(): L = 320 H = 240 fill_rect(0, 0, L, H, "blue") X = L // 2 Y = H // 2 R = min(L, H) // 4 ER = R // 6 NE = 12 def draw_etoile(cx, cy, r): for i in range(5): x1 = cx + r * cos(2 * pi * i / 5 - pi/2) y1 = cy + r * sin(2 * pi * i / 5 - pi/2) x2 = cx + (r/2.5) * cos(2 * pi * (i+0.5) / 5 - pi/2) y2 = cy + (r/2.5) * sin(2 * pi * (i+0.5) / 5 - pi/2) if i == 0: fx, fy = x1, y1 px, py = x1, y1 fill_rect(int(x1), int(y1), 2, 2, "yellow") fill_rect(int(x2), int(y2), 2, 2, "yellow") px, py = x1, y1 for i in range(NE): angle = 2 * pi * i / NE ex = X + R * cos(angle - pi/2) ey = Y + R * sin(angle - pi/2)-10 draw_etoile(ex, ey, ER) draw_string("C'EST GAGNÉ !",95,80,"white", "blue") draw_string("MERCI D'AVOIR JOUÉ !",65,120,"white", "blue") def acceuil(): fill_rect(0,0,320,240,"grey") draw_string("MEMORIE PAYS-CAPITALES",0,0,"black","gray") draw_string("Utiliser les flèches \n pour naviguer \n et OK pour séléctionner",0,40,"black","gray") draw_string("Appuyer une troisième fois OK \n pour passer au tour suivant",0,116,"black","gray") k = wait_key() if k == 4 or k == 52: return def main(): acceuil() for x in range(0,6,1): for y in range(0,4,1): fill_rect(6+x*53,5+y*50,42,28,"#ee0042") liste_coord.append([x,y]) dico_final = creer_dictionnaire() move() fin() # Appelle la fonction principale main()