首页 > 解决方案 > 显示到 pygame 时未显示 Unicode 字符

问题描述

我有两个文件,一个main.py文件和一个chess.py文件。我将我的类存储在文件中,其中包括白王 (unicode )chess.py的绘制方法。"\u2654"我在绘图方法的屏幕上显示它。当我创建一个片段的实例并调用 draw 方法时,它只显示一个矩形,而不是片段本身。但是,当我打印相同的 unicode 字符时,它会正确显示。任何帮助表示赞赏。先感谢您!

文件: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)

文件:国际象棋.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

这是代码的输出: 代码的输出

标签: pythonpython-3.xpygame

解决方案


字体不提供 unicode 字符。

试试你的系统是否支持字体“segoeuisymbol” :

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

请注意,支持的字体可以通过print(pygame.font.get_fonts()).

或者下载字体Segoe UI Symbol并创建一个pygame.font.Font

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

使用字体渲染标志:

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))


推荐阅读