Pygame 消灭星星游戏教程:消除星星逻辑错误修复
{/'title/':/'import random//nimport pgzrun as pg//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//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 = set()//n con.add((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//nupdateTiles()//npg.go()//n哪里错了?内容:程序中有一个错误:在on_mouse_down函数中,需要将stars列表更新为消除星星后的新状态,但是在代码中没有实现这一步骤。可以通过添加以下代码来解决这个问题://n//npython//nfor each in con://n stars[each[0]][each[1]] = 0//n//n//n这样就可以将被消除的星星的值更新为0。然后,可以调用updateTiles函数更新Tiles列表,以便在屏幕上显示新的星星状态。/
原文地址: https://www.cveoy.top/t/topic/pPzM 著作权归作者所有。请勿转载和采集!