0

スライド パズルを再作成しようとしていますが、以前に描画した長方形のスプライトにテキストを出力する必要があります。これは私がそれらを設定する方法です:

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

そして、これが私がそれらを描いた方法です:

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) 

これは、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:

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

私は何を間違っていますか?

4

1 に答える 1

0

Pygame の座標は、描画されているサーフェスに対して相対的です。現在 tile.image に四角形を描画している方法では、tile.image の左上を基準にして (tile.rect.x, tile.rect.y) に描画されます。ほとんどの場合、 tile.rect.x と tile.rect.y はタイルの幅と高さより大きくなるため、見えなくなります。おそらく必要なのは pygame.draw.rect(tile.image,BLUE,[0,0,TILE_SIZE,TILE_SIZE],2) です。これは、タイルの左上 (0,0) から右下 (TILE_SIZE,TILE_SIZE) までの四角形をタイルに描画します。

テキストについても同様です。たとえば、TILE_SIZE が 25 で x が 2 の場合、tile.image でテキストがブリットされる x 座標は 2*25+40 = 90 です。90 は tile.image の幅 (TILE_SIZE-1=24 でした) よりも大きくなります。 )、そのため、サーフェスの外側に描画され、非表示になります。tile.image の左上隅にテキストを描画する場合は、 tile.image.blit(label, [0,0]) を実行します。

于 2015-03-20T22:41:25.510 に答える