1 回答
TA贡献1829条经验 获得超9个赞
将以下网格打包:
grid = [[None for i in range(0, 900, 50)] for j in range(0, 900, 50)]
将图像分配给网格中的字段。例如:
grid[3][2] = RedInfantry
在应用程序循环的网格中绘制图像:
for i in range(len(grid)):
for j in range(len(grid[i])):
x, y = i * 50, j * 50
image = grid[i][j]
if image != None:
Background.blit(RedInfantry,(x, y))
此外,我建议在主应用程序循环中连续绘制整个场景:
import pygame
pygame.init()
Background = pygame.display.set_mode((900 ,900))
Green = (45,198,14)
RedInfantry = pygame.Surface((50, 50))
RedInfantry.fill((255, 0, 0))
grid = [[None for i in range(0, 900, 50)] for j in range(0, 900, 50)]
grid[3][2] = RedInfantry
run = True
while run:
# handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# draw background
Background.fill(Green)
# draw scene
for i in range(0, 900, 50):
pygame.draw.line(Background, (0, 0, 0), (0, i), (900, i))
pygame.draw.line(Background, (0, 0, 0), (i, 0), (i, 900))
for i in range(len(grid)):
for j in range(len(grid[i])):
x, y = i * 50, j * 50
image = grid[i][j]
if image != None:
Background.blit(image,(x, y))
# update dispaly
pygame.display.update()
添加回答
举报