1

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

This is the output of the code: The output of the code

Krishnan Shankar
  • 780
  • 9
  • 29

1 Answers1

0

The unicode character is not provided by the font.

Try if the font "segoeuisymbol" is supported by your system:

seguisy50 = pygame.font.SysFont("segoeuisymbol", 50)

Note, the supported fonts can be print by print(pygame.font.get_fonts()).

Alternatively download the font Segoe UI Symbol and create a pygame.font.Font

seguisy50 = pygame.font.Font("seguisym.ttf", 50)

Use the font to render the sign:

class Piece:
    # [...]

    def draw(self, win):
        print(self.text)
        win.blit(
            seguisy50.render(str(self.text), 1, (0, 0, 0)),
            (self.pos[0] * 50 + 50, self.pos[1] * 50 + 50))

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • I tried segoeuisymbol. It is supported, however, I still just get a box when I print. Also, for your information, I am using PyCharm Community Edition on HP Windows 10 PC, with Ryzen 5. Any help would be appreciated. Thank You! – Krishnan Shankar May 23 '20 at 21:42
  • @KrishnanShankar That has nothing to do with your system (I'm on windows 10, too). I can't debug the your code. Did you create `seguisy50 = pygame.font.SysFont("segoeuisymbol", 50)` and use the variable name `seguisy50`? All you have to do is to copy/past the code. I cannot do that for you. – Rabbid76 May 23 '20 at 21:47
  • Yes, I did exactly that. However, it still gives me the same problem. It shows me a box. I will add it to the question. – Krishnan Shankar May 23 '20 at 22:07