Reputation: 27
I'm trying to make a game and I want to update the rungame()
only after the mouseClickHandler
was used.
and this is what I did in 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()
and this is what runGame()
does:
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
But it never updates, for some reason, it just doesn't detect the update = True
statement in the mouseClickHandler
.
Extras:
x_coord, y_coord, country
are all numpy arrays which I have made earlier and imported into here. worldGIF
is just a picture of a map.
So tldr; I can't find a way to trigger the if update == True
part in runGame()
only where there is a Click.
These binds at the bottom:
root.after( 0, runGame )
canvas.bind( "<Button-1>", mouseClickHandler )
canvas.bind( "<Key>", keyDownHandler )
canvas.bind( "<KeyRelease>", keyUpHandler )
canvas.pack()
canvas.focus_set()
Upvotes: 0
Views: 148
Reputation: 386342
If you want runGame
to run on every mouse click, you're going to need to call it in mouseClickHandler
(assuming that function is bound to a click event)
For example:
canvas.bind("<1>", runGame)
def mouseClickHandler( event ):
global i, xMouse,yMouse,answerx,answery,score,distance,update
...
update = True
runGame()
My guess is that you don't want runGame
to run on every click, but only the code in the if
block. If that's true, move that code to a separate block and call it from mouseClickHander
, and eliminate the use of the update
variable.
For example:
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()
Upvotes: 1