我正在尝试制作游戏,并且只想在使用rungame()
之后才更新mouseClickHandler
。
这就是我在runGame()
中所做的:
def runGame():
global i,update,scoreDraw,questionA
setInitialValues()
drawObjects()
if update == True:
questionAsk()
canvas.update()
i += 1
updateObjects()
checkForCollisions()
canvas.delete(scoreDraw,questionA)
canvas.update()
这就是runGame()
的作用:
def setInitialValues():
global score, time, update, worldGIF
score = 0
time = 0
update = True
worldGIF = PhotoImage(file = "WorldMap.gif")
def drawObjects():
global worldMap,scoreStart,questionAA
worldMap = canvas.create_image(650, 400, image = worldGIF)
scoreStart = canvas.create_text(500,600,text=score, font= 'Times 24', anchor = W, fill = "white")
questionAA = canvas.create_text(400,700, text="Hello!", font= "Times 20", anchor = W,fill = "white")
canvas.update()
def questionAsk():
global x_coord, y_coord, country, question, i
global question
question = "Where is", country[i],"?"
def updateObjects():
global scoreDraw, questionA
roundScore = round(score)
scoreDraw = canvas.create_text(500,600,text=roundScore, font= 'Times 24', anchor = W, fill = "white")
questionA = canvas.create_text(400,700, text=question, font= "Times 20", anchor = W,fill = "white")
canvas.update()
def mouseClickHandler( event ):
global i, xMouse,yMouse,answerx,answery,score,distance,update
canvas.delete(scoreStart,questionAA)
xMouse = event.x
yMouse = event.y
answerx = x_coord[i]
answery = y_coord[i]
delta_x = abs(xMouse - answerx)
delta_y = abs(yMouse - answery)
distance = sqrt((delta_x)**2+(delta_y)**2)
score = score + distance
update = True
但是由于某些原因,它永远不会更新,只是没有检测到update = True
中的mouseClickHandler
语句。
其他:
x_coord, y_coord, country
都是我之前制作并导入到这里的所有numpy数组。 worldGIF
只是一张地图的图片。
所以tldr;我找不到在只有点击的情况下触发if update == True
中runGame()
部分的方法。
这些绑定在底部:
root.after( 0, runGame )
canvas.bind( "<Button-1>", mouseClickHandler )
canvas.bind( "<Key>", keyDownHandler )
canvas.bind( "<KeyRelease>", keyUpHandler )
canvas.pack()
canvas.focus_set()
答案 0 :(得分:1)
如果您希望runGame
每次鼠标单击都运行,则需要在mouseClickHandler
中调用它(假设该函数绑定到click事件)
例如:
canvas.bind("<1>", runGame)
def mouseClickHandler( event ):
global i, xMouse,yMouse,answerx,answery,score,distance,update
...
update = True
runGame()
我的猜测是,您不希望runGame
每次单击都运行,而只希望if
块中的代码运行。如果是这样,请将代码移到一个单独的块中,然后从mouseClickHander
进行调用,并取消使用update
变量。
例如:
def runGame():
global i,scoreDraw,questionA
setInitialValues()
drawObjects()
updateGame()
def updateGame():
global i
questionAsk()
canvas.update()
i += 1
updateObjects()
checkForCollisions()
canvas.delete(scoreDraw,questionA)
canvas.update()
def mouseClickHandler( event ):
global i, xMouse,yMouse,answerx,answery,score,distance
canvas.delete(scoreStart,questionAA)
...
score = score + distance
updateGame()