首页 > 解决方案 > 如何在类中显示文本

问题描述

我正在尝试将“P”作为文本粘贴到棋盘上。即使遵循先前关于如何使用方块的帮助,我也无法使用文本来做到这一点。

这种国际象棋游戏的变体由长枪兵、弓箭手和骑士进行。“P”代表长枪手。

将来,我将显示一个精灵,但目前我想显示字母(另外,因为它可能对 GUI 有帮助)

class Unit:
    def __init__(self):
        self.surface = pygame.Surface((100, 100))
        self.my_font = pygame.font.SysFont('Time New Roman', 18)
        self.img = self.my_font.render('P', 1, (255, 0, 0))
        self.surface.blit(self.img, (100, 100))

基本上我要做的是创建一个表面,设置一个字体,渲染它,然后在创建的表面上进行 blit。

以下是完整的显示代码:

import pygame
import sys

from coordinator import coordinator

# Sets up the display

pygame.init()
window_size = (800, 800)
game_window = pygame.display.set_mode(size=window_size)
pygame.display.set_caption('My Game')




class WhiteSquare:
    def __init__(self):
        self.height = int(window_size[0] / 8)
        self.width = int(window_size[1] / 8)
        self.white_square = pygame.Surface((self.height, self.width))
        self.white_square.fill((255, 255, 255))


class BlackSquare:
    def __init__(self):
        self.height = int(window_size[0] / 8)
        self.width = int(window_size[1] / 8)
        self.black_square = pygame.Surface((self.height, self.width))
        self.black_square.fill((0, 0, 0))


class ChessBoard:
    def __init__(self):
        self.ws = ws
        self.bs = bs
        self.white_columns = white_columns
        self.black_columns = black_columns

    def draw(self):
        for columns in self.white_columns:
            game_window.blit(self.ws.white_square, columns)

        for columns in self.black_columns:
            for destination in columns:
                game_window.blit(self.bs.black_square, destination)


class Unit:
    def __init__(self):
        self.surface = pygame.Surface((100, 100))
        self.my_font = pygame.font.SysFont('Time New Roman', 18)
        self.img = self.my_font.render('P', 1, (255, 0, 0))
        self.surface.blit(self.img, (100, 100))


coordinator = coordinator()
black_columns = coordinator[2], coordinator[3]
white_columns = coordinator[0] + coordinator[1]

#print('coordinator' + str(coordinator))
#print('w_columns' + str(w_columns))


ws = WhiteSquare()
bs = BlackSquare()
cb = ChessBoard()
u = Unit()



# Event loop (outer)
while 1:

    # Event loop (inner)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

    cb.draw()

    pygame.display.update()

希望这是有道理的。

谢谢

标签: pythonpygameblit

解决方案


self.imag在 的构造函数中创建Unit。但blit它在一个draw方法。实例方法draw有一个参数,即 Surface,文本必须blit在其中:

class Unit:
    def __init__(self):
        self.surface = pygame.Surface((100, 100))
        self.my_font = pygame.font.SysFont('Time New Roman', 18)
        self.img = self.my_font.render('P', 1, (255, 0, 0))

   def draw(self, surface):
        surface.blit(self.img, (100, 100))

创建u类的实例(例如)Unitu.draw在主循环中调用。该draw方法的参数是与显示器关联的 Surface ( game_window),因此在显示器上绘制文本。例如:

u = Unit()
while 1:

    # Event loop (inner)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

    # draw the scene
    cb.draw()
    u.draw(game_window)

    # update the display
    pygame.display.update()

推荐阅读