Mercurial > sypikslang
changeset 53:655a6912e0ae
Split gui stuff out of main.py
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Mon, 07 May 2012 22:10:26 +0200 |
parents | 1d3d20bdc8b9 |
children | 168cfac9a445 |
files | gamelib/gamegui.py gamelib/main.py gamelib/mainmenu.py |
diffstat | 3 files changed, 100 insertions(+), 55 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gamelib/gamegui.py Mon May 07 22:10:26 2012 +0200 @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- +# vim:fileencoding=utf-8 ai ts=4 sts=4 et sw=4 + +"""Gui for the actual game""" + +from gamelib.gui_base import Window +from gamelib.gui import BigButton +from gamelib.engine import PopWindow +from gamelib.constants import WIDTH + + +class ExitGameButton(BigButton): + + def __init__(self): + super(ExitGameButton, self).__init__(((WIDTH - 128), 10), 'Exit') + + def on_click(self): + PopWindow.post() + + +class GameWindow(Window): + """Main window for the game""" + + def __init__(self, screen): + super(GameWindow, self).__init__(screen) + exit = ExitGameButton() + self.add_child(exit)
--- a/gamelib/main.py Mon May 07 21:55:55 2012 +0200 +++ b/gamelib/main.py Mon May 07 22:10:26 2012 +0200 @@ -7,67 +7,15 @@ ''' import pygame -from gamelib.gui_base import Window -from gamelib.gui import BigButton -from gamelib.engine import Engine, AddWindow, PopWindow +from gamelib.engine import Engine +from gamelib.mainmenu import MainMenu -from gamelib.constants import WIDTH, HEIGHT, SCREEN +from gamelib.constants import SCREEN pygame.init() -class ExitGameButton(BigButton): - - def __init__(self): - super(ExitGameButton, self).__init__(((WIDTH - 128), 10), 'Exit') - - def on_click(self): - PopWindow.post() - - -class GameWindow(Window): - """Main window for the game""" - - def __init__(self, screen): - super(GameWindow, self).__init__(screen) - exit = ExitGameButton() - self.add_child(exit) - - -class StartButton(BigButton): - - def __init__(self, screen): - super(StartButton, self).__init__(((WIDTH - 128) / 2, HEIGHT / 2), - 'Start') - self.screen = screen - - def on_click(self): - game_window = GameWindow(self.screen) - AddWindow.post(game_window) - - -class QuitButton(BigButton): - - def __init__(self): - super(QuitButton, self).__init__(((WIDTH - 128) / 2, - HEIGHT / 2 + 100), 'Quit') - - def on_click(self): - pygame.event.post(pygame.event.Event(pygame.QUIT)) - - -class MainMenu(Window): - - def __init__(self, screen): - super(MainMenu, self).__init__(screen) - self.background_colour = (0, 0, 0) - button1 = StartButton(screen) - self.add_child(button1) - button2 = QuitButton() - self.add_child(button2) - - def main(): screen = pygame.display.set_mode(SCREEN) engine = Engine(screen)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gamelib/mainmenu.py Mon May 07 22:10:26 2012 +0200 @@ -0,0 +1,70 @@ +# -*- coding: utf-8 -*- +# vim:fileencoding=utf-8 ai ts=4 sts=4 et sw=4 + +"""The main menu""" + +import pygame + +from gamelib.gui_base import Window +from gamelib.gui import BigButton +from gamelib.engine import AddWindow +from gamelib.gamegui import GameWindow + +from gamelib.constants import WIDTH, HEIGHT + + +class NewGameButton(BigButton): + + def __init__(self, parent): + super(NewGameButton, self).__init__(((WIDTH - 128) / 2, HEIGHT / 2), + 'Start New Game') + self.parent = parent + + def on_click(self): + self.parent.start_new_game() + + +class ResumeGameButton(BigButton): + + def __init__(self, parent): + super(ResumeGameButton, self).__init__(((WIDTH - 128) / 2, + HEIGHT / 2 + 50), 'Resume Game') + self.parent = parent + + def on_click(self): + self.parent.resume_game() + + +class QuitButton(BigButton): + + def __init__(self): + super(QuitButton, self).__init__(((WIDTH - 128) / 2, + HEIGHT / 2 + 100), 'Quit') + + def on_click(self): + pygame.event.post(pygame.event.Event(pygame.QUIT)) + + +class MainMenu(Window): + + def __init__(self, screen): + super(MainMenu, self).__init__(screen) + self.game_window = None + self.resume = None + self.screen = screen + self.background_colour = (0, 0, 0) + button1 = NewGameButton(self) + self.add_child(button1) + button2 = QuitButton() + self.add_child(button2) + + def start_new_game(self): + self.game_window = GameWindow(self.screen) + if not self.resume: + # Add the resume button + self.resume = ResumeGameButton(self) + self.add_child(self.resume) + AddWindow.post(self.game_window) + + def resume_game(self): + AddWindow.post(self.game_window)