Streamline the archive loading mechanism
[tennix.git] / doc / tennix-test.py
blob786926b1bc436973f344f7bc4d4cfac181d3cb93
1 #!/usr/bin/python
3 import pygame
4 from pygame.constants import *
5 from math import cos
6 from math import pi
8 WIDTH = 640
9 HEIGHT = 480
10 H_FACT = 0.10
12 pygame.init()
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)
20 a = 0.2
21 b = 0.6
22 x = threed.x * (a * threed.y + b)
23 y = (threed.y - threed.z*h_factor) * (a * threed.y + b)
25 return Coord2D( x, y)
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):
32 self.x = x
33 self.y = y
34 self.z = z
36 def bottom( self):
37 return Coord3D( self.x, self.y, 0.0)
39 def twod( self):
40 return convert_3d_2d( self, H_FACT)
42 class Coord2D(object):
43 def __init__( self, x, y):
44 self.x = x
45 self.y = y
48 tennis_area = [
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
62 return Coord2D( x, y)
65 hist = []
67 MOVE_Y_DFL = 0.015
68 MOVE_X_DFL = 0.0025
70 move_y = MOVE_Y_DFL
71 move_x = MOVE_X_DFL
73 zrestitution = 0.7
74 gravity = 0.009
76 yrestitution = 0.7
78 vel_z = 0
80 draw_lines = True
82 x = 0
83 y = 0
84 z = 10
85 i = 1
86 while i > 0:
87 clock.tick( 70)
88 evt = pygame.event.poll()
89 i += 1
91 if evt.type == KEYUP and evt.key == K_ESCAPE:
92 break
94 if evt.type == MOUSEMOTION:
95 x = (evt.pos[0]*2.0/WIDTH)-1.0
96 y = (evt.pos[1]*2.0/HEIGHT)-1.0
97 elif evt.type == MOUSEBUTTONDOWN:
98 if evt.button == 1:
99 vel_z = -.2
100 mmy = MOVE_Y_DFL*1.8
101 else:
102 vel_z = -.4
103 mmy = MOVE_Y_DFL*1.2
104 if y > 0:
105 move_y = -mmy
106 else:
107 move_y = +mmy
108 else:
109 x += move_x
110 y += move_y
111 if x > 1.0 or x < -1.0:
112 move_x *= -1
113 if y > 1.0 or y < -1.0:
114 move_y *= -yrestitution
115 if z < 0:
116 vel_z *= -zrestitution
117 z = 0
118 vel_z += gravity
119 z -= vel_z
121 surface.fill( (0,0,0))
122 for a in tennis_area:
123 twod_a = twod_to_real( a[0].twod())
124 twod_b = twod_to_real( a[1].twod())
125 pygame.draw.aaline( surface, (255,255,255), (twod_a.x,twod_a.y), (twod_b.x,twod_b.y))
127 currd = Coord3D(x, y, z)
129 if i % 2 == 0:
130 hist.append( currd)
132 if len(hist) > 250:
133 hist = hist[-250:]
135 if draw_lines:
136 old = None
137 u = 0
138 for e in hist:
139 if old:
140 twod_a = twod_to_real( convert_3d_2d( old, H_FACT))
141 twod_b = twod_to_real( convert_3d_2d( e, H_FACT))
142 pygame.draw.aaline( surface, (0,u,0), (twod_a.x,twod_a.y), (twod_b.x,twod_b.y))
143 twod_a = twod_to_real( convert_3d_2d( old.bottom(), H_FACT))
144 twod_b = twod_to_real( convert_3d_2d( e.bottom(), H_FACT))
145 pygame.draw.aaline( surface, (max(0, min(255, 255-old.z/3*255)),0,u), (twod_a.x,twod_a.y), (twod_b.x,twod_b.y))
146 old = e
147 u += int(255/len(hist))
149 ballpos = twod_to_real( convert_3d_2d( currd, H_FACT))
150 twod_a = twod_to_real( convert_3d_2d( Coord3D( 0.0, 0.0), H_FACT))
151 twod_b = twod_to_real( convert_3d_2d( currd, H_FACT))
152 pygame.draw.aaline( surface, (255,0,0), (twod_a.x,twod_a.y), (twod_b.x,twod_b.y))
153 twod_a = twod_to_real( convert_3d_2d( Coord3D( 0.0, 0.0), H_FACT))
154 twod_b = twod_to_real( convert_3d_2d( Coord3D( x, y, 0), H_FACT))
155 pygame.draw.aaline( surface, (255,0,0), (twod_a.x,twod_a.y), (twod_b.x,twod_b.y))
156 twod_a = twod_to_real( convert_3d_2d( currd, H_FACT))
157 twod_b = twod_to_real( convert_3d_2d( Coord3D( x, y, 0), H_FACT))
158 pygame.draw.aaline( surface, (255,0,0), (twod_a.x,twod_a.y), (twod_b.x,twod_b.y))
160 pygame.draw.circle( surface, (50,50,50), (twod_b.x,twod_b.y), int(resize_depth( 8, currd.y)/max(1,currd.z)))
161 pygame.draw.circle( surface, (255,230,0), (ballpos.x,ballpos.y), int(resize_depth( 9, currd.y)))
163 pygame.display.flip()