def create_blocks(filename: str): background_blocks = pygame.sprite.Group() collision_blocks = pygame.sprite.Group() spritesheet = SpriteSheet("tiles_spritesheet.png") scale_block_size = 55 spritesheet_block_size = 70 # 72 x 72 file_path = get_path_name("maps", filename) with open(file_path, 'r') as f: lines = f.readlines() row = 0 for line in lines: line = line.strip() col = 0 for char in line: x = col * scale_block_size y = row * scale_block_size color = constants.WHITE can_collide = True image = None if char == ".": color = constants.BLACK can_collide = False elif char == "a": color = constants.RED image = constants.ALARM_BLOCK can_collide = False elif char == "g": color = constants.GREEN image = constants.GRASS_BLOCK elif char == "1": color = constants.GREEN image = constants.RGRASS_BLOCK elif char == "2": color = constants.GREEN image = constants.LGRASS_BLOCK elif char == "b": color = constants.BLUE image = constants.BKEY_BLOCK elif char == "e": color = constants.YELLOW image = constants.EXIT_BLOCK if image is not None: image = get_block_sprite(image, spritesheet_block_size, scale_block_size, spritesheet) block = Block(x, y, scale_block_size, scale_block_size, color, image, can_collide) if can_collide: collision_blocks.add(block) else: background_blocks.add(block) col += 1 row += 1 return background_blocks, collision_blocks
def update(self): if self.animation.current_frame == 0: if self.prev_animation_frame != self.animation.current_frame: self.animation_done = True pygame.mixer.music.load( get_path_name("media", "background_music.mp3")) pygame.mixer.music.play(-1) locked_image, collision_image, collision_mask = self.animation.get_image( ) self.image = locked_image self.prev_animation_frame = self.animation.current_frame, self.animation.update_frame()
def __init__(self): super().__init__() self.animation = Animation("Timer_SpriteSheet.png", "Timer_SpriteSheet.png", "Timer.txt", 1, "Idle") self.image, collision_image, self.mask = self.animation.get_image() self.rect = self.image.get_rect() self.rect.x = constants.SCREEN_WIDTH / 2 - self.rect.width / 2.25 self.rect.y = constants.SCREEN_HEIGHT / 2 - self.rect.height * 1.25 self.animation_done = False self.prev_animation_frame = 0 pygame.mixer.music.load(get_path_name("media", "announcer.mp3")) pygame.mixer.music.play(0)
def get_sprites_from_dict(spritesheet, sprite_dict, scale=1): spritesheet = get_path_name("images", spritesheet) spritesheet = pygame.image.load(spritesheet).convert_alpha() sprites = {} animation_speeds = {} collision_masks = {} y = 0 for key, value in sprite_dict.items(): # print(key, value) sprites[key], collision_masks[key] = get_row_animations( spritesheet, y, value[0], value[1], value[2], scale) animation_speeds[key] = value[3] y += value[1] return sprites, collision_masks, animation_speeds
def get_sprite_dict(filename): textfile_path = get_path_name("animations", filename) sprite_dict = collections.OrderedDict() with open(textfile_path) as f: lines = f.readlines() i = 0 while i < len(lines): name = lines[i].strip() dimensions = lines[i + 1].strip().split("x") dimensions = [int(x) for x in dimensions] # dashed_lines = lines[i + 2].strip() sprite_dict[name] = dimensions # print(name, dimensions, dashed_lines) i += 3 return sprite_dict
def create_block_mapping(): file_path = get_path_name("maps", "block_mapping.txt") with open(file_path, 'r') as f: lines = f.readlines() block_mapping = {} for line in lines: line = line.strip() line = line.replace("\t", "") line = line.replace(" ", "") line = line.split("/") location = eval(line[1]) letter = line[0].split("=")[1] if len(line) == 2: block = (location, True) else: block = (location, False) block_mapping[letter] = block return block_mapping
def update(self): # Will only update count down timer until it's done if not self.countdown.should_countdown(): self.countdown.update() else: # --- Game Logic --- self.player.set_room(self.rooms[self.current_room]) self.player.update(self.players) # Remove these line for networking self.player2.set_room(self.rooms[self.current_room]) self.player2.update(self.players) if (self.player.damage_taken >= 100 or self.player2.damage_taken >= 100) and not self.game_over: self.game_over = True if self.player.damage_taken < 100: self.winner = "P1Wins.png" elif self.player2.damage_taken < 100: self.winner = "P2Wins.png" pygame.mixer.music.stop() pygame.mixer.music.load(get_path_name("media", "winner.mp3")) pygame.mixer.music.play(0)
def create_blocks(filename: str): background_blocks = pygame.sprite.Group() collision_blocks = pygame.sprite.Group() spritesheet = SpriteSheet("Tiles_SpriteSheet.png") scale_block_size = 55 spritesheet_block_size = 70 # 70 x 70 file_path = get_path_name("maps", filename) block_mapping = create_block_mapping() with open(file_path, 'r') as f: lines = f.readlines() row = 0 for line in lines: line = line.strip() col = 0 for char in line: x = col * scale_block_size y = row * scale_block_size image = block_mapping[char][0] can_collide = block_mapping[char][1] image = get_block_sprite(image, spritesheet_block_size, scale_block_size, spritesheet) block = Block(x, y, scale_block_size, scale_block_size, image, can_collide) if char != ".": bg_image = get_block_sprite(constants.BACKGROUND_BLOCK, spritesheet_block_size, scale_block_size, spritesheet) bg = Block(x, y, scale_block_size, scale_block_size, bg_image, can_collide) background_blocks.add(bg) if can_collide: collision_blocks.add(block) else: background_blocks.add(block) col += 1 row += 1 return background_blocks, collision_blocks
def draw_game_over(self, surface, name): img = pygame.image.load(get_path_name("images", name)) surface.blit(img, self.rect)