Jess
Jess

Reputation: 13

Dictionary not populating correctly

I've encountered an issue with my code that I simply cannot figure out.

As per the below section of code:

After creating a 2nd dictionary (worldmap) in the Generate_Game_World class, the HQ location is stored in

self.worldmap[HQLOCATIONX,HQLOCATIONY]["Region_Type"] = "HQ"

However, after doing so it seems to fill up the entire array with the "HQ" value as seen in the test window. I simply cannot figure out why this is happening.

import pygame
from tkinter import *

test = Tk ()
test.geometry = ("640x480")

pygame.init()

WORLDSIZE = 499
HQLOCATIONX = int(WORLDSIZE/2)
HQLOCATIONY = int(WORLDSIZE/2)

class Generate_Game_World ():

    regionData = {"Region_Type" : "None", 
                  "Region_Level" : 0}

    def __init__(self, mapSize):
        self.mapSize = mapSize

    def main (self):
        # creates 2d map
        self.worldmap = {(x,y) : self.regionData for x in range(self.mapSize) for y in range (self.mapSize)}

        # Sets the HQ to worldmap(249,249)
        self.worldmap[HQLOCATIONX,HQLOCATIONY]["Region_Type"] = "HQ"

        # checks worldmap(0,0) --> (10,10) for its Region Type
        for x in range (10):
            for y in range (10):
                label = Label (text=self.worldmap[x,y]["Region_Type"]).grid(row = x, column=y)

class Game (object):
    def main (self, screen):
        gameworld = Generate_Game_World(WORLDSIZE)     
        gameworld.main()

        while 1:

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    return

                if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                    return

            test.mainloop()
            pygame.display.flip()

if __name__ == "__main__":
    screen = pygame.display.set_mode((1024, 768))
    Game().main(screen)

Upvotes: 1

Views: 128

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1123940

All your dictionary values are references to the one class-attribute regionData.

You want to create copies instead:

self.worldmap = {(x,y): self.regionData.copy() for x in range(self.mapSize) for y in range (self.mapSize)}

This uses the dict.copy() method to create a shallow copy instead; this is enough as the values of Generate_Game_World.regionData are mere strings and these are immutable so are safe to be shared.

You could also use a dictionary literal to create a new dictionary from scratch; you don't seem to be using self.regionData anywhere else so inlining is a possibility:

self.worldmap = {(x,y): {"Region_Type" : "None", "Region_Level" : 0} 
                 for x in range(self.mapSize) for y in range (self.mapSize)}

Last but not least, did you mean to use the None singleton instead of a string "None" perhaps?

Upvotes: 2

Related Questions