Pygame库存显示问题

时间:2017-11-09 14:56:12

标签: python pygame

我的问题是在收集足够的资源以便在放下它们时进入两位数时,库存显示失败并将单个数字显示为双倍。

示例:放下污垢,它会12,11,10,90,80

我想知道如何解决这个问题,使其正确倒数到0,然后在必要时返回显示单个数字。

代码:

inventory = {
    D : 0,
    G : 0,
    WA : 0,
    C : 0,
    L : 0,
    S : 0,
    WO : 0
            }

            #placing dirt
            if (event.key ==K_1):
                #get the tile to swap with the dirt
                currentTile = tilemap[playerPos[1]][playerPos[0]]
                #if we have dirt in our inventory to place
                if inventory[D] > 0:
                    #remove one dirt and place it on map
                    inventory[D] -= 1
                    tilemap[playerPos[1]][playerPos[0]] = D
                    #pick up tile that was there before
                    inventory[currentTile] += 1


            #placing grass
            if (event.key ==K_2):
                #get the tile to swap with the grass
                currentTile = tilemap[playerPos[1]][playerPos[0]]
                #if we have grass in our inventory to place
                if inventory[G] > 0:
                    #remove one grass and place it on map
                    inventory[G] -= 1
                    pygame.display.update()
                    tilemap[playerPos[1]][playerPos[0]] = G
                    #pick up tile that was there before
                    inventory[currentTile] += 1

            #placing water
            if (event.key ==K_3):
                #get the tile to swap with the water
                currentTile = tilemap[playerPos[1]][playerPos[0]]
                #if we have water in our inventory to place
                if inventory[WA] > 0:
                    #remove one water and place it on map
                    inventory[WA] -= 1
                    tilemap[playerPos[1]][playerPos[0]] = WA
                    #pick up tile that was there before
                    inventory[currentTile] += 1

            #placing coal
            if (event.key ==K_4):
                #get the tile to swap with the coal
                currentTile = tilemap[playerPos[1]][playerPos[0]]
                #if we have coal in our inventory to place
                if inventory[C] > 0:
                    #remove one coal and place it on map
                    inventory[C] -= 1
                    pygame.display.update()
                    tilemap[playerPos[1]][playerPos[0]] = C
                    #pick up tile that was there before
                    inventory[currentTile] += 1

            #placing lava
            if (event.key ==K_5):
                #get the tile to swap with the lava
                currentTile = tilemap[playerPos[1]][playerPos[0]]
                #if we have lava in our inventory to place
                if inventory[L] > 0:
                    #remove one lava and place it on map
                    inventory[L] -= 1
                    tilemap[playerPos[1]][playerPos[0]] = L
                    #pick up tile that was there before
                    inventory[currentTile] += 1

            #placing stone
            if (event.key ==K_6):
                #get the tile to swap with the stone
                currentTile = tilemap[playerPos[1]][playerPos[0]]
                #if we have stone in our inventory to place
                if inventory[S] > 0:
                    #remove one stone and place it on map
                    inventory[S] -= 1
                    tilemap[playerPos[1]][playerPos[0]] = S
                    #pick up tile that was there before
                    inventory[currentTile] += 1

            #placing wood
            if (event.key ==K_7):
                #get the tile to swap with the wood
                currentTile = tilemap[playerPos[1]][playerPos[0]]
                #if we have wood in our inventory to place
                if inventory[WO] > 0:
                    #remove one wood and place it on map
                    inventory[WO] -= 1
                    tilemap[playerPos[1]][playerPos[0]] = WO
                    #pick up tile that was there before
                    inventory[currentTile] += 1

    #loop each row
    for row in range(MAPHEIGHT):
        #loop each column
        for column in range(MAPWIDTH):
            #draw the resources images in the correct position
            DISPLAYSURF.blit(textures[tilemap[row][column]], (column*TILESIZE,row*TILESIZE))

    #display the player at the correct position
    DISPLAYSURF.blit(PLAYER,(playerPos[0]*TILESIZE,playerPos[1]*TILESIZE))

    placePosition = 10
    for item in inventory:
        DISPLAYSURF.blit(textures[item],(placePosition,MAPHEIGHT*TILESIZE+20))
        placePosition += 30
        textobj = INVFONT.render(str(inventory[item]), True, WHITE, BLACK)
        DISPLAYSURF.blit(textobj, (placePosition,MAPHEIGHT*TILESIZE+20))
        placePosition += 50
    #update the display
    pygame.display.update()

0 个答案:

没有答案