-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab3_Water_drops.py
123 lines (96 loc) · 2.65 KB
/
lab3_Water_drops.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
# [x, y, radius]
circles = []
radius = 10
game_paused=False
speed=0.3
# convert
def plotPoints(x, y, xc, yc):
glVertex2f(x + xc, y + yc)
glVertex2f(y + xc, x + yc)
glVertex2f(-y + xc, x + yc)
glVertex2f(-x + xc, y + yc)
glVertex2f(-x + xc, -y + yc)
glVertex2f(-y + xc, -x + yc)
glVertex2f(y + xc, -x + yc)
glVertex2f(x + xc, -y + yc)
def MidpointCircle(radius, xc, yc):
x = 0
y = radius
d = 1 - radius
# Initial points
glBegin(GL_POINTS)
while x < y:
if d < 0:
d = d + 2 * x + 3
x += 1
else:
d = d + 2 * (x - y) + 5
x += 1
y -= 1
plotPoints(x, y, xc, yc)
glEnd()
# Timer update radius and remove circles
def updateCircles(value):
global circles, radius, game_paused, speed
if not game_paused:
# Update radius
for circle in circles:
circle[2] += speed
#remove
new_circles = [] # approved ones store here
for circle in circles:
x, y, r = circle # Extract
# Check
if 0 <= x - r < 800 and 0 <= x + r < 800 and 0 <= y - r < 600 and 0 <= y + r < 600:
new_circles.append(circle)
circles = new_circles
glutPostRedisplay()
glutTimerFunc(16, updateCircles, 0)
# Display function
def display():
global circles
glClear(GL_COLOR_BUFFER_BIT)
glColor3f(0.5, 0.7, 1.0)
# Draw
for circle in circles:
MidpointCircle(circle[2], circle[0], circle[1])
glutSwapBuffers()
def mouseClick(button, state, x, y):
global circles, radius,game_paused
if button == GLUT_LEFT_BUTTON and state == GLUT_DOWN:
#[x, y, radius]
#print(x,y)
if not game_paused:
circles.append([x, 600 - y, radius])
glutPostRedisplay()
def keyboardListener(key, x, y):
global game_paused
if key==b" ":
game_paused = not game_paused
def specialKey(key, x, y):
global game_paused, speed
if not game_paused:
if key == GLUT_KEY_UP:
speed +=0.3
elif key == GLUT_KEY_DOWN:
if speed<=0.3:
pass
else:
speed -=0.3
glutPostRedisplay()
# Initialization
glutInit()
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB)
glutInitWindowSize(800, 600)
glutCreateWindow(b"Multiple Circles")
glClearColor(0.0, 0.0, 0.0, 0.0)
gluOrtho2D(0, 800, 0, 600)
glutDisplayFunc(display)
glutMouseFunc(mouseClick)
glutSpecialFunc(specialKey)
glutKeyboardFunc(keyboardListener)
glutTimerFunc(0, updateCircles, 0)
glutMainLoop()