annotate gamelib/mainmenu.py @ 230:745a6ee5f643

large font for main menu
author Rizmari Versfeld <rizziepit@gmail.com>
date Sat, 12 May 2012 23:18:30 +0200
parents 64ec8ff87c6e
children 05afa7ae5df3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
53
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
2 # vim:fileencoding=utf-8 ai ts=4 sts=4 et sw=4
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
3
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
4 """The main menu"""
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
5
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
6 import pygame
88
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
7 import os
74
22b65c943712 prettified main menu - temp background
Rizmari Versfeld <rizziepit@gmail.com>
parents: 63
diff changeset
8 from pygame import image
53
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
9
88
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
10 try:
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
11 import simplejson
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
12 json = simplejson
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
13 except ImportError:
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
14 import json
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
15
74
22b65c943712 prettified main menu - temp background
Rizmari Versfeld <rizziepit@gmail.com>
parents: 63
diff changeset
16 from gamelib import data
230
745a6ee5f643 large font for main menu
Rizmari Versfeld <rizziepit@gmail.com>
parents: 229
diff changeset
17 from gamelib.gui_base import Window, font_large
145
53277724645b Science button juggling.
Jeremy Thurgood <firxen@gmail.com>
parents: 143
diff changeset
18 from gamelib.gui import BigButton
53
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
19 from gamelib.engine import AddWindow
63
364ff3479ef2 Remove duplicate point tracking. Rename classes for future refactoring
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
20 from gamelib.gamegui import LabWindow
53
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
21
88
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
22 from gamelib.game_base import get_save_filename
53
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
23
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
24
74
22b65c943712 prettified main menu - temp background
Rizmari Versfeld <rizziepit@gmail.com>
parents: 63
diff changeset
25 class MainMenuButton(BigButton):
22b65c943712 prettified main menu - temp background
Rizmari Versfeld <rizziepit@gmail.com>
parents: 63
diff changeset
26 WIDTH = 276
22b65c943712 prettified main menu - temp background
Rizmari Versfeld <rizziepit@gmail.com>
parents: 63
diff changeset
27 HEIGHT = 75
76
ae2c053ad80e renamed images
Rizmari Versfeld <rizziepit@gmail.com>
parents: 75
diff changeset
28 BG_IMAGE_NORMAL = image.load(data.filepath('images/main_normal.png'))
ae2c053ad80e renamed images
Rizmari Versfeld <rizziepit@gmail.com>
parents: 75
diff changeset
29 BG_IMAGE_DOWN = image.load(data.filepath('images/main_down.png'))
74
22b65c943712 prettified main menu - temp background
Rizmari Versfeld <rizziepit@gmail.com>
parents: 63
diff changeset
30
230
745a6ee5f643 large font for main menu
Rizmari Versfeld <rizziepit@gmail.com>
parents: 229
diff changeset
31 def __init__(self, pos, text):
745a6ee5f643 large font for main menu
Rizmari Versfeld <rizziepit@gmail.com>
parents: 229
diff changeset
32 super(MainMenuButton, self).__init__(pos, text, font_large)
745a6ee5f643 large font for main menu
Rizmari Versfeld <rizziepit@gmail.com>
parents: 229
diff changeset
33
74
22b65c943712 prettified main menu - temp background
Rizmari Versfeld <rizziepit@gmail.com>
parents: 63
diff changeset
34
22b65c943712 prettified main menu - temp background
Rizmari Versfeld <rizziepit@gmail.com>
parents: 63
diff changeset
35 class NewGameButton(MainMenuButton):
53
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
36
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
37 def __init__(self, parent):
227
ebb62654f61f finalized main menu
Rizmari Versfeld <rizziepit@gmail.com>
parents: 145
diff changeset
38 super(NewGameButton, self).__init__((400, 100),
53
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
39 'Start New Game')
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
40 self.parent = parent
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
41
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
42 def on_click(self):
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
43 self.parent.start_new_game()
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
44
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
45
74
22b65c943712 prettified main menu - temp background
Rizmari Versfeld <rizziepit@gmail.com>
parents: 63
diff changeset
46 class ResumeGameButton(MainMenuButton):
53
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
47
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
48 def __init__(self, parent):
227
ebb62654f61f finalized main menu
Rizmari Versfeld <rizziepit@gmail.com>
parents: 145
diff changeset
49 super(ResumeGameButton, self).__init__((400, 170), 'Resume Game')
53
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
50 self.parent = parent
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
51
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
52 def on_click(self):
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
53 self.parent.resume_game()
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
54
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
55
74
22b65c943712 prettified main menu - temp background
Rizmari Versfeld <rizziepit@gmail.com>
parents: 63
diff changeset
56 class QuitButton(MainMenuButton):
53
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
57
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
58 def __init__(self):
227
ebb62654f61f finalized main menu
Rizmari Versfeld <rizziepit@gmail.com>
parents: 145
diff changeset
59 super(QuitButton, self).__init__((400, 240), 'Quit')
53
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
60
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
61 def on_click(self):
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
62 pygame.event.post(pygame.event.Event(pygame.QUIT))
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
63
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
64
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
65 class MainMenu(Window):
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
66
88
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
67 def __init__(self, screen, savefile):
53
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
68 super(MainMenu, self).__init__(screen)
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
69 self.game_window = None
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
70 self.resume = None
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
71 self.screen = screen
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
72 self.background_colour = (0, 0, 0)
229
64ec8ff87c6e foiled by pyflakes
Rizmari Versfeld <rizziepit@gmail.com>
parents: 227
diff changeset
73 self.background_image = image.load(data.filepath(
64ec8ff87c6e foiled by pyflakes
Rizmari Versfeld <rizziepit@gmail.com>
parents: 227
diff changeset
74 'images/main_background.jpg'))
53
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
75 button1 = NewGameButton(self)
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
76 self.add_child(button1)
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
77 button2 = QuitButton()
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
78 self.add_child(button2)
88
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
79 if savefile:
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
80 self.load_game(savefile)
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
81 else:
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
82 # See if we have an autosave file
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
83 savefile = get_save_filename()
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
84 if os.path.exists(savefile):
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
85 self.load_game(savefile)
53
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
86
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
87 def start_new_game(self):
88
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
88 self.game_window = LabWindow(self.screen, None)
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
89 self.add_resume()
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
90 AddWindow.post(self.game_window)
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
91
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
92 def load_game(self, savefile):
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
93 if os.access(savefile, os.R_OK):
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
94 f = open(savefile, 'r')
90
0823e2529c23 Add robustness around the savefile stuff
Neil Muller <drnlmuller@gmail.com>
parents: 88
diff changeset
95 try:
0823e2529c23 Add robustness around the savefile stuff
Neil Muller <drnlmuller@gmail.com>
parents: 88
diff changeset
96 game_data = json.load(f)
0823e2529c23 Add robustness around the savefile stuff
Neil Muller <drnlmuller@gmail.com>
parents: 88
diff changeset
97 except json.JSONDecodeError:
0823e2529c23 Add robustness around the savefile stuff
Neil Muller <drnlmuller@gmail.com>
parents: 88
diff changeset
98 print 'Unable to load the autosave file - skipping'
0823e2529c23 Add robustness around the savefile stuff
Neil Muller <drnlmuller@gmail.com>
parents: 88
diff changeset
99 game_data = None
88
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
100 f.close()
136
1a648d07d67e Handle broken (but valid JSON) autosave more gracefully.
Jeremy Thurgood <firxen@gmail.com>
parents: 98
diff changeset
101 try:
1a648d07d67e Handle broken (but valid JSON) autosave more gracefully.
Jeremy Thurgood <firxen@gmail.com>
parents: 98
diff changeset
102 self.game_window = LabWindow(self.screen, game_data)
1a648d07d67e Handle broken (but valid JSON) autosave more gracefully.
Jeremy Thurgood <firxen@gmail.com>
parents: 98
diff changeset
103 if game_data:
1a648d07d67e Handle broken (but valid JSON) autosave more gracefully.
Jeremy Thurgood <firxen@gmail.com>
parents: 98
diff changeset
104 self.add_resume()
1a648d07d67e Handle broken (but valid JSON) autosave more gracefully.
Jeremy Thurgood <firxen@gmail.com>
parents: 98
diff changeset
105 except Exception, e:
1a648d07d67e Handle broken (but valid JSON) autosave more gracefully.
Jeremy Thurgood <firxen@gmail.com>
parents: 98
diff changeset
106 print 'Error loading autosave (%r) - ignoring' % (e,)
88
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
107 # We stay at the main menu, so the user can can to continue or not
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
108
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
109 def add_resume(self):
53
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
110 if not self.resume:
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
111 # Add the resume button
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
112 self.resume = ResumeGameButton(self)
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
113 self.add_child(self.resume)
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
114
98
e386ec5d179b The game can end
Neil Muller <drnlmuller@gmail.com>
parents: 90
diff changeset
115 def game_over(self):
e386ec5d179b The game can end
Neil Muller <drnlmuller@gmail.com>
parents: 90
diff changeset
116 if self.resume:
e386ec5d179b The game can end
Neil Muller <drnlmuller@gmail.com>
parents: 90
diff changeset
117 self.remove_child(self.resume)
e386ec5d179b The game can end
Neil Muller <drnlmuller@gmail.com>
parents: 90
diff changeset
118 self.resume = None
e386ec5d179b The game can end
Neil Muller <drnlmuller@gmail.com>
parents: 90
diff changeset
119
53
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
120 def resume_game(self):
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
121 AddWindow.post(self.game_window)