de_et_carre.py

Created by florian-allard

Created on July 28, 2025

6.07 KB

Petit jeu de capture de territoire. Chaque joueur lance un dé puis trace autant de segments qu’indiqué par le dé, dans le but de tracer des carrés. Chaque carré entièrement tracé (4 segments) donne un point au joueur qui a terminé son tracé.

En fin de partie, le joueur qui a le plus de points gagne la partie.


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

Plateau=[[0]*6 for i in range(5)]

def dessine_de(x,y,n,clr="k"):
  for j in (0,1):
    fill_rect(x-3,y-4+20*j,19,1,clr)
    fill_rect(x-4+20*j,y-3,1,19,clr)
  motif=(("000","010","000"),("001","000","100"),("001","010","100"),("101","000","101"),("101","010","101"),("101","101","101"))[n-1]
  for i in range(3):
    for j in range(3):
      fill_rect(x+5*i,y+5*j,3,3,("w",clr)[int(motif[j][i])])

def dessine_curseur(x,y,horiz,clr):
  dX=82+30*x
  dY=33+30*y
  if horiz:
    for i in range(2):
      fill_rect(dX+5,dY+5*i,26,1,clr)
      fill_rect(dX+5+25*i,dY,1,5,clr)
  else:
    for i in range(2):
      fill_rect(dX,dY+5+25*i,5,1,clr)
      fill_rect(dX+5*i,dY+5,1,26,clr)

def dessine_grille(x1=0,x2=6,y1=0,y2=5):
  for i in range(y1,y2):
    for j in range(x1,x2):
      fill_rect(82+30*j+1,33+30*i+1,4,4,"cyan")

def remplir_curseur(x,y,horiz,clr):
  dX=82+30*x
  dY=33+30*y
  if horiz:
    fill_rect(dX+5+1,dY+1,24,4,clr)
  else:
    fill_rect(dX+1,dY+5+1,4,24,clr)

draw_string("Dé et carré",105,70,"purple")
#draw_string("(?!)",120,95,"purple")
draw_string("Valider avec OK",85,135,"purple")
for k in range(2): 
  fill_rect(70,50+k*121,180,1,"g")

for k in range(6):
  dessine_de(104+50*(k%3),19+171*(k//3),randint(1,6))

dessine_grille(-2,0,1,5)
for k in (0,1):
  dessine_grille(-2+8*k,0+8*k,1,5)
  for (i,j,h) in ((-2,1,1),(-2,1,0),(-2,2,1),(-1,1,0),(-2+k,2,0),(-2,3,1),(-2,3,0),(-1,3,0),(-2,4,1)):
    clr="rg"[randint(0,1)]
    remplir_curseur(i+8*k,j,h,clr)
    if (i,j,h) in ((-2,1,0),(-2,3,1)):
      fill_rect(82+30*i+30*8*k+10,33+30*j+10,15,15,clr)


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

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

draw_string("Présentation",100,10,(80,150,50))
Regles=("Chaque joueur lance un dé","puis trace autant de segments","qu'indiqué par le dé et gagne","un point si un des segments","tracés referme un carré de","côté 1.","","Enfin, le joueur qui a le","plus de points gagne.")
for k in range(len(Regles)):
  draw_string(Regles[k],20,33+18*k,"b")

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

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

draw_string("Dé et carré",105,5,"b")
dessine_grille()
#dessine_curseur(2,3,1,"b")
#dessine_curseur(2,3,0,"b")

Xc,Yc=3,3
chgX,chgY=1,0
mode_horiz=1
joueur=0
scores=[0,0]
for j in (0,1):
  draw_string("Joueur",10+240*j,50,"rg"[j])
  draw_string(("rouge","vert")[j],15+245*j,70,"rg"[j])
  draw_string(str(scores[j]),46+239*j,95,"rg"[j])
  fill_rect(21+239*j,95,15,15,"rg"[j])




while sum(scores)<20:
  for joueur in range(2):
    # lancer du dé
    D=randint(1,6)
    #draw_string(str(D),140,180,"rg"[joueur])
    draw_string(" "*22,50,185,"w")
    if sum(scores)==20:
      #draw_string(" "*22,50,185,"w")
      break
    dessine_de(34+239*(1-joueur),140,6,"w")
    for i in range(20):
      dessine_de(34+239*joueur,140,randint(1,6))
      sleep(0.1)
    dessine_de(34+239*joueur,140,D)
    for d in range(D):
      draw_string("Il reste "+str(D-d)+" coup"+"s"*(D-d>1)+" sur "+str(D)+" ",50,185,"rg"[joueur])
      if sum(scores)==20:
        draw_string(" "*22,50,185,"w")
        break
      #Xc,Yc=3,3
      #chgX=1
      while not ((keydown(KEY_EXE) or keydown(KEY_OK)) and ((Plateau[Yc][Xc]%2 != 1 and mode_horiz) or (Plateau[Yc][Xc]<2 and not mode_horiz))):
        if chgX or chgY:
          dessine_curseur(Xc,Yc,mode_horiz,"w")
          if not mode_horiz and chgX==1 and Xc<5:
            Xc-=1
          if mode_horiz and chgY==1 and Yc<4:
            Yc-=1
          if (Xc,Yc)==(4,4) and chgX==1:
            Yc-=1
          if (Xc,Yc)==(5,3) and chgY==1:
            Xc-=1
          if (Xc,Yc)==(0,4) and mode_horiz and chgX==-1:
            Yc-=1
          if (Xc,Yc)==(5,0) and not mode_horiz and chgY==-1:
            Xc-=1
          mode_horiz=(abs(chgX) and not (Xc>3 and chgX==1 or Xc==0 and chgX==-1 and Yc!=4)) or (Yc>2 and chgY==1 and Xc!=5) or (Yc==0 and chgY==-1 and Xc!=5)#(Xc<4 and abs(chgX)) or (Yc>2 and chgY==1) or (Yc==4 and abs(chgX))
          Xc=max(0,min(5,(Xc+chgX)))
          Yc=max(0,min(4,(Yc+chgY)))
          dessine_curseur(Xc,Yc,mode_horiz,"b")
          #draw_string(str(Xc)+" "+str(Yc),150,160,"purple")
          sleep(0.2)
        chgX=keydown(KEY_RIGHT)-keydown(KEY_LEFT)
        chgY=keydown(KEY_DOWN)-keydown(KEY_UP)
      while keydown(KEY_EXE) or keydown(KEY_OK):1
      if Plateau[Yc][Xc]%2 == 0 and mode_horiz: # horiz : 1  -- verti : 2 -- les deux : 3
        remplir_curseur(Xc,Yc,mode_horiz,"rg"[joueur])
        Plateau[Yc][Xc]+=1
        # carré fermé par son côté du haut
        if Xc<5 and Yc<4:
          if Plateau[Yc][Xc]>1 and Plateau[Yc][Xc+1]>1 and Plateau[Yc+1][Xc]%2==1:
            fill_rect(82+30*Xc+10,33+30*Yc+10,15,15,"rg"[joueur])
            scores[joueur]+=1
        # carré fermé par son côté du bas
        if Xc<5 and Yc>0:
          if Plateau[Yc-1][Xc]>1 and Plateau[Yc-1][Xc+1]>1 and Plateau[Yc-1][Xc]%2==1:
            fill_rect(82+30*Xc+10,33+30*Yc-30+10,15,15,"rg"[joueur])
            scores[joueur]+=1
        #joueur=1-joueur
      if Plateau[Yc][Xc]<2 and not mode_horiz:
        remplir_curseur(Xc,Yc,mode_horiz,"rg"[joueur])
        Plateau[Yc][Xc]+=2
      # carré fermé par son côté gauche
        if Xc<5 and Yc<4:
          if Plateau[Yc][Xc+1]>1 and Plateau[Yc][Xc]%2==1 and Plateau[Yc+1][Xc]%2==1:
            fill_rect(82+30*Xc+10,33+30*Yc+10,15,15,"rg"[joueur])
            scores[joueur]+=1
        # carré fermé par son côté droit
        if Xc>0 and Yc<4:
          if Plateau[Yc][Xc-1]>1 and Plateau[Yc][Xc-1]%2==1 and Plateau[Yc+1][Xc-1]%2==1:
            fill_rect(82+30*Xc-30+10,33+30*Yc+10,15,15,"rg"[joueur])
            scores[joueur]+=1
        #joueur=1-joueur
      draw_string(str(scores[joueur]),45+240*joueur,95,"rg"[joueur])

if scores[0]==scores[1]:
  draw_string("Egalité",120,180,"b")
else:
  draw_string("Joueur "+("rouge","vert")[scores[0]<scores[1]]+" a gagné avec",35,170,"rg"[scores[0]<scores[1]])
  draw_string(str(max(scores))+" cases remplies sur 20 !",30,190,"rg"[scores[0]<scores[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.