forked from venom443/SpaceShip-Starwars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
69 lines (54 loc) · 1.96 KB
/
utils.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
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
# Copyright (C) 2019 Andrés Segovia
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import pygame
def load_image(path, use_transparency=False, rect_img=(0, 0)):
width, height = rect_img
# Loading image
try:
image = pygame.image.load(path)
except (pygame.error):
print("Could not load the image: ", path)
raise (SystemExit)
# Verify if we add transparency
if use_transparency:
image = image.convert()
else:
image = image.convert_alpha()
# Scale the image to the specified size, if required
if width >= 1 and height >= 1:
image = scale_image(image, (width, height))
else: # There will be no changes, it will keep its original size
pass
return image
def scale_image(image, size_required):
scaled_img = pygame.transform.scale(image, size_required)
return scaled_img
def load_sound(filename, sound_lvl=1.0):
class WithoutSound:
def play(self):
pass
if not pygame.mixer or not pygame.mixer.get_init():
return WithoutSound()
path = os.path.join('data', 'sounds', filename)
try:
sound = pygame.mixer.Sound(path)
except (pygame.error):
print("Could not load the sound: ", path)
raise (SystemExit)
sound.set_volume(sound_lvl) # Setting the sound volume
return sound