无法绘制二维数组,pygame

时间:2016-11-01 15:18:01

标签: arrays pygame 2d

我目前正在尝试使用pygame创建网格框。我的代码在下面,我似乎无法想出这个。我的目标是让pygame抽出100个矩形。

import pygame

pygame.init()

pygame.display.set_caption("2D Grid")




screen = pygame.display.set_mode((800, 600))

WHITE = (255, 255, 255)

BLACK = (0, 0, 0)

RED = (255, 0, 0)

GREEN = (0, 255, 0)

BLUE = (0, 0, 255)

w = 60

grid = [[n]*10 for n in range(10)]
print grid

def drawGrid():

    x = 0
    y = 0
    for row in grid:
        for col in row:
            pygame.Rect(x , y, w, w)
            x = x + w
        y = y + w
        x = 0

running = True

while running:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            running = False

    pygame.draw.rect(screen, WHITE, drawGrid())
    pygame.display.flip()


pygame.quit()

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,毕竟这很简单。

def drawGrid():

x = 0
y = 0
for row in grid:
    for col in row:
        box = pygame.Rect(x, y, w, w)
        pygame.draw.rect(screen, WHITE, box)
        pygame.Surface.blit(screen, space_img, box)
        x = x + w
    y = y + w
    x = 0

我尝试在绘制矩形时调用绘图功能。所以我只是试着调用函数,当然这很有效。 :)