Initial commit from version 1e-99
[beatscape.git] / mainmenu.py
1 import pygame
2 import thread
3 from fx import *
4 import event
5
6 class MainMenu:
7         fx = None
8         fxlock = None
9         period = 428
10         circuitlist = [[(590,0), (550,40), (200,40), (170,70), (170,310)],
11                        [(570,0), (540,30), (190,30), (160,60), (20,60), (20,220), (50,250), (50,420), (90,460), (610,460)],
12                        [(0,220), (40,260), (40,480)],
13                        [(180,0), (180,20), (150,50), (20,50), (20,0)]]
14         # (label, message)
15         menuitems = [("play","fileselect"), ("quit","quit")]
16         subtitle = "holy shit parallax!!!11!1"
17         version = 1e-99
18
19         def __init__(self):
20                 self.fxlock = thread.allocate_lock()
21                 self.fx = []
22                 self.menufx = []
23                 self.position = 0
24                 self.bar = PulseLine([(0,0),(130,0)],(200,200,255),self.period,5)
25                 thread.start_new_thread(self.fxloader,())
26
27         def fxloader(self):
28                 self.fxlock.acquire()
29                 self.fx.append(Blank())
30                 for l in self.circuitlist:
31                         o = PulseLine(l,(255,30,30),self.period)
32                         self.fx.append(o)
33                 self.fxlock.release()
34
35                 o = Image('gfx/title.png',(200,40))
36                 self.fxlock.acquire()
37                 self.fx.append(o)
38                 self.fxlock.release()
39
40                 n = 0
41                 for l in self.menuitems:
42                         o = Text(l[0],'font/Nano.ttf',18,(30,70 + 20*n), (255,30,30))
43                         self.fxlock.acquire()
44                         self.fx.append(o)
45                         self.menufx.append(o)
46                         self.fxlock.release()
47                         n += 1
48
49                 o = Text(self.subtitle,'font/Nano.ttf',15,(600,100),(255,30,30))
50                 o.align = ALIGN_RIGHT
51                 self.fxlock.acquire()
52                 self.fx.append(o)
53                 self.fxlock.release()
54
55                 o = Text("version " + str(self.version),'font/Nano.ttf',14,(610,440),(255,30,30))
56                 o.align = ALIGN_RIGHT
57                 self.fxlock.acquire()
58                 self.fx.append(o)
59                 self.fxlock.release()
60
61                 o = PlaneScroll('gfx/chips_layer1.jpg',(-0.12,0.06),2.0,0.75)
62                 self.fxlock.acquire()
63                 self.fx.insert(1,o)
64                 self.fxlock.release()
65
66                 o = PlaneScroll('gfx/chips_layer2.jpg',(-0.06,0.03))
67                 self.fxlock.acquire()
68                 self.fx.remove(self.fx[0])
69                 self.fx.insert(0,o)
70                 self.fxlock.release()
71                         
72         def draw(self,t):
73                 self.fxlock.acquire()
74                 for f in self.fx:
75                         f.draw(t)
76                 self.fxlock.release()
77                 self.bar.location = (30, 68 + 20*self.position)
78                 self.bar.draw(t)
79                 self.bar.location = (30, 68 + 20*self.position + 20)
80                 self.bar.draw(t)
81
82         def run(self):
83                 screen = pygame.display.get_surface()
84                 decided = 0
85                 pygame.mixer.music.load('snd/0x00.ogg')
86                 pygame.mixer.music.set_volume(0.75)
87                 pygame.mixer.music.play(-1)
88                 stab = pygame.mixer.Sound('snd/stab.ogg')
89                 stab.set_volume(0.25)
90                 start_t = pygame.time.get_ticks()
91
92                 while decided == 0:
93                         t = pygame.time.get_ticks() - start_t
94                         for e in pygame.event.get():
95                                 act = event.parseevent(e)
96                                 if act == event.CANCEL:
97                                         decided = 'quit'
98                                 elif act == event.DOWN:
99                                         self.position += 1
100                                 elif act == event.UP:
101                                         self.position -= 1
102                                 elif act == event.OK:
103                                         decided = self.menuitems[self.position][1]
104
105                         self.position = self.position % len(self.menuitems)
106                         self.draw(t)
107                         pygame.display.flip()
108
109                 pygame.mixer.music.fadeout(1500)
110                 stab.play()
111
112                 start_t2 = pygame.time.get_ticks()
113                 dt = 0
114                 blanksurf = pygame.surface.Surface((640,480)).convert()
115                 blanksurf.fill( (0,0,0) )
116                 while dt < 1500:
117                         dt = pygame.time.get_ticks() - start_t2
118                         if dt < 1000:
119                                 self.draw(pygame.time.get_ticks() - start_t)
120                                 blanksurf.set_alpha(int(dt / 1000.0 * 255.0))
121                                 screen.blit(blanksurf,(0,0))
122                         else:
123                                 screen.fill( (0,0,0) )
124                         self.menufx[self.position].draw(0)
125                         if dt >= 1000:
126                                 blanksurf.set_alpha(int((dt-1000) / 500.0 * 255.0))
127                                 screen.blit(blanksurf,(0,0))
128                         pygame.display.flip()
129
130                 return [decided]