puissance4_0.py

Created by florian-allard

Created on July 31, 2020

2.87 KB

Jeu du Puissance 4, basique. La version suivante (améliorée) est disponible ici, une version légèrement optimisée est disponible ici et la version terminée est disponible .


from kandinsky import *
from ion import *
from random import randint
from time import sleep
kd = keydown
KE = KEY_EXE
KO = KEY_OK
purple = (160,0,255)

def init():
  global Jeu
  Jeu=[[0 for m in range(7)] for n in range(6)]
  draw_string(" "*18,70,3)
  draw_string(" "*17,80,21)
  # nettoyage du plateau de jeu
  for i in range(7):
    for j in range(6):
      jeton(i,j,'white')
  
# Affichage du curseur
def curseur(x,couleur='orange'):
  draw_string("v",30+30*x,22,couleur)

# Déplacement du curseur
def deplacement():
  global x
  dx = kd(KEY_RIGHT)-kd(KEY_LEFT)
  if dx !=0:
    curseur(x,'white')#draw_string(" ",45+30*x,32)
    x = (x+dx)%7
    curseur(x,couleur)
    while (dx != 0) and (kd(KEY_LEFT) or kd(KEY_RIGHT)):True

def descente(x):
  global Jeu
  l=0
  while l<6 and Jeu[l][x]==0: # tant que la case est libre
    sleep(0.1)
    if l>0:
      jeton(x,l-1,'white')
      Jeu[l-1][x]=0
    jeton(x,l,couleur)
    Jeu[l][x]=num_joueur
    l+=1
  while kd(KE) or kd(KO):True

def jeton(x,y,couleur):
  draw_string("o",35-5+30*x,55-9+30*y,couleur)

def test_fin():
  #global score ?
  for k in [1,2]:
    if str(k)*4 in chaine:
      draw_string("Joueur"+str(k)+" a gagné",85,3,couleur)
      curseur(x,'white')
      draw_string("Appuyer sur EXE",85,21,purple)
      draw_string("Score : ",240,110,'purple')
      score[k-1]+=1
      draw_string("J"+str(k)+" : "+str(score[k-1]),240,110+18*k,couleur)
      if score[2-k]==0:
        draw_string("J"+str(3-k)+" : 0",240,110+18*(3-k),'blue'*(couleur=='red')+'red'*(couleur=='blue'))
      return True
  return False

def fin():
  global chaine
  for lig in range(6):
    chaine="".join(str(Jeu[lig][col]) for col in range(7))
    if test_fin():
      return True
  for col in range(7):
    chaine="".join(str(Jeu[lig][col]) for lig in range(6))
    if test_fin():
      return True
  for diag in range(3,8+1):
    chaine="".join(str(Jeu[k][diag-k]) for k in range(max(0,diag-6),min(diag,5)+1))
    if test_fin():
      return True
  for diag in range(3,8+1):
    chaine="".join(str(Jeu[k][6-(diag-k)]) for k in range(max(0,diag-6),min(diag,5)+1))
    if test_fin():
      return True
  if sum(int(Jeu[k][l]) for k in range(6) for l in range(7))==63:
    draw_string("Personne n'a gagné",70,3)
    curseur(x,'white')
    draw_string("Appuyer sur EXE",85,21,purple)
    return True
  return False

num_joueur=1 # vaudra 1 ou 2
x=3
for i in range(8): # tracé du plateau
  fill_rect(20+30*i,40,1,181,'orange')
  fill_rect(20,40+min(30*i,30*6),211,1,'orange')
score=[0,0]
init()
while True:
  couleur='blue'*(num_joueur==1)+'red'*(num_joueur==2)
  curseur(x,couleur)
  while not (kd(KE) or kd(KO)):
    deplacement()
  if Jeu[0][x] == 0:
    descente(x)
    tour_fini=1
  while kd(KE) or kd(KO):True
  if tour_fini==1:
    num_joueur = 3-num_joueur
    tour_fini=0
  if fin():
    while not (kd(KE) or kd(KO)):True
    init()
    while kd(KE) or kd(KO):True

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.