Optional sanity check for actions.
[naja.git] / naja / scenes / game.py
1 """
2 Gameboard scene.
3 """
4
5 import pygame.locals as pgl
6 import random
7
8 from naja.constants import KEYS, TUNES
9 from naja.events import SceneChangeEvent, finish_event
10 from naja.options import options
11 from naja.scenes.scene import Scene
12 from naja.sound import sound
13 from naja.widgets.board import BoardWidget
14 from naja.widgets.player_bits import PlayerBitsWidget
15 from naja.widgets.game_bits import GameBitsWidget
16 from naja.widgets.info_area import InfoAreaWidget
17 from naja.widgets.robot import RobotWidget
18
19
20 class GameScene(Scene):
21     """
22     Gameboard scene.
23     """
24
25     def __init__(self, state):
26         super(GameScene, self).__init__(state)
27         self.add(PlayerBitsWidget((0, 0), state))
28         info = InfoAreaWidget((480, 0), state)
29         self.add(BoardWidget((0, 60), state, info))
30         self.add(GameBitsWidget((0, 540), state))
31         self.add(info)
32         self.add(RobotWidget(state))
33         sound.play_sound('startup.ogg')
34         background_track = random.choice(TUNES)
35         sound.play_music(background_track, 0.25)
36
37     def handle_scene_event(self, ev):
38         from naja.scenes.menu import MenuScene
39         if ev.type == pgl.KEYDOWN:
40             if ev.key in KEYS.QUIT:
41                 sound.stop()
42                 SceneChangeEvent.post(MenuScene)
43                 return
44             if options.cheat_enabled:
45                 if ev.key == pgl.K_0:
46                     self.state.player.bits.toggle_bit(0)
47                 elif ev.key == pgl.K_1:
48                     self.state.player.bits.toggle_bit(1)
49                 elif ev.key == pgl.K_2:
50                     self.state.player.bits.toggle_bit(2)
51                 elif ev.key == pgl.K_3:
52                     self.state.player.bits.toggle_bit(3)
53                 elif ev.key == pgl.K_4:
54                     self.state.player.bits.toggle_bit(4)
55                 elif ev.key == pgl.K_5:
56                     self.state.player.bits.toggle_bit(5)
57                 elif ev.key == pgl.K_6:
58                     self.state.player.bits.toggle_bit(6)
59                 elif ev.key == pgl.K_7:
60                     self.state.player.bits.toggle_bit(7)
61                 elif ev.key == pgl.K_EQUALS:
62                     self.state.gameboard.gain_health()
63                 return finish_event()