【问题标题】:Unicode Characters not showing when displaying to pygame [duplicate]显示到pygame时未显示Unicode字符[重复]
【发布时间】:2020-05-23 16:06:34
【问题描述】:

我有两个文件,一个main.py 文件和一个chess.py 文件。我将我的类存储在chess.py 文件中,其中包括白王的绘制方法(unicode "\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)

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

这是代码的输出:

【问题讨论】:

  • 字体不提供unicode字符。
  • @Rabbid76,那么你推荐哪种字体。我已经尝试了 10 多种,包括 Serif、Verdana、Arial 等。
  • "segoeuisymbol"。查看答案。

标签: python python-3.x pygame


【解决方案1】:

字体不提供 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))

【讨论】:

  • 我试过 segoeuisymbol。支持它,但是,我打印时仍然只是得到一个盒子。另外,为了您的信息,我在 HP Windows 10 PC 上使用 PyCharm 社区版和 Ryzen 5。任何帮助将不胜感激。谢谢!
  • @KrishnanShankar 这与您的系统无关(我也在 Windows 10 上)。我无法调试您的代码。您是否创建了seguisy50 = pygame.font.SysFont("segoeuisymbol", 50) 并使用了变量名seguisy50?您所要做的就是复制/粘贴代码。我不能为你这样做。
  • 是的,我就是这么做的。但是,它仍然给我同样的问题。它向我展示了一个盒子。我会将其添加到问题中。
猜你喜欢
  • 2018-11-03
  • 2021-11-22
  • 1970-01-01
  • 1970-01-01
  • 2012-08-08
  • 2011-05-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多