chess2.py

Created by schraf

Created on September 15, 2022

1.81 KB

La variable part contient la partie à jouer.

Amélioration possible : Appuie d’une touche pour passer au coup suivant

Playlist Youtube sur la NUMWORKS (jeux, programmation Python…)


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

# Pieces echecs
P = [0,120,252,252,120,120,252,510,510,0]
T = [0,438,438,510,252,252,252,510,510,0]
C = [0,56,124,124,240,248,252,510,510,0]
F = [48,56,156,220,478,510,252,510,510,0]
R = [48,120,48,252,510,252,252,510,510,0]
D = [72,120,306,438,180,252,252,510,510,0]
pos="TCFDRFCTPPPPPPPP"
part="e2e4 e7e5 g1f3 b8c6 f1b5 a7a6 b5a4 g8f6 d2d4 e5d4 e4e5 f6e4 e1g1 h1f1 f8e7 f1e1 e4c5 a4c6 d7c6 f3d4 e8g8 h8f8 b1c3 c5e6 d4f5 e7g5 d1g4 e6d4 c1g5 c8f5 g4g3 d8c8 a1d1 d4c2 e1e2 c8e6 g5f6 f5g6 f6g7 g8g7 e2c2 a8d8 c2d2 e6e7 h2h4 g7h8 g3g5 e7g5 h4g5 d8d2 d1d2 g6f5 f2f4 h8g7 g1f2 h7h5 g5h6+ g7h6 f2f3 f8g8 c3e4 f5g4+ f3e3 h6g6 e4f6"
g,h=30,30

def dessin(x,y,p,co=0):
  car(g+20*x,h+20*y,20,coul(x,y))
  if p != "":
   for c in range(10):
     for l in range(10):
      if p[l]>>c & 1:
        car(g+20*x+2*c,h+20*y+2*l,2,co)

def coul(x,y):
  return (110,110,220) if (x+y)%2==0 else (0,0,0)

def jouer(i,v):
  x1, y1 = ord(v[0])-97,8-int(v[1])
  x2, y2 = ord(v[2])-97,8-int(v[3])
  for c in range(20):
    sleep(.01)
    for l in range(20):
      p = get_pixel(g+20*x1+c,h+20*y1+l)  
      if p in [(240,148,48),(248,252,248)]:
        set_pixel(g+20*x2+c,h+20*y2+l,p)
      else:
        set_pixel(g+20*x2+c,h+20*y2+l,coul(x2,y2))
      set_pixel(g+20*x1+c,h+20*y1+l,coul(x1,y1)) 
      

def jeu(p):
  for i,v in enumerate(p.split(" ")):
    jouer(i,v)
    sleep(.5)
  return "FIN"  
    
def car(x,y,t,c):
  for i in range(t*t): 
    set_pixel(x+i%t,y+i//t,c)
    
def go(part):
  for c in range(8):
    for l in range(8):
      dessin(c,l,"")       
    draw_string(str(c+1),g-20,h+1+7*20-20*c)
    draw_string(chr(97+c),g+5+20*c,h-20)
  for v in range(16):
    dessin(v%8,v//8,eval(pos[v]),(240,150,50))
    dessin(v%8,7-(v//8),eval(pos[v]),(248,255,248))
  jeu(part)
go(part)