在代码中存在一个错误。在循环中,我们应该使用con集合的副本tempSet来遍历,而不是直接遍历con集合。这是因为在循环中我们要修改con集合,但在遍历过程中不能修改集合。修改后的代码如下:\n\npython\ndef on_mouse_down(pos,button):\n ic = int(pos[1]/TILE_SIZE)\n jc = int(pos[0]/TILE_SIZE)\n con = {(ic,jc)}\n for k in range(20):\n tempSet = copy.deepcopy(con)\n for each in tempSet:\n i = each[0]\n j = each[1]\n clid = stars[i][j]\n if i > 0 and stars[i-1][j] == clid:\n con.add((i-1,j))\n if i < 9 and stars[i+1][j] == clid:\n con.add((i+1, j))\n if j > 0 and stars[i][j-1] == clid:\n con.add((i, j-1))\n if j < 9 and stars[i][j+1] == clid:\n con.add((i, j+1))\n tempSet.clear()\n for each in con:\n stars[each[0]][each[1]] = 0\n\n updateTiles()\n\n\n请注意,我们还将tempSet.clear()的位置从内循环移到外循环中。这样,我们在内循环之后才清空tempSet集合,确保在内循环中不会清空con集合。


原文地址: https://www.cveoy.top/t/topic/pPyH 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录