如何增量增加同一字典中的所有元组

2024-09-24 08:33:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一本字典,可以生成“地图” e、 g.元组(x,y):mappile

mapGenCode = {(1, 1): 0, (2, 1): 1, (3, 1): 0, (1, 2): 0, (2, 2): 2, (3, 2): 1}

我试图实现的是,当按下键盘箭头时,同一字典中的所有x/y值都会增加+1/-1

if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_LEFT:
            x_loc = x_loc-1
            x_map -1

mapRefresh(x_map, y_map)

def mapRefresh(x, y):

    if x == -1:
        for (key1, key2), value in mapGenCode.iteritems():
            newkeyX = key1 -1
            mapGenCode[newKey1, key2] = value

我以前遇到过这个错误

runtimeerror: dictionary changed size during iteration

Tags: eventmapif字典value地图键盘pygame