def fetch_images(texture, width, height, spacing=0, margin=0, by_count=False): ''' Returns an image list generated from a given texture and either the image size or the number of images in a sprite sheet. :param textue: texture to fetch images from :param width: width of images, or number of columns if by_count=True :param height: height of images, or number of rows if by_count=True :param spacing: space between each image in the texture :param margin: margin of empty space around the edge of texture :param by_count: set True to use width and height value to calculate frame size from width and height of spritesheet :rvalue: list of pygame._sdl2.video.Image objects ''' tex_width, tex_height = texture.get_rect().size if type(texture) == Image: marginx = margin + image.srcrect.x marginy = margin + image.srcrect.y else: marginx = marginy = margin if by_count: width = tex_width // width height = tex_height // height r = pg.Rect(0, 0, width, height) tiles = [] for y in range(margin, tex_height + margin - height + 1, height + spacing): for x in range(margin, tex_width + margin - width + 1, width + spacing): r.x = x r.y = y im = Image(texture) im.srcrect = r.copy() tiles.append(im) return tiles
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 sprite = Something(Image(tex, (0, 0, tex.width / 2, tex.height / 2))) sprite.rect.x = 250 sprite.rect.y = 50 # sprite2 = Something(Image(sprite.image)) sprite2 = Something(Image(tex)) sprite2.rect.x = 250 sprite2.rect.y = 250 sprite2.rect.w /= 2 sprite2.rect.h /= 2 group = pg.sprite.Group() group.add(sprite2) group.add(sprite) import math
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) tex2 = Texture.from_surface(renderer2, load_img("asprite.bmp")) renderer2.clear() tex2.draw() renderer2.present() del tex2 full = 0 tex = Image(tex) surf = pg.Surface((64, 64)) streamtex = Texture(renderer, (64, 64), streaming=True) tex_update_interval = 1000 next_tex_update = pg.time.get_ticks() while running: for event in pg.event.get(): if event.type == pg.QUIT: running = False elif getattr(event, "window", None) == win2: if (event.type == pg.KEYDOWN and event.key == pg.K_ESCAPE or event.type == pg.WINDOWEVENT and event.event == pg.WINDOWEVENT_CLOSE): win2.destroy()
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) tex2 = Texture.from_surface(renderer2, load_img('asprite.bmp')) renderer2.clear() tex2.draw() renderer2.present() del tex2 full = 0 tex = Image(tex, (0, 0, tex.width, tex.height)) while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif getattr(event, 'window', None) == win2: if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE or\ event.type == pygame.WINDOWEVENT and event.event == pygame.WINDOWEVENT_CLOSE: win2.destroy() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: running = False elif event.key == pygame.K_LEFT: x -= 5 elif event.key == pygame.K_RIGHT: