Add circle creation and highlight

This commit is contained in:
2019-03-18 22:48:24 +01:00
parent 952832745d
commit 8d047086e1

View File

@@ -1,5 +1,6 @@
import tkinter as tk
import color
from random import randint
class Circle:
def __init__(self, canvas: tk.Canvas, x, y, radius):
@@ -7,7 +8,7 @@ class Circle:
self.x = x
self.y = y
self.radius = radius
self.value = 9
self.value = 3
self.circle_id = canvas.create_oval(x - radius, y - radius, x + radius, y + radius, outline = color.PURPLE, width = 2)
self.text_id = canvas.create_text(x, y, text = self.value, fill = color.PURPLE, font = (None, int(radius / 1.5)))
@@ -20,6 +21,12 @@ class Circle:
self.value -= 1
self.canvas.itemconfigure(self.text_id, text = self.value)
def highlight(self, state: bool):
c = color.BROWN if state else color.PURPLE
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
@@ -27,11 +34,17 @@ class Circle:
return d <= self.radius
# var
canvas = None
mouse_x = 0
mouse_y = 0
tick = 0
STEP_TICK = 60
# Circle
circle = None
CIRCLE_RADIUS = 50
SPACE = 5
# Main
def main(c: tk.Canvas):
@@ -41,19 +54,33 @@ def main(c: tk.Canvas):
canvas = c
c.bind('<Motion>', motion)
# Test
circle = Circle(c, 50, 50, 100)
circle.decrement()
circle = create_circle()
# Loop
def loop(deltatime: float, difficulty: float):
global tick
print(circle.isInside(mouse_x, mouse_y))
print(difficulty)
# Highlight
if circle.isInside(mouse_x, mouse_y):
circle.highlight(True)
else:
circle.highlight(False)
tick += 1
# 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