import randomimport turtleimport pgzrun as primport datetimeimport timeimport copyTILE_SIZE = 50WIDTH = 10 TILE_SIZEHEIGHT = 10 TILE_SIZETITLE = 消灭星星stars = for i in range10 row = for j in ra
代码中存在一些错误。首先,需要在开始时导入所需的模块。正确的导入顺序应该是:import random,import turtle,import pgzrun as pr,import datetime,import time,import copy。
其次,在更新瓷砖时,应该先清空Tiles列表,然后再添加新的瓷砖。可以在updateTiles()函数的开头添加Tiles.clear()语句。
最后,在鼠标点击事件的处理函数on_mouse_down()中,需要添加代码来重新绘制屏幕。可以在updateTiles()函数的末尾添加draw()函数来重新绘制屏幕。
以下是修改后的代码:
import random
import turtle
import pgzrun as pr
import datetime
import time
import copy
TILE_SIZE = 50
WIDTH = (10 * TILE_SIZE)
HEIGHT = (10 * TILE_SIZE)
TITLE = '消灭星星'
stars = []
for i in range(10):
row = []
for j in range(10):
x = random.randint(1,6)
row.append(x)
stars.append(row)
Tiles = []
def updateTiles():
Tiles.clear()
for i in range(10):
for j in range(10):
tile = Actor('star'+str(stars[i][j]))
tile.left = j * TILE_SIZE
tile.top = i * TILE_SIZE
Tiles.append(tile)
updateTiles()
def draw():
screen.clear()
screen.fill("black")
for tile in Tiles:
tile.draw()
for i in range(10):
for j in range(10):
screen.draw.text(str(stars[i][j]),(j*TILE_SIZE,i*TILE_SIZE),fontsize=35,color="white")
def on_mouse_down(pos,button):
ic = int(pos[1]/TILE_SIZE)
jc = int(pos[0]/TILE_SIZE)
con = {(ic,jc)}
for k in range(20):
tempSet = copy.deepcopy(con)
for each in tempSet:
i = each[0]
j = each[1]
clid = stars[i][j]
if i > 0 and stars[i-1][j] == clid:
con.add((i-1,j))
if i < 9 and stars[i+1][j] == clid:
con.add((i+1, j))
if j > 0 and stars[i][j-1] == clid:
con.add((i, j-1))
if j < 9 and stars[i][j+1] == clid:
con.add((i, j+1))
tempSet.clear()
for each in con:
stars[each[0]][each[1]] = 0
updateTiles()
draw()
pr.go()
请注意,这只是修复了代码中的一些错误,并不保证整个程序的逻辑正确。如果还有其他问题,请提供更多详细信息,以便进一步帮助您
原文地址: http://www.cveoy.top/t/topic/h6Em 著作权归作者所有。请勿转载和采集!