4 from pygame
.constants
import *
13 pygame
.display
.set_mode( (WIDTH
,HEIGHT
))
14 surface
= pygame
.display
.get_surface()
15 clock
= pygame
.time
.Clock()
17 def convert_3d_2d( threed
, h_factor
):
18 #x = threed.x * (0.1 * threed.y + 0.9)
19 #y = (threed.y - threed.z*h_factor) * (0.15 * threed.y + 0.85)
22 x
= threed
.x
* (a
* threed
.y
+ b
)
23 y
= (threed
.y
- threed
.z
*h_factor
) * (a
* threed
.y
+ b
)
27 def resize_depth( size
, depth
):
28 return size
*(0.75+depth
/4)
30 class Coord3D(object):
31 def __init__( self
, x
, y
, z
= 0.0):
37 return Coord3D( self
.x
, self
.y
, 0.0)
40 return convert_3d_2d( self
, H_FACT
)
42 class Coord2D(object):
43 def __init__( self
, x
, y
):
49 ( Coord3D( -1, -1), Coord3D( -1, 1 ) ),
50 ( Coord3D( -1, -1), Coord3D( 1, -1) ),
51 ( Coord3D( -1, 1), Coord3D( 1, 1) ),
52 ( Coord3D( 1, -1), Coord3D( 1, 1) ),
53 ( Coord3D( -1, 0, 1), Coord3D( -1, 0, 0) ),
54 ( Coord3D( -1, 0, 1), Coord3D( 1, 0, 1) ),
55 ( Coord3D( 1, 0, 1), Coord3D( 1, 0, 0) ),
56 ( Coord3D( -1, 0, 0), Coord3D( 1, 0, 0) ),
59 def twod_to_real( twod
):
60 x
= twod
.x
* WIDTH
/2.1 + WIDTH
/2
61 y
= twod
.y
* HEIGHT
/2.1 + HEIGHT
/2
75 evt
= pygame
.event
.poll()
78 if evt
.type == KEYUP
and evt
.key
== K_ESCAPE
:
81 if evt
.type == MOUSEMOTION
:
82 x
= (evt
.pos
[0]*2.0/WIDTH
)-1.0
83 y
= (evt
.pos
[1]*2.0/HEIGHT
)-1.0
87 if x
> 1.0 or x
< -1.0:
89 if y
> 1.0 or y
< -1.0:
92 surface
.fill( (0,0,0))
94 twod_a
= twod_to_real( a
[0].twod())
95 twod_b
= twod_to_real( a
[1].twod())
96 pygame
.draw
.aaline( surface
, (255,255,255), (twod_a
.x
,twod_a
.y
), (twod_b
.x
,twod_b
.y
))
98 currd
= Coord3D( x
, y
, 1.2+1.2*cos( y
*2*pi
))
110 twod_a
= twod_to_real( convert_3d_2d( old
, H_FACT
))
111 twod_b
= twod_to_real( convert_3d_2d( e
, H_FACT
))
112 pygame
.draw
.aaline( surface
, (0,u
,0), (twod_a
.x
,twod_a
.y
), (twod_b
.x
,twod_b
.y
))
113 twod_a
= twod_to_real( convert_3d_2d( old
.bottom(), H_FACT
))
114 twod_b
= twod_to_real( convert_3d_2d( e
.bottom(), H_FACT
))
115 pygame
.draw
.aaline( surface
, (0,0,u
), (twod_a
.x
,twod_a
.y
), (twod_b
.x
,twod_b
.y
))
117 u
+= int(255/len(hist
))
119 ballpos
= twod_to_real( convert_3d_2d( currd
, H_FACT
))
120 twod_a
= twod_to_real( convert_3d_2d( Coord3D( 0.0, 0.0), H_FACT
))
121 twod_b
= twod_to_real( convert_3d_2d( currd
, H_FACT
))
122 pygame
.draw
.aaline( surface
, (255,0,0), (twod_a
.x
,twod_a
.y
), (twod_b
.x
,twod_b
.y
))
123 twod_a
= twod_to_real( convert_3d_2d( Coord3D( 0.0, 0.0), H_FACT
))
124 twod_b
= twod_to_real( convert_3d_2d( Coord3D( x
, y
, 0), H_FACT
))
125 pygame
.draw
.aaline( surface
, (255,0,0), (twod_a
.x
,twod_a
.y
), (twod_b
.x
,twod_b
.y
))
126 twod_a
= twod_to_real( convert_3d_2d( currd
, H_FACT
))
127 twod_b
= twod_to_real( convert_3d_2d( Coord3D( x
, y
, 0), H_FACT
))
128 pygame
.draw
.aaline( surface
, (255,0,0), (twod_a
.x
,twod_a
.y
), (twod_b
.x
,twod_b
.y
))
130 pygame
.draw
.circle( surface
, (50,50,50), (twod_b
.x
,twod_b
.y
), int(resize_depth( 8, currd
.y
)/max(1,currd
.z
)))
131 pygame
.draw
.circle( surface
, (255,230,0), (ballpos
.x
,ballpos
.y
), int(resize_depth( 9, currd
.y
)))
133 pygame
.display
.flip()