-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
120 lines (103 loc) · 3.86 KB
/
util.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
import pygame
import codecs
import os
from locals import *
# Some general utility functions here
pygame.font.init()
# The Cosmetica font seemed to be broken... had trouble rendering small text
# with underscores (_) ... it's weird.
smallfont = pygame.font.Font("data/Vera.ttf", 14)
bigfont = pygame.font.Font("data/Vera.ttf", 24)
def get_config_path():
pathname = ""
try:
pathname = os.environ["HOME"] + "/.funnyboat"
except:
try:
pathname = os.environ["APPDATA"] + "/Funny Boat"
except:
print("Couldn't get environment variable for home directory")
pathname = "."
if not os.path.exists(pathname):
os.mkdir(pathname)
return pathname
def str_to_bool(text):
ltext = text.lower().strip()
if ltext in ("on", "true", "yes"):
return True
else:
return False
def load_config():
filename = get_config_path() + "/config"
if not os.path.exists(filename):
return
f = codecs.open(filename, "r", "utf_8")
for line in f.readlines():
parts = line.split("\t")
if len(parts) < 2:
continue
variable,value = parts
variable = variable.lower()
if variable == "alpha":
pass
# This is disabled because it's no longer in the in-game options
# and it seems to be acting up in weird ways, so it's better
# to just ditch it. The code is littered with it still, so I'm
# not completely removing the variable yet.
#Variables.alpha = str_to_bool(value)
elif variable == "particles":
Variables.particles = str_to_bool(value)
elif variable == "music":
Variables.music = str_to_bool(value)
elif variable == "name":
Variables.name = value.strip()[:32]
elif variable == "sound":
Variables.sound = str_to_bool(value)
elif variable == "aa":
Variables.aa = str_to_bool(value)
f.close()
def save_config():
filename = get_config_path() + "/config"
if not os.path.exists(get_config_path()):
os.mkdir(get_config_path())
f = codecs.open(filename, "w", "utf_8")
print("alpha\t%s" % Variables.alpha, file=f)
print("particles\t%s" % Variables.particles, file=f)
print("music\t%s" % Variables.music, file=f)
print("name\t%s" % Variables.name, file=f)
print("sound\t%s" % Variables.sound, file=f)
print("aa\t%s" % Variables.aa, file=f)
f.close()
def load_image(name):
image = pygame.image.load("data/" + name + ".png").convert_alpha()
if not Variables.alpha:
# this is kinda not useful. you can't even get Variables.alpha set
# without modifying the config file by hand
# in case someone does, at least it won't crash the game if they don't
# have Numeric installed
w,h = image.get_size()
for i in range(w):
for j in range(h):
pixel = image.get_at((i,j))
if pixel[3] > 127:
image.set_at((i,j), (pixel[0], pixel[1], pixel[2], 255))
else:
image.set_at((i,j), (pixel[0], pixel[1], pixel[2], 0))
#surfarray = pygame.surfarray.pixels_alpha(image)
#for i in range(len(surfarray)):
# for j in range(len(surfarray[i])):
# if surfarray[i][j] > 127:
# surfarray[i][j] = 255
# else:
# surfarray[i][j] = 0
return image
def load_sound(name):
return pygame.mixer.Sound("data/" + name + ".ogg")
def load_music(name):
# The all-caps ogg is because the original file just happened to be that way
pygame.mixer.music.load("data/" + name + ".ogg")
def rotate(surf, angle):
if Variables.aa:
return pygame.transform.rotozoom(surf, angle, 1.0)
else:
return pygame.transform.rotate(surf, angle)