如何向Surface添加线条

时间:2014-04-05 00:44:33

标签: python pygame sprite pygame-surface

由于某种原因,这些线条不会出现在屏幕上。

这是我尝试过的事情

class Grid(Sprite):
    def __init__(self):
        self.grid = pygame.Surface(size)
        self.grid.set_colorkey((0,0,0))

    def draw(self):
        # DRAW TILE LINES ----------------------------------------------------------
        grid_x = 0
        grid_y = 0
        for i in range(GRID_SIZE):
            pygame.draw.aaline(self.grid,BLACK,[grid_x,UPPER_BOUNDARY],[grid_x,LOWER_BOUNDARY],1)
            pygame.draw.aaline(self.grid,BLACK,[LEFT_BOUNDARY,y],[RIGHT_BOUNDARY,grid_y],1)
            grid_x += TILE_SIZE
            grid_y += TILE_SIZE
        # tile test
        pygame.draw.rect(screen,BLACK,(49*TILE_SIZE,34*TILE_SIZE,TILE_SIZE,TILE_SIZE))
        screen.blit(self.grid,(0,0))

创建对象:

grid = Grid()

调用类:(在主程序循环中)

grid.draw()

2 个答案:

答案 0 :(得分:0)

您需要将draw函数传递给screen个实例。否则你把它搞砸了。

def draw(self, screen):
    #...

还记得在主循环中调用pygame.display.update()。如果您有任何问题,请在下面发表评论。祝你好运!

答案 1 :(得分:0)

你的主循环中可能没有pygame.display.update()调用。

我看到你创建了一个Grid对象。由于您的网格没有变化,您可以将线条一次绘制到网格上,然后只将网格显示在主循环的主屏幕上。

class Grid(Sprite):
    def __init__(self):
        self.grid = pygame.Surface(size)
        self.grid.set_colorkey((0,0,0))
        self.createGrid()

    def createGrid(self):
        grid_x = 0
        grid_y = 0
        for i in range(GRID_SIZE):
            pygame.draw.aaline(self.grid,BLACK,[grid_x,UPPER_BOUNDARY],[grid_x,LOWER_BOUNDARY],1)
            pygame.draw.aaline(self.grid,BLACK,[LEFT_BOUNDARY,y],[RIGHT_BOUNDARY,grid_y],1)
            grid_x += TILE_SIZE
            grid_y += TILE_SIZE

    def draw(self,screen):
        pygame.draw.rect(screen,BLACK,(49*TILE_SIZE,34*TILE_SIZE,TILE_SIZE,TILE_SIZE))
        screen.blit(self.grid,(0,0))