def _load_images(self, file): images = {} for image_name in os.listdir(file): if image_name == "frame.png": image = SpriteSheet(f"{file}\\{image_name}") image_names = [ "topleft", "top", "topright", "left", "center", "right", "bottomleft", "bottom", "bottomright" ] images["frame"] = dict( zip(image_names, image.load_grid((0, 0, 16, 16), 3, 3, -1))) elif image_name == "slider.png": image = SpriteSheet(f"{file}\\{image_name}") image_names = ["left", "line", "right", "slider"] images["slider"] = dict( zip(image_names, image.load_strip((0, 0, 8, 8), 4, -1))) elif image_name == "checkbox.png": image = SpriteSheet(f"{file}\\{image_name}") image_names = ["unchecked", "checked"] images["checkbox"] = dict( zip(image_names, image.load_strip((0, 0, 10, 10), 2, -1))) elif image_name == "arrows.png": image = SpriteSheet(f"{file}\\{image_name}") image_names = ["left", "right"] images["arrows"] = dict( zip(image_names, image.load_strip((0, 0, 8, 7), 2, -1))) else: images[re.match(r"[^.]+", image_name).group()] = pygame.image.load( f"{file}\\{image_name}").convert_alpha() return images
def __init__(self): """ Constructor """ super().__init__() #------ Attributes # Speed of the player self.change_x = 0 self.change_y = 0 #This list holds images for our player self.walking_frames_l = [] self.walking_frames_r = [] #Direction of walking self.direction = "R" sprite_sheet = SpriteSheet("walk_sheet2.png") #Load all the left facing images into a list self.walking_frames_l = sprite_sheet.load_strip([0, 0, 50, 62], 4, constants.WHITE) #Load all the left facing images then flip them right for image in self.walking_frames_l: image = pygame.transform.flip(image, True, False) self.walking_frames_r.append(image) #Set the image tge player starts with self.image = self.walking_frames_r[0] #Set a reference to the image rect self.rect = self.image.get_rect()
class Dust3(Prop): def __init__(self, x, y): super().__init__() self.filename = "props\\dust3.png" self.spritesheet = SpriteSheet(self.filename) self.start_rect = (0, 0, 5, 61) self.x, self.y, self.width, self.height = x, y, 5, 61 self.interval = 6 self.frequency = 2000 self.images = self.spritesheet.load_strip(self.start_rect, 25, -1) def blitme(self, screen): if random.randint(0, self.frequency) < 10 or self.blit_counter > 1: if self.blit_counter >= self.interval * len(self.images): self.blit_counter = 0 screen.blit(self.images[self.blit_counter // self.interval], self.rect) self.blit_counter += 1
class Bonfire(Prop): def __init__(self): super().__init__() self.filename = "props\\Bonfire.png" self.spritesheet = SpriteSheet(self.filename) self.start_rect = (0, 0, 32, 32) self.x, self.y, self.width, self.height = 160, 298, 32, 32 self.interval = 5 self.images = self.spritesheet.load_strip(self.start_rect, 3, -1) def blitme(self, screen): if self.blit_counter >= len(self.images) * self.interval: self.blit_counter = 0 screen.blit(self.images[self.blit_counter // self.interval], self.rect, special_flags=pygame.BLEND_RGBA_ADD) self.blit_counter += 1
class Stork(pygame.sprite.Sprite): def __init__(self, game): super().__init__() self.creature = 'stork' self.direction = random.randint(0, 1) self.creature_state = 0. self.game = game self.creature_surf_size = CREATURE_SPEC_LIST.get(self.creature)[0] self.surf = pygame.Surface(self.creature_surf_size) self.rect = self.surf.get_rect() self.num_anim = 10 self.first_hit_y = self.game.map.start_pos[1] self.pos = self.game.vec(0, 0) self.vel = self.game.vec(0, 0) self.animate() self.vel.x = 3 self.pos.x = 0 self.pos.y = 150 def move(self): self.animate() self.creature_state += 0.3 if self.creature_state >= self.num_anim: self.creature_state = 0. self.pos += self.vel self.rect.bottomright = self.pos def animate(self): self.num_anim = 10 image_path = os.path.join('bin', 'resources', 'creatures', f'stork_flying_right.png') self.player_ss = SpriteSheet(image_path) rect = (0, 0, 200, 180) self.player_frames = self.player_ss.load_strip(rect, self.num_anim, colorkey=(255, 255, 255)) self.image = self.player_frames[int( self.creature_state)] # Updates rect def draw(self, surface): surface.blit(self.image, self.rect)
class Grammofon(Prop): def __init__(self): super().__init__() self.filename = "props\\grammofon.png" self.spritesheet = SpriteSheet(self.filename) self.start_rect = (0, 0, 15, 25) self.x, self.y, self.width, self.height = 10, 278, 15, 25 self.interval = 6 self.images = self.spritesheet.load_strip(self.start_rect, 2, -1)
class Kettle(Prop): def __init__(self): super().__init__() self.filename = "props\\kettle.png" self.spritesheet = SpriteSheet(self.filename) self.start_rect = (0, 0, 28, 24) self.x, self.y, self.width, self.height = 442, 64, 28, 24 self.interval = 6 self.images = self.spritesheet.load_strip(self.start_rect, 3, -1)
class OvenFire(Prop): def __init__(self): super().__init__() self.filename = "props\\ovenfire.png" self.spritesheet = SpriteSheet(self.filename) self.start_rect = (0, 0, 32, 32) self.x, self.y, self.width, self.height = 400, 82, 32, 32 self.interval = 6 self.images = self.spritesheet.load_strip(self.start_rect, 3, -1)
class SewerDrop(Prop): def __init__(self): super().__init__() self.filename = "props\\SewerDrop.png" self.spritesheet = SpriteSheet(self.filename) self.start_rect = (0, 0, 16, 48) self.x, self.y, self.width, self.height = 340, 160, 16, 48 self.interval = 6 self.images = self.spritesheet.load_strip(self.start_rect, 10, -1)
class Mom(pygame.sprite.Sprite): def __init__(self, game): super().__init__() self.creature = 'mom' self.direction = random.randint(0, 1) self.creature_state = 0. self.game = game self.creature_surf_size = CREATURE_SPEC_LIST.get(self.creature)[0] self.surf = pygame.Surface(self.creature_surf_size) self.rect = self.surf.get_rect() self.first_hit_y = self.game.map.start_pos[1] self.pos = self.game.vec(0, 0) self.vel = self.game.vec(0, 0) self.acc = self.game.vec(0, 0) self.animate() self.vel.x = 1 self.pos.x = 100 self.pos.y = 510 def move(self): self.acc = self.game.vec(0, 0.5) self.animate() self.creature_state += 0.15 if self.creature_state >= self.num_anim: self.creature_state = 0. self.acc.x += self.vel.x * self.game.FRIC self.vel += self.acc self.pos += self.vel + 0.5 * self.acc self.rect.bottomright = self.pos def maskcollide(self, ground_collection): for ground in ground_collection: collision_point = pygame.sprite.collide_mask(ground, self) if collision_point and ground.mask.get_at(collision_point) > 0: self.current_ground = ground return collision_point def animate(self): self.acc.x += self.game.ACC self.num_anim = 4 image_path = os.path.join('bin', 'resources', 'creatures', f'mom_walking_transparent.png') self.player_ss = SpriteSheet(image_path) rect = (0, 0, 130, 150) self.player_frames = self.player_ss.load_strip(rect, self.num_anim) self.image = self.player_frames[int(self.creature_state)] self.rect = self.surf.get_rect() def update(self): hits_2 = self.maskcollide(self.game.ground) if self.vel.y > 0.5: if hits_2: new_ys = [ y for x, y in self.current_ground.coords if x == int(self.pos.x) ] if new_ys and min(new_ys) < self.pos.y + 5: self.vel.y = 0 if self.first_hit_y >= self.pos.y - 1 and self.first_hit_y <= self.pos.y: self.pos.y = self.first_hit_y else: self.first_hit_y = self.pos.y elif hits_2 and self.vel.y >= 0: new_ys = [ y for x, y in self.current_ground.coords if x == int(self.pos.x) ] if new_ys and min(new_ys) < self.pos.y + 5: self.pos.y = min(new_ys) self.last_y = self.pos.y self.vel.y = 0 elif new_ys is None: self.pos.y = self.last_y self.vel.y = 0 def draw(self, surface): surface.blit(self.image, self.rect)
class Isabella(pygame.sprite.Sprite): def __init__(self, game): super().__init__() self.creature = 'isabella' self.direction = 1 self.creature_state = 0. self.game = game self.creature_surf_size = CREATURE_SPEC_LIST.get(self.creature)[0] self.surf = pygame.Surface(self.creature_surf_size) self.rect = self.surf.get_rect() self.first_hit_y = self.game.map.start_pos[1] self.pos = self.game.vec(0, 0) self.vel = self.game.vec(0, 0) self.acc = self.game.vec(0, 0) self.animate() self.vel.x = 0.5 self.pos.x = 100 self.pos.y = 510 def move(self): self.acc = self.game.vec(0, 0.5) self.animate() self.creature_state += 0.3 if self.creature_state >= self.num_anim: self.creature_state = 0. if (self.pos.x > 400) and (self.game.player.pos.x < 400): self.direction = 0 else: if self.game.player.pos.x > self.pos.x: self.direction = 1 self.pos += self.vel elif self.game.player.pos.x < self.pos.x and self.pos.x < 400: self.direction = 1 self.pos += self.vel else: self.direction = 0 self.rect.bottomright = self.pos def animate(self): if self.direction == 1: self.acc.x += self.game.ACC self.num_anim = 10 image_path = os.path.join('bin', 'resources', 'creatures', f'isabella_crawling_right.png') else: image_path = os.path.join('bin', 'resources', 'creatures', f'isabella_crawling_left.png') self.player_ss = SpriteSheet(image_path) rect = (0, 0, 70, 50) self.player_frames = self.player_ss.load_strip(rect, self.num_anim) if self.direction == 1: self.image = self.player_frames[int(self.creature_state)] else: self.image = self.player_frames[1] self.rect = self.surf.get_rect() def draw(self, surface): surface.blit(self.image, self.rect)
class Creature(pygame.sprite.Sprite): def __init__(self, game, creature): super().__init__() self.creature = creature self.direction = random.randint(0, 1) self.creature_state = 0. self.game = game self.creature_surf_size = CREATURE_SPEC_LIST.get(self.creature)[0] self.surf = pygame.Surface(self.creature_surf_size) self.rect = self.surf.get_rect() self.first_hit_y = self.game.map.start_pos[1] self.pos = self.game.vec(0, 0) self.vel = self.game.vec(0, 0) self.acc = self.game.vec(0, 0) self.animate() self.vel.x = 1 if self.direction == 0: self.pos.x = random.randint(100, 600) self.pos.y = 510 if self.direction == 1: self.pos.x = random.randint(600, 900) self.pos.y = 510 def move(self): self.acc = self.game.vec(0, 0.5) # Causes the enemy to change directions upon reaching the end of screen if self.pos.x >= (self.game.screen_width - 20): self.direction = 1 elif self.pos.x <= self.creature_surf_size[0]: self.direction = 0 self.animate() self.creature_state += 0.15 if self.creature_state >= self.num_anim: self.creature_state = 0. self.acc.x += self.vel.x * self.game.FRIC self.vel += self.acc self.pos += self.vel + 0.5 * self.acc self.rect.bottomright = self.pos def maskcollide(self, ground_collection): for ground in ground_collection: collision_point = pygame.sprite.collide_mask(ground, self) if collision_point and ground.mask.get_at(collision_point) > 0: self.current_ground = ground return collision_point def animate(self): self.num_anim = 3 if self.direction == 0: self.acc.x += self.game.ACC image_path = os.path.join('bin', 'resources', 'creatures', f'{self.creature}_walking_right.png') else: self.acc.x += -self.game.ACC image_path = os.path.join('bin', 'resources', 'creatures', f'{self.creature}_walking_left.png') self.player_ss = SpriteSheet(image_path) rect = (0, 0, CREATURE_SPEC_LIST.get(self.creature)[0][0], CREATURE_SPEC_LIST.get(self.creature)[0][1]) self.player_frames = self.player_ss.load_strip(rect, self.num_anim) self.image = self.player_frames[int(self.creature_state)] def update(self): hits_2 = self.maskcollide(self.game.ground) if self.vel.y > 0.5: if hits_2: new_ys = [ y for x, y in self.current_ground.coords if x == int(self.pos.x) ] if new_ys and min(new_ys) < self.pos.y + 5: self.vel.y = 0 if self.first_hit_y >= self.pos.y - 1 and self.first_hit_y <= self.pos.y: self.pos.y = self.first_hit_y else: self.first_hit_y = self.pos.y elif hits_2 and self.vel.y >= 0: new_ys = [ y for x, y in self.current_ground.coords if x == int(self.pos.x) ] if new_ys and min(new_ys) < self.pos.y + 5: self.pos.y = min(new_ys) self.last_y = self.pos.y self.vel.y = 0 elif new_ys is None: self.pos.y = self.last_y self.vel.y = 0 def draw(self, surface): surface.blit(self.image, self.rect)
class Dad(pygame.sprite.Sprite): def __init__(self, game): super().__init__() self.creature = 'dad' self.creature_state = 0. self.direction = 1 self.game = game self.creature_surf_size = CREATURE_SPEC_LIST.get(self.creature)[0] self.surf = pygame.Surface(self.creature_surf_size) self.rect = self.surf.get_rect() self.num_anim = 12 self.pos = self.game.vec(0, 0) self.vel = self.game.vec(0, 0) self.animate() self.vel.x = 2 self.pos.x = 1024 self.pos.y = 510 def move(self): self.animate() # Causes the enemy to change directions upon reaching the end of screen if self.pos.x >= (self.game.screen_width - 20): self.direction = 1 elif self.pos.x <= self.creature_surf_size[0]: self.direction = 0 self.creature_state += 0.3 if self.creature_state >= self.num_anim: self.creature_state = 0. if self.direction == 1: self.pos -= self.vel else: self.pos += self.vel self.rect.bottomright = self.pos def animate(self): self.num_anim = 12 if self.direction == 0: image_path = os.path.join('bin', 'resources', 'creatures', f'{self.creature}_walking_right.png') else: image_path = os.path.join('bin', 'resources', 'creatures', f'{self.creature}_walking_left.png') self.player_ss = SpriteSheet(image_path) rect = (0, 0, CREATURE_SPEC_LIST.get(self.creature)[0][0], CREATURE_SPEC_LIST.get(self.creature)[0][1]) self.player_frames = self.player_ss.load_strip(rect, self.num_anim) self.image = self.player_frames[int(self.creature_state)] def draw(self, surface): surface.blit(self.image, self.rect)