chasse_au_tresor.py

Created by cent20

Created on May 17, 2023

8.07 KB

𝗖𝗵𝗮𝘀𝘀𝗲_𝗮𝘂_𝘁𝗿𝗲́𝘀𝗼𝗿


Python Game 🎮 v1.00 for Numworks, all models.

By Adam Y. avril 2023.

Un projet libre réalisé dans la cadre de l'enseignement de spécialité NSI.

Learn more about Chasse au trésor on: nsi.xyz/chasse_au_tresor (FR)


from random import randint
from kandinsky import fill_rect as fr
from kandinsky import draw_string as ds
from ion import keydown

jaune = (255, 181, 49)
rouge = (249, 65, 68)
orange = (243, 114, 44)
vert = (144, 190, 109)
violet = (148, 113, 222)

choix = 0

plateau_sup = [[0, 0, 0, 0, 0],
               [0, 0, 0, 0, 0],
               [0, 0, 0, 0, 0],
               [0, 0, 0, 0, 0],
               [0, 0, 0, 0, 0]]

plateau_inf = [["V", "V", "V", "V", "V"],
               ["V", "V", "V", "V", "V"],
               ["V", "V", "V", "V", "V"],
               ["V", "V", "V", "V", "V"],
               ["V", "V", "V", "V", "V"]]

choix_couleurs = {0: ('Facile', 128, 70, vert),
                  1: ('Moyen', 133, 100, orange),
                  2: ('Difficile', 115, 130, rouge),
                  3: ('Tutoriel', 118, 160, jaune)}

vies = [3]
nb_piques = [11]
nb_fausses = [3]
cases_retournees = []
cases_marquees = []

# détecte quand l'utilisateur relâche une touche
# tiré de https://my.numworks.com/python/cent20/menu
def attente(touches):
  while True:
    for nb in touches:
      if keydown(nb):
        while keydown(nb):
          True
        return nb

def curseur(x, y, c):
  fr(70 + 35 * x, 31 + 35 * y, 35, 1, c)
  fr(70 + 35 * x, 31 + 35 * y, 1, 35, c)
  fr(70 + 35 * x, 66 + 35 * y, 35, 1, c)
  fr(105 + 35 * x, 31 + 35 * y, 1, 35, c)

# aide : https://my.numworks.com/python/schraf/demineur
def grille():
  for i in range(6):
    if i < 6:
      fr(70, 31 + 35 * i, 176, 1, 'black')
    fr(70 + 35 * i, 31, 1, 176, 'black')

# affiche les cases marquées et retournées
def affiche():
  for coord1 in cases_marquees:
    fr(71 + 35 * coord1[0], 32 + 35 * coord1[1], 35, 2, rouge)
    fr(71 + 35 * coord1[0], 32 + 35 * coord1[1], 2, 35, rouge)
    fr(71 + 35 * coord1[0], 64 + 35 * coord1[1], 35, 2, rouge)
    fr(103 + 35 * coord1[0], 32 + 35 * coord1[1], 2, 35, rouge)
  for coord2 in cases_retournees:
    if plateau_inf[coord2[0]][coord2[1]] == 'V':
      couleur = vert
    elif plateau_inf[coord2[0]][coord2[1]] == 'O':
      couleur = orange
    elif plateau_inf[coord2[0]][coord2[1]] == 'R':
      couleur = rouge
    elif plateau_inf[coord2[0]][coord2[1]] == 'S':
      couleur = 'grey'
    fr(71 + 35 * coord2[0], 32 + 35 * coord2[1], 34, 34, couleur)

# vérifie l'état de la partie (gagné, perdu, etc.)
def verif(x, y):
  if plateau_inf[x][y] == 'X':
    fr(80, 60, 160, 100, 'black')
    ds("Bien joué !", 110, 100, 'white', 'black')
    attente([4, 52, 17])
    menu()
  elif plateau_inf[x][y] == 'S':
    vies[0] -= 1
    if vies[0] < 1:
      fr(90, 10, 130, 15, 'white')
      fr(80, 60, 160, 100, 'black')
      ds("Perdu !", 130, 100, 'white', 'black')
      attente([4, 52, 17])
      menu()

# réinitialise la partie
def reset():
  for x in range(5):
    for y in range(5):
      plateau_sup[x][y] = 0
      plateau_inf[x][y] = 'V'
  cases_retournees.clear()
  cases_marquees.clear()
  vies[0] = 3

def menu():
  global choix
  fr(0, 0, 320, 240, 'white')
  fr(0, 200, 320, 40, violet)
  ds('Chasse au trésor', 80, 20, 'black')
  ds('-----------------', 75, 40, 'black')
  ds("Code by nsi.xyz/chasse-au-tresor", 0, 202,'white', violet)
  while True:
    for i in range(4):
      texte, x, y, couleur = choix_couleurs[i]
      ds(texte, x, y, couleur if i == choix else 'black') # affichage des options
    touche = attente([1, 2, 4, 52])   
    if touche == 1 and choix > 0:
      choix -= 1
    elif touche == 2 and choix < 3:
      choix += 1
    elif touche == 4 or touche == 52:
      if choix == 0:
        nb_piques[0] = 11
        jeu()
      if choix == 1:
        nb_piques[0] = 13
        jeu()
      if choix == 2:
        nb_piques[0] = 15
        jeu()
      if choix == 3:
        tutoriel()

def tutoriel():
  fr(0, 0, 320, 240, 'white')
  ds("Votre but est de trouver la", 5, 0, 'black')
  ds("case qui cache le trésor.", 5, 15, 'black')
  ds("Un chiffre indique le nombre", 5, 40, 'black')
  ds("de cases adjacentes qui cachent", 5, 55, 'black')
  ds("un pique (4 max).", 5, 70, 'black')
  ds("Une case retournée affiche :", 5, 95, 'black')
  ds("- vert : loin du trésor,", 10, 110, 'black')
  ds("- vert", 10, 110, vert)
  ds("- orange : 2-4 cases du trésor,", 10, 125, 'black')
  ds("- orange", 10, 125, orange)
  ds("- rouge : 1-2 cases du trésor,", 10, 140, 'black')
  ds("- rouge", 10, 140, rouge)
  ds("Tombez sur plus de trois piques", 5, 165, 'black')
  ds("et vous perdez.", 5, 180, 'black')
  ds("/!\\ Trois couleurs sont fausses.", 5, 205, 'black')
  attente([4, 52, 17])
  menu()

# aide : https://my.numworks.com/python/schraf/demineur
def jeu():
  x, y = 0, 0
  reset()
  init_plateau()
  fr(0, 0, 320, 240, 'white')
  grille()
  while True:
    fr(100, 10, 130, 15, 'white')
    ds("<3\t" * vies[0], 88, 8, 'black')
    affiche()
    for a in range(0, 5):
      for b in range(0, 5):
        couleur = 'white'
        if (a, b) in cases_retournees:
          if plateau_inf[a][b] == 'V':
            couleur = vert
          elif plateau_inf[a][b] == 'O':
            couleur = orange
          elif plateau_inf[a][b] == 'R':
            couleur = rouge
          elif plateau_inf[a][b] == 'S':
            couleur = 'grey'
        ds(str(plateau_sup[a][b]), 83 + 35 * a, 41 + 35 * b, 'black', couleur) # affiche les chiffres
    while True:
      curseur(x, y, jaune)
      x_bis, y_bis = x, y
      touche = attente([0, 1, 2, 3, 4, 52, 17])
      if touche == 4 or touche == 52:
        if (x, y) not in cases_retournees:
          cases_retournees.append((x, y)) # retourne une case
          verif(x, y)
          break
      elif touche == 17:
        if (x, y) not in cases_marquees:
          cases_marquees.append((x, y)) # marque une case
        break
      elif x > 0 and touche == 0:
        x -= 1
      elif x < 4 and touche == 3:
        x += 1
      elif y > 0 and touche == 1:
        y -= 1
      elif y < 4 and touche == 2:
        y += 1
      curseur(x_bis, y_bis, 'black')
        

def init_plateau():
  # -------------------- plateau inférieur
  # placement du trésor
  tresor_x = randint(0, 4)
  tresor_y = randint(0, 4)  
  plateau_inf[tresor_x][tresor_y] = "X"
  # placement des cases rouges et orange
  tresor_x, tresor_y = randint(0, 4), randint(0, 4)
  plateau_inf[tresor_x][tresor_y] = "X"
  for i in range(tresor_x-2, tresor_x+3):
    for j in range(tresor_y-2, tresor_y+3):
      if 0 <= i <= 4 and 0 <= j <= 4 and (i, j) != (tresor_x, tresor_y):
        if abs(tresor_x-i) <= 1 and abs(tresor_y-j) <= 1:
          plateau_inf[i][j] = "R"
        elif abs(tresor_x-i) <= 2 and abs(tresor_y-j) <= 2:
          plateau_inf[i][j] = "O"
  # placement des piques
  for _ in range(nb_piques[0]):
    while True:
      pique_x = randint(0, 4)
      pique_y = randint(0, 4)
      if plateau_inf[pique_x][pique_y] != "S" and plateau_inf[pique_x][pique_y] != "X":
        plateau_inf[pique_x][pique_y] = "S"
        break
  # placement des fausses couleurs
  for _ in range(nb_fausses[0]):
    fausses_originales = []
    while True:
      fausse_x = randint(0, 4)
      fausse_y = randint(0, 4)
      if plateau_inf[fausse_x][fausse_y] != "S" and plateau_inf[fausse_x][fausse_y] != "X":
        if (fausse_x, fausse_y) not in fausses_originales:
          fausses_originales.append((fausse_x, fausse_y))
          while True:
            espace_nouv = randint(0, 2)
            if espace_nouv == 0 and plateau_inf[fausse_x][fausse_y] != "V":
              plateau_inf[fausse_x][fausse_y] = "V"
              break
            elif espace_nouv == 1 and plateau_inf[fausse_x][fausse_y] != "O":
              plateau_inf[fausse_x][fausse_y] = "O"
              break
            elif espace_nouv == 2 and plateau_inf[fausse_x][fausse_y] != "R":
              plateau_inf[fausse_x][fausse_y] = "R"
              break
          break
  # -------------------- plateau supérieur
  for ligne in range(0, 5):
    for colonne in range(0, 5):
      if plateau_inf[ligne][colonne] == "S":
        if ligne < 4: plateau_sup[ligne + 1][colonne] += 1
        if colonne < 4: plateau_sup[ligne][colonne + 1] += 1
        if ligne > 0: plateau_sup[ligne - 1][colonne] += 1
        if colonne > 0: plateau_sup[ligne][colonne - 1] += 1

menu()

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.