Skip to content

Commit a381edc

Browse files
committed
used and implemented the location module
1 parent 686ccad commit a381edc

File tree

7 files changed

+19
-43
lines changed

7 files changed

+19
-43
lines changed

animations.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
import pygame
77

88
import animation
9+
import locations
910

10-
DIRECTORY = os.path.join('res', 'export')
1111

1212
_surfaces = {}
1313
class Surfaces(enum.Enum):
@@ -95,7 +95,7 @@ def get_filename(self):
9595
elif self == Surfaces.COFFEE_BROKEN: fn = 'coffee-broken.png'
9696
elif self == Surfaces.EXPLOSION: fn = 'bug-explode.png'
9797
else: raise Exception('Unknown Surface Type: {}'.format(str(self)))
98-
return os.path.join(DIRECTORY, fn)
98+
return locations.image(fn)
9999

100100
def get_surface(self):
101101
global _surfaces

bugreport.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
import build_info
1010

11+
import locations
12+
1113
# keep those trailing whites!!
1214
TEXT = """
1315
Sorry!
@@ -56,15 +58,15 @@
5658
def bugreport(exc_info=None) -> str:
5759
typ, value, frames = exc_info if exc_info is not None else sys.exc_info()
5860
trace = []
59-
path_to_strip = os.path.dirname(__file__)
61+
path_to_strip = locations.get_root()
6062
strip_path = lambda s: s[len(path_to_strip) + 1:] if s.startswith(path_to_strip) else s
6163
for frame in traceback.extract_tb(frames, 32):
6264
fn = strip_path(frame[0])
6365
ln = frame[1]
6466
fu = frame[2]
6567
tx = frame[3]
6668
t = '{}:{} {} {}'.format(fn, ln, fu, tx)
67-
trace.append(t)
69+
trace.insert(0, t)
6870

6971
def prepare(value):
7072
iterable = None
@@ -94,8 +96,7 @@ def prepare(value):
9496
system_system=prepare(system),
9597
system_command=prepare(' '.join(sys.argv)))
9698

97-
report_directory = os.path.expanduser('~')
98-
report_file = os.path.join(report_directory, 'coder-bug.txt')
99+
report_file = locations.user('bugreport.txt')
99100

100101
# with 'w' we override,... but i dont want to grow the file uncontrolled,
101102
# so maybe i rewrite it later to append,.. but then we need some kind of
@@ -112,7 +113,8 @@ def prepare(value):
112113
except:
113114
pass
114115

115-
print(report)
116+
if not getattr(sys, 'frozen', False):
117+
print(report)
116118

117119

118120
return report_file

coder.py

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,6 @@
1010
import splash
1111
import highscore
1212

13-
if not pygame.font: print ('Warning, fonts disabled')
14-
if not pygame.mixer: print ('Warning, sound disabled')
15-
16-
def load_image(name, colorkey=None):
17-
fullname = os.path.join('data', name)
18-
try:
19-
image = pygame.image.load(fullname)
20-
except pygame.error:
21-
print ('Cannot load image:', name)
22-
image = image.convert()
23-
if colorkey is not None:
24-
if colorkey is -1:
25-
colorkey = image.get_at((0,0))
26-
image.set_colorkey(colorkey, RLEACCEL)
27-
return image, image.get_rect()
28-
29-
def load_sound(name):
30-
class NoneSound:
31-
def play(self): pass
32-
if not pygame.mixer:
33-
return NoneSound()
34-
fullname = os.path.join('data', name)
35-
try:
36-
sound = pygame.mixer.Sound(fullname)
37-
except pygame.error:
38-
print ('Cannot load sound:', wav)
39-
return sound
4013

4114
def init():
4215
pygame.mixer.pre_init(44100, -16, 2, 4096)

highscore.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import quit_
1010

1111
import background
12+
import locations
1213

1314
from collections import namedtuple
1415

@@ -30,7 +31,7 @@ def __init__(self, new_entry: Entry = None) -> None:
3031
self._new_entry_font = pygame.font.Font('DejaVuSansMono-Bold.ttf', 20)
3132
self._entry_surfaces = None
3233
self._background = None
33-
self._filename = os.path.join(os.path.expanduser('~'), '.coder-game', 'highscore.dat')
34+
self._filename = locations.user('highscore.dat')
3435

3536
def _load(self):
3637
if os.path.exists(self._filename):
@@ -44,9 +45,6 @@ def _load(self):
4445
raise Exception('Unknown Highscore Data Version')
4546

4647
def _save(self):
47-
d = os.path.dirname(self._filename)
48-
if not os.path.exists(d):
49-
os.mkdir(d)
5048
with open(self._filename, 'wb') as f:
5149
data = {
5250
'version': 1,

quit_.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
import state_manager
33
import pygame
44
import os
5+
import locations
56

67
class Quit(state_manager.State):
78
def __init__(self) -> None:
89
super().__init__()
910
self._tick = 3000
1011
self._skip = False
11-
self._surface = pygame.image.load(os.path.join('res', 'export', 'quit.png')).convert_alpha()
12+
self._surface = pygame.image.load(locations.image('quit.png')).convert_alpha()
1213

1314

1415
def render(self) -> None:

sound.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from enum import auto
66

77
import pygame
8+
import locations
89

910
class Sounds(enum.Enum):
1011
MUSIC = auto()
@@ -27,7 +28,7 @@ def get_filename(self):
2728
elif self == Sounds.MENU_HOVER: fn = 'menu-hover.ogg'
2829
elif self == Sounds.MENU_SELECT: fn = 'menu-select.ogg'
2930
else: raise Exception('Unknown Sound: {}'.format(str(self)))
30-
return os.path.join('res', 'sounds', fn)
31+
return locations.sound(fn)
3132

3233
_sounds = None
3334
class Sound(object):

splash.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import quit_
1010
import about
1111
import consts
12+
import locations
1213

1314
import pygame
1415

@@ -21,7 +22,7 @@ def __init__(self) -> None:
2122
self._tick = 2500
2223
self._idx = 0
2324
self._splashs_loaded = False
24-
self._surfaces = [pygame.image.load(os.path.join('res', 'export', 'metagamejam-splash.png')).convert_alpha()]
25+
self._surfaces = [pygame.image.load(locations.image('metagamejam-splash.png')).convert_alpha()]
2526

2627

2728

@@ -48,8 +49,8 @@ def update(self, delta: int, fps: float) -> None:
4849
if self._was_rendered:
4950
if not self._splashs_loaded:
5051
self._surfaces += [
51-
pygame.image.load(os.path.join('res', 'export', 'coder-splash.png')).convert_alpha(),
52-
pygame.image.load(os.path.join('res', 'export', 'thinkdownstairs-splash.png')).convert_alpha()]
52+
pygame.image.load(locations.image('coder-splash.png')).convert_alpha(),
53+
pygame.image.load(locations.image('thinkdownstairs-splash.png')).convert_alpha()]
5354
self._splashs_loaded = True
5455
return
5556
if self._initialized:

0 commit comments

Comments
 (0)