def __init__(self, renderer, filename, size): ''' Initialize TextureFont for use with pygame._sdl2 GPU renderer :param renderer: pygame._sdl2.video.Renderer to draw on :param filename: path to a pygame.font.Font compatible file (ttf) :param size: point size for font ''' font = pg.font.Font(filename, size) self.cmap = {} self.renderer = renderer self.height = font.get_height() tot = 0 for c in char_map: tot += font.size(c)[0] surf = pg.surface.Surface((tot, font.get_height()), flags=pg.SRCALPHA) self.height = font.get_height() tot = 0 for c in char_map: rend = font.render(c, 1, (255, 255, 255)) wi = rend.get_width() surf.blit(rend, (tot, 0)) self.cmap[c] = pg.Rect(tot, 0, wi, self.height) tot += wi self.blank = self.cmap[' '] self.texture = Texture.from_surface(renderer, surf)
def pygame_sd2_image_loader(renderer: Renderer, filename: str, colorkey, **kwargs): """ pytmx image loader for pygame """ image = pygame.image.load(filename) parent_rect = image.get_rect() texture = Texture.from_surface(renderer, image) def load_image(rect=None, flags=None) -> PygameSDL2Tile: if rect: assert parent_rect.contains(rect) else: rect = parent_rect angle, flipx, flipy = handle_flags(flags) rect = Rect(*rect) size = rect.size return PygameSDL2Tile( texture=texture, srcrect=rect, size=size, angle=angle, center=None, flipx=flipx, flipy=flipy, ) return load_image
def load_texture(renderer, filename): ''' Returns an texture loaded from given image file and attached to given renderer. :param renderer: active pygame._sdl2.video.Renderer object :param filename: path to image file :rvalue: texture object ''' try: surf = pg.image.load(str(filename)) except: raise FileNotFoundError('cannot open: ', filename) return Texture.from_surface(renderer, surf)
pygame.key.set_repeat(1000, 10) for driver in get_drivers(): print(driver) import random answer = messagebox("I will open two windows! Continue?", "Hello!", info=True, buttons=('Yes', 'No', 'Chance'), return_button=0, escape_button=1) if answer == 1 or (answer == 2 and random.random() < .5): import sys sys.exit(0) win = Window('asdf', resizable=True) renderer = Renderer(win) tex = Texture.from_surface(renderer, load_img('alien1.gif')) running = True x, y = 250, 50 clock = pygame.time.Clock() backgrounds = [(255,0,0,255), (0,255,0,255), (0,0,255,255)] bg_index = 0 renderer.draw_color = backgrounds[bg_index] win2 = Window('2nd window', size=(256, 256), always_on_top=True) win2.opacity = 0.5 win2.set_icon(load_img('bomb.gif')) renderer2 = Renderer(win2)
raise SystemExit("This example requires pygame 2 and SDL2.") from pygame._sdl2 import Window, Texture, Image, Renderer data_dir = os.path.join(os.path.split(os.path.abspath(__file__))[0], "data") def load_img(file): return pg.image.load(os.path.join(data_dir, file)) pg.display.init() pg.key.set_repeat(10, 10) win = Window("asdf", resizable=True) renderer = Renderer(win) tex = Texture.from_surface(renderer, load_img("alien1.gif")) class Something(pg.sprite.Sprite): def __init__(self, img): pg.sprite.Sprite.__init__(self) self.rect = img.get_rect() self.image = img self.rect.w *= 5 self.rect.h *= 5 img.origin = self.rect.w / 2, self.rect.h / 2
answer = messagebox( "I will open two windows! Continue?", "Hello!", info=True, buttons=("Yes", "No", "Chance"), return_button=0, escape_button=1, ) if answer == 1 or (answer == 2 and random.random() < 0.5): import sys sys.exit(0) win = Window("asdf", resizable=True) renderer = Renderer(win) tex = Texture.from_surface(renderer, load_img("alien1.gif")) running = True x, y = 250, 50 clock = pg.time.Clock() backgrounds = [(255, 0, 0, 255), (0, 255, 0, 255), (0, 0, 255, 255)] bg_index = 0 renderer.draw_color = backgrounds[bg_index] win2 = Window("2nd window", size=(256, 256), always_on_top=True) win2.opacity = 0.5 win2.set_icon(load_img("bomb.gif")) renderer2 = Renderer(win2)