0

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

这是我尝试过的

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()
4

2 回答 2

0

您需要将您的draw函数传递给一个screen实例。否则,您将它一事无成。

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

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

于 2014-04-05T13:55:28.837 回答
0

您的主循环中可能缺少一个pygame.display.update()调用。

我看到您创建了一个网格对象。由于您的网格不会改变,您可以在网格上绘制一次线条,然后只在主循环中将网格粘贴到主屏幕上。

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))
于 2014-04-05T17:42:02.410 回答