importtkinter,randomdefmouseclick(e):ifw.bbox(b)[0]<e.x<w.bbox(b)[0]+80and10<e.y<w.bbox(b)[1]+80:score['text']=str(int(score['text'])+1)returnmoveBox(timer=10000)GUI.unbind('<Button-1>')score['text']="Game Over! Final Score: "+score['text']defmoveBox(timer=1000):w.move(b,((10+100*int(random.random()*5))-w.bbox(b)[0]),0)GUI.after(timer,moveBox)GUI=tkinter.Tk()GUI.title("Only click the red box; good luck!")w=tkinter.Canvas(GUI,width=500,height=90,background='black')w.pack(fill="both")b=w.create_rectangle(10,10,80,80,fill='red')score=tkinter.Label(GUI,text='0')score.pack()GUI.bind('<Button-1>',mouseclick)moveBox()GUI.mainloop()
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.