annotate gamelib/mainmenu.py @ 227:ebb62654f61f

finalized main menu
author Rizmari Versfeld <rizziepit@gmail.com>
date Sat, 12 May 2012 23:09:16 +0200
parents 53277724645b
children 64ec8ff87c6e
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
53
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
17 from gamelib.gui_base import Window
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
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
22 from gamelib.constants import WIDTH, HEIGHT
88
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
23 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
24
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
25
74
22b65c943712 prettified main menu - temp background
Rizmari Versfeld <rizziepit@gmail.com>
parents: 63
diff changeset
26 class MainMenuButton(BigButton):
22b65c943712 prettified main menu - temp background
Rizmari Versfeld <rizziepit@gmail.com>
parents: 63
diff changeset
27 WIDTH = 276
22b65c943712 prettified main menu - temp background
Rizmari Versfeld <rizziepit@gmail.com>
parents: 63
diff changeset
28 HEIGHT = 75
76
ae2c053ad80e renamed images
Rizmari Versfeld <rizziepit@gmail.com>
parents: 75
diff changeset
29 BG_IMAGE_NORMAL = image.load(data.filepath('images/main_normal.png'))
ae2c053ad80e renamed images
Rizmari Versfeld <rizziepit@gmail.com>
parents: 75
diff changeset
30 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
31
22b65c943712 prettified main menu - temp background
Rizmari Versfeld <rizziepit@gmail.com>
parents: 63
diff changeset
32
22b65c943712 prettified main menu - temp background
Rizmari Versfeld <rizziepit@gmail.com>
parents: 63
diff changeset
33 class NewGameButton(MainMenuButton):
53
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
34
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
35 def __init__(self, parent):
227
ebb62654f61f finalized main menu
Rizmari Versfeld <rizziepit@gmail.com>
parents: 145
diff changeset
36 super(NewGameButton, self).__init__((400, 100),
53
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
37 'Start New Game')
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
38 self.parent = parent
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
39
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
40 def on_click(self):
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
41 self.parent.start_new_game()
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
42
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
43
74
22b65c943712 prettified main menu - temp background
Rizmari Versfeld <rizziepit@gmail.com>
parents: 63
diff changeset
44 class ResumeGameButton(MainMenuButton):
53
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
45
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
46 def __init__(self, parent):
227
ebb62654f61f finalized main menu
Rizmari Versfeld <rizziepit@gmail.com>
parents: 145
diff changeset
47 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
48 self.parent = parent
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
49
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
50 def on_click(self):
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
51 self.parent.resume_game()
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
52
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
53
74
22b65c943712 prettified main menu - temp background
Rizmari Versfeld <rizziepit@gmail.com>
parents: 63
diff changeset
54 class QuitButton(MainMenuButton):
53
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
55
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
56 def __init__(self):
227
ebb62654f61f finalized main menu
Rizmari Versfeld <rizziepit@gmail.com>
parents: 145
diff changeset
57 super(QuitButton, self).__init__((400, 240), 'Quit')
53
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
58
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
59 def on_click(self):
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
60 pygame.event.post(pygame.event.Event(pygame.QUIT))
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
61
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
62
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
63 class MainMenu(Window):
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
64
88
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
65 def __init__(self, screen, savefile):
53
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
66 super(MainMenu, self).__init__(screen)
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
67 self.game_window = None
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
68 self.resume = None
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
69 self.screen = screen
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
70 self.background_colour = (0, 0, 0)
227
ebb62654f61f finalized main menu
Rizmari Versfeld <rizziepit@gmail.com>
parents: 145
diff changeset
71 self.background_image = image.load(data.filepath('images/main_background.jpg'))
53
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
72 button1 = NewGameButton(self)
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
73 self.add_child(button1)
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
74 button2 = QuitButton()
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
75 self.add_child(button2)
88
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
76 if savefile:
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
77 self.load_game(savefile)
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
78 else:
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
79 # See if we have an autosave file
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
80 savefile = get_save_filename()
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
81 if os.path.exists(savefile):
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
82 self.load_game(savefile)
53
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
83
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
84 def start_new_game(self):
88
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
85 self.game_window = LabWindow(self.screen, None)
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
86 self.add_resume()
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
87 AddWindow.post(self.game_window)
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
88
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
89 def load_game(self, savefile):
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
90 if os.access(savefile, os.R_OK):
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
91 f = open(savefile, 'r')
90
0823e2529c23 Add robustness around the savefile stuff
Neil Muller <drnlmuller@gmail.com>
parents: 88
diff changeset
92 try:
0823e2529c23 Add robustness around the savefile stuff
Neil Muller <drnlmuller@gmail.com>
parents: 88
diff changeset
93 game_data = json.load(f)
0823e2529c23 Add robustness around the savefile stuff
Neil Muller <drnlmuller@gmail.com>
parents: 88
diff changeset
94 except json.JSONDecodeError:
0823e2529c23 Add robustness around the savefile stuff
Neil Muller <drnlmuller@gmail.com>
parents: 88
diff changeset
95 print 'Unable to load the autosave file - skipping'
0823e2529c23 Add robustness around the savefile stuff
Neil Muller <drnlmuller@gmail.com>
parents: 88
diff changeset
96 game_data = None
88
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
97 f.close()
136
1a648d07d67e Handle broken (but valid JSON) autosave more gracefully.
Jeremy Thurgood <firxen@gmail.com>
parents: 98
diff changeset
98 try:
1a648d07d67e Handle broken (but valid JSON) autosave more gracefully.
Jeremy Thurgood <firxen@gmail.com>
parents: 98
diff changeset
99 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
100 if game_data:
1a648d07d67e Handle broken (but valid JSON) autosave more gracefully.
Jeremy Thurgood <firxen@gmail.com>
parents: 98
diff changeset
101 self.add_resume()
1a648d07d67e Handle broken (but valid JSON) autosave more gracefully.
Jeremy Thurgood <firxen@gmail.com>
parents: 98
diff changeset
102 except Exception, e:
1a648d07d67e Handle broken (but valid JSON) autosave more gracefully.
Jeremy Thurgood <firxen@gmail.com>
parents: 98
diff changeset
103 print 'Error loading autosave (%r) - ignoring' % (e,)
88
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
104 # 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
105
74ce25ec2073 Autosave & load support
Neil Muller <drnlmuller@gmail.com>
parents: 79
diff changeset
106 def add_resume(self):
53
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
107 if not self.resume:
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
108 # Add the resume button
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
109 self.resume = ResumeGameButton(self)
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
110 self.add_child(self.resume)
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
111
98
e386ec5d179b The game can end
Neil Muller <drnlmuller@gmail.com>
parents: 90
diff changeset
112 def game_over(self):
e386ec5d179b The game can end
Neil Muller <drnlmuller@gmail.com>
parents: 90
diff changeset
113 if self.resume:
e386ec5d179b The game can end
Neil Muller <drnlmuller@gmail.com>
parents: 90
diff changeset
114 self.remove_child(self.resume)
e386ec5d179b The game can end
Neil Muller <drnlmuller@gmail.com>
parents: 90
diff changeset
115 self.resume = None
e386ec5d179b The game can end
Neil Muller <drnlmuller@gmail.com>
parents: 90
diff changeset
116
53
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
117 def resume_game(self):
655a6912e0ae Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
118 AddWindow.post(self.game_window)