-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgamestate.py
228 lines (193 loc) · 8.84 KB
/
gamestate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#! /usr/bin/python
# GameState and all its inherited classes
#
# The GameState class provides the foundation for the possible game states.
# Included in this file are the MainMenuState and OptionsMenuState.
#
# These states each recieve input through an update() function and print to the
# screen using a display() function. The update() function returns an integer
# representation of the state the game should be in, given the input.
#
# gamestate.py is part of Sentience in Space.
#
# Sentience in Space is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Sentience in Space is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Sentience in Space. If not, see <http://www.gnu.org/licenses/>.
import os, sys, pygame, hashlib
from consts import *
from settings import *
from menu import *
from character import *
from inventory import *
from journal import *
from npc import *
from shiplayout import *
# States
MAIN_MENU_STATE = 0
IN_GAME_STATE = 1
LOAD_STATE = 2
SAVE_STATE = 3
EXIT_STATE = 4
SETTINGS_STATE = 5
# IMJ_STATE = 6 OLD FUNCTIONALITY
PUZZLE_STATE = 7
OPTIONS_MENU_STATE = 8
INVENTORY_STATE = 9
JOURNAL_STATE = 10
MAP_STATE = 11
#-------------------------------------------------------------------------------
#---[ GameState Class ]---------------------------------------------------------
#-------------------------------------------------------------------------------
## This class is used as a template to allow easy transition between game states
## by declaring common functionality
class GameState:
def __init__(self, state_id):
# self.state_id is the int id associated with the GameState subclass
self.state_id = state_id
## ---[ update ]------------------------------------------------------------
# @param self The class itself, Python standard
# @param event A pygame event
#
# Updates the GameState, returns the (new) game state
def update(self, event):
pass
## ---[ display ]----------------------------------------------------------
#
# Prints the game state's current image to the screen
def display(self):
pass
## ---[ checkstatechanges ]--------------------------------------------------
# @param self The class itself, Python standard
# @param event A pygame event
#
# Checks if the event indicates that the state should change and returns the
# state associated with the given event
def checkstatechange(self, event):
if event.type == pygame.KEYDOWN:
# If Inventory key is pressed:
if event.key == KEYBINDINGS[KB_INVENTORY]:
if self.state_id == INVENTORY_STATE:
return IN_GAME_STATE
return INVENTORY_STATE
# If journal key is pressed
elif event.key == KEYBINDINGS[KB_JOURNAL]:
if self.state_id == JOURNAL_STATE:
return IN_GAME_STATE
return JOURNAL_STATE
# If map key is pressed
elif event.key == KEYBINDINGS[KB_MAP]:
if self.state_id == MAP_STATE:
return IN_GAME_STATE
return MAP_STATE
# Temporarily way to enter puzzle state ("p" key)
elif event.key == pygame.K_p:
if self.state_id == PUZZLE_STATE:
return IN_GAME_STATE
return PUZZLE_STATE
# If Escape is pressed
elif event.key == pygame.K_ESCAPE:
if self.state_id == OPTIONS_MENU_STATE:
return IN_GAME_STATE
return OPTIONS_MENU_STATE
def setflags(self, flags):
self.flags = flags
def getflags(self):
return self.flags
#-------------------------------------------------------------------------------
#---[ MainMenuState Class ]-----------------------------------------------------
#-------------------------------------------------------------------------------
## This class handles the functionality for the main menu (allowing "new game,"
## "load game" and "exit" options)
#
class MainMenuState (GameState):
def __init__(self, screen, save_exists, state_id):
self.state_id = state_id
self.save_exists = save_exists
self.ship = pygame.image.load(BACKGROUND_SHIP_DIR)
self.screen = screen
self.font = pygame.font.Font(GAME_NAME_FONT_DIR, GAME_NAME_FONT_SIZE)
# Menu to display "New Game, "Load Game," and "Quit" options
self.menu = cMenu(50, 50, 20, 5, 'vertical', 100, screen,
[('New Game', IN_GAME_STATE, None, True),
('Load Game', LOAD_STATE, None, self.save_exists),
('Modify Settings', SETTINGS_STATE, None, True),
('Quit', EXIT_STATE, None, True)])
self.menu.set_center(True, True)
self.menu.set_alignment('center', 'center')
## ---[ update ]------------------------------------------------------------
def update(self, event):
state = MAIN_MENU_STATE
if event.type == pygame.KEYDOWN or event.type == EVENT_CHANGE_STATE:
rectList, state = self.menu.update(event, MAIN_MENU_STATE)
return state
## ---[ display ]----------------------------------------------------------
def display(self):
self.screen.blit(self.font.render(GAME_NAME, GAME_NAME_FONT_ANTIALIAS, GAME_NAME_FONT_COLOR), GAME_NAME_FONT_BOX)
self.screen.blit(self.ship, BACKGROUND_SHIP_BOX)
self.menu.draw_buttons()
#-------------------------------------------------------------------------------
#---[ OptionsMenuState Class ]--------------------------------------------------
#-------------------------------------------------------------------------------
## This class handles the functionality for the options menu (allowing
## "resume game," "save game," "load game," "modify settings" and "exit" options)
#
class OptionsMenuState (GameState):
def __init__(self, screen, save_exists, state_id):
self.state_id = state_id
self.save_exists = save_exists
self.menu = cMenu(50, 50, 20, 5, 'vertical', 100, screen,
[('Resume Game', IN_GAME_STATE, None, True),
('Save Game', SAVE_STATE, None, True),
('Load Game', LOAD_STATE, None, save_exists),
('Modify Settings', SETTINGS_STATE, None, True),
('Quit', EXIT_STATE, None, True)])
self.menu.set_center(True, True)
self.menu.set_alignment('center', 'center')
## ---[ loadable ]----------------------------------------------------------
# Changes whether the "Load" option is available
def loadable(self, save_exists = True):
self.menu.set_selectable( 'Load Game', save_exists )
## ---[ update ]------------------------------------------------------------
def update(self, event):
state = OPTIONS_MENU_STATE
if event.type == pygame.KEYDOWN or event.type == EVENT_CHANGE_STATE:
rectList, state = self.menu.update(event, OPTIONS_MENU_STATE)
if self.checkstatechange(event) == IN_GAME_STATE:
return IN_GAME_STATE
return state
## ---[ display ]----------------------------------------------------------
def display(self):
self.menu.draw_buttons()
#-------------------------------------------------------------------------------
#---[ SettingsState Class ]-----------------------------------------------------
#-------------------------------------------------------------------------------
## This class handles the functionality changing game settings
class SettingsState (GameState):
def __init__(self, screen, state_id):
self.screen = screen
self.state_id = state_id
self.cf = MAIN_MENU_STATE
self.settings = Settings()
## ---[ update ]------------------------------------------------------------
def update(self, event):
changed_state = self.checkstatechange(event)
if event.key == pygame.K_ESCAPE:
return self.cf
if event.type == pygame.KEYDOWN:
self.settings.update(event)
return self.state_id
## ---[ display ]----------------------------------------------------------
def display(self):
self.settings.display(self.screen)
## ---[ calledfrom ]-----------------------------------------------------------
def calledfrom(self, state):
self.cf = state