-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawCircle.py
106 lines (64 loc) · 2.79 KB
/
drawCircle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
###################################################
# Implementation of the Midpoint Circle algorithm #
###################################################
#
# https://en.wikipedia.org/wiki/Midpoint_circle_algorithm
# this is a very optimised way to draw a circle on screen (when the pixel resolution is not infinite)
#
# Click anywhere on the windows to draw a circle.
from tkinter import *
import time
from time import sleep
root = Tk()
root.title = "Game"
root.resizable(0,0)
root.wm_attributes("-topmost", 1)
colours = ["#ffffff","#00ffff","#ff00ff","#ffff00","#0000ff","#ff0000","#00ff00"]
canvas = Canvas(root, width=640, height=480, bd=0, highlightthickness=0, bg="#000000")
canvas.pack()
class Circle:
def __init__(self, delay=0):
self.delay = delay
self.colour = 0
self.canvas = canvas
self.WIDTH, self.HEIGHT = 640, 480
self.CENTREX, self.CENTREY = int(self.WIDTH/2) , int(self.HEIGHT/2)
canvas.bind("<Button-1>", self.canvas_onclick)
self.img = PhotoImage(width=self.WIDTH, height=self.HEIGHT)
#self.draw()
self.img2 = canvas.create_image((self.WIDTH/2, self.HEIGHT/2), image=self.img, state="normal")
mainloop()
def canvas_onclick(self, event):
self.draw()
def drawPoint(self,color , X,Y):
if X > 0 and Y > 0:
self.img.put(color, (X, Y))
def draw(self):
radius = 100
self.colour = (self.colour + 1 ) % len(colours)
centerX = root.winfo_pointerx() - root.winfo_rootx()
centerY = root.winfo_pointery() - root.winfo_rooty()
y = 0
x = radius
radiusError = 0
while y <= x:
self.drawPoint(colours[self.colour], centerX + x,centerY + y)
self.drawPoint(colours[self.colour], centerX + y,centerY + x)
self.drawPoint(colours[self.colour], centerX - x,centerY + y)
self.drawPoint(colours[self.colour], centerX - y,centerY + x)
self.drawPoint(colours[self.colour], centerX + x,centerY - y)
self.drawPoint(colours[self.colour], centerX + y,centerY - x)
self.drawPoint(colours[self.colour], centerX - x,centerY - y)
self.drawPoint(colours[self.colour], centerX - y,centerY - x)
if radiusError <= 0:
y = y + 1
radiusError = radiusError + 2 * y + 1
if radiusError > 0:
x = x - 1
radiusError = radiusError - 2 * x - 1
sleep(self.delay)
canvas.itemconfigure(self.img2, image=self.img)
root.update_idletasks()
root.update()
# The delay help to visualise how circles are drawn
Crcl = Circle(0.001)