from tkinter import *
from random import randrange as rnd
def new_rect():
global x,y,rect,rcon
canvas.delete(rect)
x = rnd(100,700)
y = rnd(100,500)
rcon = rnd(0,3)
if (rcon == 0):
colorval = "#%02x%02x%02x" % (255, 0, 0)
rect = canvas.create_rectangle(x,y,x+100,y+100,outline=colorval, fill=colorval)
else:
colorval = "#%02x%02x%02x" % (255, 255, 0)
rect = canvas.create_rectangle(x,y,x+100,y+100,outline=colorval, fill=colorval)
root.after(1000,new_rect)
def click(event):
global points, text,rcon
canvas.delete(text)
canvas.delete(rect)
if (event.x - x >= 0
and event.x - x <= 100
and event.y - y >= 0
and event.y - y <= 100
and rcon == 0):
points += 10
else:
points -= 100
text = canvas.create_text(50,20,text=str(points), font = 'Arial 20')
root = Tk()
root.geometry('800x600')
canvas = Canvas(root,bg='white')
canvas.pack(fill=BOTH,expand=1)
rcon = 0
x = 0
y = 0
rect = canvas.create_rectangle(x,y,x+100,y+100,outline="#fb0", fill="#fb0")
text = canvas.create_text(20,20,text=0, font = 'Arial 20')
points = 0
new_rect()
canvas.bind('<Button-1>', click)
mainloop()
|