Apr-05-2017, 06:55 PM
I have been trying to work on coding a chess game for some fun, i know it is very inefficient and so forth but this does not bother me largely as i am not that great at python. so if i could get some help with this problem and as to why its happening it would be much appreciated.
from tkinter import * HEIGHT = 800 WIDTH = 800 window = Tk() window.title('BOARD') c = Canvas(window, width=WIDTH, height=HEIGHT, bg='grey') c.pack() clickt = 0 piece = 0 def grid(): block = 800/8 a0 = c.create_polygon(0 ,0, block, 0, block, block, 0, block, fill='white', outline='grey') a1 = c.create_polygon(block, 0, 2*block, 0, 2*block, block, block, block, fill='grey', outline='grey') a2 = c.create_polygon(2*block, 0, 3*block, 0, 3*block, block, 2*block, block, fill='white', outline='grey') a3 = c.create_polygon(3*block, 0, 4*block, 0, 4*block, block, 3*block, block, fill='grey', outline='grey') a4 = c.create_polygon(4*block, 0, 5*block, 0, 5*block, block, 4*block, block, fill='white', outline='grey') a5 = c.create_polygon(5*block, 0, 6*block, 0, 6*block, block, 5*block, block, fill='grey', outline='grey') a6 = c.create_polygon(6*block, 0, 7*block, 0, 7*block, block, 6*block, block, fill='white', outline='grey') a7 = c.create_polygon(7*block, 0, 8*block, 0, 8*block, block, 7*block, block, fill='grey', outline='grey') b0 = c.create_polygon(0, block, block, block, block, 2*block, 0, 2*block, fill='grey', outline='grey') b1 = c.create_polygon(block, block, 2*block, block, 2*block, 2*block, block, 2*block, fill='white', outline='grey') b2 = c.create_polygon(2*block, block, 3*block, block, 3*block, 2*block, 2*block, 2*block, fill='grey', outline='grey') b3 = c.create_polygon(3*block, block, 4*block, block, 4*block, 2*block, 3*block, 2*block, fill='white', outline='grey') b4 = c.create_polygon(4*block, block, 5*block, block, 5*block, 2*block, 4*block, 2*block, fill='grey', outline='grey') b5 = c.create_polygon(5*block, block, 6*block, block, 6*block, 2*block, 5*block, 2*block, fill='white', outline='grey') b6 = c.create_polygon(6*block, block, 7*block, block, 7*block, 2*block, 6*block, 2*block, fill='grey', outline='grey') b7 = c.create_polygon(7*block, block, 8*block, block, 8*block, 2*block, 7*block, 2*block, fill='white', outline='grey') c0 = c.create_polygon(0 ,2*block, block, 2*block, block, 3*block, 0, 3*block, fill='white', outline='grey') c1 = c.create_polygon(block, 2*block, 2*block, 2*block, 2*block, 3*block, block, 3*block, fill='grey', outline='grey') c2 = c.create_polygon(2*block, 2*block, 3*block, 2*block, 3*block, 3*block, 2*block, 3*block, fill='white', outline='grey') c3 = c.create_polygon(3*block, 2*block, 4*block, 2*block, 4*block, 3*block, 3*block, 3*block, fill='grey', outline='grey') c4 = c.create_polygon(4*block, 2*block, 5*block, 2*block, 5*block, 3*block, 4*block, 3*block, fill='white', outline='grey') c5 = c.create_polygon(5*block, 2*block, 6*block, 2*block, 6*block, 3*block, 5*block, 3*block, fill='grey', outline='grey') c6 = c.create_polygon(6*block, 2*block, 7*block, 2*block, 7*block, 3*block, 6*block, 3*block, fill='white', outline='grey') c7 = c.create_polygon(7*block, 2*block, 8*block, 2*block, 8*block, 3*block, 7*block, 3*block, fill='grey', outline='grey') d0 = c.create_polygon(0, 3*block, block, 3*block, block, 4*block, 0, 4*block, fill='grey', outline='grey') d1 = c.create_polygon(block, 3*block, 2*block, 3*block, 2*block, 4*block, block, 4*block, fill='white', outline='grey') d2 = c.create_polygon(2*block, 3*block, 3*block, 3*block, 3*block, 4*block, 2*block, 4*block, fill='grey', outline='grey') d3 = c.create_polygon(3*block, 3*block, 4*block, 3*block, 4*block, 4*block, 3*block, 4*block, fill='white', outline='grey') d4 = c.create_polygon(4*block, 3*block, 5*block, 3*block, 5*block, 4*block, 4*block, 4*block, fill='grey', outline='grey') d5 = c.create_polygon(5*block, 3*block, 6*block, 3*block, 6*block, 4*block, 5*block, 4*block, fill='white', outline='grey') d6 = c.create_polygon(6*block, 3*block, 7*block, 3*block, 7*block, 4*block, 6*block, 4*block, fill='grey', outline='grey') d7 = c.create_polygon(7*block, 3*block, 8*block, 3*block, 8*block, 4*block, 7*block, 4*block, fill='white', outline='grey') e0 = c.create_polygon(0 ,4*block, block, 4*block, block, 5*block, 0, 5*block, fill='white', outline='grey') e1 = c.create_polygon(block, 4*block, 2*block, 4*block, 2*block, 5*block, block, 5*block, fill='grey', outline='grey') e2 = c.create_polygon(2*block, 4*block, 3*block, 4*block, 3*block, 5*block, 2*block, 5*block, fill='white', outline='grey') e3 = c.create_polygon(3*block, 4*block, 4*block, 4*block, 4*block, 5*block, 3*block, 5*block, fill='grey', outline='grey') e4 = c.create_polygon(4*block, 4*block, 5*block, 4*block, 5*block, 5*block, 4*block, 5*block, fill='white', outline='grey') e5 = c.create_polygon(5*block, 4*block, 6*block, 4*block, 6*block, 5*block, 5*block, 5*block, fill='grey', outline='grey') e6 = c.create_polygon(6*block, 4*block, 7*block, 4*block, 7*block, 5*block, 6*block, 5*block, fill='white', outline='grey') e7 = c.create_polygon(7*block, 4*block, 8*block, 4*block, 8*block, 5*block, 7*block, 5*block, fill='grey', outline='grey') f0 = c.create_polygon(0, 5*block, block, 5*block, block, 6*block, 0, 6*block, fill='grey', outline='grey') f1 = c.create_polygon(block, 5*block, 2*block, 5*block, 2*block, 6*block, block, 6*block, fill='white', outline='grey') f2 = c.create_polygon(2*block, 5*block, 3*block, 5*block, 3*block, 6*block, 2*block, 6*block, fill='grey', outline='grey') f3 = c.create_polygon(3*block, 5*block, 4*block, 5*block, 4*block, 6*block, 3*block, 6*block, fill='white', outline='grey') f4 = c.create_polygon(4*block, 5*block, 5*block, 5*block, 5*block, 6*block, 4*block, 6*block, fill='grey', outline='grey') f5 = c.create_polygon(5*block, 5*block, 6*block, 5*block, 6*block, 6*block, 5*block, 6*block, fill='white', outline='grey') f6 = c.create_polygon(6*block, 5*block, 7*block, 5*block, 7*block, 6*block, 6*block, 6*block, fill='grey', outline='grey') f7 = c.create_polygon(7*block, 5*block, 8*block, 5*block, 8*block, 6*block, 7*block, 6*block, fill='white', outline='grey') g0 = c.create_polygon(0 ,6*block, block, 6*block, block, 7*block, 0, 7*block, fill='white', outline='grey') g1 = c.create_polygon(block, 6*block, 2*block, 6*block, 2*block, 7*block, block, 7*block, fill='grey', outline='grey') g2 = c.create_polygon(2*block, 6*block, 3*block, 6*block, 3*block, 7*block, 2*block, 7*block, fill='white', outline='grey') g3 = c.create_polygon(3*block, 6*block, 4*block, 6*block, 4*block, 7*block, 3*block, 7*block, fill='grey', outline='grey') g4 = c.create_polygon(4*block, 6*block, 5*block, 6*block, 5*block, 7*block, 4*block, 7*block, fill='white', outline='grey') g5 = c.create_polygon(5*block, 6*block, 6*block, 6*block, 6*block, 7*block, 5*block, 7*block, fill='grey', outline='grey') g6 = c.create_polygon(6*block, 6*block, 7*block, 6*block, 7*block, 7*block, 6*block, 7*block, fill='white', outline='grey') g7 = c.create_polygon(7*block, 6*block, 8*block, 6*block, 8*block, 7*block, 7*block, 7*block, fill='grey', outline='grey') h0 = c.create_polygon(0, 7*block, block, 7*block, block, 8*block, 0, 8*block, fill='grey', outline='grey') h1 = c.create_polygon(block, 7*block, 2*block, 7*block, 2*block, 8*block, block, 8*block, fill='white', outline='grey') h2 = c.create_polygon(2*block, 7*block, 3*block, 7*block, 3*block, 8*block, 2*block, 8*block, fill='grey', outline='grey') h3 = c.create_polygon(3*block, 7*block, 4*block, 7*block, 4*block, 8*block, 3*block, 8*block, fill='white', outline='grey') h4 = c.create_polygon(4*block, 7*block, 5*block, 7*block, 5*block, 8*block, 4*block, 8*block, fill='grey', outline='grey') h5 = c.create_polygon(5*block, 7*block, 6*block, 7*block, 6*block, 8*block, 5*block, 8*block, fill='white', outline='grey') h6 = c.create_polygon(6*block, 7*block, 7*block, 7*block, 7*block, 8*block, 6*block, 8*block, fill='grey', outline='grey') h7 = c.create_polygon(7*block, 7*block, 8*block, 7*block, 8*block, 8*block, 7*block, 8*block, fill='white', outline='grey') ar = 50 br = 150 cr = 250 dr = 350 er = 450 fr = 550 gr = 650 hr = 750 def pieces(): global places bpawn1 = c.create_text(50,150, text='\u265F', font=('Helventica',50),tag='bpawn') bpawn2 = c.create_text(150,150, text='\u265F', font=('Helventica',50),tag='bpawn') bpawn3 = c.create_text(250,150, text='\u265F', font=('Helventica',50),tag='bpawn') bpawn4 = c.create_text(350,150, text='\u265F', font=('Helventica',50),tag='bpawn') bpawn5 = c.create_text(450,150, text='\u265F', font=('Helventica',50),tag='bpawn') bpawn6 = c.create_text(550,150, text='\u265F', font=('Helventica',50),tag='bpawn') bpawn7 = c.create_text(650,150, text='\u265F', font=('Helventica',50),tag='bpawn') bpawn8 = c.create_text(750,150, text='\u265F', font=('Helventica',50),tag='bpawn') brook1 = c.create_text(50,50, text='\u265C', font=('Helventica',50)) brook2 = c.create_text(750,50, text='\u265C', font=('Helventica',50)) bknight1 = c.create_text(150,50, text='\u265E', font=('Helventica',50)) bknight2 = c.create_text(650,50, text='\u265E', font=('Helventica',50)) bbish1 = c.create_text(250,50, text='\u265D', font=('Helventica',50)) bbish2 = c.create_text(550,50, text='\u265D', font=('Helventica',50)) bqueen = c.create_text(350,50, text='\u265B', font=('Helventica',50)) bking = c.create_text(450,50, text='\u265A', font=('Helventica',50)) wpawn1 = c.create_text(50,650, text='\u2659', font=('Helventica',50),tag='wpawn') wpawn2 = c.create_text(150,650, text='\u2659', font=('Helventica',50),tag='wpawn') wpawn3 = c.create_text(250,650, text='\u2659', font=('Helventica',50),tag='wpawn') wpawn4 = c.create_text(350,650, text='\u2659', font=('Helventica',50),tag='wpawn') wpawn5 = c.create_text(450,650, text='\u2659', font=('Helventica',50),tag='wpawn') wpawn6 = c.create_text(550,650, text='\u2659', font=('Helventica',50),tag='wpawn') wpawn7 = c.create_text(650,650, text='\u2659', font=('Helventica',50),tag='wpawn') wpawn8 = c.create_text(750,650, text='\u2659', font=('Helventica',50),tag='wpawn') wrook1 = c.create_text(50,750, text='\u2656', font=('Helventica',50)) wrook2 = c.create_text(750,750, text='\u2656', font=('Helventica',50)) wknight1 = c.create_text(150,750, text='\u2658', font=('Helventica',50)) wknight2 = c.create_text(650,750, text='\u2658', font=('Helventica',50)) wbish1 = c.create_text(250,750, text='\u2657', font=('Helventica',50)) wbish2 = c.create_text(550,750, text='\u2657', font=('Helventica',50)) wqueen = c.create_text(350,750, text='\u2655', font=('Helventica',50)) wking = c.create_text(450,750, text='\u2654', font=('Helventica',50)) places = [bpawn1,bpawn2,bpawn3,bpawn4,bpawn4,bpawn5,bpawn6,bpawn7,bpawn8,brook1,brook2,bknight1,bknight2,bbish1,bbish2,bqueen,bking,wpawn1,wpawn2,wpawn3,wpawn4,wpawn4,wpawn5,wpawn6,wpawn7,wpawn8,wrook1,wrook2,wknight1,wknight2,wbish1,wbish2,wqueen,wking] def Click(event): global places global clickt global piece print(places) if clickt == 0: for i in range(len(places)): x,y = c.coords(places[i]) print(places[i]) if abs(event.x - x)<50 and abs(event.y - y)<50: c.itemconfig(places[i],font=('Helventica',60),fill='blue') piece = places[i] if x < 100: x = 50 elif x < 200 and x > 100: x = 150 elif x < 300 and x > 200: x = 250 elif x < 400 and x > 300: x = 350 elif x < 500 and x > 400: x = 450 elif x < 600 and x > 500: x = 550 elif x < 700 and x > 600: x = 650 elif x < 800 and x > 700: x = 750 print(x) if y < 100: y = 50 elif y < 200 and y > 100: y = 150 elif y < 300 and y > 200: y = 250 elif y < 400 and y > 300: y = 350 elif y < 500 and y > 400: y = 450 elif y < 600 and y > 500: y = 550 elif y < 700 and y > 600: y = 650 elif y < 800 and y > 700: y = 750 print(y) print(piece) clickt = 1 elif clickt == 1: for i in range(len(places)): x,y = c.coords(places[i]) if abs(event.x - x)<50 and abs(event.y - y)<50: c.itemconfig(places[i],font=('Helventica',50),fill='black') piece = 0 if x < 100: x = 50 elif x < 200 and x > 100: x = 150 elif x < 300 and x > 200: x = 250 elif x < 400 and x > 300: x = 350 elif x < 500 and x > 400: x = 450 elif x < 600 and x > 500: x = 550 elif x < 700 and x > 600: x = 650 elif x < 800 and x > 700: x = 750 print(x) if y < 100: y = 50 elif y < 200 and y > 100: y = 150 elif y < 300 and y > 200: y = 250 elif y < 400 and y > 300: y = 350 elif y < 500 and y > 400: y = 450 elif y < 600 and y > 500: y = 550 elif y < 700 and y > 600: y = 650 elif y < 800 and y > 700: y = 750 print(y) clickt = 0 else: move(piece,event.x,event.y) c.bind('<1>', Click) def move(piece,x,y): global clickt if x < 100: x = 50 elif x < 200 and x > 100: x = 150 elif x < 300 and x > 200: x = 250 elif x < 400 and x > 300: x = 350 elif x < 500 and x > 400: x = 450 elif x < 600 and x > 500: x = 550 elif x < 700 and x > 600: x = 650 elif x < 800 and x > 700: x = 750 print(x) if y < 100: y = 50 elif y < 200 and y > 100: y = 150 elif y < 300 and y > 200: y = 250 elif y < 400 and y > 300: y = 350 elif y < 500 and y > 400: y = 450 elif y < 600 and y > 500: y = 550 elif y < 700 and y > 600: y = 650 elif y < 800 and y > 700: y = 750 print(y) x1,y1=c.coords(piece) a=c.gettags(piece) if 'bpawn' in a: if y > y1 and y1 + 200 > y: c.delete(piece) b=c.create_text(x,y, text='\u265F', font=('Helventica',50),tags='bpawn') places.append(b) piece = 0 clickt = 0 else: print("no") grid() pieces()