This repository has been archived by the owner on Jul 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileUtils.py
100 lines (80 loc) · 2.8 KB
/
fileUtils.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
import os.path
import pygame
from colors import black
main_dir = os.path.split(os.path.abspath(__file__))[0]
class DummySound:
def play(self): pass
def load_leaderboard():
file_name = os.path.join(main_dir, 'data', 'leaderboard.txt')
file = open(file_name, 'r')
file_content = file.readlines()
file.close()
leaders_array = []
for item in file_content:
leaders_array.append(item.replace('\n', '').split(','))
return leaders_array
def load_image(file):
color_key = (255, 0, 255)
file = os.path.join(main_dir, 'data', file)
try:
surface = pygame.image.load(file)
if surface.get_alpha():
surface = surface.convert_alpha()
else:
surface = surface.convert()
surface.set_colorkey(color_key)
except pygame.error:
raise SystemExit('Could not load image "%s" %s' % (file, pygame.get_error()))
return surface
def load_images(*files):
images = []
for file in files:
images.append(load_image(file))
return images
def load_all_gfx(directory, colorkey=(255, 0, 255), accept=(".png", ".jpg", ".bmp")):
"""Load all graphics with extensions in the accept argument. If alpha
transparency is found in the image the image will be converted using
convert_alpha(). If no alpha transparency is detected image will be
converted using convert() and colorkey will be set to colorkey."""
graphics = {}
for pic in os.listdir(directory):
name, ext = os.path.splitext(pic)
if ext.lower() in accept:
img = pygame.image.load(os.path.join(directory, pic))
if img.get_alpha():
img = img.convert_alpha()
else:
img = img.convert()
img.set_colorkey(colorkey)
graphics[name] = img
return graphics
def load_sound(file):
if not pygame.mixer:
return DummySound()
file = os.path.join(main_dir, 'data', file)
try:
sound = pygame.mixer.Sound(file)
return sound
except pygame.error:
print('Warning, unable to load, %s' % file)
return DummySound()
def load_music(file):
if not pygame.mixer:
return DummySound()
file = os.path.join(main_dir, 'data', file)
try:
pygame.mixer.music.load(file)
except pygame.error:
print('Warning, unable to load, %s' % file)
def text_objects(text, font):
text_surface = font.render(text, True, black)
return text_surface, text_surface.get_rect()
"""
screen.fill(white)
largeText = pygame.font.Font('freesansbold.ttf', 115)
TextSurf, TextRect = text_objects("A bit Racey", largeText)
TextRect.center = ((DISPLAY_WIDTH / 2), (DISPLAY_HEIGHT / 2))
screen.blit(TextSurf, TextRect)
pygame.display.update()
clock.tick(15)
"""