Mercurial > sypikslang
comparison gamelib/mainmenu.py @ 88:74ce25ec2073
Autosave & load support
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Wed, 09 May 2012 20:07:14 +0200 |
parents | 8d1cf0cbe5e1 |
children | 0823e2529c23 |
comparison
equal
deleted
inserted
replaced
87:d93e1ea2bd0d | 88:74ce25ec2073 |
---|---|
2 # vim:fileencoding=utf-8 ai ts=4 sts=4 et sw=4 | 2 # vim:fileencoding=utf-8 ai ts=4 sts=4 et sw=4 |
3 | 3 |
4 """The main menu""" | 4 """The main menu""" |
5 | 5 |
6 import pygame | 6 import pygame |
7 import os | |
7 from pygame import image | 8 from pygame import image |
9 | |
10 try: | |
11 import simplejson | |
12 json = simplejson | |
13 except ImportError: | |
14 import json | |
8 | 15 |
9 from gamelib import data | 16 from gamelib import data |
10 from gamelib.gui_base import Window | 17 from gamelib.gui_base import Window |
11 from gamelib.gui import BigButton | 18 from gamelib.gui import BigButton |
12 from gamelib.engine import AddWindow | 19 from gamelib.engine import AddWindow |
13 from gamelib.gamegui import LabWindow | 20 from gamelib.gamegui import LabWindow |
14 | 21 |
15 from gamelib.constants import WIDTH, HEIGHT | 22 from gamelib.constants import WIDTH, HEIGHT |
23 from gamelib.game_base import get_save_filename | |
16 | 24 |
17 | 25 |
18 class MainMenuButton(BigButton): | 26 class MainMenuButton(BigButton): |
19 WIDTH = 276 | 27 WIDTH = 276 |
20 HEIGHT = 75 | 28 HEIGHT = 75 |
55 pygame.event.post(pygame.event.Event(pygame.QUIT)) | 63 pygame.event.post(pygame.event.Event(pygame.QUIT)) |
56 | 64 |
57 | 65 |
58 class MainMenu(Window): | 66 class MainMenu(Window): |
59 | 67 |
60 def __init__(self, screen): | 68 def __init__(self, screen, savefile): |
61 super(MainMenu, self).__init__(screen) | 69 super(MainMenu, self).__init__(screen) |
62 self.game_window = None | 70 self.game_window = None |
63 self.resume = None | 71 self.resume = None |
64 self.screen = screen | 72 self.screen = screen |
65 self.background_colour = (0, 0, 0) | 73 self.background_colour = (0, 0, 0) |
66 self.background_image = image.load(data.filepath('images/temp.jpg')) | 74 self.background_image = image.load(data.filepath('images/temp.jpg')) |
67 button1 = NewGameButton(self) | 75 button1 = NewGameButton(self) |
68 self.add_child(button1) | 76 self.add_child(button1) |
69 button2 = QuitButton() | 77 button2 = QuitButton() |
70 self.add_child(button2) | 78 self.add_child(button2) |
79 if savefile: | |
80 self.load_game(savefile) | |
81 else: | |
82 # See if we have an autosave file | |
83 savefile = get_save_filename() | |
84 if os.path.exists(savefile): | |
85 self.load_game(savefile) | |
71 | 86 |
72 def start_new_game(self): | 87 def start_new_game(self): |
73 self.game_window = LabWindow(self.screen) | 88 self.game_window = LabWindow(self.screen, None) |
89 self.add_resume() | |
90 AddWindow.post(self.game_window) | |
91 | |
92 def load_game(self, savefile): | |
93 if os.access(savefile, os.R_OK): | |
94 f = open(savefile, 'r') | |
95 game_data = json.load(f) | |
96 f.close() | |
97 self.game_window = LabWindow(self.screen, game_data) | |
98 self.add_resume() | |
99 # We stay at the main menu, so the user can can to continue or not | |
100 | |
101 def add_resume(self): | |
74 if not self.resume: | 102 if not self.resume: |
75 # Add the resume button | 103 # Add the resume button |
76 self.resume = ResumeGameButton(self) | 104 self.resume = ResumeGameButton(self) |
77 self.add_child(self.resume) | 105 self.add_child(self.resume) |
78 AddWindow.post(self.game_window) | |
79 | 106 |
80 def resume_game(self): | 107 def resume_game(self): |
81 AddWindow.post(self.game_window) | 108 AddWindow.post(self.game_window) |