-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chess.py
executable file
·91 lines (77 loc) · 4.13 KB
/
Chess.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from tkinter import *
from Board import *
import math
import sys
from Game import Game
class Chess:
def __init__(self,window):
self.window=window
self.BOARD_HEIGHT = 480
self.BOARD_WIDTH = 480
self.NUM_X_CELLS = 8
self.NUM_Y_CELLS = 8
self.board=Canvas(window, height=self.BOARD_HEIGHT, width=self.BOARD_WIDTH)
self.cell_height = self.BOARD_HEIGHT/self.NUM_Y_CELLS
self.cell_width = self.BOARD_WIDTH/self.NUM_X_CELLS
self.images=[]
#Create board
def create_board(self):
current_height = 0
current_width = 0
for i in range (0,self.NUM_Y_CELLS):
for j in range (0,self.NUM_X_CELLS):
if i%2 == 0:
if j%2 == 0:
self.board.create_rectangle(current_width, current_height, current_width+self.cell_width, current_height+self.cell_height, fill="#61b0ff")
else:
self.board.create_rectangle(current_width, current_height, current_width+self.cell_width, current_height+self.cell_height, fill="#ff9661")
else:
if j%2==0:
self.board.create_rectangle(current_width, current_height, current_width+self.cell_width, current_height+self.cell_height, fill="#ff9661")
else:
self.board.create_rectangle(current_width, current_height, current_width+self.cell_width, current_height+self.cell_height, fill="#61b0ff")
current_width += self.cell_width
current_height += self.cell_height
current_width = 0
self.board.pack()
def display_pices(self,board):
i = 1
for row in board.board:
j = 1
for pice in row:
if pice["m"] != "" and pice["p"]== "":
self.board.create_rectangle(self.x_coordinates_to_size(j)-((self.BOARD_WIDTH/self.NUM_X_CELLS)/2),self.y_coordinates_to_size(i)-((self.BOARD_HEIGHT/self.NUM_Y_CELLS)/2), self.x_coordinates_to_size(j)+self.cell_width-((self.BOARD_WIDTH/self.NUM_X_CELLS)/2), self.y_coordinates_to_size(i)+self.cell_height-((self.BOARD_HEIGHT/self.NUM_Y_CELLS)/2), fill="#00FF3A")
elif pice['p'] == "" and pice['m'] == "":
pass
elif pice["p"] != "" and pice["m"] == "":
self.images.append(PhotoImage(file=pice["p"].figure))
self.board.create_image(self.x_coordinates_to_size(j),self.y_coordinates_to_size(i),image=self.images[-1])
else:
self.board.create_rectangle(self.x_coordinates_to_size(j)-((self.BOARD_WIDTH/self.NUM_X_CELLS)/2),self.y_coordinates_to_size(i)-((self.BOARD_HEIGHT/self.NUM_Y_CELLS)/2), self.x_coordinates_to_size(j)+self.cell_width-((self.BOARD_WIDTH/self.NUM_X_CELLS)/2), self.y_coordinates_to_size(i)+self.cell_height-((self.BOARD_HEIGHT/self.NUM_Y_CELLS)/2), fill="#00FF3A")
self.images.append(PhotoImage(file=pice["p"].figure))
self.board.create_image(self.x_coordinates_to_size(j),self.y_coordinates_to_size(i),image=self.images[-1])
j+=1
i += 1
self.board.pack()
def x_coordinates_to_size(self,x):
return (x*(self.BOARD_WIDTH/self.NUM_X_CELLS))-((self.BOARD_WIDTH/self.NUM_X_CELLS)/2)
def y_coordinates_to_size(self,y):
return (y*(self.BOARD_HEIGHT/self.NUM_Y_CELLS))-((self.BOARD_HEIGHT/self.NUM_Y_CELLS)/2)
def size_to_x_coordinates(self,sizeX):
return math.ceil(sizeX/(self.BOARD_WIDTH/self.NUM_X_CELLS))-1
def size_to_y_coordinates(self,sizeY):
return math.ceil(sizeY/(self.BOARD_HEIGHT/self.NUM_Y_CELLS))-1
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Ussage: python3 Chess.py <Game Mode>")
print("Game Mode {<1> : 2 Players, <2> : Player vs IA} ")
exit(0)
window = Tk()
window.title("Chess")
chess = Chess(window)
chess.create_board()
board = Board()
chess.display_pices(board)
game=Game(chess, board, sys.argv[1])
game.start_game()
window.mainloop()