Add circle creation and highlight
This commit is contained in:
39
game/osu.py
39
game/osu.py
@@ -1,5 +1,6 @@
|
|||||||
import tkinter as tk
|
import tkinter as tk
|
||||||
import color
|
import color
|
||||||
|
from random import randint
|
||||||
|
|
||||||
class Circle:
|
class Circle:
|
||||||
def __init__(self, canvas: tk.Canvas, x, y, radius):
|
def __init__(self, canvas: tk.Canvas, x, y, radius):
|
||||||
@@ -7,7 +8,7 @@ class Circle:
|
|||||||
self.x = x
|
self.x = x
|
||||||
self.y = y
|
self.y = y
|
||||||
self.radius = radius
|
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.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)))
|
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.value -= 1
|
||||||
self.canvas.itemconfigure(self.text_id, text = self.value)
|
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):
|
def isInside(self, x, y):
|
||||||
dx = x - self.x
|
dx = x - self.x
|
||||||
dy = y - self.y
|
dy = y - self.y
|
||||||
@@ -27,11 +34,17 @@ class Circle:
|
|||||||
|
|
||||||
return d <= self.radius
|
return d <= self.radius
|
||||||
|
|
||||||
|
# var
|
||||||
canvas = None
|
canvas = None
|
||||||
mouse_x = 0
|
mouse_x = 0
|
||||||
mouse_y = 0
|
mouse_y = 0
|
||||||
tick = 0
|
tick = 0
|
||||||
|
STEP_TICK = 60
|
||||||
|
|
||||||
|
# Circle
|
||||||
circle = None
|
circle = None
|
||||||
|
CIRCLE_RADIUS = 50
|
||||||
|
SPACE = 5
|
||||||
|
|
||||||
# Main
|
# Main
|
||||||
def main(c: tk.Canvas):
|
def main(c: tk.Canvas):
|
||||||
@@ -41,19 +54,33 @@ def main(c: tk.Canvas):
|
|||||||
canvas = c
|
canvas = c
|
||||||
c.bind('<Motion>', motion)
|
c.bind('<Motion>', motion)
|
||||||
|
|
||||||
# Test
|
circle = create_circle()
|
||||||
circle = Circle(c, 50, 50, 100)
|
|
||||||
circle.decrement()
|
|
||||||
|
|
||||||
# Loop
|
# Loop
|
||||||
def loop(deltatime: float, difficulty: float):
|
def loop(deltatime: float, difficulty: float):
|
||||||
global tick
|
global tick
|
||||||
|
|
||||||
print(circle.isInside(mouse_x, mouse_y))
|
# Highlight
|
||||||
print(difficulty)
|
if circle.isInside(mouse_x, mouse_y):
|
||||||
|
circle.highlight(True)
|
||||||
|
else:
|
||||||
|
circle.highlight(False)
|
||||||
|
|
||||||
tick += 1
|
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
|
# Mouse
|
||||||
def motion(event):
|
def motion(event):
|
||||||
global mouse_x, mouse_y
|
global mouse_x, mouse_y
|
||||||
|
|||||||
Reference in New Issue
Block a user