我正在创建一个类,它将返回一个带有正方形的表面,文本如下所示:
方块(当前)显示用户游戏的颜色,文本是玩家名称。在计算出相似曲面阵列的位置后,我会blit
屏幕上的这些曲面。
我可以轻松生成text
曲面。我不想将屏幕表面传递给pygame.draw.rect
的类;而是分别创建矩形;合并矩形和文本表面,然后返回此分组曲面。我目前的课程定义很简单:
from pygame.locals import *
from pygame import font as pyfont, event as pyevent, display as pydisplay
from sys import exit
class Info:
GRAY = ( 214, 171, 127 )
def __init__( self, name, colour ):
pyfont.init()
self.name = name
self.colour = colour
self.font = pyfont.Font( None, 36 )
def generate( self ):
text = self.font.render( self.name, True, self.GRAY )
# The rectangle surface to be drawn here
return text
答案 0 :(得分:1)
在阅读了一些pygame文档后,我已经将类更新为:
import pygame
from pygame.locals import *
from pygame import font as pyfont, event as pyevent, display as pydisplay
from sys import exit
class Info:
GRAY = ( 214, 171, 127 )
def __init__( self, surface, name, colour ):
pyfont.init()
self.surface = surface
self.name = name
self.colour = colour
self.font = pyfont.Font( None, 36 )
def generate( self ):
t = pygame.Rect( 5, 5, 32, 32 )
pygame.draw.rect( self.surface, self.colour, t, 0 )
text = self.font.render( self.name, True, self.GRAY )
return self.surface.blit( text, (50, 5) )
可以按如下方式使用:
if __name__ == "__main__":
x = pygame.Rect( 50, 50, 250, 100 )
screen = pydisplay.set_mode( (1024, 576) )
pydisplay.set_caption( "Title goes" )
srf = screen.subsurface( x )
x = Info( srf, "hjpotter92", (230, 150, 51) )
c = pygame.time.Clock()
screen.fill( (255, 255, 255) )
while True:
pydisplay.update()
c.tick( 1 )
print x.generate()
for event in pyevent.get():
if event.type == QUIT:
exit( 0 )
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
exit( 0 )
但这是一个肮脏/犯规解决方案,并产生类似的东西:
我愿意改变当前的设计。否则,我必须事先计算Info
类的其他实例的位置。