Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python for循环执行次数不足_Python_Loops_Pygame - Fatal编程技术网

python for循环执行次数不足

python for循环执行次数不足,python,loops,pygame,Python,Loops,Pygame,我想用pygame做一个游戏。该程序涉及循环通过一系列对象,并确定它们是否与另一个对象碰撞。 但是,当我运行for循环时,它的执行次数不如列表中的对象多 print(f"Length of array: {str(len(Crate.Crates))}") counter = 0 for crate in Crate.Crates: counter += 1 if crate.colide and crate.rect.colliderect

我想用pygame做一个游戏。该程序涉及循环通过一系列对象,并确定它们是否与另一个对象碰撞。 但是,当我运行for循环时,它的执行次数不如列表中的对象多

print(f"Length of array: {str(len(Crate.Crates))}")
counter = 0
   for crate in Crate.Crates:
       counter += 1
       if crate.colide and crate.rect.colliderect(self.explosionRect):
           crate.hitByBall()
print(f"loop has executed {str(counter)} times")
运行此代码后得到的输出如下:

Length of array: 25
loop has executed 23 times
以下是GitHub存储库,您可以在其中找到完整的代码:
(上面提到的代码片段位于game/crates/Bomb/crates.py)

如果方法
crater.hitbybyball()
从列表中删除对象,则必须迭代列表的副本(另请参见):

用于板条箱中的板条箱。板条箱:

用于板条箱中的板条箱。板条箱[:]:

打高尔夫球是做什么的?它是否删除列表中的对象?如果是这样,请在板条箱中的板条箱的列表中迭代一个副本。
。板条箱[:]:
尝试在for loopThank@rabbi76之后打印数组长度。在列表副本上进行迭代非常有效