gpt4 book ai didi

python - 如何将pygame中的多个图像添加到网格上?

转载 作者:行者123 更新时间:2023-12-04 10:24:41 26 4
gpt4 key购买 nike

这是我如何尝试将单位放在屏幕上的代码,但是我可以将它们分成单独的部分,它只显示一种类型。

grid = [[None for i in range(0, 900, 50)] for j in range(0, 900, 50)]
grid[0][2] = RedInfantry
grid[1][2] = RedAntiAirMissleLauncher
grid[2][2] = RedAntiAirTank
grid[3][2] = RedBomber
grid[4][2] = RedHelicopter
grid[5][2] = RedJeep
grid[6][2] = RedMobileGun
grid[7][2] = RedRocketLauncher
grid[8][2] = RedTank
grid[9][2] = RedTransporter

grid[0][9] = BlueInfantry
grid[1][9] = BlueAntiAirMissleLauncher
grid[2][9] = BlueAntiAirTank
grid[3][9] = BlueBomber
grid[4][9] = BlueHelicopter
grid[5][9] = BlueJeep
grid[6][9] = BlueMobileGun
grid[7][9] = BlueRocketLauncher
grid[8][9] = BlueTank
grid[9][9] = BlueTransporter



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(RedInfantry,(x, y))



# update dispaly
pygame.display.update()

我如何将它们变成可以移动的单独部分,而不是它们都是同一块,我想在网格上有多个不同的图像。

This is what has happened

最佳答案

你不断地对图像进行闪烁 RedInfantry :

Background.blit(RedInfantry,(x, y))


您必须对存储在网格中的图像进行 blit,而不是 RedInfantry :

image = grid[i][j]
if image != None:
Background.blit(image, (x, y))

关于python - 如何将pygame中的多个图像添加到网格上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60671773/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com