def test_filmstrip_with2by3_returnsSixFrames(self): spritesheet = SpriteSheet(make_pixel_grid(5, 2), 5, 2) filmstrip = spritesheet.filmstrip(0, 10) self.assertEqual(10, len(filmstrip)) for i, frame in enumerate(filmstrip): r, g, b, alpha = frame.get_at((0, 0)) self.assertEqual(i, r) self.assertEqual((1, 1), frame.get_size())
def test_filmstrip_withSubArea_returnsSubarea(self): sub_area = (2, 0, 2, 2) image = make_pixel_grid(5, 2) spritesheet = SpriteSheet(image, 5, 2, sub_area) filmstrip = spritesheet.filmstrip(0, 4) self.assertEqual(4, len(filmstrip)) expected = [2, 3, 7, 8] for i, frame in enumerate(filmstrip): r, g, b, alpha = frame.get_at((0, 0)) self.assertEqual(expected[i], r) self.assertEqual((1, 1), frame.get_size())
def make_small_impact(x, y): image = game_assets.load_image("small_short_effect_hit.png") sprite_sheet = SpriteSheet(image, columns=16, rows=1, sub_area=(0, 0, 3, 1)) return VisualEffect(x, y, sprite_sheet, reverse=True)
def extract_filmstrips(sprite_sheet: SpriteSheet): filmstrips = SimpleNamespace() filmstrip = sprite_sheet.filmstrip() step = len(filmstrip) // 4 if not step: filmstrips.walk_south = filmstrips.walk_west = \ filmstrips.walk_east = filmstrips.walk_north = filmstrip else: n = 0 filmstrips.walk_south = filmstrip[n:n + step] n += step filmstrips.walk_west = filmstrip[n:n + step] n += step filmstrips.walk_east = filmstrip[n:n + step] n += step filmstrips.walk_north = filmstrip[n:n + step] return filmstrips
class SceneryType: def __init__(self, sprite_sheet, is_solid=False, animated=True): self.sprite_sheet = sprite_sheet self.is_solid = is_solid self.animated = animated wood_sheet2 = game_assets.load_image("wood-sheet2.png") liquids = game_assets.load_image("liquids32.png") vegetation = game_assets.load_image("vegetation.png") lava = game_assets.load_image("lava.png") scenery_types = { "#": SceneryType(SpriteSheet(wood_sheet2, columns=8, rows=16, sub_area=(7, 3, 1, 1)), is_solid=True), "~": SceneryType( SpriteSheet(liquids, columns=16, rows=12, sub_area=(0, 0, 6, 1))), ".": SceneryType(SpriteSheet(lava, columns=10, rows=1)), "_": SceneryType(SpriteSheet(wood_sheet2, columns=8, rows=16, sub_area=(0, 4, 1, 2)), animated=False), ",": SceneryType(SpriteSheet(wood_sheet2,
def import_tiles(self, filename, columns, rows): image = pygame.image.load(game_assets.image_file(filename)) terrain_sheet = SpriteSheet(image, columns, rows) self.tiles.extend(terrain_sheet.filmstrip())
def _make_sprite_sheet(filename, sheet_dimensions, sub_area=None): image = load_image(filename) columns, rows = sheet_dimensions return SpriteSheet(image, columns=columns, rows=rows, sub_area=sub_area)
def play(): pygame.mixer.pre_init(frequency=44100) pygame.init() pygame.mixer.init(frequency=44100) screen = pygame.display.set_mode( (SCREEN_WIDTH, SCREEN_HEIGHT)) #, pygame.FULLSCREEN) background = screen.copy() intro.play(screen) clock = pygame.time.Clock() tile_manager = tiles.TileManager() tile_manager.import_tiles("terrain.png", 8, 16) game_map = tiles.TileMap(tile_manager.tiles[7]) world = interfaces.SpriteGroups() floorplan.create_objects(0, world, game_map) game_map.render(background, 0, 0, SCREEN_WIDTH // game_map.tile_width, SCREEN_HEIGHT // game_map.tile_height) screen.blit(background, (0, 0)) player_character = Character(PlayerCharacterType.TOBY) player = Player(500, 500, player_character, world) move_to_nearest_empty_space(player, world, 100) world.all.add(player) world.solid.add(player) make_monster(MonsterType.ZOMBIE_GENERATOR, 200, randint(0, screen.get_height()), world) make_monster(MonsterType.ZOMBIE_GENERATOR, 800, randint(0, screen.get_height()), world) heart_sheet = SpriteSheet(pygame.image.load(image_file("heart.png")), 1, 1) health_bar = ScoreBar(30, 30, heart_sheet, 100, 10, scale=0.1) world.hud.add(health_bar) player.add_observer(health_bar, "vitality") while True: world.all.clear(screen, background) world.hud.clear(screen, background) world.all.update() for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: return if event.type == pygame.KEYDOWN and event.key == pygame.K_F12: debug_filmstrips(screen, world.all) screen.blit(background, (0, 0)) kb = pygame.key.get_pressed() player.handle_keyboard(kb) if kb[pygame.K_RETURN]: player.shoot() player.move(world.solid) for monster in world.monster: monster.notify(player) monster.move(world.solid) monster.do_actions(world) for missile in world.missile: missile.move(world.solid) if out_of_bounds(missile): missile.kill() missile_hits = pygame.sprite.groupcollide(world.solid, world.missile, False, False) for hit, missiles in missile_hits.items(): for m in missiles: m.on_impact(hit, world) pick_ups = pygame.sprite.spritecollide( player, world.items, dokill=True, collided=pygame.sprite.collide_mask) for item in pick_ups: item.on_pick_up(player) world.all.draw(screen) world.hud.draw(screen) pygame.display.flip() clock.tick(20)
def use_spritesheet(self, sprite_sheet: SpriteSheet): self.filmstrips.walk_south = sprite_sheet.filmstrip(0, 3) self.filmstrips.walk_west = sprite_sheet.filmstrip(3, 3) self.filmstrips.walk_east = sprite_sheet.filmstrip(6, 3) self.filmstrips.walk_north = sprite_sheet.filmstrip(9, 3) return self.filmstrips
def test_sub_area_indices_withSubArea_returnsOnlySubAreaIndices(self): sub_area = (2, 0, 2, 2) spritesheet = SpriteSheet(make_pixel_grid(5, 2), 5, 2, sub_area) result = spritesheet.sub_area_indices() self.assertEqual([2, 3, 7, 8], result)
def test_surface_by_index_returnsSurface(self): spritesheet = SpriteSheet(make_pixel_grid(5, 2), 5, 2) for i in range(10): surface = spritesheet.surface_by_index(i) r, g, b, alpha = surface.get_at((0, 0)) self.assertEqual(i, r)