-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
460 lines (353 loc) · 15.4 KB
/
main.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import time
import math
b_cwiid = True
wm_addr = '00:1E:35:7B:96:5D'
try:
import cwiid
except ImportError:
print "no cwiid =/"
b_cwiid = False
for arg in sys.argv:
try:
bool_teste = False
arg_splitted = arg.split('=')
if arg_splitted[0] == "-w" or arq_splitted[0] == "--wiimote-address":
wm_addr = arg_splitted[1]
except:
bool_teste = False
from pandac.PandaModules import *
loadPrcFile("./config.prc")
import direct.directbase.DirectStart
from direct.task import Task
from direct.actor import Actor
from direct.showbase import DirectObject
from direct.gui.OnscreenText import OnscreenText
from direct.fsm import FSM
from direct.interval.IntervalGlobal import *
#tirando o cursor do mouse
from pandac.PandaModules import WindowProperties
props = WindowProperties()
props.setCursorHidden(True)
base.win.requestProperties(props)
import control
from level import *
from screens import *
import options
class Game(FSM.FSM):
def __init__(self):
FSM.FSM.__init__(self, 'Game')
self.wm = None
#controla ou nao o menu com wiimote
#self.bool_wii = b_cwiid
self.bool_wii = False
self.defaultTransitions = {
'Title' : [ 'Options', 'Training', 'LevelSelect', 'Exit' ],
'Training': [ 'Load', 'Title', 'Options', 'Result'],
'Options' : [ 'Title' , 'Level'],
'LevelSelect' : [ 'Level', 'Title'],
'Load' : ['LevelSelect','Training', 'Level'],
'Level' : [ 'Result', 'Load', 'Options'],
'Result' : [ 'Level', 'Title', 'Training'],
}
self.options = options.MoonBunnyOptions()
self.start_level_sfx = loader.loadSfx('./sound/start_level.wav')
self.theme = loader.loadMusic('./sound/always.wav')
self.theme.setLoop(True)
logo_sound= loader.loadSfx('./sound/elefante.wav')
self.logo = OnscreenImage(image='./image/tromba_logo.png', scale=(512.0/base.win.getXSize(), 1 ,256.0/base.win.getYSize()), pos = (0.0, 2.0, 0.0), parent=render2d)
self.logo.setTransparency(TransparencyAttrib.MAlpha)
Sequence(
LerpFunc(self.logo.setAlphaScale, fromData=.0, toData=1, duration=.5),
SoundInterval(logo_sound, duration=4.0),
LerpFunc(self.logo.setAlphaScale, fromData=1, toData=.0, duration=.5),
Func(self.request,'Title')
).start()
def connect_wii(self, end):
try:
self.wm = cwiid.Wiimote(end)
except RuntimeError:
pass
## Title state
def enterTitle(self):
if self.theme.status() == 1:
self.theme.play()
self.title_screen = TitleScreen()
def exitTitle(self):
self.title_screen.clear()
def filterTitle(self, request, args):
if request == 'nav-up' or request == 'nav-down':
self.title_screen.option_changed(request.replace('nav-', ''))
if request == 'nav-confirm':
#~ if self.title_screen.option_pressed() == 'start':
#~ return ("Load", 's')
if self.title_screen.option_pressed() == 'start':
return ("LevelSelect")
if self.title_screen.option_pressed() == 'training':
return ("Load", 't')
if self.title_screen.option_pressed() == 'options':
return 'Options'
if self.title_screen.option_pressed() == 'exit':
return 'Exit'
if request == 'nav-back':
return 'Exit'
## LevelSelect state
def enterLevelSelect(self):
if self.theme.status() == 1:
self.theme.play()
self.level_select = LevelSelectScreen(self.options)
def exitLevelSelect(self):
self.level_select.clear()
def filterLevelSelect(self, request, args):
if request == 'nav-confirm':
level_name = self.level_select.option_pressed()
return ("Load", 's', level_name)
if request == 'nav-back':
return 'Title'
if request == 'nav-left'or request == 'nav-right':
self.level_select.option_changed(request.replace('nav-', ''))
## Options state
def enterOptions(self):
self.options_screen = OptionsScreen(self.options)
def exitOptions(self):
self.options_screen.clear()
self.options.save()
def filterOptions(self, request, args):
if request == 'nav-left' or request == 'nav-right':
self.options_screen.option_changed(request)
if request == 'nav-back' or request == 'nav-confirm':
return 'Title'
## Load state
def enterLoad(self, tipo, l_n=''):
if self.theme.status() == 1:
self.theme.play()
self.load_screen = LoadScreen(self.options)
self.tipo = tipo
self.level_name = l_n
def exitLoad(self):
pass
def filterLoad(self, request, args):
if request == 'nav-confirm':
if self.tipo == 's':
return ('Level', self.level_name, 'Normal', self.load_screen)
elif self.tipo == 't':
return ('Training', 'rain_of_love', 'Normal', self.load_screen)
if request == 'nav-back':
self.load_screen.clear()
return 'Title'
#conectando wiimote e tratando eventuais erros
def connect_wiimote(self, wm_addr):
try:
self.wm = cwiid.Wiimote(wm_addr)
except RuntimeError:
pass
self.wm.led = cwiid.LED1_ON
if uses_nunchuk(self.options):
self.wm.rpt_mode = cwiid.RPT_BTN | cwiid.RPT_ACC | cwiid.RPT_IR | cwiid.RPT_NUNCHUK
else:
if uses_wii_ir(self.options):
self.wm.rpt_mode = cwiid.RPT_BTN | cwiid.RPT_ACC | cwiid.RPT_IR
else:
self.wm.rpt_mode = cwiid.RPT_BTN | cwiid.RPT_ACC
## Training state
def enterTraining(self, level, difficulty, load_screen):
self.theme.stop()
self.ls = load_screen
#verifica se a cwiid esta instalada na maquina e se o controle escolhido eh envolve o Wiimote
if b_cwiid and uses_wii(self.options):
self.connect_wiimote(wm_addr)
self.level = Level(level, difficulty=difficulty, options=self.options, wm=self.wm, b_training=True)
#caso nao utilize o Wiimote
else:
self.level = Level(level, difficulty=difficulty, options=self.options, b_training = True)
Sequence(Func(self.ls.clear), SoundInterval(self.start_level_sfx), Func(self.level.setup), Func(self.level.play)).start()
def exitTraining(self):
self.level_name = self.level.name
self.level_score = self.level.score
self.rank_stats = (self.level.judgement_stats, self.level.n_rings)
del self.level
self.level = None
if uses_wii(self.options):
self.wm.led = 0
return 'Title'
## Level state
def enterLevel(self, level, difficulty, load_screen):
self.theme.stop()
self.ls = load_screen
#verifica se a cwiid esta instalada na maquina e se o controle escolhido eh envolve o Wiimote
if b_cwiid and uses_wii(self.options):
self.connect_wiimote(wm_addr)
self.level = Level(level, difficulty=difficulty, options=self.options, wm=self.wm)
#caso nao utilize o Wiimote
else:
self.level = Level(level, difficulty=difficulty, options=self.options)
Sequence(Func(self.ls.clear), SoundInterval(self.start_level_sfx), Func(self.level.setup), Func(self.level.play)).start()
def exitLevel(self):
self.level_name = self.level.name
self.level_score = self.level.score
self.rank_stats = (self.level.judgement_stats, self.level.n_rings)
del self.level
self.level = None
if uses_wii(self.options):
self.wm.led = 0
## Result
def enterResult(self):
rank = self.calculate_rank(*self.rank_stats)
self.save_score(self.level_name, rank, self.level_score)
self.result_screen = ResultScreen(rank, self.level_score, self.rank_stats[0])
def exitResult(self):
self.result_screen.clear()
def filterResult(self, request, args):
if request == 'nav-confirm':
return 'Title'
if request == 'nav-back':
return 'Title'
def enterExit(self):
sys.exit(0)
def save_score(self, levelname, rank, score):
if not self.options.has_section('hiscores'):
self.options.add_section('hiscores')
if self.options.has_option('hiscores', levelname):
old_rank, old_score = self.options.get('hiscores', levelname).split(',')
if rank != 's' and rank != 'ss' and old_rank != 'ss':
if ord(old_rank) < ord(rank):
rank = old_rank
if int(old_score) > score:
score = old_score
self.options.set('hiscores', levelname, "%s,%s" % (rank, str(score)))
self.options.save()
def calculate_rank(self, stats, n):
rates = {
"PERFECT": float(stats['PERFECT'] / float(n)),
"GOOD": float(stats['GOOD'] / float(n)),
"OK" : float(stats['OK'] / float(n)),
"BAD" : float(stats['BAD'] / float(n)),
"MISS": float(stats['MISS'] / float(n))
}
if(rates['PERFECT'] ==1):
rank = 'ss'
elif( rates['MISS'] <= 0 and rates['BAD'] <= 0.1 and rates['PERFECT'] >=0.5):
rank = 's'
elif( rates['MISS'] <= 0.05 and (rates['MISS'] + rates['BAD'] <= 0.2) and (rates['GOOD'] + rates['PERFECT'] >=0.4) ):
rank = 'a'
elif( (rates['MISS'] + rates['BAD'] <= 0.3) and (rates['GOOD'] + rates['PERFECT'] >=0.3) ):
rank = 'b'
elif( (rates['MISS'] + rates['BAD'] <= 0.4) and (rates['GOOD'] + rates['PERFECT'] >=0.2) ):
rank = 'c'
else:
rank = 'f'
return rank
class WiimoteHandler(DirectObject.DirectObject):
def __init__(self, game):
self.game = game
self.last_button = 0
taskMgr.add(self.ctask_CheckEvents, "cwiid-event")
def ctask_CheckEvents(self, task):
if not game.bool_nunchuk:
if self.game.wm.state['buttons'] == cwiid.BTN_UP:
if self.last_button != cwiid.BTN_UP:
messenger.send("nav-up")
self.last_button = cwiid.BTN_UP
elif self.game.wm.state['buttons'] == cwiid.BTN_DOWN:
if self.last_button != cwiid.BTN_DOWN:
messenger.send("nav-down")
self.last_button = cwiid.BTN_DOWN
elif self.game.wm.state['buttons'] == cwiid.BTN_LEFT:
if self.last_button != cwiid.BTN_LEFT:
messenger.send("nav-left")
self.last_button = cwiid.BTN_LEFT
elif self.game.wm.state['buttons'] == cwiid.BTN_RIGHT:
if self.last_button != cwiid.BTN_RIGHT:
messenger.send("nav-right")
self.last_button = cwiid.BTN_RIGHT
elif self.game.wm.state['buttons'] == cwiid.BTN_A:
if self.last_button != cwiid.BTN_A:
messenger.send("nav-confirm")
self.last_button = cwiid.BTN_A
elif self.game.wm.state['buttons'] == cwiid.BTN_B:
if self.last_button != cwiid.BTN_B:
messenger.send("nav-back")
self.last_button = cwiid.BTN_B
else:
self.last_button = 0
else:
#Left
if self.game.wm.state['nunchuk']['stick'][0] < 50:
if self.last_button != 1:
messenger.send("nav-left")
self.last_button = 1
#Down
elif self.game.wm.state['nunchuk']['stick'][1] < 50:
if self.last_button != 2:
messenger.send("nav-down")
self.last_button = 2
#Right
elif self.game.wm.state['nunchuk']['stick'][0] >200:
if self.last_button != 3:
messenger.send("nav-right")
self.last_button = 3
#Up
elif self.game.wm.state['nunchuk']['stick'][1] > 200:
if self.last_button != 4:
messenger.send("nav-up")
self.last_button = 4
elif self.game.wm.state['buttons'] == cwiid.BTN_A:
if self.last_button != cwiid.BTN_A:
messenger.send("nav-confirm")
self.last_button = cwiid.BTN_A
elif self.game.wm.state['buttons'] == cwiid.BTN_B:
if self.last_button != cwiid.BTN_B:
messenger.send("nav-back")
self.last_button = cwiid.BTN_B
else:
self.last_button = 0
return Task.cont
class Controller(DirectObject.DirectObject):
def __init__(self, game):
self.game = game
self.accept("nav-up", self.game.request, ['nav-up'])
self.accept("nav-down", self.game.request, ['nav-down'])
self.accept("nav-left", self.game.request, ['nav-left'])
self.accept("nav-right", self.game.request, ['nav-right'])
self.accept("nav-confirm", self.game.request, ['nav-confirm'])
self.accept("nav-back", self.game.request, ['nav-back'])
self.accept("level-finished", self.game.request, ['Result'])
self.accept("training-finished", self.game.request, ['Options'])
class JoystickManager:
def __init__(self):
pygame.init()
pygame.joystick.init()
taskMgr.add(self.ctask_CheckEvents, "pygame-event")
print "Joysticks available:", pygame.joystick.get_count()
self.joy_list = [pygame.joystick.Joystick(i) for i in range(pygame.joystick.get_count())]
if self.joy_list:
pass
def init_joy(self, id):
self.joy_list[id].init()
def set_current_joy(self, id):
pass
def ctask_CheckEvents(self, task):
pygame.event.pump()
if self.joy.get_button(2): messenger.send("joy-button", ["A"])
if self.joy.get_button(1): messenger.send("joy-button", ["B"])
if self.joy.get_button(3): messenger.send("joy-button", ["C"])
if self.joy.get_button(0): messenger.send("joy-button", ["D"])
return Task.cont
if __name__ == '__main__':
base.enableParticles()
base.disableMouse()
base.setBackgroundColor(.0, .0, .0, .0)
control.KeyNavMapper()
try:
pygame.init()
pygame.joystick.init()
j = pygame.joystick.Joystick(0)
j.init()
control.JoyNavMapper(j).activate()
except pygame.error, e:
print e
game = Game()
Controller(game)
run()