-
Notifications
You must be signed in to change notification settings - Fork 1
/
stgs.py
251 lines (209 loc) · 7.77 KB
/
stgs.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import os, sys
import pygame
import colors
import math
TITLE = "The Caverns"
LOADING_TEXT = [
"A darkness has fallen upon this once beautiful land.",
"What was once full of life is now permeated with the stench of death and decay",
"Lake crystal clear and sky pure blue turned red by the smoke that chokes the air.",
"Where there were once animals and people living in harmony,",
"There are now only creatures of darkness plotting their evil machinations.",
"Where there was once a great kingdon of dwarves, their halls filled with splendor,",
"There is now only the remains of their dark and dusty halls..."
]
LOADING_SCREEN_SHOWN_BEFORE = False
DEBUG = False
#### Establishes file paths ####
try:
PATH = sys._MEIPASS # Tries to see if the project is built
except AttributeError:
PATH = os.path.dirname(os.path.realpath(__file__))
# Path to the asset folder
ASSETSPATH = os.path.join(PATH, 'assets')
#### Gets file for saving settings in game. Every variable set here is default. Clearing the settings file should load everything as default. ####
if PATH == os.path.dirname(os.path.realpath(__file__)): #Checks if game is running from local path or has gamedata stored in appdata
saveFile = os.path.join(PATH, 'save.p')
else:
saveFile = os.path.join(os.getenv('APPDATA'), 'theCaverns', 'save.p') # Gets save file from appdata
try:
with open(saveFile, 'r') as b:
b.close() # Just Checks if the file exists
except FileNotFoundError:
os.mkdir(os.path.join(os.getenv('APPDATA'), 'theCaverns'))
print(saveFile)
#### Either centers the player no matter what (False) or doesn't scroll over the boundary of the level (True and preferred) ####
CAMLIMIT = True
SHOWFPS = True
#### FPS BOIS ####
FPS = 60
#### Volumes ####
musicVolume = 1
fxVolume = 1
#### Returns the asset's path ####
def asset(*args):
'''Returns asset path given asset name'''
global ASSETSPATH
r = ASSETSPATH
for arg in args:
r = os.path.join(r, arg)
return r
def sAsset(assetName):
'''Returns sound path given sound name'''
global ASSETSPATH
return os.path.join(ASSETSPATH, 'sounds', assetName)
def fAsset(assetName):
'''Returns font path given font name'''
global ASSETSPATH
return os.path.join(ASSETSPATH, 'fonts', assetName)
def tAsset(assetName):
'''Returns Tiled file path given font name'''
global ASSETSPATH
return os.path.join(ASSETSPATH, 'Tiled', assetName)
# Custom Generated fonts
fgenedfs = {}
def fgen(fn, s):
"""Generate a custom font"""
rn = fn + "-" + s
if not fgenedfs[rn]:
fgenedfs[rn] = pygame.font.Font(fAsset(fn), s)
return fgenedfs[rn]
#### Establishes window size ####
winWidth, winHeight = 1280, 720
winFlags = pygame.HWSURFACE
iconPath = asset('logo.jpeg')
#### Anti-Aliasing on text ####
aalias = True
#### Defines what key binding is set for each action ####
keySet = {
'start': pygame.K_s,
'retry':[pygame.K_r],
'toggleCam': pygame.K_o,
'map': pygame.K_m,
'interact':pygame.K_SPACE,
'pRight': [pygame.K_RIGHT, pygame.K_d],
'pLeft': [pygame.K_LEFT, pygame.K_a],
'pUp': [pygame.K_UP, pygame.K_w],
'pDown':[pygame.K_DOWN, pygame.K_s],
'fullScreen': pygame.K_f,
'pause': pygame.K_p
}
joystickDisabled = True
pygame.joystick.init()
joysticks = [pygame.joystick.Joystick(i) for i in range(pygame.joystick.get_count())]
joystickConnected = True if len(joysticks) > 0 else False
joystickEnabled = True if joystickConnected and not joystickDisabled else False
def checkJoysticks():
'''
STILL IN BETA TESTING
'''
global joystickEnabled
joystickEnabled = True if joystickConnected and not joystickDisabled else False
def getJoy1():
'''
STILL IN BETA TESTING
'''
return joysticks[0] if len(joysticks) > 0 else False
#### Changes movement from flying to platforming ####
platformer = True
def checkKey(move):
'''Handy Dandy class for checking the status of keys given a
a) keyword for the built in mapped buttons
b) a list of pygame keys in which it returns true for any
c) or just a single pygame key
pygame key being pygame.K_a or pygame.K_UP'''
keys = pygame.key.get_pressed()
if isinstance(move, str):
try:
for k in keySet[move]:
if keys[k]:
return True
except TypeError:
if keys[keySet[move]]:
return True
else:
try:
for k in move:
if keys[k]:
return True
except TypeError:
if keys[move]:
return True
return False
class Spritesheet:
'''utility class for loading and parsing spritesheets'''
def __init__(self, filePath):
'''Takes a filepath or file'''
try:
self.image = pygame.image.load(filePath).convert_alpha()
except TypeError:
self.image = filePath.copy().convert_alpha()
self.width = self.image.get_width()
self.height = self.image.get_height()
def get_image(self, x, y, width, height):
''' Grabs an image out of the spritesheet image
takes: x, y, width, height'''
img = pygame.Surface((width, height), pygame.SRCALPHA)
# img.fill((0, 0, 0, 0))
img.blit(self.image, (0, 0), (x, y, width, height))
img = pygame.transform.scale(img, (width, height))
return img.convert_alpha()
# Loads all fonts if program is run indirectly
if __name__ != '__main__':
fonts = {
'title1': pygame.font.Font(fAsset('YuseiMagic-Regular.ttf'), 42),
'main-title1': pygame.font.Font(fAsset('PixelLove.ttf'), 68),
'subtitle1': pygame.font.Font(fAsset('YuseiMagic-Regular.ttf'), 37),
'2': pygame.font.Font(fAsset('YuseiMagic-Regular.ttf'), 25),
'3': pygame.font.Font(fAsset('YuseiMagic-Regular.ttf'), 28),
'description1': pygame.font.Font(fAsset('PottaOne-Regular.ttf'), 24),
'title2': pygame.font.Font(fAsset('PixelLove.ttf'), 40),
'caption1': pygame.font.Font(fAsset('YuseiMagic-Regular.ttf'), 24),
'effect1': pygame.font.Font(fAsset('YuseiMagic-Regular.ttf'), 18),
'gameover': pygame.font.Font(fAsset('YuseiMagic-Regular.ttf'), 60),
'victory': pygame.font.Font(fAsset('YuseiMagic-Regular.ttf'), 72),
'menu1': pygame.font.Font(fAsset('YuseiMagic-Regular.ttf'), 15)
}
def dist(vec1, vec2):
'''Distance formula between two pygame Vectors'''
dist1 = (vec1.x-vec2.x)**2
dist2 = (vec1.y-vec2.y)**2
return math.sqrt(dist1+dist2)
import pickle
def loadSave(file):
'''Load save
STILL IN BETA TESTING
'''
try:
with open(file, 'rb') as f:
data = pickle.load(f)
items = list(data.items())
for k, v in items:
globals()[k] = v
checkJoysticks()
except FileNotFoundError:
print("No Save File")
def saveData(file, game):
'''Save game settings
STILL IN BETA TESTING
'''
saveDict = { # Each value must corresponde to a global variable in this file
'musicVolume': game.mixer.musicVolume,
'fxVolume': game.mixer.fxVolume,
'aalias': game.antialiasing,
'SHOWFPS': game.showFps,
'joystickDisabled': game.joystickDisabled,
"LOADING_SCREEN_SHOWN_BEFORE": game.loadingScreenShownBefore
}
with open(file, 'wb') as f:
pickle.dump(saveDict, f)
def tGet(objT, strValue, default=False):
'''Somewhat redundant function for forcing properties out of a Tiled object
Takes: objT (Tiled object), strValue (keyword), default=False (customize default value)'''
try:
return objT.properties[strValue]
except KeyError:
return default
def now():
'''Returns number of cycles that pygame has been ticking'''
return pygame.time.get_ticks()