solitaire_billes.py

Created by florian-allard

Created on July 15, 2025

5.25 KB

Jeu du solitaire avec les billes (ou bâtonnets). On choisit une bille (avec les flèches, validation avec OK) puis la direction dans laquelle on veut la déplacer. Le seul déplacement autorisé est celui qui permet de passer par-dessus une autre bille tout en arrivant sur une case libre. La bille par-dessus laquelle on a sauté est alors retirée du plateau.

Le but est de n’avoir plus qu’une bille à la fin de la partie, sans être à court de déplacement avant.


from kandinsky import *
from ion import *
from time import *

plateau = []

def initialiser():
  global plateau
  plateau=[[[0,1][1<k<5 or 1<i<5] for k in range(7)] for i in range(7)]
  plateau[3][3]=0

#for k in range(7):
#  print(*plateau[k])

def afficher_piece(x,y,clr):
  #fill_rect(72+25*x,15+25*y,16,16,clr)
  dX=72+25*x
  dY=15+25*y
  fill_rect(dX+5,dY,6,1,clr)
  fill_rect(dX+3,dY+1,10,1,clr)
  fill_rect(dX+2,dY+2,12,1,clr)
  fill_rect(dX+1,dY+3,14,2,clr)
  fill_rect(dX,dY+5,16,6,clr)
  fill_rect(dX+1,dY+11,14,2,clr)
  fill_rect(dX+2,dY+13,12,1,clr)
  fill_rect(dX+3,dY+14,10,1,clr)
  fill_rect(dX+5,dY+15,6,1,clr)

def afficher_plateau():
  for k in range(7):
    for i in range(7):
      if 1<k<5 or 1<i<5:
        afficher_piece(k,i,"wb"[plateau[i][k]])

def afficher_curseur(k,i,clr):
  for a in range(2):
    fill_rect(72-3+25*k,15-3+25*i+21*a,22,1,clr)
    fill_rect(72-3+25*k+21*a,15-3+25*i,1,22,clr)



draw_string("Problème du solitaire",55,80,"purple")
draw_string("Valider avec OK",85,130,"purple")
for k in range(2): 
  fill_rect(60,50+k*122,200,1,"g")

for k in range(8):
  afficher_piece(-1+3*(k%4),7*(k//4),"brgbbgrb"[k])
#for carte in Pioche:
#  DessineCarte(8,20,carte)
#  sleep(1)
#  DessineCarte(8,20,carte,effacement=True)
while not (keydown(KEY_EXE) or keydown(KEY_OK)):1
while keydown(KEY_EXE) or keydown(KEY_OK):1

while 1:
  fill_rect(0,0,320,222,"w")

  draw_string("Présentation",100,10,(80,150,50))
  Regles=("Le but est d'avoir une seule","bille restante. Une bille est","retirée du plateau lorsqu'une","autre bille lui est passé","par-dessus en arrivant sur","un emplacement vide.","","Sélectionner une pièce : OK","Déplacer une pièce : flèches","Réinitialiser le jeu : Home")
  for k in range(len(Regles)):
    draw_string(Regles[k],20,33+18*k,"b")

  #draw_string("Valider avec OK",85,192,"purple")

  while not (keydown(KEY_EXE) or keydown(KEY_OK)):1
  while keydown(KEY_EXE) or keydown(KEY_OK):1

  fill_rect(0,0,320,222,"w")

  dX=20
  dY=8
  H=200-20
  for k in range(H):
    fill_rect(dX+max(H//4,abs(H//2-k)),dY+k,min(H,3*H//2-abs(2*k-H)),1,"cyan")

  initialiser()
  afficher_plateau()
  #afficher_cadre(3,5,"r")
  #afficher_cadre(3,1,"r")
  #afficher_cadre(4,2,"r")

  #print(str(sum(sum(plateau[k]) for k in range(7))))
  x=3
  y=3
  chgX=-1
  chgY=0

  while sum(sum(plateau[k]) for k in range(7))>1:
    if keydown(KEY_HOME):
      initialiser()
      afficher_plateau()
    
    if chgX or chgY:
      afficher_curseur(x,y,"cyan")
      x=(x+chgX)%7
      y=(y+chgY)%7
      while not (1<x<5 or 1<y<5):
        x=(x+chgX)%7
        y=(y+chgY)%7
      afficher_curseur(x,y,"b")
      sleep(0.2)
    chgX=keydown(KEY_RIGHT)-keydown(KEY_LEFT)
    chgY=keydown(KEY_DOWN)-keydown(KEY_UP)

    if plateau[y][x]==1 and (keydown(KEY_EXE) or keydown(KEY_OK)):
      while keydown(KEY_OK) or keydown(KEY_EXE):1
      while not (keydown(KEY_OK) or keydown(KEY_EXE)):
        coups_valides=0
        # droite
        if (x<3) or (x<5 and 1<y<5):
          if plateau[y][x+1]==1 and plateau[y][x+2]==0: #coup valide
            coups_valides+=1
            afficher_piece(x+1,y,"g")
            afficher_curseur(x,y,"g")
            if keydown(KEY_RIGHT):
              plateau[y][x]=0
              plateau[y][x+1]=0
              plateau[y][x+2]=1
              afficher_curseur(x,y,"cyan")
              x+=2
              afficher_curseur(x,y,"b")
              afficher_plateau()
              sleep(0.2)
              break
        # gauche
        if (x>3) or (x>1 and 1<y<5):
          if plateau[y][x-1]==1 and plateau[y][x-2]==0: #coup valide
            coups_valides+=1
            afficher_piece(x-1,y,"g")
            afficher_curseur(x,y,"g")
            if keydown(KEY_LEFT):
              plateau[y][x]=0
              plateau[y][x-1]=0
              plateau[y][x-2]=1
              afficher_curseur(x,y,"cyan")
              x-=2
              afficher_curseur(x,y,"b")
              afficher_plateau()
              sleep(0.2)
              break
        # haut
        if (y>3) or (y>1 and 1<x<5):
          if plateau[y-1][x]==1 and plateau[y-2][x]==0: #coup valide
            coups_valides+=1
            afficher_piece(x,y-1,"g")
            afficher_curseur(x,y,"g")
            if keydown(KEY_UP):
              plateau[y][x]=0
              plateau[y-1][x]=0
              plateau[y-2][x]=1
              afficher_curseur(x,y,"cyan")
              y-=2
              afficher_curseur(x,y,"b")
              afficher_plateau()
              sleep(0.2)
              break
        # bas
        if (y<3) or (y<5 and 1<x<5):
          if plateau[y+1][x]==1 and plateau[y+2][x]==0: #coup valide
            coups_valides+=1
            afficher_piece(x,y+1,"g")
            afficher_curseur(x,y,"g")
            if keydown(KEY_DOWN):
              plateau[y][x]=0
              plateau[y+1][x]=0
              plateau[y+2][x]=1
              afficher_curseur(x,y,"cyan")
              y+=2
              afficher_curseur(x,y,"b")
              afficher_plateau()
              sleep(0.2)
              break
        if coups_valides==0:
          break
      if keydown(KEY_OK) or keydown(KEY_EXE) or coups_valides==0:
        afficher_plateau()
        afficher_curseur(x,y,"b")
      while keydown(KEY_OK) or keydown(KEY_EXE):1

  draw_string("Félicitations !",85,204,"purple")

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.