-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileReader.py
More file actions
59 lines (53 loc) · 1.83 KB
/
fileReader.py
File metadata and controls
59 lines (53 loc) · 1.83 KB
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
from definitions import *
import os
"""
This module handles reading and writing to savefiles
Calls definitions to change game settings
"""
import re
def make_new_save_file():
"""
Called if there is no save file present, for whatever reason.
Just make the new file, set settings to default
:return: None
"""
with open('savefile.txt', 'w') as file:
file_xRes = 320
file.write("xRes = %d\n" % file_xRes)
file_yRes = 240
file.write('yRes = %d\n' % file_yRes)
fullscreen = 0
file.write(('fullscreen = %d\n' % fullscreen))
def read_game_settings():
"""
Only really called on game startup - reads the game settings from last time.
:return: None
"""
global WIDTH, HEIGHT, fullscreen
if os.path.isfile('savefile.txt'):
with open('savefile.txt', 'r') as file:
for line in file:
match = re.match(r"(\w+) = (\d+)", line)
if match:
if "xRes" in match.group(1):
WIDTH = int(match.group(2))
if "yRes" in match.group(1):
HEIGHT = int(match.group(2))
if "fullscreen" in match.group(1):
if '0' in match.group(2):
fullscreen = False
elif '1' in match.group(2):
fullscreen = True
else:
print("Invalid fullscreen read.")
else:
make_new_save_file()
def save_game_settings():
"""
Save all the settings, no point only saving one bit that might have changed
:return:
"""
with open('savefile.txt', 'w') as file:
file.write("xRes = %d\n" % WIDTH)
file.write('yRes = %d\n' % HEIGHT)
file.write(('fullscreen = %d\n' % fullscreen))