Snippets

Joel Couch My brother wanted me to make the best game I could in 20 lines.

Created by Joel Couch
import tkinter, random
def mouseclick(e):
    if w.bbox(b)[0]<e.x<w.bbox(b)[0]+80 and 10<e.y<w.bbox(b)[1]+80:
        score['text'] = str( int(score['text']) + 1 )
        return moveBox(timer=10000)
    GUI.unbind('<Button-1>')
    score['text'] = "Game Over!  Final Score: " + score['text']
def moveBox(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)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.