forked from rainlash/lex-talionis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
133 lines (118 loc) · 5.42 KB
/
main.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
# __ __________ ___
# | | | ____\ \ / /
# | | | |__ \ V /
# | | | __| > <
# | `----.| |____ / . \
# |_______||_______/__/ \__\
# .___________. ___ __ __ ______ .__ __. __ _______.
# | | / \ | | | | / __ \ | \ | | | | / |
# `---| |----` / ^ \ | | | | | | | | | \| | | | | (----`
# | | / /_\ \ | | | | | | | | | . ` | | | \ \
# | | / _____ \ | `----.| | | `--' | | |\ | | | .----) |
# |__| /__/ \__\ |_______||__| \______/ |__| \__| |__| |_______/
# MAIN STRUCTURES USED
# Game Loop (get events, logic, update, draw)
# FrameRate independant animations (mostly :))
# State Machine Stack -> to help keep the game logic sane
# Arbitrary Components for Statuses, Skills, and Items
# Massive Unit Object
# Super-serializable save states
# === CODE ====================================================================
# === IMPORT MODULES ==========================================================
import os
# Custom imports
import Code.imagesDict as imagesDict
import Code.GlobalConstants as GC
import Code.configuration as cf
from Code import GameStateObj, Engine
# === MAIN FUNCTION ===========================================================
def main():
# logger = logging.getLogger(__name__)
# Set Volume
Engine.music_thread.set_volume(cf.OPTIONS['Music Volume'])
imagesDict.set_sound_volume(cf.OPTIONS['Sound Volume'], GC.SOUNDDICT)
gameStateObj = GameStateObj.GameStateObj()
metaDataObj = {}
gameStateObj.metaDataObj = metaDataObj
run(gameStateObj, metaDataObj)
# === Main Game Loop ===
def run(gameStateObj, metaDataObj):
my_list = gameStateObj.stateMachine.state[-5:]
while True:
if cf.OPTIONS['debug']:
my_new_list = gameStateObj.stateMachine.state[-5:]
if my_new_list != my_list:
logger.debug('Current states %s', [state.name for state in gameStateObj.stateMachine.state])
my_list = my_new_list
# logger.debug('Active Menu %s', gameStateObj.activeMenu)
Engine.update_time()
# Get events
eventList = Engine.build_event_list()
# === UPDATE USER STATES ===
mapSurf, repeat = gameStateObj.stateMachine.update(eventList, gameStateObj, metaDataObj)
while repeat:
# We don't need to process the eventList more than once I think
mapSurf, repeat = gameStateObj.stateMachine.update([], gameStateObj, metaDataObj)
# Update global sprite counters
GC.PASSIVESPRITECOUNTER.update()
GC.ACTIVESPRITECOUNTER.update()
GC.CURSORSPRITECOUNTER.update()
# Update global music thread
Engine.music_thread.update(eventList)
new_size = (GC.WINWIDTH * cf.OPTIONS['Screen Size'], GC.WINHEIGHT * cf.OPTIONS['Screen Size'])
Engine.push_display(mapSurf, new_size, GC.DISPLAYSURF)
# Keep gameloop (update, renders, etc) ticking
Engine.update_display()
gameStateObj.playtime += GC.FPSCLOCK.tick(GC.FPS)
# === END OF MAIN GAME LOOP ===
def handle_debug_logs():
counter = 5 # Increments all old debug logs. Destroys ones older than 5 runs.
while counter > 0:
fp = ''.join(['./Saves/debug.log.', str(counter)])
if os.path.exists(fp):
if counter == 5:
os.remove(fp)
else:
os.rename(fp, ''.join(['./Saves/debug.log.', str(counter + 1)]))
counter -= 1
def inform_error():
print("=== === === === === ===")
print('Damn. Another bug :(')
print("Quick! Copy this error log and send it to rainlash!")
print('Or send the file "Saves/debug.log.1" to rainlash!')
print('Thank you!')
print("=== === === === === === \n")
# ____________________________________________________________________________#
# === START === START === START === START === START === START === START === #
if __name__ == '__main__':
import logging, traceback
logger = logging.getLogger(__name__)
try:
handle_debug_logs()
except WindowsError:
print("Error! Debug logs in use -- Another instance of this is already running!")
Engine.terminate()
if cf.OPTIONS['debug']:
my_level = logging.DEBUG
else:
my_level = logging.WARNING
logging.basicConfig(filename='./Saves/debug.log.1', filemode='w',
level=my_level, format='%(levelname)8s:%(module)20s: %(message)s')
try:
main()
except Exception as e:
logger.exception(e)
inform_error()
print('Main Crash {0}'.format(str(e)))
# Now print exception to screen
import time
time.sleep(0.5)
traceback.print_exc()
time.sleep(0.5)
Engine.final(crash=True)
inform_error()
if cf.OPTIONS['cheat']:
time.sleep(10)
else:
time.sleep(20)
# === END === END === END === END === END === END === END === END === END === #