puissance4.py

Created by marius-layan

Created on February 05, 2024

1.7 KB

Voici une reconstitution du jeu puissance 4, un je de société bien connu. Le but est simple: Tour à tour, 2 joueurs placent un pion dans la colonne de leur choix, le pion coulisse alors jusqu’à la position la plus basse possible, le vainqueur est le joueur qui réalise le premier un alignement (horizontal, vertical ou diagonal) consécutif d’au moins quatre pions de sa couleur. (je n’ai pas pu implémenter la victoire d’un joueur, donc c’est à vous d’être attentif, comme dans le vrai jeu)

pour changer de colonne: [LEFT] ou [RIGHT]

pour valider: [OK]

BON JEU !


from random import *
from kandinsky import *
from ion import *
def ok():
  global l1,y
  if a==0:
    if l1[0+b]==[]:l1[0+b]=0;y=187
    elif l1[7+b]==[]:l1[7+b]=0;y=152
    elif l1[14+b]==[]:l1[14+b]=0;y=117
    elif l1[21+b]==[]:l1[21+b]=0;y=82
    elif l1[28+b]==[]:l1[28+b]=0;y=47
    elif l1[35+b]==[]:l1[35+b]=0;y=12
  else:
    if l1[0+b]==[]:l1[0+b]=1;y=187
    elif l1[7+b]==[]:l1[7+b]=1;y=152
    elif l1[14+b]==[]:l1[14+b]=1;y=117
    elif l1[21+b]==[]:l1[21+b]=1;y=82
    elif l1[28+b]==[]:l1[28+b]=1;y=47
    elif l1[35+b]==[]:l1[35+b]=1;y=12
  fill_rect(x,y,32,23,colors)
  while keydown(KEY_OK)==True:continue
while True:
  for i in range(8):fill_rect(i*44,0,12,222,'blue')
  for i in range(8):fill_rect(0,i*35,320,12,'blue')
  l1,a,x,win=[[] for i in range(42)],randint(0,1),12,0
  while True:
    if a==0:a=1
    else:a=0
    fill_rect(x,0,32,5,'green')
    if a==0:fill_rect(0,217,320,10,'red')
    else:fill_rect(0,217,320,10,'yellow')
    while True:
      if keydown(KEY_LEFT):
        fill_rect(x,0,32,5,'blue')
        if x>12:x-=44
        fill_rect(x,0,32,5,'green')
        while keydown(KEY_LEFT):continue
      if keydown(KEY_RIGHT):
        fill_rect(x,0,32,5,'blue')
        if x<276:x+=44
        fill_rect(x,0,32,5,'green')
        while keydown(KEY_RIGHT):continue
      if keydown(KEY_OK):
        if a==0:colors='red'
        else:colors='yellow'
        if x==12 and l1[35]==[]:b=0;ok();break
        elif x==56 and l1[36]==[]:b=1;ok();break
        elif x==100 and l1[37]==[]:b=2;ok();break
        elif x==144 and l1[38]==[]:b=3;ok();break
        elif x==188 and l1[39]==[]:b=4;ok();break
        elif x==232 and l1[40]==[]:b=5;ok();break
        elif x==276 and l1[41]==[]:b=6;ok();break