boulesbleues.py

Created by florian-allard

Created on August 10, 2024

2.91 KB

Il faut retrouver la position des 3 boules bleues parmi les 5 boules. À chaque essai, le nombre de boules bleues bien placées est donné (toujours entre 1 et 3).

On joue avec les flèches et la touche OK.


from math import *
from random import randint
from ion import *
from kandinsky import *
from time import sleep

cyan=(0,255,255)
bleu=(100,100,255)
violet_clair=(255,160,255)
marron=(100,50,0)
vert_clair=(100,255,100)
vert_fonce=(0,150,0)

def boule(x,y,clr):
  fill_rect(x+3,y,4,1,clr)
  fill_rect(x+1,y+1,8,2,clr)
  fill_rect(x,y+3,10,4,clr)
  fill_rect(x+1,y+7,8,2,clr)
  fill_rect(x+3,y+9,4,1,clr)


draw_string("Les trois boules bleues",45,10,"b")
fill_rect(100,33,120,1,cyan)
draw_string("Retrouve l'emplacement des",30,40,"b")
draw_string("trois boules bleues.",60,60,"b")
draw_string("Déplace-les avec les flèches",20,90,"purple")
draw_string("gauche et droite.",80,110,"purple")
draw_string("Valide avec EXE ou OK.",50,140,"purple")
fill_rect(100,163,120,1,cyan)
draw_string("Choix de la difficulté : ",35,170,vert_fonce)
draw_string("7 essais  5 essais  3 essais",20,190,vert_fonce)
sleep(2)
choix=1
chg=-1
while not (keydown(KEY_EXE) or keydown(KEY_OK)):
  if chg !=0 :
    for k in (0,1):
      draw_string("[]"[k],7+100*choix+96*k,190,"w")
    choix=(choix+chg)%3
    for k in (0,1):
      draw_string("[]"[k],7+100*choix+96*k,190,"r")
    sleep(0.3)
  chg=keydown(KEY_RIGHT)-keydown(KEY_LEFT)
while keydown(KEY_EXE) or keydown(KEY_OK):1

while 1:
  fill_rect(0,0,320,222,"brown")
  fill_rect(20,10,280,202,marron)
  draw_string("Trouve l'emplacement",60,15,violet_clair,marron)
  draw_string("des 3 boules bleues.",60,30,violet_clair,marron)


  N = 0
  while bin(N)[2:].count("1") != 3:
    N = randint(7,28)
    #N="0"*(5-len(N))+N

  Nbin = bin(N)[2:]
  Nbin = "0"*(5-len(Nbin))+Nbin
    
  #draw_string(Nbin,120,0)
  nbessais = 0
  D = 27
  chg = 1

  while D != N and nbessais < 2*(2-choix)+3:
    sleep(0.5)
    Dbin = "0"*(5-len(bin(D)[2:]))+bin(D)[2:]
    for r in range(5):
      boule(50+15*r,55+20*nbessais,("w",bleu)[int(Dbin[r])])
      #draw_string("0",50+15*r,60+20*nbessais,"wb"[int(Dbin[r])],marron)    
    while not (keydown(KEY_EXE) or keydown(KEY_OK)):
      if chg:
        D = ((D+chg)-7)%22+7
        while bin(D)[2:].count("1") != 3:
          D = ((D+chg)-7)%22+7
        #print(D)
        #print("0"*(5-len(bin(D)[2:]))+bin(D)[2:])
        Dbin = "0"*(5-len(bin(D)[2:]))+bin(D)[2:]
        for r in range(5):
          boule(50+15*r,55+20*nbessais,("w",bleu)[int(Dbin[r])])
          #draw_string("0",50+15*r,60+20*nbessais,("w",bleu)[int(Dbin[r])],marron)
        sleep(0.2)
      chg = -keydown(KEY_RIGHT)+keydown(KEY_LEFT)
    
    while keydown(KEY_EXE) or keydown(KEY_OK):1
    NbOk = sum(int(Nbin[k])*int(Dbin[k]) for k in range(5))
    draw_string(str(NbOk)+" bien placée"+"s"*(NbOk>1),136,50+20*nbessais,cyan,marron)
    nbessais += 1

  sleep(1)
  #fill_rect(45,185,230,30,marron)
  draw_string(("Réussi en "+str(nbessais)+" coup"+"s"*(nbessais>1)+" !!!","Perdu, essaye encore.")[N!=D],55,188,vert_clair,marron)
  while not (keydown(KEY_EXE) or keydown(KEY_OK)):1
  while keydown(KEY_EXE) or keydown(KEY_OK):1

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.