-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab1_task1_House_rain.py
226 lines (172 loc) · 4.87 KB
/
lab1_task1_House_rain.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
from OpenGL.GL import *
from OpenGL.GLUT import *
import random
#vars
background_color = (0.0, 0.0, 0.0)
door_color = (0.855, 0.647, 0.125)
house_color = (0.5, 0.5, 0.5) #gray
roof_color = (0.7, 0.1, 0.1) #red
rain_color = (0.0, 0.0, 1.0) #blue
rain_x1 = [0.0] * 30 # X-coordinates of raindrop start points
rain_y1 = [0.0] * 30 # Y-coordinates of raindrop start points
rain_x2 = [0.0] * 30 # X2-coordinates of raindrop end points
rain_y2 = [0.0] * 30 # Y3-coordinates of raindrop end points
num_raindrops = 30 # 30 rain drops generate in an iteration
def drawBackground():
glColor3fv(background_color)
glBegin(GL_QUADS)
glVertex2f(-1.0, -1.0)
glVertex2f(1.0, -1.0)
glVertex2f(1.0, 1.0)
glVertex2f(-1.0, 1.0)
glEnd()
def drawHouse():
# House
glColor3fv(house_color)
glBegin(GL_TRIANGLES)
glVertex2f(-0.35, 0.2)
glVertex2f(-0.35, -0.2)
glVertex2f(0.0, 0.2)
glEnd()
glBegin(GL_TRIANGLES)
glVertex2f(0.35, 0.2)
glVertex2f(0.35, -0.2)
glVertex2f(0.0, 0.2)
glEnd()
glBegin(GL_TRIANGLES)
glVertex2f(-0.35, -0.2)
glVertex2f(0.35, -0.2)
glVertex2f(0.0, 0.2)
glEnd()
#door
glColor3fv(door_color)
glBegin(GL_TRIANGLES)
glVertex2f(-0.05, -0.2)
glVertex2f(0.05, -0.2)
glVertex2f(-0.05, 0.1)
glEnd()
glBegin(GL_TRIANGLES)
glVertex2f(-0.05, -0.2)
glVertex2f(0.05, -0.2)
glVertex2f(0.05, 0.1)
glEnd()
glBegin(GL_TRIANGLES)
glVertex2f(-0.05, 0.1)
glVertex2f(0.05, 0.1)
glVertex2f(0.0, -0.2)
glEnd()
#draw knob
glPointSize(6) #pixel size. by default 1 thake
glColor3f(0.0, 0.0, 0.0)
glBegin(GL_POINTS)
glVertex2f(0.02,-0.02) #jekhane show korbe pixel
glEnd()
#window
glColor3fv(door_color)
glBegin(GL_TRIANGLES)
glVertex2f(0.1, 0.1)
glVertex2f(0.2, 0.1)
glVertex2f(0.15, -0.05)
glEnd()
glBegin(GL_TRIANGLES)
glVertex2f(0.1, 0.1)
glVertex2f(0.1, -0.05)
glVertex2f(0.15, -0.05)
glEnd()
glBegin(GL_TRIANGLES)
glVertex2f(0.2, -0.05)
glVertex2f(0.2, 0.1)
glVertex2f(0.15, -0.05)
glEnd()
#window lines
glColor3f(0.0, 0.0, 0.0)
glLineWidth(2.0)
glBegin(GL_LINES)
glVertex2f(0.15, 0.1)
glVertex2f(0.15, -0.05)
glEnd()
glBegin(GL_LINES)
glVertex2f(0.1, 0.025)
glVertex2f(0.2, 0.025)
glEnd()
# Roof
glColor3fv(roof_color)
glBegin(GL_TRIANGLES)
glVertex2f(-0.35, 0.2)
glVertex2f(0.35, 0.2)
glVertex2f(0.0, 0.5)
glEnd()
def drawRain():
glColor3fv(rain_color)
glLineWidth(1)
glBegin(GL_LINES)
# 3 layers of rain
for j in range(3):
#total 30 points draw each layer
for i in range(num_raindrops):
glVertex2f(rain_x1[i], rain_y1[i])
glVertex2f(rain_x2[i], rain_y2[i])
#updating rain points for the next iteration
for k in range(30):
x=random.uniform(0,0.5)
rain_y1[k]=rain_y1[k]-x
rain_y2[k]=rain_y2[k]-x
glEnd()
def display():
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
drawBackground()
# Draw the house
drawHouse()
# Draw the rain
drawRain()
glutSwapBuffers()
def initializeRain():
for i in range(num_raindrops):
rain_x1[i] =random.uniform(-1, 1) #randomly take a value in the range
rain_y1[i] = 1.0 #up
rain_x2[i] = rain_x1[i] #same as x1
rain_y2[i] = rain_y1[i] - 0.1 #down
def changeRainDirection(direction):
if direction == -1:
for i in range(30):
rain_x2[i] = rain_x2[i] - 0.05
elif direction == 1:
for j in range(30):
rain_x2[j] = rain_x2[j] + 0.05
def specialKeyListener(key, x, y):
global rain_direction
if key == GLUT_KEY_LEFT:
rain_direction = -1
elif key == GLUT_KEY_RIGHT:
rain_direction = 1
initializeRain()
changeRainDirection(rain_direction)
glutPostRedisplay()
def keyboardListener(key, x, y):
global background_color
if key==b" ":
print("GOT SPACEBAR")
if key==b'n':
background_color = (background_color[0] - 0.2, background_color[1] - 0.2, background_color[2] - 0.2)
print("day")
if key==b'd':
background_color = (background_color[0] + 0.2, background_color[1] + 0.2, background_color[2] + 0.2)
print("night")
initializeRain()
glutPostRedisplay()
def mouse_click(button, state, x, y):
initializeRain()
glutPostRedisplay()
#init
glutInit()
glutInitWindowSize(800, 800)
glutCreateWindow(b"House and Rain")
#call func
glutDisplayFunc(display)
glutSpecialFunc(specialKeyListener)
glutKeyboardFunc(keyboardListener)
# Initialize raindrop positions
initializeRain()
glutMouseFunc(mouse_click)
glutMainLoop()