Glitchd
Glitchd

Reputation: 361

Pygame issue : maybe something to do with dictionaries?

I am making a sort of 2D Minecraft and the inventory won't work. i have been following this tutorial: http://usingpython.com/adding-an-inventory/ and heres an: 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()

something in this:

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

Doesn't seem to be working and is making it repeat 8 times(the rest of the code works fine) I have checked through for things that don't look right and there is nothing I can see. If anyone could help I would be very grateful.

Upvotes: 3

Views: 37

Answers (1)

Rabbid76
Rabbid76

Reputation: 210996

This issue is caused, because the relevant code block is in a loop:

for item in resources:

and resources is initialized:

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]

So of course, the output for DIRT, GRASS, WATER, COAL, LAVA and STONE is generated multiple times.

Use set() to generate a set object of unique items from the list:

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 

Upvotes: 1

Related Questions