简体   繁体   中英

How to add multiple images in pygame onto a grid?

This is the code of how i tried to put units onto the screen however i can make them into individual pieces, it only shows one type.

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

How do i make them into individual pieces that i can the move instead of them all being the same piece, i want to have multiple of different images on the grid.

这就是发生的事情

You constantly blit the image RedInfantry :

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

You have to blit the image which is stored in the grid rather than RedInfantry :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM