[英]How to incrementally increase all tuples within the same dictionary
I have a dictionary which generates a "map" eg tuple (x, y) : mapTile 我有一本字典,它生成一个“地图”,例如元组(x,y):mapTile
mapGenCode = {(1, 1): 0, (2, 1): 1, (3, 1): 0, (1, 2): 0, (2, 2): 2, (3, 2): 1}
What I'm trying to achieve is when an keyboard arrow is pushed all the x/y values within the same dictionary are incremented by +1/-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
I have previously run into this error 我以前遇到过这个错误
runtimeerror: dictionary changed size during iteration
Instead of "for key, value in dic", you can consider following code 您可以考虑以下代码,而不是“ for key,dic中的值”
while key in dic:
func()
key += 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.