{"title":"import random\nimport turtle\nimport pgzrun as pr\nimport datetime\nimport time\nimport copy\n\nTILE_SIZE = 50\nWIDTH = (10 * TILE_SIZE)\nHEIGHT = (10 * TILE_SIZE)\nTITLE = '消灭星星'\n\nstars = []\nfor i in range(10):\n row = []\n for j in range(10):\n x = random.randint(1,6)\n row.append(x)\n stars.append(row)\n\nTiles = []\ndef updateTiles():\n for i in range(10):\n for j in range(10):\n tile = Actor('star'+str(stars[i][j]))\n tile.left = j * TILE_SIZE\n tile.top = i * TILE_SIZE\n Tiles.append(tile)\n\nupdateTiles()\n\ndef draw():\n screen.clear()\n screen.fill("black")\n for tile in Tiles:\n tile.draw()\n for i in range(10):\n for j in range(10):\n screen.draw.text(str(stars[i][j]),(jTILE_SIZE,iTILE_SIZE),fontsize=35,color="white")\n\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 con.clear()\n for each in tempSet:\n stars[each[0]][each[1]] = 0\n\n updateTiles() \n\npr.go()\n\n在代码中存在一个错误。在点击鼠标时,应该更新con集合,而不是tempSet集合。修改代码如下:\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 con.clear()\n for each in tempSet:\n stars[each[0]][each[1]] = 0\n\n updateTiles()\n\n\n这样修改后,点击鼠标时应该能正确更新con集合并消除星星。


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

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