简体   繁体   English

如何在Pygame的子画面中添加“矩形”或文本?

[英]How do I add 'Rect's or text to a sprite in Pygame?

I'm trying to recreate a slide puzzle and I need to print to text to previously drawn rectangular sprites. 我正在尝试重新创建幻灯片拼图 ,我需要将文本打印到先前绘制的矩形精灵上。 This is how I set them up: 这是我设置它们的方式:

class Tile(Entity):
    def __init__(self,x,y):
        self.image = pygame.Surface((TILE_SIZE-1,TILE_SIZE-1))
        self.image.fill(LIGHT_BLUE)
        self.rect = pygame.Rect(x,y,TILE_SIZE-1,TILE_SIZE-1)
        self.isSelected = False
        self.font = pygame.font.SysFont('comicsansms',22) # Font for the text is defined

And this is how I've draw them: 这就是我绘制它们的方式:

def drawTiles(self):
    number = 0
    number_of_tiles = 15

    x = 0
    y = 1

    for i in range(number_of_tiles):
        label = self.font.render(str(number),True,WHITE) # Where the label is defined. I just want it to print 0's for now.
        x += 1
        if x > 4:
            y += 1
            x = 1

        tile = Tile(x*TILE_SIZE,y*TILE_SIZE)
        tile.image.blit(label,[x*TILE_SIZE+40,y*TILE_SIZE+40]) # How I tried to print text to the sprite. It didn't show up and didn't error, so I suspect it must have been drawn behind the sprite.
        tile_list.append(tile) 

This is how I tried to add Rect's (when it is clicked on with the mouse): 这是我尝试添加Rect的方法(当用鼠标单击时):

# Main program loop
for tile in tile_list:
    screen.blit(tile.image,tile.rect)
    if tile.isInTile(pos):
        tile.isSelected = True
        pygame.draw.rect(tile.image,BLUE,[tile.rect.x,tile.rect.y,TILE_SIZE,TILE_SIZE],2)
    else:
        tile.isSelected = False

isInTile: isInTile:

def isInTile(self,mouse_pos):
    if self.rect.collidepoint(mouse_pos): return True

What am I doing wrong? 我究竟做错了什么?

Coordinates in Pygame are relative to the surface that's being drawn on. Pygame中的坐标是相对于正在绘制的表面的。 The way you're currently drawing the rects on tile.image makes it draw at (tile.rect.x, tile.rect.y) relative to the topleft of tile.image. 您当前在tile.image上绘制矩形的方式使其相对于tile.image的左上角在(tile.rect.x,tile.rect.y)处绘制。 Most times tile.rect.x and tile.rect.y will be greater than the tile width and height, so it will be invisible. 大多数情况下tile.rect.x和tile.rect.y会大于图块的宽度和高度,因此将不可见。 What you probably want is pygame.draw.rect(tile.image,BLUE,[0,0,TILE_SIZE,TILE_SIZE],2). 您可能想要的是pygame.draw.rect(tile.image,BLUE,[0,0,TILE_SIZE,TILE_SIZE],2)。 This draws a rectangle on the tile from the topleft (0,0) of the tile to the bottom right (TILE_SIZE,TILE_SIZE). 这会在图块上从图块的左上角(0,0)到右下角(TILE_SIZE,TILE_SIZE)绘制一个矩形。

The same goes for the text. 文本也是如此。 For example if TILE_SIZE is 25 and x is 2, the x coordinate where the text is blit on tile.image is 2*25+40 = 90. 90 is bigger than the width of tile.image (which was TILE_SIZE-1=24), so it will draw outside of the surface, making it invisible. 例如,如果TILE_SIZE为25,x为2,则在tile.image上将文本变暗的x坐标为2 * 25 + 40 =90。90大于tile.image的宽度(TILE_SIZE-1 = 24 ),因此它将绘制在表面的外部,使其不可见。 If you want to draw the text in the top left corner of tile.image, do tile.image.blit(label, [0,0]). 如果要在tile.image的左上角绘制文本,请执行tile.image.blit(label,[0,0])。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM