import tkinter as tk import color from random import randint class Circle: def __init__(self, canvas: tk.Canvas, x, y, radius): self.canvas = canvas self.x = x self.y = y self.radius = radius self.value = 3 self.circle_id = canvas.create_oval(x - radius, y - radius, x + radius, y + radius, outline = ACCENT_COLOR, width = 2) self.text_id = canvas.create_text(x, y, text = self.value, fill = ACCENT_COLOR, font = (None, int(radius / 1.5))) def delete(self): self.canvas.delete(self.circle_id) self.canvas.delete(self.text_id) def decrement(self): self.value -= 1 self.canvas.itemconfigure(self.text_id, text = self.value) def highlight(self, state: bool): c = SECONDARY_COLOR if state else ACCENT_COLOR self.canvas.itemconfigure(self.circle_id, outline = c) self.canvas.itemconfigure(self.text_id, fill = c) def isInside(self, x, y): dx = x - self.x dy = y - self.y d = (dx**2 + dy**2)**0.5 return d <= self.radius # var canvas = None mouse_x = 0 mouse_y = 0 tick = 0.0 STEP_TICK = 60.0 # Circle circle = None CIRCLE_RADIUS = 50 SPACE = 5 # Colors ACCENT_COLOR = color.BROWN SECONDARY_COLOR = color.GREEN # Main def main(c: tk.Canvas): global canvas, circle # Ini circle = None canvas = c c.bind('', motion) # Loop def loop(deltatime: float, difficulty: float, end): global tick, circle # First circle if circle == None: circle = create_circle() # Step elif tick >= STEP_TICK: # Tick or end? if circle.value == 0: # Check if circle.isInside(mouse_x, mouse_y): circle.delete() circle = create_circle(difficulty) tick = 0.0 return else: end() return else: circle.decrement() tick = 0.0 # Highlight if circle.isInside(mouse_x, mouse_y): circle.highlight(True) else: circle.highlight(False) tick += difficulty # Circle def create_circle(difficulty = 1.0): # Get width, height = int(canvas["width"]), int(canvas["height"]) radius = CIRCLE_RADIUS / difficulty + 15 # Border x_range = (int(radius + SPACE), int(width - radius - SPACE)) y_range = (int(radius + SPACE), int(height - radius - SPACE)) # Create return Circle(canvas, randint(x_range[0], x_range[1]), randint(y_range[0], y_range[1]), radius) # Mouse def motion(event): global mouse_x, mouse_y mouse_x = event.x mouse_y = event.y