-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwormsegments.py
executable file
·212 lines (175 loc) · 5 KB
/
wormsegments.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
#!/usr/bin/python2.5
# In this Python program, I test my "camera" by
# drawing a coordinate axis and several rotating
# cubes.
# As much as I wanted to use PyAllegro, I had to move
# to PyGame instead; alpy just needs too much work, is
# too unstable, and is too slow in making progress.
import sys
import pygame
# This module imports all the little modules I will
# need for this program.
import math3d
# First, we need to initialize the graphics system.
pygame.init()
# size = width, height = 320, 400
size = width, height = 625, 400
xoffset = 50
yoffset = 50
offset = xoffset/2, yoffset/2
screen = pygame.display.set_mode(size)
black = 0, 0, 0
white = 255, 255, 255
bgcolor = white
# We can now create a camera object. This will be
# what we use to draw things to the screen.
camera = math3d.camera(width-xoffset, height-yoffset)
# We will be rotating eight different cubes. The
# following vectors will be used to position each
# cube.
vec = math3d.vector
positions = [ vec(50, 50, 50), vec(-50, 50, 50),
vec(-50, -50, 50), vec(50, -50, 50),
vec(50, 50, -50), vec(-50, 50, -50),
vec(-50, -50, -50), vec(50, -50, -50) ]
cube = []
#for i in range(8):
#cube.append(math3d.wireframe('littlecube.dat',
#pos = positions[i], axis=positions[(i+1)%8]))
cube.append(math3d.wireframe('worm_middle_segment.dat', pos = vec(0, 0, 0), axis = vec(1, 0, 0)))
cube.append(math3d.wireframe('worm_head_tail.dat', pos = vec(0, 0, 100), axis = vec(1, 0, 0)))
cube.append(math3d.wireframe('worm_ball_type_I.dat', pos = vec(0, 0, 200), axis = vec(1, 0, 0)))
cube.append(math3d.wireframe('worm_elbow.dat', pos = vec(0, 0, -100), axis = vec(1, 0, 0)))
# For visual reference, we will also draw a
# coordinate axis.
coords = math3d.wireframe('coords.dat')
camera.film.fill(bgcolor)
# Rotate cubes around Fwd axis
i = 0
nextscreen = 0
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
nextscreen = 1
if nextscreen == 1:
break
camera.film.fill(bgcolor)
camera.draw_wf(coords)
camera.setrot(i, math3d.Fwd)
for v in cube:
camera.draw_wf(v)
# v.setangle(i)
i = (i+1)%256
camera.drawOnto(screen, offset)
pygame.display.flip()
## Rotate cubes around Left axis
nextscreen = 0
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
nextscreen = 1
if nextscreen == 1:
break
camera.film.fill(bgcolor)
camera.draw_wf(coords)
camera.setrot(i, math3d.Left)
for v in cube:
camera.draw_wf(v)
# v.setangle(i)
i = (i+1)%256
camera.drawOnto(screen, offset)
pygame.display.flip()
## Rotate boxes around Up axis
nextscreen = 0
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
nextscreen = 1
if nextscreen == 1:
break
camera.film.fill(bgcolor)
camera.draw_wf(coords)
camera.setrot(i, math3d.Up)
for v in cube:
camera.draw_wf(v)
# v.setangle(i)
i = (i+1)%256
camera.drawOnto(screen, offset)
pygame.display.flip()
## Rotate boxes around a diagonal axis
## (subject to change)
nextscreen = 0
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
nextscreen = 1
if nextscreen == 1:
break
camera.film.fill(bgcolor)
camera.draw_wf(coords)
camera.setrot(i, math3d.vector(5,5,2))
for v in cube:
camera.draw_wf(v)
# v.setangle(i)
i = (i+1)%256
camera.drawOnto(screen, offset)
pygame.display.flip()
## Just rotate the cubes
nextscreen = 0
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
nextscreen = 1
if nextscreen == 1:
break
camera.film.fill(bgcolor)
camera.draw_wf(coords)
for v in cube:
camera.draw_wf(v)
# v.setangle(i)
i = (i+1)%256
camera.drawOnto(screen, offset)
pygame.display.flip()
## Let the cubes "ride off" in the distance
i = 0
nextscreen = 0
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
nextscreen = 1
if nextscreen == 1:
break
camera.film.fill(bgcolor)
camera.draw_wf(coords)
camera.settrans(math3d.vector(0, 0, -i*i))
for v in cube:
camera.draw_wf(v)
# v.setangle(i)
i = i+1
camera.drawOnto(screen, offset)
pygame.display.flip()
# Finally, we freeze things into position.
camera.drawOnto(screen, offset)
pygame.display.flip()
# Here we wait until the user presses a key, or turns off the system.
nextscreen = 0
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
nextscreen = 1
if nextscreen == 1:
break