-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy path2d-drawTriangle.py
51 lines (43 loc) · 2.13 KB
/
2d-drawTriangle.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
import pyglet
import pyglet.gl
import math
from random import randint
import colors
from triangleClass import triangleClass
# initialize a list of triangles
triangles = []
# populate the list of triangles
triangles.append(triangleClass('triangle1', 'blue', 0, 0, 20))
triangles.append(triangleClass('triangle2', 'hotpink', 0, 0, 20))
triangles.append(triangleClass('triangle3', 'yellow', 0, 0, 20))
triangles.append(triangleClass('triangle4', 'red', 0, 0, 20))
triangles.append(triangleClass('triangle5', 'green', 0, 0, 20))
class graphicsWindow(pyglet.window.Window):
def __init__(self):
super(graphicsWindow, self).__init__() # constructor for graphicsWindow class
for i in range(0,len(triangles)):
triangles[i].setCentreCoordinates(self.width / 2, self.height / 2)
triangles[i].updateVertices()
triangles[i].setVelocity(i+1,i+2)
triangles[i].setThetaIncrement(5*i)
def update(self, dt):
# print("Updating the center of the triangle")
for i in range(0,len(triangles)):
triangles[i].updateCoordinates(self.width, self.height )
triangles[i].updateVertices()
triangles[i].updateTheta()
triangles[i].rotateVertices()
def on_draw(self):
pyglet.gl.glClear(pyglet.gl.GL_COLOR_BUFFER_BIT) # clear the graphics buffer
for i in range(0,len(triangles)):
# calculate the list of vertices required to draw the triangle
vertexList = triangles[i].getVertices()
# use pyGlet commands to draw lines between the vertices for the triangle
lineColor = triangles[i].getColor() # openGL color specification
pyglet.gl.glColor3f(colors.color[lineColor][0], colors.color[lineColor][1], colors.color[lineColor][2])
vertexList.draw(pyglet.gl.GL_LINE_LOOP) # draw
# this is the main game engine loop
if __name__ == '__main__':
window = graphicsWindow() # initialize a window class
pyglet.clock.schedule_interval(window.update, 1 / 20.0) # tell pyglet the on_draw() & update() timestep
pyglet.app.run() # run the infinite pyglet loop