{"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 = []\n\n\ndef updateTiles():\n Tiles.clear()\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\n\nupdateTiles()\n\n\ndef draw():\n screen.clear()\n for tile in Tiles:\n tile.draw()\n for i in range(10):\n for j in range(10):\n screen.draw.text(\n str(stars[i][j]), (jTILE_SIZE, iTILE_SIZE), fontsize=35, color="white")\n\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 tempSet.clear()\n for each in con:\n stars[each[0]][each[1]] = 0\n\n updateTiles()\n\n draw()\n\npr.run()\n\n\n# 缺少的 update 函数和 updateTiles 函数定义\ndef update():\n pass\n\ndef updateTiles():\n Tiles.clear()\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\n\n# 在 draw 函数中调用 update 函数\ndef draw():\n screen.clear()\n update()\n for tile in Tiles:\n tile.draw()\n for i in range(10):\n for j in range(10):\n screen.draw.text(\n str(stars[i][j]), (jTILE_SIZE, iTILE_SIZE), fontsize=35, color="white")\n\n\n# 优化建议:\n# 1. 将 updateTiles 函数改名为 update_tiles 以符合 Python 命名规范。\n# 2. 在 updateTiles 函数中使用列表推导式简化代码。\n# 3. 将 on_mouse_down 函数中重复的代码提取出来,提高代码的可读性和可维护性。\n# 4. 使用更直观的变量名,例如使用 clicked_stars 替代 con,使用 star_index 替代 each。\n# 5. 使用 enumerate 函数简化循环代码。\n\n# 改进后的代码如下:\n\nimport 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 = []\n\n\ndef update_tiles():\n Tiles = [Actor('star'+str(stars[i][j])) for i in range(10) for j in range(10)]\n for i, tile in enumerate(Tiles):\n tile.left = (i % 10) * TILE_SIZE\n tile.top = (i // 10) * TILE_SIZE\n\n\ndef update():\n pass\n\n\ndef draw():\n screen.clear()\n update()\n for tile in Tiles:\n tile.draw()\n for i in range(10):\n for j in range(10):\n screen.draw.text(\n str(stars[i][j]), (jTILE_SIZE, iTILE_SIZE), fontsize=35, color="white")\n\n\ndef on_mouse_down(pos, button):\n ic = int(pos[1]/TILE_SIZE)\n jc = int(pos[0]/TILE_SIZE)\n clicked_stars = {(ic, jc)}\n for _ in range(20):\n temp_stars = clicked_stars.copy()\n for star_index in temp_stars:\n i, j = star_index\n clid = stars[i][j]\n if i > 0 and stars[i-1][j] == clid:\n clicked_stars.add((i-1, j))\n if i < 9 and stars[i+1][j] == clid:\n clicked_stars.add((i+1, j))\n if j > 0 and stars[i][j-1] == clid:\n clicked_stars.add((i, j-1))\n if j < 9 and stars[i][j+1] == clid:\n clicked_stars.add((i, j+1))\n temp_stars.clear()\n for star_index in clicked_stars:\n i, j = star_index\n stars[i][j] = 0\n\n update_tiles()\n\n draw()\n\npr.run()


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

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