Mercurial > sypikslang
annotate gamelib/mainmenu.py @ 130:8f717b2b3378
Advance milestone
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Thu, 10 May 2012 16:46:18 +0200 |
parents | e386ec5d179b |
children | 1a648d07d67e |
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 | 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 | 10 try: |
11 import simplejson | |
12 json = simplejson | |
13 except ImportError: | |
14 import json | |
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 |
655a6912e0ae
Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
18 from gamelib.gui import BigButton |
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 | 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 | 29 BG_IMAGE_NORMAL = image.load(data.filepath('images/main_normal.png')) |
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): |
79 | 36 super(NewGameButton, self).__init__(((WIDTH - self.WIDTH) / 2, |
37 (HEIGHT - self.HEIGHT) / 2 - self.HEIGHT), | |
53
655a6912e0ae
Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
38 'Start New Game') |
655a6912e0ae
Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
39 self.parent = parent |
655a6912e0ae
Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
40 |
655a6912e0ae
Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
41 def on_click(self): |
655a6912e0ae
Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
42 self.parent.start_new_game() |
655a6912e0ae
Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
43 |
655a6912e0ae
Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
44 |
74
22b65c943712
prettified main menu - temp background
Rizmari Versfeld <rizziepit@gmail.com>
parents:
63
diff
changeset
|
45 class ResumeGameButton(MainMenuButton): |
53
655a6912e0ae
Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
46 |
655a6912e0ae
Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
47 def __init__(self, parent): |
74
22b65c943712
prettified main menu - temp background
Rizmari Versfeld <rizziepit@gmail.com>
parents:
63
diff
changeset
|
48 super(ResumeGameButton, self).__init__(((WIDTH - self.WIDTH) / 2, |
79 | 49 (HEIGHT - self.HEIGHT) / 2), '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): |
74
22b65c943712
prettified main menu - temp background
Rizmari Versfeld <rizziepit@gmail.com>
parents:
63
diff
changeset
|
59 super(QuitButton, self).__init__(((WIDTH - self.WIDTH) / 2, |
79 | 60 (HEIGHT - self.HEIGHT) / 2 + self.HEIGHT), 'Quit') |
53
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 def on_click(self): |
655a6912e0ae
Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
63 pygame.event.post(pygame.event.Event(pygame.QUIT)) |
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 |
655a6912e0ae
Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
66 class MainMenu(Window): |
655a6912e0ae
Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
67 |
88 | 68 def __init__(self, screen, savefile): |
53
655a6912e0ae
Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
69 super(MainMenu, self).__init__(screen) |
655a6912e0ae
Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
70 self.game_window = None |
655a6912e0ae
Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
71 self.resume = None |
655a6912e0ae
Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
72 self.screen = screen |
655a6912e0ae
Split gui stuff out of main.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
73 self.background_colour = (0, 0, 0) |
75 | 74 self.background_image = image.load(data.filepath('images/temp.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 | 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) | |
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 | 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') | |
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 | 100 f.close() |
101 self.game_window = LabWindow(self.screen, game_data) | |
90
0823e2529c23
Add robustness around the savefile stuff
Neil Muller <drnlmuller@gmail.com>
parents:
88
diff
changeset
|
102 if game_data: |
0823e2529c23
Add robustness around the savefile stuff
Neil Muller <drnlmuller@gmail.com>
parents:
88
diff
changeset
|
103 self.add_resume() |
88 | 104 # We stay at the main menu, so the user can can to continue or not |
105 | |
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 | 112 def game_over(self): |
113 if self.resume: | |
114 self.remove_child(self.resume) | |
115 self.resume = None | |
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) |