blanche2.py

Created by schraf

Created on October 24, 2022

6.34 KB

Le script d’origine avec les explications est ici : https://my.numworks.com/python/schraf/blanche

Ajout d’un 4e jeu nommé “CARRE” et basé sur le jeu CERCLE de Blanche

CARRE

Un carré de taille variable s’affiche ainsi qu’une jauge à gauche.

Appuyez sur + ou - pour définir le niveau de la force que vous voulez appliquer au lancer de votre balle. Appuyez ensuite sur EXE pour la lancer. Celle-ci doit s’arrêter dans la zone orange, si ce n’est pas le cas vous avez 3 essais pour réussir à trouver la bonne force.

Remarque : Il est possible que la balle fasse plusieurs tours, ce n’est pas grave, il faut juste qu’elle s’arrête au bon endroit !

Description du code du jeu n°4

# Le carré est numéroté de 0 (en haut à gauche)
# et les n° font le tour dans le sens inverse
# des aiguilles d'une montre
# pos_carre cherche les coord (x,y) suivant le n° i
def pos_carre(i, cote):
  i %= 4 * cote
  if i <= cote: return 0,i
  if i <= 2 * cote: return i - cote, cote
  if i <= 3 * cote: return cote, 3 * cote - i
  return 4 * cote - i, 0

# Dessin d'une balle pour le jeu 4
def boule4(c,cote,i):
  x, y = pos_carre(i, cote)
  boule(0, c, 115 - 11 * cote + 22 * x, 119 - 11 * cote + 22 * y)

def jeu4(niv):
  essai = 0
  # Facile = taille 4 ou 5 et Difficile = taille 3 à 5
  cote = randint(niv - 1,4)
  # La force que le joueur doit trouver
  force = randint(3,16)
  titre(0, "CARRE")
  while essai < 3:
  	# Dessin du carré
    for i in range(4 * cote):
      boule4(RO if i > 3 * cote else WH, cote, i)
    # La balle bleue à la position 0
    boule4(BL, cote, 0)
    # Dessin de la jauge à gauche
    fill_rect(15,80,20,102,BL)
    fill_rect(16,81,18,100,WH)  
    draw_string("+",20,60,BL)
    draw_string("-",20,185,BL)
    # Le joueur choisit la force
    t,h = 0,0
    while t != 2: 
     


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 = 4
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(rect = True): 
  draw_string("EXE",110,190,BL)
  key([KEY_EXE])
  draw_string("   ",110,190,WH,WH)
  if rect: 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 pos_carre(i, cote):
  i %= 4 * cote
  if i <= cote: return 0,i
  if i <= 2 * cote: return i - cote, cote
  if i <= 3 * cote: return cote, 3 * cote - i
  return 4 * cote - i, 0

def boule4(c,cote,i):
  x, y = pos_carre(i, cote)
  boule(0, c, 115 - 11 * cote + 22 * x, 119 - 11 * cote + 22 * y)

def jeu4(niv):
  essai = 0
  cote = randint(niv - 1,4)
  force = randint(3,16)
  titre(0, "CARRE")
  while essai < 3:
    for i in range(4 * cote):
      boule4(RO if i > 3 * cote else WH, cote, i)
    boule4(BL, cote, 0)
    fill_rect(15,80,20,102,BL)
    fill_rect(16,81,18,100,WH)  
    draw_string("+",20,60,BL)
    draw_string("-",20,185,BL)
    t,h = 0,0
    while t != 2: 
      choix = [KEY_PLUS,KEY_MINUS,KEY_EXE]
      if h == 0: choix.remove(KEY_EXE)
      t = key(choix)
      sleep(.1)
      if t == 0: h = min(h + 4, 100)
      elif t == 1: h = max(h - 4, 0)
      fill_rect(16,81,18,100,WH)   
      fill_rect(16,181 - h,18,h,BL)
    essai += 1
    n = int(h * cote / force)
    for i in range(n):
      boule4(RO if i % (4 * cote) > 3 * cote else WH, cote, i)
      boule4(BL, cote, i + 1)
      sleep(.2)
    if (n % (4 * cote)) > 3 * cote:
      parle(PH[1])
      return 1
    elif essai < 3:
      suite()
    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
    defis = list(range(NBJEUX))
    for i in range(4):
      n = choice(defis)
      defis.remove(n)
      cles.append(eval("jeu"+str(1 + n))(niv))
    titre(2,"Vous gagnez")
    parle("{} cles".format(sum(cles)))
    cles = []
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.