comparison 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
comparison
equal deleted inserted replaced
52:1d3d20bdc8b9 53:655a6912e0ae
1 # -*- coding: utf-8 -*-
2 # vim:fileencoding=utf-8 ai ts=4 sts=4 et sw=4
3
4 """The main menu"""
5
6 import pygame
7
8 from gamelib.gui_base import Window
9 from gamelib.gui import BigButton
10 from gamelib.engine import AddWindow
11 from gamelib.gamegui import GameWindow
12
13 from gamelib.constants import WIDTH, HEIGHT
14
15
16 class NewGameButton(BigButton):
17
18 def __init__(self, parent):
19 super(NewGameButton, self).__init__(((WIDTH - 128) / 2, HEIGHT / 2),
20 'Start New Game')
21 self.parent = parent
22
23 def on_click(self):
24 self.parent.start_new_game()
25
26
27 class ResumeGameButton(BigButton):
28
29 def __init__(self, parent):
30 super(ResumeGameButton, self).__init__(((WIDTH - 128) / 2,
31 HEIGHT / 2 + 50), 'Resume Game')
32 self.parent = parent
33
34 def on_click(self):
35 self.parent.resume_game()
36
37
38 class QuitButton(BigButton):
39
40 def __init__(self):
41 super(QuitButton, self).__init__(((WIDTH - 128) / 2,
42 HEIGHT / 2 + 100), 'Quit')
43
44 def on_click(self):
45 pygame.event.post(pygame.event.Event(pygame.QUIT))
46
47
48 class MainMenu(Window):
49
50 def __init__(self, screen):
51 super(MainMenu, self).__init__(screen)
52 self.game_window = None
53 self.resume = None
54 self.screen = screen
55 self.background_colour = (0, 0, 0)
56 button1 = NewGameButton(self)
57 self.add_child(button1)
58 button2 = QuitButton()
59 self.add_child(button2)
60
61 def start_new_game(self):
62 self.game_window = GameWindow(self.screen)
63 if not self.resume:
64 # Add the resume button
65 self.resume = ResumeGameButton(self)
66 self.add_child(self.resume)
67 AddWindow.post(self.game_window)
68
69 def resume_game(self):
70 AddWindow.post(self.game_window)