-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameStatClass_WinterIsComing.py
129 lines (115 loc) · 4.24 KB
/
GameStatClass_WinterIsComing.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
#import modules
import pickle #for savegame
import sys
import datetime #for savegame timestamp
class GameStats:
"""This is a static class that does not need any instance.
Saves state for players, inventory, rooms, items player for the whole game"""
__listP = []
__currentPlayerId = 0
__currentRoom = None #holds the current room
__dictInventory = {} #holds inventory objects sorted by number
__listRoomsVisited = [] #holds room numbers
__listItemsYielded = [] #holds item numbers
#----------------------------------------------
# Gameplay related methods
#----------------------------------------------
@classmethod
def Quit(cls, guiRoot):
try:
guiRoot.audioStream.end() #if existing, end audio stream (experimental)
except:
pass
guiRoot.quit() #First end the gui's bindings
guiRoot.destroy() #Then close gui
sys.exit() #finally, exit program.
@classmethod
def Save(cls):
saveTime = datetime.datetime.now()
with open('savegame.dat', 'wb') as svGame:
pickle.dump([cls.__listP \
, cls.__dictInventory \
, cls.__listRoomsVisited \
, cls.__listItemsYielded \
, cls.__currentRoom \
, saveTime], svGame, protocol=2)
@classmethod
def Load(cls):
loadTime = datetime.datetime.now()
with open('savegame.dat', 'rb') as svGame:
cls.__listP \
, cls.__dictInventory \
, cls.__listRoomsVisited \
, cls.__listItemsYielded \
, cls.__currentRoom \
, saveTime \
= pickle.load(svGame)
return (loadTime - saveTime) #time difference between loading and saving
#----------------------------------------------
# Player related methods
#----------------------------------------------
@classmethod
def NextPlayer(cls):
"""Selects and returns the next player from the list"""
if cls.__currentPlayerId < (len(cls.__listP)-1):
#next player
cls.__currentPlayerId += 1
else:
#start again with first player
cls.__currentPlayerId = 0
@classmethod
def GetCurrentPlayer(cls):
return cls.__listP[cls.__currentPlayerId]
@classmethod
def GetNextPlayer(cls):
cPlayer = cls.GetCurrentPlayer()
cls.NextPlayer()
retPl = cls.GetCurrentPlayer()
while not cPlayer == cls.GetCurrentPlayer():
#skip players to go back to currentPlayer
cls.NextPlayer()
return retPl
@classmethod
def GetListPlayers(cls):
return cls.__listP
@classmethod
def SetListPlayers(cls, listP):
cls.__listP = listP
#----------------------------------------------
# Room and place related methods
#----------------------------------------------
@classmethod
def GetCurrentRoom(cls):
if cls.__currentRoom == None:
raise Exception("No room object available. Ending game.")
else:
return cls.__currentRoom
@classmethod
def SetCurrentRoom(cls, room):
cls.__currentRoom = room
@classmethod
def GetRoomsVisited(cls):
return cls.__listRoomsVisited
@classmethod
def AddRoomsVisited(cls):
cls.__listRoomsVisited.append(cls.__currentRoom.number)
#----------------------------------------------
# Item and inventory related methods
#----------------------------------------------
@classmethod
def AddToInventory(cls, item):
if item.number not in cls.__listItemsYielded:
cls.__listItemsYielded.append(item.number)
#add to inventory
cls.__dictInventory[item.number] = item
return cls.__dictInventory
@classmethod
def GetInventory(cls):
return cls.__dictInventory
@classmethod
def GetItemsYielded(cls):
return cls.__listItemsYielded
@classmethod
def DelFromInventory(cls, itemNumber):
del cls.__dictInventory[itemNumber]
return cls.__dictInventory