from naja.sound import sound
from naja.options import parse_args
from naja.scenes.menu import MenuScene
-from naja.gamestate import GameState
def main():
sound.init()
screen = pygame.display.get_surface()
- state = GameState.new()
- scene = MenuScene(state)
- engine = Engine(screen, scene, state)
+ scene = MenuScene(None)
+ engine = Engine(screen, scene, None)
engine.run()
import pygame.locals as pgl
-from naja.constants import KEYS
+from naja.constants import KEYS, PALETTE
from naja.events import SceneChangeEvent, QuitGameEvent
from naja.scenes.scene import Scene
from naja.scenes.credits import CreditsScene
super(MenuScene, self).__init__(state)
selector = SelectorWidget()
self.add(selector)
+ y_offset = 0
- run_game = TextWidget((100, 50), 'Game', fontsize=32, colour='white')
- run_game.add_callback(
- 'click', lambda event: SceneChangeEvent.post(GameScene))
- selector.add(run_game)
+ y_offset += 50
+ resume = TextWidget(
+ (100, y_offset), 'Resume Game', colour=PALETTE.WHITE,
+ unselectable_colour=PALETTE.GREY)
+ resume.add_callback('click', self.scene_callback(GameScene))
+ resume.set_selectable_callback(lambda: state is not None)
+ selector.add(resume)
- new_game = TextWidget((100, 100), 'New', fontsize=32, colour='white')
- new_game.add_callback(
- 'click', lambda event: SceneChangeEvent.post(NewGameScene))
- selector.add(new_game)
+ y_offset += 50
+ new = TextWidget((100, y_offset), 'New Game', colour=PALETTE.WHITE)
+ new.add_callback('click', self.scene_callback(NewGameScene))
+ selector.add(new)
- credits = TextWidget(
- (100, 150), 'Credits', fontsize=32, colour='white')
- credits.add_callback(
- 'click', lambda event: SceneChangeEvent.post(CreditsScene))
- selector.add(credits)
-
- load = TextWidget((100, 200), 'Load', fontsize=32, colour='white')
- load.add_callback(
- 'click', lambda event: SceneChangeEvent.post(LoadGameScene))
+ y_offset += 50
+ load = TextWidget((100, y_offset), 'Load', colour=PALETTE.WHITE)
+ load.add_callback('click', self.scene_callback(LoadGameScene))
selector.add(load)
- save = TextWidget((100, 250), 'Save', fontsize=32, colour='white')
- save.add_callback(
- 'click', lambda event: SceneChangeEvent.post(SaveGameScene))
+ y_offset += 50
+ save = TextWidget(
+ (100, y_offset), 'Save', colour=PALETTE.WHITE,
+ unselectable_colour=PALETTE.GREY)
+ save.add_callback('click', self.scene_callback(SaveGameScene))
+ save.set_selectable_callback(lambda: state is not None)
selector.add(save)
- quit = TextWidget((100, 300), 'Quit', fontsize=32, colour='white')
+ y_offset += 50
+ credits = TextWidget((100, y_offset), 'Credits', colour=PALETTE.WHITE)
+ credits.add_callback('click', self.scene_callback(CreditsScene))
+ selector.add(credits)
+
+ y_offset += 50
+ quit = TextWidget((100, y_offset), 'Quit', colour=PALETTE.WHITE)
quit.add_callback('click', lambda event: QuitGameEvent.post())
selector.add(quit)
+ def scene_callback(self, scene_class):
+ return lambda event: SceneChangeEvent.post(scene_class)
+
def handle_scene_event(self, ev):
if ev.type == pgl.KEYDOWN:
if ev.key in KEYS.QUIT:
class Widget(object):
+ _is_selectable_func = None
+
def __init__(self, pos, size=None):
self.pos = pos
self.size = size or (0, 0)
callback(event)
return bool(self.callbacks[event])
+ def set_selectable_callback(self, func):
+ self._is_selectable_func = func
+
+ def is_selectable(self):
+ """Return False if this widget isn't selectable by SelectorWidget."""
+ if self._is_selectable_func is not None:
+ return self._is_selectable_func()
+ else:
+ return True
+
class Container(object):
def __init__(self, *widgets):
super(SelectorWidget, self).render(surface)
pos = self.selector.get_rect()
selected = self.widgets[self.position]
+ if not selected.is_selectable():
+ selected = self.change_pos(1)
+
pos = pos.move(selected.pos)
pos = pos.move(-pos.width * 1.5, (selected.size[1] - pos.height) / 2)
surface.blit(self.selector, pos)
+ def change_pos(self, change):
+ self.position = (self.position + change) % len(self.widgets)
+ while not self.widgets[self.position].is_selectable():
+ self.position = (self.position + change) % len(self.widgets)
+ return self.widgets[self.position]
+
def handle_event(self, ev):
if ev.type == pgl.KEYDOWN:
if ev.key in KEYS.UP + KEYS.DOWN:
if ev.key in KEYS.DOWN:
- self.position += 1
+ self.change_pos(1)
else:
- self.position -= 1
- self.position %= len(self.widgets)
+ self.change_pos(-1)
sound.play_sound('zoop.ogg', volume=0.05)
return True
elif ev.key in KEYS.SELECT:
class TextWidget(Widget):
def __init__(self, pos, text, size=None, fontname=None, fontsize=None,
- colour=None):
+ colour=None, unselectable_colour=None):
super(TextWidget, self).__init__(pos, size)
self.text = text
self.fontname = fontname or FONT
self.fontsize = (fontsize or FONT_SIZE) // EIGHT_BIT_SCALE
self.colour = convert_colour(colour or PALETTE.BLACK)
+ if unselectable_colour is not None:
+ unselectable_colour = convert_colour(unselectable_colour)
+ self.unselectable_colour = unselectable_colour
def render_line(self, text):
- text_surf = self.font.render(text, True, self.colour)
+ colour = self.colour
+ if not self.is_selectable() and self.unselectable_colour is not None:
+ colour = self.unselectable_colour
+ text_surf = self.font.render(text, True, colour)
text_rect = text_surf.get_rect()
return pygame.transform.scale(
text_surf, (text_rect.width * EIGHT_BIT_SCALE,