-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
120 lines (97 loc) · 2.72 KB
/
main.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
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
import numpy
# settings
screen_height = 800
screen_width = 800
line_colour = (0, 0, 205)
class Pyramid:
vertices = [
[1, -1, -1],
[1, -1, 1],
[-1, -1, 1],
[-1, -1, -1],
[0, 1, 0]
]
edges = (
(0, 1),
(0, 3),
(0, 4),
(1, 4),
(1, 2),
(2, 4),
(2, 3),
(3, 4)
)
surfaces = (
(1, 2, 4),
(0, 1, 2, 3),
(0, 1, 4),
(2, 3, 4)
)
def __init__(self, scale=1):
self.edges = Pyramid.edges
self.vertices = list(numpy.multiply(numpy.array(Pyramid.vertices), scale))
self.surfaces = Pyramid.surfaces
def draw(self):
self.fill_sides()
glLineWidth(5)
glBegin(GL_LINES)
for edge in self.edges:
for vertex in edge:
glColor3f(0, 0, 1)
glVertex3fv(self.vertices[vertex])
glEnd()
def move(self, x, y, z):
self.vertices = list(map(lambda vertex: (vertex[0] + x, vertex[1] + y, vertex[2] + z), self.vertices))
def fill_sides(self):
glBegin(GL_QUADS)
for surface in self.surfaces:
for vertex in surface:
glColor3f(1, 0.482, 0)
glVertex3fv(self.vertices[vertex])
glEnd()
def main():
pygame.init()
display = (screen_height, screen_width)
# set pygame up for 3d graphics
pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
gluPerspective(45, (display[0]/display[1]), 0.1, 50)
# moves back along z direction
glTranslatef(0, 0, -20)
# draw pyramid as a solid object
glEnable(GL_DEPTH_TEST)
p = Pyramid(2)
velocity = 0.1
clock = pygame.time.Clock()
while True:
clock.tick(60)
# continually rotate pyramid
glRotatef(velocity * 10, 0, 1, 0)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
p.move(-velocity, 0, 0)
if keys[pygame.K_RIGHT]:
p.move(velocity, 0, 0)
if keys[pygame.K_UP]:
p.move(0, velocity, 0)
if keys[pygame.K_DOWN]:
p.move(0, -velocity, 0)
if keys[pygame.K_w]:
p.move(0, 0, velocity)
if keys[pygame.K_s]:
p.move(0, 0, -velocity)
if keys[pygame.K_a]:
glRotatef(-velocity*10, 0, 1, 0)
if keys[pygame.K_d]:
glRotatef(velocity*10, 0, 1, 0)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
p.draw()
pygame.display.flip()
main()