SHARE
TWEET

Untitled

a guest Apr 24th, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from tkinter import *
  2.  
  3.  
  4. def fun(l:list, x):
  5.     print(l)
  6.     photo = PhotoImage(file = 'White_Rook.png')
  7.     for i in range(64):
  8.         if board.boxes[i].coordinates == l:
  9.             print(board.boxes[i].file)
  10.             board.boxes[i].photo = PhotoImage("White_Rook.png")
  11.             board.boxes[i].button.config(image = photo)
  12.             board.boxes[i].file = "White_Rook.png"
  13.             board.boxes[i].button.grid(row = l[0], column = l[1])
  14.  
  15.  
  16.  
  17. class box:
  18.     def __init__(self, coords:list, Master, color:str = "None", FigureName:str = "None"):
  19.         self.master = Master
  20.         names = ["Pawn", "Rook", "Knight", "Bishop", "Queen", "King"]
  21.         if len(coords) != 2:
  22.             raise NameError("Wrong coords")
  23.         else:
  24.             self.coordinates = coords
  25.         if color != "Black" and color != "White" and color != "None":
  26.             raise NameError("Wrong piece color")
  27.         else:
  28.             self.PieceColor = color
  29.  
  30.         if color == "Black":
  31.             if FigureName == "Pawn":
  32.                 self.file = "Black_Pawn.png"
  33.                 self.Figure = Pawn(self.PieceColor, self.coordinates)
  34.                 self.Figure_Name = FigureName
  35.             elif FigureName == "Knight":
  36.                 self.file = "Black_Knight.png"
  37.                 self.Figure = Knight(self.PieceColor, self.coordinates)
  38.                 self.Figure_Name = FigureName
  39.             elif FigureName == "Bishop":
  40.                 self.file = "Black_Bishop.png"
  41.                 self.Figure = Bishop(self.PieceColor, self.coordinates)
  42.                 self.Figure_Name = FigureName
  43.             elif FigureName == "Rook":
  44.                 self.file = "Black_Rook.png"
  45.                 self.Figure = Rook(self.PieceColor, self.coordinates)
  46.                 self.Figure_Name = FigureName
  47.             elif FigureName == "Queen":
  48.                 self.file = "Black_Queen.png"
  49.                 self.Figure = Queen(self.PieceColor, self.coordinates)
  50.                 self.Figure_Name = FigureName
  51.             elif FigureName  == "King":
  52.                 self.file = "Black_King.png"
  53.                 self.Figure = King(self.PieceColor, self.coordinates)
  54.                 self.Figure_Name = FigureName
  55.         elif color == "White":
  56.             if FigureName == "Pawn":
  57.                 self.file = "White_Pawn.png"
  58.                 self.Figure = Pawn(self.PieceColor, self.coordinates)
  59.                 self.Figure_Name = FigureName
  60.             elif FigureName == "Knight":
  61.                 self.file = "White_Knight.png"
  62.                 self.Figure = Knight(self.PieceColor, self.coordinates)
  63.                 self.Figure_Name = FigureName
  64.             elif FigureName == "Bishop":
  65.                 self.file = "White_Bishop.png"
  66.                 self.Figure = Bishop(self.PieceColor, self.coordinates)
  67.                 self.Figure_Name = FigureName
  68.             elif FigureName == "Rook":
  69.                 self.file = "White_Rook.png"
  70.                 self.Figure = Rook(self.PieceColor, self.coordinates)
  71.                 self.Figure_Name = FigureName
  72.             elif FigureName == "Queen":
  73.                 self.file = "White_Queen.png"
  74.                 self.Figure = Queen(self.PieceColor, self.coordinates)
  75.                 self.Figure_Name = FigureName
  76.             elif FigureName  == "King":
  77.                 self.file = "White_King.png"
  78.                 self.Figure = King(self.PieceColor, self.coordinates)
  79.                 self.Figure_Name = FigureName
  80.  
  81.  
  82.         if FigureName not in names:
  83.             self.Figure = NoPiece(self.coordinates)
  84.             self.Figure_Name = "None"
  85.         if FigureName in names:
  86.             self.photo = PhotoImage(file = self.file)
  87.         else:
  88.             self.file = "None.png"
  89.             self.photo = PhotoImage(file = "None.png")
  90.  
  91.         self.button = Button(Master, image = self.photo, bg = "#69230a")
  92.  
  93.         if (self.coordinates[0] + self.coordinates[1]) % 2 == 1:
  94.             self.button = Button(Master, image = self.photo, bg = "#69230a")
  95.         else:
  96.             self.button = Button(Master, image = self.photo, bg = "white")
  97.  
  98.         if (self.Figure_Name in names) and (self.PieceColor == "White" or self.PieceColor == "Black"):
  99.             self.State = 1
  100.         elif self.Figure_Name == "None":
  101.             self.State = 0
  102.         else:
  103.             raise NameError("Wrong Piece name")
  104.  
  105.         self.button.bind("<Button>", self.funkcja)
  106.  
  107.     def funkcja(self, event):
  108.         fun(self.coordinates, self)
  109.  
  110. class Board:
  111.     def __init__(self, _boxes:list, Master):
  112.         self.boxes = _boxes
  113.         for i in range(64):
  114.             self.boxes[i].button.grid(row = int(i / 8), column = i % 8)
  115.  
  116.  
  117.  
  118.     def MovePiece(self, x, event):
  119.         if x == 0:
  120.             print("black rook")
  121.         else:
  122.             print("Not rook")
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131. class Piece:
  132.     def __init__(self, _coords:list):
  133.         if len(_coords) == 2:
  134.             self.coords = _coords
  135.  
  136.     def move(self, event):
  137.         pass
  138.  
  139. class Pawn(Piece):
  140.     def __init__(self, _color:str, _coords:list):
  141.         if len(_coords) == 2:
  142.             self.coords = _coords
  143.  
  144.         self.st = Status()
  145.  
  146.         colors = ["White", "Black"]
  147.         if _color in colors:
  148.             self.Color = _color
  149.         else:
  150.             raise NameError("WrongColorException")
  151.         self.moved = 0
  152.  
  153.  
  154.  
  155.  
  156.     def move(self, event):
  157.         if self.Color == "White":
  158.             print("White pawn moves")
  159.  
  160.  
  161.  
  162.  
  163.  
  164.         elif self.Color == "Black":
  165.             print("Black Pawn moves")
  166.  
  167. class Knight(Piece):
  168.     def __init__(self, _color:str ,_coords:list):
  169.         if len(_coords) == 2:
  170.             self.coords = _coords
  171.         self.st = Status()
  172.         colors = ["White", "Black"]
  173.         if _color in colors:
  174.             self.Color = _color
  175.         else:
  176.             raise NameError("WrongColorException")
  177.  
  178.     def move(self, event):
  179.         print("Knight")
  180.  
  181. class Bishop(Piece):
  182.     def __init__(self, _color:str, _coords:list):
  183.         if len(_coords) == 2:
  184.             self.coords = _coords
  185.  
  186.         self.st = Status()
  187.         colors = ["White", "Black"]
  188.         if _color in colors:
  189.             self.Color = _color
  190.         else:
  191.             raise NameError("WrongColorException")
  192.  
  193.     def move(self, event):
  194.         print("Bishop")
  195.  
  196. class Rook(Piece):
  197.     def __init__(self, _color:str, _coords:list):
  198.         if len(_coords) == 2:
  199.             self.coords = _coords
  200.  
  201.         self.st = Status()
  202.         colors = ["White", "Black"]
  203.         if _color in colors:
  204.             self.Color = _color
  205.         else:
  206.             raise NameError("WrongColorException")
  207.  
  208.     def move(self, event):
  209.         print("Rook")
  210.  
  211. class Queen(Piece):
  212.     def __init__(self, _color:str, _coords:list):
  213.         if len(_coords) == 2:
  214.             self.coords = _coords
  215.         self.st = Status()
  216.         colors = ["White", "Black"]
  217.         if _color in colors:
  218.             self.Color = _color
  219.         else:
  220.             raise NameError("WrongColorException")
  221.  
  222.     def move(self, event):
  223.         print("Queen")
  224.  
  225. class King(Piece):
  226.     def __init__(self, _color:str, _coords:list):
  227.         if len(_coords) == 2:
  228.             self.coords = _coords
  229.  
  230.         self.st = Status()
  231.         colors = ["White", "Black"]
  232.         if _color in colors:
  233.             self.Color = _color
  234.         else:
  235.             raise NameError("WrongColorException")
  236.  
  237.     def move(self, event):
  238.         print("King")
  239.  
  240.  
  241. class NoPiece(Piece):
  242.     def __init__(self, _coords:list):
  243.         if len(_coords) == 2:
  244.             self.coords = _coords
  245.         self.st = Status()
  246.  
  247.     def move(self, event):
  248.         print("No figure on this box")
  249.  
  250.  
  251. class Status:
  252.     Status = 0
  253.     def __init__(self):
  254.         self.currentCoords = [-1, -1]
  255.  
  256.  
  257.  
  258.  
  259.  
  260. root = Tk()
  261. root.title("Cześć Klaudiusz xD")
  262.  
  263. p1 = box([0, 0], root, "Black", "Rook")
  264. p2 = box([0, 1], root, "Black", "Knight")
  265. p3 = box([0, 2], root, "Black", "Bishop")
  266. p4 = box([0, 3], root, "Black", "Queen")
  267. p5 = box([0, 4], root, "Black", "King")
  268. p6 = box([0, 5], root, "Black", "Bishop")
  269. p7 = box([0, 6], root, "Black", "Knight")
  270. p8 = box([0, 7], root, "Black", "Rook")
  271. p9 = box([1, 0], root, "Black", "Pawn")
  272. p10 = box([1, 1], root, "Black", "Pawn")
  273. p11 = box([1, 2], root, "Black", "Pawn")
  274. p12 = box([1, 3], root, "Black", "Pawn")
  275. p13 = box([1, 4], root, "Black", "Pawn")
  276. p14 = box([1, 5], root, "Black", "Pawn")
  277. p15 = box([1, 6], root, "Black", "Pawn")
  278. p16 = box([1, 7], root, "Black", "Pawn")
  279. p17 = box([2, 0], root)
  280. p18 = box([2, 1], root)
  281. p19 = box([2, 2], root)
  282. p20 = box([2, 3], root)
  283. p21 = box([2, 4], root)
  284. p22 = box([2, 5], root)
  285. p23 = box([2, 6], root)
  286. p24 = box([2, 7], root)
  287. p25 = box([3, 0], root)
  288. p26 = box([3, 1], root)
  289. p27 = box([3, 2], root)
  290. p28 = box([3, 3], root)
  291. p29 = box([3, 4], root)
  292. p30 = box([3, 5], root)
  293. p31 = box([3, 6], root)
  294. p32 = box([3, 7], root)
  295. p33 = box([4, 0], root)
  296. p34 = box([4, 1], root)
  297. p35 = box([4, 2], root)
  298. p36 = box([4, 3], root)
  299. p37 = box([4, 4], root)
  300. p38 = box([4, 5], root)
  301. p39 = box([4, 6], root)
  302. p40 = box([4, 7], root)
  303. p41 = box([5, 0], root)
  304. p42 = box([5, 1], root)
  305. p43 = box([5, 2], root)
  306. p44 = box([5, 3], root)
  307. p45 = box([5, 4], root)
  308. p46 = box([5, 5], root)
  309. p47 = box([5, 6], root)
  310. p48 = box([5, 7], root)
  311. p49 = box([6, 0], root, "White", "Pawn")
  312. p50 = box([6, 1], root, "White", "Pawn")
  313. p51 = box([6, 2], root, "White", "Pawn")
  314. p52 = box([6, 3], root, "White", "Pawn")
  315. p53 = box([6, 4], root, "White", "Pawn")
  316. p54 = box([6, 5], root, "White", "Pawn")
  317. p55 = box([6, 6], root, "White", "Pawn")
  318. p56 = box([6, 7], root, "White", "Pawn")
  319. p57 = box([7, 0], root, "White", "Rook")
  320. p58 = box([7, 1], root, "White", "Knight")
  321. p59 = box([7, 2], root, "White", "Bishop")
  322. p60 = box([7, 3], root, "White", "Queen")
  323. p61 = box([7, 4], root, "White", "King")
  324. p62 = box([7, 5], root, "White", "Bishop")
  325. p63 = box([7, 6], root, "White", "Knight")
  326. p64 = box([7, 7], root, "White", "Rook")
  327.  
  328. GlobalBoxes = [p1,  p2,  p3,  p4,  p5,  p6,  p7,  p8,
  329.                p9, p10, p11, p12, p13, p14, p15, p16,
  330.                p17, p18, p19, p20, p21, p22, p23, p24,
  331.                p25, p26, p27, p28, p29, p30, p31, p32,
  332.                p33, p34, p35, p36, p37, p38, p39, p40,
  333.                p41, p42, p43, p44, p45, p46, p47, p48,
  334.                p49, p50, p51, p52, p53, p54, p55, p56,
  335.                p57, p58, p59, p60, p61, p62, p63, p64]
  336.  
  337. GlobalClick = 0
  338.  
  339.  
  340. board = Board(GlobalBoxes, root)
  341.  
  342.  
  343.  
  344. root.mainloop()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top