Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

๐Ÿ“š The CoCalc Library - books, templates and other resources

132928 views
License: OTHER
1
#!/usr/bin/env python
2
#
3
# Copyright 2019 the original author or authors.
4
#
5
# Licensed under the Apache License, Version 2.0 (the "License");
6
# you may not use this file except in compliance with the License.
7
# You may obtain a copy of the License at
8
#
9
# http://www.apache.org/licenses/LICENSE-2.0
10
#
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS,
13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
# See the License for the specific language governing permissions and
15
# limitations under the License.
16
#
17
import os
18
19
import pygame
20
from pygame.compat import geterror
21
from pygame.constants import RLEACCEL
22
23
main_dir = os.path.split(os.path.abspath(__file__))[0]
24
# main_dir = 'vqe_playground/utils/data'
25
data_dir = os.path.join(main_dir, 'data')
26
# data_dir = 'vqe_playground/utils/data/'
27
28
def load_image(name, colorkey=None):
29
fullname = os.path.join(data_dir, name)
30
# fullname = data_dir + name
31
# รŸprint('fullname:', fullname)
32
try:
33
image = pygame.image.load(fullname)
34
except pygame.error:
35
print ('Cannot load image!:', fullname)
36
raise SystemExit(str(geterror()))
37
image = image.convert()
38
if colorkey is not None:
39
if colorkey is -1:
40
colorkey = image.get_at((0,0))
41
image.set_colorkey(colorkey, RLEACCEL)
42
return image, image.get_rect()
43
44
45
def load_mem_image(buf, colorkey=None):
46
try:
47
buf.seek(0)
48
image = pygame.image.load(buf)
49
except pygame.error:
50
print ('Cannot load mem image!:', buf)
51
raise SystemExit(str(geterror()))
52
image = image.convert()
53
if colorkey is not None:
54
if colorkey is -1:
55
colorkey = image.get_at((0,0))
56
image.set_colorkey(colorkey, RLEACCEL)
57
return image, image.get_rect()
58
59
60
def load_sound(name):
61
class NoneSound:
62
def play(self): pass
63
if not pygame.mixer or not pygame.mixer.get_init():
64
return NoneSound()
65
fullname = os.path.join(data_dir, name)
66
try:
67
sound = pygame.mixer.Sound(fullname)
68
except pygame.error:
69
print ('Cannot load sound: %s' % fullname)
70
raise SystemExit(str(geterror()))
71
return sound
72
73
74