Pygame问题:也许与字典有关?

时间:2019-02-22 21:26:25

标签: python python-3.x pygame

我正在制作2D Minecraft,因此无法使用库存。 我一直在遵循本教程: http://usingpython.com/adding-an-inventory/ 和继承人: Image of code, look at bottom part of window

import pygame
import sys
from pygame.locals import *
import random

pygame.display.set_caption('First game')

BLACK = (0, 0, 0 )
BROWN = (153, 76, 0 )
GREEN = (0, 255, 0 )
BLUE = (0, 0, 255 )
RED = (255, 0, 0)
GREY = (100, 100, 100)
WHITE = (255, 255, 255)

DIRT = 0
GRASS = 1
WATER = 2
COAL = 3
LAVA = 4
STONE = 5
DIAMOND = 6

TILESIZE = 30
MAPWIDTH = 32
MAPHEIGHT= 16

picture1 = pygame.image.load('images/dirt.png')
picture1 = pygame.transform.scale(picture1, (TILESIZE, TILESIZE))

picture2 = pygame.image.load('images/grass.png')
picture2 = pygame.transform.scale(picture2, (TILESIZE, TILESIZE))

picture3 = pygame.image.load('images/water.png')
picture3 = pygame.transform.scale(picture3, (TILESIZE, TILESIZE))

picture4 = pygame.image.load('images/coal.png')
picture4 = pygame.transform.scale(picture4, (TILESIZE, TILESIZE))

picture5 = pygame.image.load('images/lava.png')
picture5 = pygame.transform.scale(picture5, (TILESIZE, TILESIZE))

picture6 = pygame.image.load('images/stone.png')
picture6 = pygame.transform.scale(picture6, (TILESIZE, TILESIZE))

picture7 = pygame.image.load('images/diamond.png')
picture7 = pygame.transform.scale(picture7, (TILESIZE, TILESIZE))

textures = {
    DIRT       :   picture1,
    GRASS  :   picture2,
    WATER  :   picture3,
    COAL     :   picture4,
    LAVA     :    picture5,
    STONE  :    picture6,
    DIAMOND :  picture7
    }

inventory = {
    DIRT       :   0,
    GRASS  :   0,
    WATER  :   0,
    COAL     :   0,
    LAVA      :    0,
    STONE  :    0,
    DIAMOND : 0
}

face = pygame.image.load('images/steeveface.png')
face = pygame.transform.scale(face, (TILESIZE-4, TILESIZE-4))

for rw in range(MAPHEIGHT):
    for cl in range(MAPWIDTH):
        randomNumber = random.randint(0,10)
        if randomNumber <=5:
            tile = DIAMOND
        else:
            tile = STONE

resources = [DIRT,DIRT,DIRT,DIRT,DIRT,DIRT,DIRT,DIRT,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,WATER,WATER,WATER,WATER,WATER,COAL,COAL,COAL,COAL,COAL,COAL,LAVA,LAVA,LAVA,LAVA,LAVA,STONE,STONE,STONE,STONE,STONE,STONE,STONE,tile]
tilemap = [ [random.choice(resources) for w in range(MAPWIDTH)] for h in range(MAPHEIGHT)]


pygame.init()
DISPLAYSURF = pygame.display.set_mode((MAPWIDTH*TILESIZE, MAPHEIGHT*TILESIZE + 50))

INVFONT = pygame.font.Font('fonts/ZCOOL.ttf', 18)


playerPos = [0,0]

clock = pygame.time.Clock()
pygame.key.set_repeat()
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == KEYDOWN:
            if event.key == K_RIGHT and playerPos[0] < MAPWIDTH - 1:
                playerPos[0] += 1
            if event.key == K_LEFT and playerPos[0] > 0:
                playerPos[0] -= 1
            if event.key == K_UP and playerPos[1] > 0:
                playerPos[1] -= 1
            if event.key == K_DOWN and playerPos[1] < MAPHEIGHT -1:
                playerPos[1] += 1
            if event.key == K_SPACE:
                currentTile = tilemap[playerPos[1]][playerPos[0]]
                inventory[currentTile] += 1
                tilemap[playerPos[1]][playerPos[0]] = DIRT

            if (event.key == K_1):
                currentTile = tilemap[playerPos[1]][playerPos[0]]

                if inventory[DIRT] > 0:
                    inventory[DIRT] -= 1
                    tilemap[playerPos[1]][playerPos[0]] = DIRT
                    inventory[currentTile] += 1  
    placePosition = 10
    for item in resources:

        DISPLAYSURF.blit(textures[item],(placePosition,MAPHEIGHT*TILESIZE+20))
        placePosition += 30
        pygame.display.update()
        textObj = INVFONT.render(str(inventory[item]), True, WHITE, BLACK)
        DISPLAYSURF.blit(textObj,(placePosition,MAPHEIGHT*TILESIZE+20)) 
        placePosition += 50

    for row in range(MAPHEIGHT):
        for column in range(MAPWIDTH):
            DISPLAYSURF.blit(textures[tilemap[row][column]], (column*TILESIZE, row*TILESIZE,TILESIZE,TILESIZE))

    DISPLAYSURF.blit(face, (playerPos[0]*TILESIZE+2,playerPos[1]*TILESIZE+2))
    pygame.display.update()

其中的内容

DISPLAYSURF.blit(textures[item],(placePosition,MAPHEIGHT*TILESIZE+20))
placePosition += 30
pygame.display.update()
textObj = INVFONT.render(str(inventory[item]), True, WHITE, BLACK)
DISPLAYSURF.blit(textObj,(placePosition,MAPHEIGHT*TILESIZE+20)) 
placePosition += 50

似乎没有用,并且使其重复了8次(其余代码工作正常) 我检查了看起来不正确的东西,看不到任何东西。如果有人可以帮助我,我将非常感激。

1 个答案:

答案 0 :(得分:1)

由于相关代码块处于循环中,所以导致了此问题:

for item in resources:

resources已初始化:

resources = [DIRT,DIRT,DIRT,DIRT,DIRT,DIRT,DIRT,DIRT,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,WATER,WATER,WATER,WATER,WATER,COAL,COAL,COAL,COAL,COAL,COAL,LAVA,LAVA,LAVA,LAVA,LAVA,STONE,STONE,STONE,STONE,STONE,STONE,STONE,tile]

因此,当然会多次生成DIRTGRASSWATERCOALLAVASTONE的输出。

使用set()从列表中生成一组唯一的项目:

for item in set(resources):

    DISPLAYSURF.blit(textures[item],(placePosition,MAPHEIGHT*TILESIZE+20))
    placePosition += 30
    pygame.display.update()
    textObj = INVFONT.render(str(inventory[item]), True, WHITE, BLACK)
    DISPLAYSURF.blit(textObj,(placePosition,MAPHEIGHT*TILESIZE+20)) 
    placePosition += 50