๐ The CoCalc Library - books, templates and other resources
License: OTHER
#!/usr/bin/env python1#2# Copyright 2019 the original author or authors.3#4# Licensed under the Apache License, Version 2.0 (the "License");5# you may not use this file except in compliance with the License.6# You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing, software11# distributed under the License is distributed on an "AS IS" BASIS,12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13# See the License for the specific language governing permissions and14# limitations under the License.15#16import os1718import pygame19from pygame.compat import geterror20from pygame.constants import RLEACCEL2122main_dir = os.path.split(os.path.abspath(__file__))[0]23# main_dir = 'vqe_playground/utils/data'24data_dir = os.path.join(main_dir, 'data')25# data_dir = 'vqe_playground/utils/data/'2627def load_image(name, colorkey=None):28fullname = os.path.join(data_dir, name)29# fullname = data_dir + name30# รprint('fullname:', fullname)31try:32image = pygame.image.load(fullname)33except pygame.error:34print ('Cannot load image!:', fullname)35raise SystemExit(str(geterror()))36image = image.convert()37if colorkey is not None:38if colorkey is -1:39colorkey = image.get_at((0,0))40image.set_colorkey(colorkey, RLEACCEL)41return image, image.get_rect()424344def load_mem_image(buf, colorkey=None):45try:46buf.seek(0)47image = pygame.image.load(buf)48except pygame.error:49print ('Cannot load mem image!:', buf)50raise SystemExit(str(geterror()))51image = image.convert()52if colorkey is not None:53if colorkey is -1:54colorkey = image.get_at((0,0))55image.set_colorkey(colorkey, RLEACCEL)56return image, image.get_rect()575859def load_sound(name):60class NoneSound:61def play(self): pass62if not pygame.mixer or not pygame.mixer.get_init():63return NoneSound()64fullname = os.path.join(data_dir, name)65try:66sound = pygame.mixer.Sound(fullname)67except pygame.error:68print ('Cannot load sound: %s' % fullname)69raise SystemExit(str(geterror()))70return sound71727374