FPS Limiting for the main menu display code
[tennix.git] / doc / tennix-test.py
blobbd3d890c7c4eb159aeed7c3f90834039bfde1c8a
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 = 0.015
68 move_x = 0.0025
70 x = 0
71 y = 0
72 i = 1
73 while i > 0:
74 clock.tick( 70)
75 evt = pygame.event.poll()
76 i += 1
78 if evt.type == KEYUP and evt.key == K_ESCAPE:
79 break
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
84 else:
85 x += move_x
86 y += move_y
87 if x > 1.0 or x < -1.0:
88 move_x *= -1
89 if y > 1.0 or y < -1.0:
90 move_y *= -1
92 surface.fill( (0,0,0))
93 for a in tennis_area:
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 ))
100 if i % 2 == 0:
101 hist.append( currd)
103 if len(hist) > 250:
104 hist = hist[-250:]
106 old = None
107 u = 0
108 for e in hist:
109 if old:
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))
116 old = e
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()