I have two files, a main.py
file and a chess.py
file. I have my classes stored in the chess.py
file, which includes a draw method for a White King (unicode "\u2654"
). I am displaying this on the screen in the draw method. When I create an instance of the piece and call the draw method, it only displays a rectangle, instead of the piece itself. However, when I print the same unicode character, it displays it correctly. Any help is appreciated. Thank You in advance!
File: main.py:
import pygame
from chess import King, Queen, Rook, Bishop, Knight, Pawn
pygame.init()
pygame.font.init()
W = 800
H = 500
win = pygame.display.set_mode((W, H))
pygame.display.set_caption("Chess Game")
win.fill((255, 255, 255))
times30 = pygame.font.SysFont("timesnewroman", 30, bold=True)
times50 = pygame.font.SysFont("timesnewroman", 50)
clock = pygame.time.Clock()
rects = [
[pygame.Rect(i * 50 + 51, j * 50 + 51, 49, 49) for i in range(8)] for j in range(8)
]
def draw(win, king):
# Draw Checkerboard
white = True
for rect_list in rects:
for rect in rect_list:
if white:
pygame.draw.rect(win, (255, 255, 255), rect)
else:
pygame.draw.rect(win, (152, 80, 60), rect)
white = not white
white = not white
# Draw Lines
for i in range(9):
pygame.draw.line(win, (0, 0, 0), (i * 50 + 50, 50), (i * 50 + 50, 450), 3)
pygame.draw.line(win, (0, 0, 0), (50, i * 50 + 50), (450, i * 50 + 50), 3)
for j in range(8):
if i == 0:
win.blit(times30.render(str(8 - j), 1, (0, 0, 0)), (i * 50 + 20, j * 50 + 60))
if i == 8:
win.blit(times30.render(str(8 - j), 1, (0, 0, 0)), (i * 50 + 65, j * 50 + 60))
if j == 0 and i != 8:
win.blit(times30.render((" " + chr(i + 97) + " ").upper(), 1, (0, 0, 0)), (i * 50 + 50, j * 50 + 10))
if j == 7 and i != 8:
win.blit(times30.render((" " + chr(i + 97) + " ").upper(), 1, (0, 0, 0)), (i * 50 + 50, j * 50 + 110))
king.draw(win)
pygame.display.update()
myKing = King("WK1", "white", (4, 0), rects[0][4])
def main(win):
while True:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
draw(win, myKing)
if __name__ == '__main__':
main(win)
File: chess.py:
import pygame
pygame.init()
pygame.font.init()
times50 = pygame.font.SysFont("timesnewroman", 50)
class Piece:
def __init__(self, name, color, pos, rect):
self.name = name
self.color = color
self.startpos = pos
self.pos = pos
self.rect = rect
self.text = ""
def draw(self, win):
print(self.text)
win.blit(
times50.render(str(self.text), 1, (0, 0, 0)),
(self.pos[0] * 50 + 50, self.pos[1] * 50 + 50))
def move(self, pos, pieces):
if self.check_position(pos, pieces):
self.pos = pos
else:
print("Nope!")
def check_position(self, pos, pieces):
pass
class King(Piece):
def __init__(self, name, color, pos, rect):
super().__init__(name, color, pos, rect)
self.text = "\u265A" if color == "black" else "\u2654"
# print(self.color, "King,", self.name, "at", str(self.pos) + ",", "started at", self.startpos, "on", str(self.rect) + ":",
# self.text)
def check_position(self, pos, pieces):
if abs(self.pos[0] - pos[0]) == 1 or abs(self.pos[1] - pos[1]) == 1:
for piece in pieces:
if not (piece.color == self.color and piece.pos == pos):
return True