blanche.py

Created by schraf

Created on October 24, 2022

5.04 KB

Petits jeux inspirés des épreuves de Blanche dans Fort Boyard.

COMBINAISON

Vous devez reconstituer une combinaison de 5 balles bleues ou blanches. Vous disposez de 4 essais en niveau 1 et de 3 en niveau 2. Après chaque essai, le nombre de carrés bleus correctement positionnés est indiqué.

STOP CHUTE

Appuyez sur EXE pour lancer le jeu.
Après un temps aléatoire, un carré bleu va apparaitre dans une des 5 cases. Vous devez appuyer sur la touche correspondante de votre calculatrice (1,2,3,+ ou -) dans les 4 dixièmes de seconde (en niveau 1) ou 3 dixièmes de seconde (au niveau 2). Vous avez 3 essais.

SOUFFLE

Un temps en secondes va s’afficher. Appuyez sur EXE puis comptez ce nombre de secondes dans votre tête et pressez sur la touche +. Le carré bleu se déplace alors vers le carré rouge et vous indique si vous êtes trop loin du temps ou si vous avez gagné. Vous avez 3 essais. Le niveau 1 vous donne un droit à l’erreur de 0,2s et le niveau 2 de 0,1s.

from kandinsky import *
from random import choice, randint
from ion import *
from time import sleep,monotonic

BL, WH, RO = (80,176,216), (248,252,248), (232,120,88)
# Etiquettes (Labels) pour le visuel
LAB = "123+-"
# Les touches autorisées
TOUCHES = [KEY_ONE,KEY_TWO,KEY_THREE,KEY_PLUS,KEY_MINUS]
NBJEUX = 3
# Phrases de Blanche
PH = ["VOUS AVEZ PERDU !","BRAVO !","RATE !"]
# Liste des clés
cles = []

pal = [[255,255,255],[169,233,249],...,[100,123,137]]
im = "A56B3...A2CB"

# ---
# Affichage des clés en bleu avec fond orange si perdu
def aff_cles():
  for i in range(len(cles)):
    draw_string(chr(254), 10+12*i,200,BL if cles[i] else RO)

# ---
# Voir vidéo sur l'utilisation de Gimp pour créer la chaine "im"
def visage():
  r,i = 0,0
  while r < len(im):
   s = im[r]
   n = ""
   r += 1
   while r < len(im) and "9" >= im[r] >= "0"


from kandinsky import *
from random import choice, randint
from ion import *
from time import sleep,monotonic

BL, WH, RO = (80,176,216), (248,252,248), (232,120,88)
LAB = "123+-"
TOUCHES = [KEY_ONE,KEY_TWO,KEY_THREE,KEY_PLUS,KEY_MINUS]
NBJEUX = 3
PH = ["VOUS AVEZ PERDU !"," BRAVO ! ","  RATE ! "]
cles = []

pal = [[255,255,255],[169,233,249],[214,240,248],[81,178,222],[44,123,166],[144,169,183],[240,218,201],[250,205,168],[251,190,142],[200,155,133],[224,149,103],[141,96,76],[182,107,62],[29,67,106],[237,123,93],[100,123,137]]
im = "A56B3CB2DCA2CA2BCABDECA2BFA56CB2C2B2DA5BCAB2DCA2C2A57C3ACB2CA4BCAB4A61C6B2A3C2A2C3BCA61C5B2CA2C2ACBC4A2CA59C5B2A2BCAB2A3C3FA59C3BCB3ACA2BCA2G2FD2A59C2ACBCB2C2ACFH2I3JFEA60C2AC3BAJIHKI7JA61DFGJ2CGKI2H3I6A61EJHIKHI3H2IK2J2K2IA61CFJI3HIHIKL2KJK4A62FL3KIHIKM3L4MKA62DJL2MKI2K2ML5MKA62DNL4IHIKMJ2L2KIHA62DNLJL2KHI2K4I2H2A61CDEJ2K2IH2IH8A61CDEJI3H3IH8A61CDEJI3H2I3H5I2A61CDELI3H3I4H2I2KA61BDENJI3HI3KI4K2A61BDENLKI3K4I3K2OA61BD2N2JKIK4I4K3A61BD2N3KI2K2I3K5A61BD2EN2PK2O6K4A61BD2EN2PGKO5K5A61BD2EN2EFJKO2K2I3K2A61CD3N2EFPJK2I5KMA61CBD2ENE3PHIH2I2KM2A61CBD2E6FI3K2M3A61CBD2E5PF2JL2M4A62B2DE4PF4PLM4A62B2D2E3PFG2JLK3M2A62B2D2E3PFH2J2K5A62CBD2E3FJHIJK4I2A62CBD2E3FJHJ2K3I3A62CB2D2E2FJ4K3I2HA62CB2D2E2FJ2L2K2I3GA62CB2D3F2JL2MK2I3GA62CB2D3F2JLMK2I3HCA62CB2D3F2L2K2I4GCA62CBD4F2L2KI4HABA62CBD4F2LKI5GCBA62B4DEPFJI5HGCBA61CB4DPFJKI4H2ACBA61B5DPFJI4H2GCB2A60CB5PFGHI5HGCB2A59CB2CBDF2JGH2I3H2ACB2A59CBCB2DFJGH2I4HGCB3A58C4BF3GH3I4HGCB3A57C4B2F2GH4I3HGACB3A57BC3BF2CGH3I3H2GC2BC2A56B2C3F3GH4I2HG3CB2C2A55CBC3BF2G2H6G3ACB2C2A55CBC2BFCG3H5G3A2CB"

def aff_cles():
  for i in range(len(cles)):
    draw_string(chr(254), 10+12*i,200,BL if cles[i] else RO)

def visage():
  r,i = 0,0
  while r<len(im):
   s = im[r]
   n = ""
   r += 1
   while r < len(im) and "9" >= im[r] >= "0":
    n += im[r]
    r += 1
   nb = 1 if n == "" else int(n)  
   c = pal[ord(s)-65]
   for j in range(nb):
    if s != "A": fill_rect(4*(i%80),2+4*(i//80),4,4,(c[0],c[1],c[2]))
    i += 1
  sleep(.5) 

def key(t,duree = -1):
  dep = monotonic()
  while True:
    for (i, k) in enumerate(t):
      if keydown(k): return i
    if duree > 0 and monotonic() - dep > duree: return -1  

def marge(txt): return 125 - 5 * len(txt)

def titre(n, t):
  fill_rect(0,0,320,222,WH)  
  fill_rect(0,20,250,25,BL)
  txt = ["ADRESSE","CHANCE","MES DEFIS"][n]
  draw_string(txt,marge(txt),23,WH,BL)
  draw_string(t,marge(t),50,BL)
  visage()
  aff_cles()

def suite(): 
  draw_string("EXE",110,190,BL)
  key([KEY_EXE])
  draw_string("   ",110,190,WH,WH)
  fill_rect(0,80,240,20,WH)

def vider():
  for i in range(5):
    boule(i, WH)
    draw_string(LAB[i],35+40*i,155,BL)

def parle(t):
  draw_string(t,marge(t),80,WH,BL)  
  suite() 

def boule(n,c,d = 30, y = 130):
  x = d + 40 * n
  if c == WH: 
    fill_rect(x,y,20,20,BL)  
    fill_rect(x+1,y+1,18,18,WH)
  else:
    fill_rect(x,y,20,20,c)      
  
def jeu1(niv): 
  titre(1, "COMBINAISON")
  blanche = []
  essai = 0
  numeros = list(range(5))
  for i in range(3):
    v = choice(numeros)
    blanche.append(v)
    numeros.remove(v)
  gagne = False
  while True:
    vider()
    joueur = []
    while len(joueur) != 3:
      t = key(TOUCHES)
      if t not in joueur: 
        joueur.append(t)
        boule(t,BL)    
    identiques = len([k for k in range(3) if joueur[k] in blanche])
    essai += 1
    if identiques == 3:
      parle(PH[1])
      return 1
    elif essai < niv:
      parle("BIEN = {}".format(identiques))
    else:
      for i in blanche:
        draw_string(LAB[i],35+40*i,155,WH,RO)
      parle(PH[0])
      return 0 


def jeu2(niv):
  titre(0, "STOP CHUTE")
  essai = 0
  while True:
    delai = randint(100,500) / 100
    pos = choice(range(5))    
    vider()
    suite()
    sleep(delai)
    boule(pos,BL)
    t = key(TOUCHES,niv/10)
    essai += 1
    if t == pos:
      parle(PH[1])
      return 1
    elif essai < 3:
      boule(pos,RO)
      parle(PH[2])
    else:
      boule(pos,RO)
      parle(PH[0])
      return 0 

def bouge(pos,c,d):
  boule(pos,c,50,80)
  sleep(.1)
  boule(pos,WH,50,80)
  if pos <= 0 or pos >= 3: d *= -1
  pos += d
  return [pos,d]

def jeu3(niv):
  essai = 0
  erreur = .2 if niv == 1 else .1
  duree = randint(3,10)
  while essai < 3:
    titre(0, "SOUFFLE +")
    boule(0,RO,210,120)
    boule(0,BL,10,120)
    parle("{} s".format(duree))
    t = monotonic()
    while not(keydown(KEY_PLUS)): True
    essai += 1
    d = monotonic() - t
    n = int(20 * d / duree)
    for i in range(n):
      fill_rect(10 + 10 * i,120,20,20,BL) 
      sleep(.2)
      if i != n-1: fill_rect(10 + 10 * i,120,20,20,WH) 
    if abs(duree - d) < erreur:
        parle("{} {n:.1f} s".format(PH[1],n=d))
        return 1
    elif essai < 3:
      boule(0,RO,210,120)
      parle("{} {n:.1f} s".format(PH[2],n=d))
    else:
      parle(PH[0])
      return 0 


def go():
  global cles
  while True:
    titre(2,"Niveau 1 ou 2 ?")
    t = key([KEY_ONE,KEY_TWO])
    niv = 4 - t
    for i in range(5):
      n = 1+choice(range(NBJEUX))
      cles.append(eval("jeu"+str(n))(niv))
    titre(2,"Vous gagnez")
    parle("{} cles".format(sum(cles)))
    cles = []
go()