view gamelib/mainmenu.py @ 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
children 364ff3479ef2
line wrap: on
line source

# -*- 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)