Checkers game in tkinter
CalioPur
-
Phil_1857 Posted messages 1883 Registration date Status Member Last intervention -
Phil_1857 Posted messages 1883 Registration date Status Member Last intervention -
Hello, I want to make a checkers game with tkinter, I’m at the very beginning and I want diagonal squares of the clicked piece to turn green, but it doesn’t work. can you help me? here is my code :
Configuration: Windows / Firefox 85.0
from tkinter import*
WIDTH=400
HEIGHT=400
plateau=[[1,0,1,0,1,0,1,0,1,0],
[0,1,0,1,0,1,0,1,0,1],
[1,0,1,0,1,0,1,0,1,0],
[0,1,0,1,0,1,0,1,0,1],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[2,0,2,0,2,0,2,0,2,0],
[0,2,0,2,0,2,0,2,0,2],
[2,0,2,0,2,0,2,0,2,0],
[0,2,0,2,0,2,0,2,0,2]]
def placerLesPions(cnv,plateau):
for i in range (10):
for j in range (10):
if plateau[i][j]==1:
cnv.create_oval((i*(WIDTH/10),j*(HEIGHT/10),(i*(WIDTH/10)+(WIDTH/10)),(j*(HEIGHT/10)+(HEIGHT/10))), fill='black', outline='')
elif plateau[i][j]==2:
cnv.create_oval((i*(WIDTH/10),j*(HEIGHT/10),(i*(WIDTH/10)+(WIDTH/10)),(j*(HEIGHT/10)+(HEIGHT/10))), fill='bisque', outline='')
def souris(event):
global cnv
#print("x", event.x, end=" ")
#print("y", event.y)
x=int(event.x//(HEIGHT/10))
y=int(event.y//(WIDTH/10))
options(x,y)
def options(x,y):
global cnv
global plateau
if plateau[x][y]==1 or plateau[x][y]==2:
if plateau[x+1][y+1]==0:
plateau[x+1][y+1]=5
if plateau[x+1][y-1]==0:
plateau[x+1][y-1]=5
if plateau[x-1][y+1]==0:
plateau[x-1][y+1]=5
if plateau[x-1][y-1]==0:
plateau[x-1][y-1]=5
for j in range(10):
for i in range (10):
print(plateau[i][j], end=' ')
print()
print()
# Le constructeur Tk
my_root=Tk()
cnv=Canvas(my_root, width=WIDTH, height=HEIGHT, background='papaya whip')
cnv.pack()
for i in range (10):
for j in range (10):
if i==0 or i%2==0:
if j==0 or j%2==0:
cnv.create_rectangle((i*(WIDTH/10),j*(HEIGHT/10),(i*(WIDTH/10)+(WIDTH/10)),(j*(HEIGHT/10)+(HEIGHT/10))), fill='sienna4', outline='')
if i%2==1:
if j%2==1:
cnv.create_rectangle((i*(WIDTH/10),j*(HEIGHT/10),(i*(WIDTH/10)+(WIDTH/10)),(j*(HEIGHT/10)+(HEIGHT/10))), fill='sienna4', outline='')
for i in range (10):
for j in range (10):
if plateau[i][j]==5:
cnv.create_rectangle((i*(WIDTH/10),j*(HEIGHT/10),(i*(WIDTH/10)+(WIDTH/10)),(j*(HEIGHT/10)+(HEIGHT/10))), fill='lime green', outline='')
placerLesPions(cnv,plateau)
cnv.bind("<Button-1>", souris)
my_root.update
my_root.mainloop()
Configuration: Windows / Firefox 85.0
5 answers
Hi,
I haven’t worked with tkinter in a while, but in algorithmics, you could try to create a function that returns a list of squares to green. The function would take the clicked square as input parameters, then you compute the squares by adding/removing an integer value n to i and j, and at each valid step you add the square to the final list as long as the result is not less than 0 (the first square is [0][0]) nor greater than N ([N][N] being the last square).
Then, in an update-display function, you retrieve the return value of the first function as input parameters.
I haven’t worked with tkinter in a while, but in algorithmics, you could try to create a function that returns a list of squares to green. The function would take the clicked square as input parameters, then you compute the squares by adding/removing an integer value n to i and j, and at each valid step you add the square to the final list as long as the result is not less than 0 (the first square is [0][0]) nor greater than N ([N][N] being the last square).
Then, in an update-display function, you retrieve the return value of the first function as input parameters.
I already assign the boxes that are supposed to be green, unfortunately, it’s the display that isn’t working ^^
Hello,
please copy/paste here your complete code with the code tags
how-to:
https://codes-sources.commentcamarche.net/faq/11288-les-balises-de-code
Visually, it should look like this (with syntax highlighting), and not like what you displayed:
please copy/paste here your complete code with the code tags
how-to:
https://codes-sources.commentcamarche.net/faq/11288-les-balises-de-code
Visually, it should look like this (with syntax highlighting), and not like what you displayed:
def test(): print('test') test()
To make it cleaner and more homogeneous, you could even create the board drawing inside a function:
Apart from that, your code displays a checkerboard and prints the array contents when you click a square, but you don’t look for the diagonal to paint it green ...
Also, what’s confusing when you want to locate the coordinates of the squares is that in the plateau variable the 1s (therefore the blacks) are at the top and the 2s (therefore the whites) at the bottom, and graphically the blacks are on the left and the whites on the right
And then, instead of using all these magic numbers, I would do this:
and further:
etc ...
# -*- coding:Latin-1 -*- from tkinter import* WIDTH=400 HEIGHT=400 plateau=[[1,0,1,0,1,0,1,0,1,0], [0,1,0,1,0,1,0,1,0,1], [1,0,1,0,1,0,1,0,1,0], [0,1,0,1,0,1,0,1,0,1], [0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0], [2,0,2,0,2,0,2,0,2,0], [0,2,0,2,0,2,0,2,0,2], [2,0,2,0,2,0,2,0,2,0], [0,2,0,2,0,2,0,2,0,2]] def creer_plateau(): for i in range (10): for j in range (10): if i==0 or i%2==0: if j==0 or j%2==0: cnv.create_rectangle((i*(WIDTH/10),j*(HEIGHT/10),(i*(WIDTH/10)+(WIDTH/10)),(j*(HEIGHT/10)+(HEIGHT/10))), fill='sienna4', outline='') if i%2==1: if j%2==1: cnv.create_rectangle((i*(WIDTH/10),j*(HEIGHT/10),(i*(WIDTH/10)+(WIDTH/10)),(j*(HEIGHT/10)+(HEIGHT/10))), fill='sienna4', outline='') for i in range (10): for j in range (10): if plateau[i][j]==5: cnv.create_rectangle((i*(WIDTH/10),j*(HEIGHT/10),(i*(WIDTH/10)+(WIDTH/10)),(j*(HEIGHT/10)+(HEIGHT/10))), fill='lime green', outline='') def placerLesPions(plateau): for i in range (10): for j in range (10): if plateau[i][j]==1: cnv.create_oval((i*(WIDTH/10),j*(HEIGHT/10),(i*(WIDTH/10)+(WIDTH/10)),(j*(HEIGHT/10)+(HEIGHT/10))), fill='black', outline='') elif plateau[i][j]==2: cnv.create_oval((i*(WIDTH/10),j*(HEIGHT/10),(i*(WIDTH/10)+(WIDTH/10)),(j*(HEIGHT/10)+(HEIGHT/10))), fill='bisque', outline='') def souris(event): global cnv x=int(event.x//(HEIGHT/10)) y=int(event.y//(WIDTH/10)) options(x,y) def options(x,y): global cnv global plateau if plateau[x][y]==1 or plateau[x][y]==2: if plateau[x+1][y+1]==0: plateau[x+1][y+1]=5 if plateau[x+1][y-1]==0: plateau[x+1][y-1]=5 if plateau[x-1][y+1]==0: plateau[x-1][y+1]=5 if plateau[x-1][y-1]==0: plateau[x-1][y-1]=5 for j in range(10): for i in range (10): print(plateau[i][j], end=' ') print() print() my_root=Tk() cnv=Canvas(my_root, width=WIDTH, height=HEIGHT, background='papaya whip') cnv.pack() cnv.bind("<Button-1>", souris) creer_plateau() placerLesPions(plateau) my_root.mainloop() Apart from that, your code displays a checkerboard and prints the array contents when you click a square, but you don’t look for the diagonal to paint it green ...
Also, what’s confusing when you want to locate the coordinates of the squares is that in the plateau variable the 1s (therefore the blacks) are at the top and the 2s (therefore the whites) at the bottom, and graphically the blacks are on the left and the whites on the right
And then, instead of using all these magic numbers, I would do this:
BLACK = 1 WHITE = 2
and further:
if plateau[i][j]==BLACK:
etc ...