Hello, I would like to create a chess game GUI but I have a problem with my code. When I run my programm, at the beginning, my code is supposed to read a ".lvl" file to place pieces on the chessboard (same method as in Mario Sokoban). However, it doesn't work because pieces aren't display. Therefore, I would like to know : "What's wrong with my code ?"
Thanks in advance for your help :).
from tkinter import *
root=Tk()
#Variables for draw_board()
rows = 8
columns = 8
color1 = "#b35821" #Flery Orange
color2 = "#efcb9d" #New Tan
dim_square = 64
tile_center = dim_square/2
tiles=8
FEN_file="FEN_board.lvl"
#Variables for get_position()
x1=0
y1=0
x2=0
y2=0
clic=0
tour=0
tb=1800
tw=1800
clic1=0
#Variables for draw_pieces()
#k == King
#q == queen
#r == Rook
#b == Bishop
#n == Knight
#p == Pawn
#Importation Images Pièces NOIRES
blackb=PhotoImage(file="./pieces_image/blackb.gif")
blackk=PhotoImage(file="./pieces_image/blackk.gif")
blackn=PhotoImage(file="./pieces_image/blackn.gif")
blackp=PhotoImage(file="./pieces_image/blackp.gif")
blackq=PhotoImage(file="./pieces_image/blackq.gif")
blackr=PhotoImage(file="./pieces_image/blackr.gif")
#Importation Images Pièces BLANCHES
whiteb=PhotoImage(file="./pieces_image/whiteb.gif")
whitek=PhotoImage(file="./pieces_image/whitek.gif")
whiten=PhotoImage(file="./pieces_image/whiten.gif")
whitep=PhotoImage(file="./pieces_image/whitep.gif")
whiteq=PhotoImage(file="./pieces_image/whiteq.gif")
whiter=PhotoImage(file="./pieces_image/whiter.gif")
pieces=[blackb,blackk,blackn,blackp,blackq,blackr,whiteb,whitek,whiten,whitep,whiteq,whiter]
canvas=Canvas(root, width=512, height=512)
canvas.pack(expand=1, fill=BOTH)
board_representation=[]
for i in range(tiles):
board_representation.append([""]*tiles)
def FEN_file_check(fname):
try:
f = open(fname,'r')
f.close()
return True
except:
return False
def interpretor(line):
i,j=0,0
for letter in line:
if letter=="r" or letter=="n" or letter=="b" or letter=="q" or letter=="k" or letter=="p" or letter=="R" or letter=="N" or letter=="B" or letter=="Q" or letter=="K" or letter=="P":
board_representation[i][j]=letter
for p in pieces:
piecesx=i
piecesy=j
if i<tiles-1:
i=i+1
elif j<tiles-1:
j=j+1
i=0
def display_game(turn):
canvas.delete(ALL)
draw_chessboard()
i,j=0,0
for i in range(tiles):
for j in range(tiles):
board_representation[i][j]=" "
if FEN_file_check(FEN_file):
fich=open(FEN_file,"r")
i= 1
while i<=turn:
line=fich.readline()
i=i+1
if line =="":
messagebox.showinfo("ERROR, EMPTY FILE !")
else :
interpretor(line)
fich.close
canvas.bind('<Button-1>', move_image)
canvas.bind('<Button-1>', get_position, add="+")
else:
messagebox.showerror("ERROR","FILE NOT FOUND !")
def refresh():
canvas.delete(ALL)
i,j=0,0
for i in range(tiles):
for j in range(tiles):
if board_representation[i][j]=="r" :
canvas.create_image(tile_center+i*dim_square,tile_center+j*dim_square,image=blackr)
canvas.pack()
elif board_representation[i][j]=="n" :
canvas.create_image(tile_center+i*dim_square,tile_center+j*dim_square,image=blackn)
canvas.pack()
elif board_representation[i][j]=="b" :
canvas.create_image(tile_center+i*dim_square,tile_center+j*dim_square,image=blackb)
canvas.pack()
elif board_representation[i][j]=="q" :
canvas.create_image(tile_center+i*dim_square,tile_center+j*dim_square,image=blackq)
canvas.pack()
elif board_representation[i][j]=="k" :
canvas.create_image(tile_center+i*dim_square,tile_center+j*dim_square,image=blackk)
canvas.pack()
elif board_representation[i][j]=="p" :
canvas.create_image(tile_center+i*dim_square,tile_center+j*dim_square,image=blackp)
canvas.pack()
elif board_representation[i][j]=="R" :
canvas.create_image(tile_center+i*dim_square,tile_center+j*dim_square,image=whiter)
canvas.pack()
elif board_representation[i][j]=="N" :
canvas.create_image(tile_center+i*dim_square,tile_center+j*dim_square,image=whiten)
canvas.pack()
elif board_representation[i][j]=="B" :
canvas.create_image(tile_center+i*dim_square,tile_center+j*dim_square,image=whiteb)
canvas.pack()
elif board_representation[i][j]=="Q" :
canvas.create_image(tile_center+i*dim_square,tile_center+j*dim_square,image=whiteq)
canvas.pack()
elif board_representation[i][j]=="K" :
canvas.create_image(tile_center+i*dim_square,tile_center+j*dim_square,image=whitek)
canvas.pack()
elif board_representation[i][j]=="P" :
canvas.create_image(tile_center+i*dim_square,tile_center+j*dim_square,image=whitep)
canvas.pack()
def move_validation(xdirection,ydirection):
global turn
move=False
if board_representation[piecesx+xdirection][piecesy+ydirection]==" ":
move=True
if move==True:
piecesx=piecesx+xdirection
piecesy=piecesy+ydirection
refresh()
def draw_chessboard():
color = color2
for r in range(rows):
color = color1 if color == color2 else color2
for c in range(columns):
x1 = (c * dim_square)
y1 = ((7-r) * dim_square)
x2 = x1 + dim_square
y2 = y1 + dim_square
canvas.create_rectangle(x1, y1, x2, y2, fill=color, tags="area")
color = color1 if color == color2 else color2
def draw_pieces():
canvas.create_image(32,32, image=blackk, anchor=CENTER, state=NORMAL, tags="blackk")
canvas.create_image(96,32, image=blackp, anchor=CENTER, state=NORMAL, tags="blackp")
def get_position(event):
global clic,x1,y1,x2,y2,tour,tb,tw,clic1
if clic==0:
x1=event.x
y1=event.y
coordinates.delete(0.0,END)
coordinates.insert(END,"clic detecte en x1="+str(x1)+" , y1 = " +str(y1))
clic=1
elif clic==1:
x2=event.x
y2=event.y
coordinates.delete(0.0,END)
coordinates.insert(END,"clic detecte en x2="+str(x2)+" , y2 = " +str(y2))
clic=0
tour=(tour+1)
if (tour%2)==1:
clic1=1
if (tour%2)==0:
clic1=2
def move_image(event):
global x1,y1,x2,y2,img, init_image
init_image=[]
if clic==0: #First Clic
x1,y1=canvas.coords("blackk")
closest=canvas.find_closest(x1,x1,halo=32)
x1=int(x1)
y1=int(y1)
init_image.append(x1)
init_image.append(y1)
print(init_image)
elif clic==1: #Second Clic
x2=event.x
y2=event.y
canvas.delete("blackk")
canvas.create_image(x2, y2, image=blackk,anchor=CENTER,tags="blackk")
draw_chessboard()
"""draw_pieces()"""
turn = 1
display_game(1)
canvas.bind('<Button-1>', move_image)
canvas.bind('<Button-1>', get_position, add="+")
coordinates=Text(root,height=2)
coordinates.pack()
root.mainloop()