如何仅在有点击事件时更新我的 tkinter 游戏?
How can I update my tkinter game only when there is a click event?
我正在尝试制作游戏,我想仅在使用 mouseClickHandler
后更新 rungame()
。
这就是我在 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
但它从不更新,出于某种原因,它只是没有检测到 mouseClickHandler
中的 update = True
语句。
额外内容:
x_coord, y_coord, country
都是我之前制作并导入到这里的numpy数组。 worldGIF
只是一张地图的图片。
所以 tldr;我找不到一种方法来触发 runGame()
中的 if update == True
部分,只有在有点击的地方。
这些绑定在底部:
root.after( 0, runGame )
canvas.bind( "<Button-1>", mouseClickHandler )
canvas.bind( "<Key>", keyDownHandler )
canvas.bind( "<KeyRelease>", keyUpHandler )
canvas.pack()
canvas.focus_set()
如果您希望每次点击鼠标时 runGame
到 运行,您将需要在 mouseClickHandler
中调用它(假设该函数绑定到点击事件)
例如:
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()
我正在尝试制作游戏,我想仅在使用 mouseClickHandler
后更新 rungame()
。
这就是我在 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
但它从不更新,出于某种原因,它只是没有检测到 mouseClickHandler
中的 update = True
语句。
额外内容:
x_coord, y_coord, country
都是我之前制作并导入到这里的numpy数组。 worldGIF
只是一张地图的图片。
所以 tldr;我找不到一种方法来触发 runGame()
中的 if update == True
部分,只有在有点击的地方。
这些绑定在底部:
root.after( 0, runGame )
canvas.bind( "<Button-1>", mouseClickHandler )
canvas.bind( "<Key>", keyDownHandler )
canvas.bind( "<KeyRelease>", keyUpHandler )
canvas.pack()
canvas.focus_set()
如果您希望每次点击鼠标时 runGame
到 运行,您将需要在 mouseClickHandler
中调用它(假设该函数绑定到点击事件)
例如:
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()