我的代码遇到了一个我根本无法弄清楚的问题。
根据以下代码部分:
(worldmap)
在类中创建第二个字典后Generate_Game_World
,总部位置存储在
self.worldmap[HQLOCATIONX,HQLOCATIONY]["Region_Type"] = "HQ"
但是,这样做之后,它似乎用测试窗口中看到的“HQ”值填充了整个数组。我根本无法弄清楚为什么会这样。
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)