Merge branch 'master' of git://ctpug.org.za/naja
[naja.git] / naja / scenes / new_game.py
1 """
2 Load and save scenes.
3 """
4
5 import pygame.locals as pgl
6
7 from naja.constants import KEYS
8 from naja.events import SceneChangeEvent, LoadGameEvent
9 from naja.gamestate import GameState
10 from naja.scenes.scene import Scene
11 from naja.widgets.image_box import ImageBox
12 from naja.widgets.selector import SelectorWidget
13 from naja.widgets.text import TextWidget
14
15
16 class NewGameScene(Scene):
17     def __init__(self, state):
18         super(NewGameScene, self).__init__(state)
19
20         background = ImageBox(
21             (0, 0), "screens/splash.png")
22         self.add(background)
23
24         selector = SelectorWidget()
25         selector.position = 1
26         self.add(selector)
27
28         y_offset, y_diff = 270, 40
29         x_offset = 400
30
31         y_offset += y_diff
32         title = TextWidget(
33             (x_offset, y_offset), 'NEW GAME', colour='white',
34             centre=True)
35         self.add(title)
36
37         y_offset += y_diff
38         question = TextWidget(
39             (x_offset, y_offset), 'Select difficulty:', colour='white',
40             centre=True)
41         self.add(question)
42
43         y_offset += y_diff
44         easy = TextWidget(
45             (x_offset, y_offset), 'Easy', fontsize=32, colour='white',
46             centre=True)
47         easy.add_callback('click', self.easy_game)
48         selector.add(easy)
49
50         y_offset += y_diff
51         standard = TextWidget(
52             (x_offset, y_offset), 'Standard', fontsize=32, colour='white',
53             centre=True)
54         standard.add_callback('click', self.standard_game)
55         selector.add(standard)
56
57         y_offset += y_diff
58         hard = TextWidget(
59             (x_offset, y_offset), 'Hard', fontsize=32, colour='white',
60             centre=True)
61         hard.add_callback('click', self.hard_game)
62         selector.add(hard)
63
64     def easy_game(self, event):
65         self.start_game(GameState.new(max_health=5, wins_required=3))
66
67     def standard_game(self, event):
68         self.start_game(GameState.new(max_health=4, wins_required=4))
69
70     def hard_game(self, event):
71         self.start_game(GameState.new(max_health=3, wins_required=5))
72
73     def start_game(self, state):
74         from naja.scenes.game import GameScene
75         LoadGameEvent.post(state=state)
76         SceneChangeEvent.post(GameScene)
77
78     def handle_scene_event(self, ev):
79         if ev.type == pgl.KEYDOWN and ev.key in KEYS.QUIT:
80             from naja.scenes.menu import MenuScene
81             SceneChangeEvent.post(MenuScene)
82             return