python - 如何在类中显示文本
问题描述
我正在尝试将“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()
希望这是有道理的。
谢谢
解决方案
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
类的实例(例如)Unit
。u.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()
推荐阅读
- jquery - JQuery 不响应 POST 请求
- oracle - 在 PL/SQL 中将二进制转换为 varchar
- html - CSS 转换为 div 提供轮廓
- x86-64 - 64 位机器中虚拟地址的高 16 位设置为零。为什么?
- javascript - Height not increasing using setInterval (creating border around screen)
- java - 解释模拟匹配(str)与相同(str)的这种行为的原因
- angular - Angular6从HTML调用组件
- r - 从 R 中是和否值的两列导出列联真值表
- python - 使用 lambda 的字典理解将内存地址作为值返回
- python - python - 在混合类型列表中查找最后两个数字的索引