2023-07-28 19:34:53 +01:00
|
|
|
import pygame.draw
|
|
|
|
|
2023-08-22 16:31:16 +01:00
|
|
|
from utilities.constants import SQUARE_SIZE, GREY, CROWN, GREEN
|
2023-07-28 19:34:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Piece:
|
2023-09-18 20:11:39 +01:00
|
|
|
def __init__(self, row: int, col: int, colour: int) -> None:
|
|
|
|
"""
|
|
|
|
Initialises the piece class, which represents a piece on the board. Constructor for the piece class
|
|
|
|
:param row: Row of the piece
|
|
|
|
:param col: Column of the piece
|
|
|
|
:param colour: Colour of the piece
|
|
|
|
"""
|
2023-07-28 19:34:53 +01:00
|
|
|
self.row = row
|
|
|
|
self.col = col
|
|
|
|
self.colour = colour
|
|
|
|
self.king = False
|
|
|
|
self.x = 0
|
|
|
|
self.y = 0
|
|
|
|
self.calcPosition()
|
|
|
|
self.padding = 20
|
|
|
|
self.border = 2
|
2023-08-22 16:31:16 +01:00
|
|
|
self.green = (144, 184, 59)
|
|
|
|
self.white = (255, 255, 255)
|
2023-07-28 19:34:53 +01:00
|
|
|
|
2023-09-18 20:11:39 +01:00
|
|
|
def calcPosition(self) -> None:
|
|
|
|
"""
|
|
|
|
Calculates the position of the piece
|
|
|
|
:return: None
|
|
|
|
"""
|
2023-07-28 19:34:53 +01:00
|
|
|
self.x = SQUARE_SIZE * self.col + SQUARE_SIZE // 2
|
|
|
|
self.y = SQUARE_SIZE * self.row + SQUARE_SIZE // 2
|
|
|
|
|
2023-09-18 20:11:39 +01:00
|
|
|
def makeKing(self) -> None:
|
|
|
|
"""
|
|
|
|
Makes the piece a king
|
|
|
|
:return: None
|
|
|
|
"""
|
2023-07-28 19:34:53 +01:00
|
|
|
self.king = True
|
|
|
|
|
2023-09-18 20:11:39 +01:00
|
|
|
def draw(self, win) -> None:
|
|
|
|
"""
|
|
|
|
Draws the piece
|
|
|
|
:param win: The window to draw the piece on
|
|
|
|
:return: None
|
|
|
|
"""
|
2023-07-28 19:34:53 +01:00
|
|
|
radius = SQUARE_SIZE // 2 - self.padding
|
|
|
|
pygame.draw.circle(win, GREY, (self.x, self.y), radius + self.border)
|
2023-08-22 16:31:16 +01:00
|
|
|
pygame.draw.circle(win, self.green if self.colour == GREEN else self.white, (self.x, self.y), radius)
|
2023-07-28 19:34:53 +01:00
|
|
|
if self.king:
|
|
|
|
win.blit(CROWN, (self.x - CROWN.get_width() // 2, self.y - CROWN.get_height() // 2))
|
|
|
|
|
2023-09-18 20:11:39 +01:00
|
|
|
def move(self, row: int, col: int) -> None:
|
|
|
|
"""
|
|
|
|
Moves the piece to a new position
|
|
|
|
:param row: Row to move to
|
|
|
|
:param col: Column to move to
|
|
|
|
:return: None
|
|
|
|
"""
|
2023-07-28 19:34:53 +01:00
|
|
|
self.row = row
|
|
|
|
self.col = col
|
|
|
|
self.calcPosition()
|
|
|
|
|
2023-09-18 20:11:39 +01:00
|
|
|
def __repr__(self) -> str:
|
|
|
|
"""
|
|
|
|
String representation of the piece
|
|
|
|
:return: String representation of the colour
|
|
|
|
"""
|
2023-07-28 19:34:53 +01:00
|
|
|
return str(self.colour)
|