pyramid.py

Created by schraf

Created on January 28, 2025

5.93 KB

Jeu Pyramide proposé pour le Grand Concours 2024-2025 - Programmation Python !


from random import random
from kandinsky import *
from turtle import *
from ion import *
from time import sleep

R,N,B,F,G=(255,0,0),3*(0,),3*(255,),(80,130,50),3*(80,)
BTS = KEY_LEFT, KEY_RIGHT,KEY_DOWN, KEY_OK, KEY_UP
FIG=[16,56,124,254,511,254,124,56,16],[56,56,56,471,511,471,16,56,511],[16,56,124,254,511,511,443,56,511],[198,495,511,511,511,254,124,56,16]
COUL=R,N,N,R
VAL=1,2,3,4,5,6,7,8,9,10,"V","D","R"

def milieu(l,t):return (l-10*len(t))//2

def message(txt,coul,y=100):
 fill_rect(0,y,384,35,coul)
 draw_string(txt,100,y+9,B, coul)
 
def wait(b):
 while True:
  for i in b:
   if keydown(i): 
        v = i
        while v == keydown(i): continue
        return b.index(v) 

def somme(arr,jeu):
 if len(arr)==0:return 0
 total=sum(jeu.cartes[i][j].valeur if i<8 else jeu.retourne[-1].valeur for (i,j) in arr)
 return total

def est_fin_de_partie(jeu):
 if jeu.possible():return False
 else:
  message("GAME OVER !",R)
  return True

class Carte:
 def __init__(self,couleur,figure,valeur):
  self.couleur=couleur
  self.figure=figure
  self.valeur=valeur
  self.est_visible=False
  self.devant=[]
  self.derriere=[]
  self.etat=0

class Pyramide:
 def __init__(self):
  self.cartes=[]
  self.tas_cache=[Carte(COUL[i//13],FIG[i//13],1+(i%13)) for i in range(52)]
  self.retourne=[]
  self.construire_pyramide()

 def construire_pyramide(self):
  self.tas_cache=sorted(self.tas_cache,key=lambda x: random())
  for i in range(7):
   self.cartes.append([])
   for j in range(i+1):
    carte=self.tas_cache.pop()
    carte.est_visible=True
    self.cartes[-1].append(carte)
  for i in range(7):
   for j in range(i+1):
    carte=self.cartes[i][j]
    carte.devant=self.cartes_devant(i,j)
    carte.derriere=self.cartes_derriere(i,j)

 def afficher_tas(self):
  fill_rect(20,20,25,30,B)
  for y in range(26):
   for x in range(21):
    coul=R if (x+y)%2 else B
    set_pixel(22+x,22+y,coul)

 def cartes_devant(self,i,j):
  if i==6:return []
  return [self.cartes[i+1][j],self.cartes[i+1][j+1]]

 def cartes_derriere(self,i,j):
  if i==0:return []
  cartes=[]
  if j>0:cartes.append(self.cartes[i-1][j-1])
  try:cartes.append(self.cartes[i-1][j])
  finally:return cartes

 def afficher_pyramide(self):
  fill_rect(0,0,320,222,F)
  for i in range(7):
   for j in range(i+1):
    self.dessiner_carte(i,j)
  self.afficher_tas()
  

 def possible(self):
  libres=self.cartes_libres()
  if (8,0) in libres:libres.remove((8,0))
  reste=[self.cartes[i][j].valeur for (i,j) in libres]
  tous=reste+[carte.valeur for carte in self.tas_cache]
  tous+=[carte.valeur for carte in self.retourne]
  if 13 in tous:return True
  i=0
  for u in reste:
   j=0
   for v in tous:
    if i!=j and u+v==13:return True
    j+=1
   i+=1
  return False

 def cartes_libres(self):
  r=[]
  for j in range(7):
   for i in range(7-j):
    carte=self.cartes[6-i][j]
    if carte.est_visible and not carte.devant:
     r.append((6-i,j))
  if len(self.retourne)>0:r.append((8,0))
  return r

 def tirage(self):
  if len(self.tas_cache)>0:
   boucle=False
   carte=self.tas_cache.pop()
   self.retourne.append(carte)
   self.dessiner_carte(8,0)
   if len(self.tas_cache)==0:
    fill_rect(20,20,25,30,F)
  elif len(self.retourne):
   boucle=True
   self.tas_cache=self.retourne
   self.tas_cache.reverse()
   self.retourne=[]
   self.afficher_tas()
   self.tirage()
  
  return boucle

 def lg2xy(self,i,j):
  return 160-15*i+30*j,10+28*i

 def marque_carte(self,etat,i,j):
  if i!=8:
   x,y=self.lg2xy(i,j)
   carte=self.cartes[i][j]
  else:
   x,y=50,20
   carte=self.retourne[-1]
  if etat==2:carte.etat=3-carte.etat
  elif etat==1 and carte.etat!=2:carte.etat=1
  elif carte.etat!=2 or etat==-1:carte.etat=0
  coul=(B,F,R)[carte.etat]
  fill_rect(x+4,y+25,17,3,coul)
  
 def dessiner_carte(self,i,j):
  if i!=8:
   x,y=self.lg2xy(i,j)
   carte=self.cartes[i][j]
  else:
   x,y=50,20
   carte=self.retourne[-1]
  fill_rect(x-1,y-1,27,32,N)
  fill_rect(x,y,25,30,B)
  texte=str(VAL[carte.valeur-1])
  draw_string(texte,x+milieu(25,texte),y+9,N)
  for c in range(9):
   for l in range(9):
    if carte.figure[l] >> c & 1:set_pixel(x+2+c,y+2+l,carte.couleur)

 def supprimer_carte(self,i,j):
  if i!=8:
   x,y=self.lg2xy(i,j)
   fill_rect(x-1,y-1,27,32,F)
   self.cartes[i][j].est_visible=False
   arr=(-1,-1),(-1,0)
   for (a,b) in arr:
    if i >= 1 and 0 <= j+b<len(self.cartes[i+a]) and self.cartes[i+a][j+b].est_visible:
     if b<0:
      fill_rect(x-1,y-1,12,4,N)
      fill_rect(x-2,y-1,12,3,B)
     else:
      fill_rect(x+14,y-1,12,4,N)
      fill_rect(x+15,y-1,12,3,B)
     try:
      self.cartes[i+a][j+b].devant.remove(self.cartes[i][j])
     except:continue
  else:
   carte=self.retourne.pop()
   if len(self.retourne):self.dessiner_carte(8,0)
   else:fill_rect(49,19,27,32,F)
  
def go():
 jeu=Pyramide()
 jeu.afficher_pyramide()
 enCours=True
 while enCours:
  libres=jeu.cartes_libres()
  if libres in [[],[(8,0)]]:
   message(" YOU WIN !!",N)
   return
  idPyr=0
  idGlo=0
  cartes_choisies=[]
  jeu.marque_carte(1,*libres[idPyr])
  total=0
  while total!=13:
   (i,j)=libres[idGlo]
   sleep(.3)
   t=wait(BTS)
   if t==3: # OK
    jeu.marque_carte(2,i,j)
    if (i,j) in cartes_choisies:
     cartes_choisies.remove((i,j))
    if (i!=8 and jeu.cartes[i][j].etat==2) or i==8:
     cartes_choisies.append((i,j))
     if len(cartes_choisies)>2:
      jeu.marque_carte(-1,*cartes_choisies[0])
      cartes_choisies=cartes_choisies[1:]
    total=somme(cartes_choisies,jeu)
   elif t in (0,1): # gauche|droite
    jeu.marque_carte(0,i,j)
    nbLibres=len(libres)-(len(jeu.retourne)>0)
    if idGlo!=-1:
     idPyr=(idPyr-(t==0)+(t==1))%nbLibres
    idGlo=idPyr
    jeu.marque_carte(1,*libres[idGlo])
   elif t in (2,4): # BACK|OK
    idGlo=-1
    if t==2:
     boucle=jeu.tirage()
     if not (enCours):
      break
     libres=jeu.cartes_libres()
    if jeu.retourne: jeu.marque_carte(1,8,0)
   if est_fin_de_partie(jeu):return True

  for (i,j) in cartes_choisies:jeu.supprimer_carte(i,j)
  if jeu.retourne:
   jeu.dessiner_carte(8,0)
   jeu.marque_carte(-1,8,0)

go()

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.