def __init__(self, map_name): # Game Setup self.map_name = map_name self.level = game.Level(self.map_name) self.state = "game" # Display Setup self.display = pygame.display.set_mode(self.level.DISPLAY_SIZE) # adapts to map size pygame.display.set_caption(constants.DISPLAY_NAME + " - " + os.path.basename(map_name)) self.clock = pygame.time.Clock() self.back_button = pygame_gui.Button(paths.uiPath + "backwhite.png", paths.uiPath + "backwhite-hover.png", 5, 5) # self.back_button.resize(self.level.TILE_SIZE, self.level.TILE_SIZE) # Player Setup spawn = game.get_pixel_position(self.level, self.level.get_spawn()) spawn = [position + self.level.PLAYER_PADDING/2 for position in spawn] # center player in tile self.player = game.UserPLayer(spawn, [0, 0], self.level) # Enemy Setup self.enemies = [] for enemy in self.level.enemies: if enemy["type"] == "er": self.enemies.append(game.RandomPatrol(self.level, enemy["spawn"])) elif enemy["type"] == "es": self.enemies.append(game.EnemySeeker(self.level, self.player, enemy["spawn"])) elif enemy["type"] == "ep": self.enemies.append(game.EnemyPatrol(self.level, enemy["patrol"])) else: raise exceptions.InvalidEnemyType("%s not recognised" % enemy["type"])
def __init__(self, level, spawn, points=10): # level is custom object, requiring .format for grid format and .walls for collision_objects patrol = get_random_patrol(level, spawn, points) super().__init__(level, patrol) # Positioning unit i =n square of first point on its patrol position = game.get_pixel_position(self.linked_level, patrol[0]) position = [ position[0] + level.ENEMY_PADDING / 2, position[1] + level.ENEMY_PADDING / 2 ] self.format = level.format self.speed = 2 self.patrol = patrol self.current_target = 0 # index of patrol list self.path = [ ] # works as stack. index 0 is always next square target, pop of when reached for next. # Sprite Image Setup self.image = pygame.image.load(paths.imagePath + "enemy-random.png").convert_alpha() self.image = pygame.transform.scale( self.image, [level.ENEMY_SIZE, level.ENEMY_SIZE]) self.rect = self.image.get_rect().move(position) self.target_tile_rect = pygame.Rect([0, 0, 0, 0])
def update(self): self.move() if self.timer.is_active(): if self.timer.has_ended(): self.step_patrol() self.update_path() elif len(self.path) > 0: # Checking if reached next tile, if so stepping target to next in path. x, y = game.get_pixel_position(self.linked_level, self.path[0]) self.target_rect = pygame.Rect(x, y, self.linked_level.TILE_SIZE, self.linked_level.TILE_SIZE) self.target_rect.inflate_ip(-self.linked_level.ENEMY_PADDING, -self.linked_level.ENEMY_PADDING) reached_target = False if self.rect.top <= self.target_rect.top and self.vector[1] < 0: reached_target = True elif self.rect.bottom >= self.target_rect.bottom and self.vector[ 1] > 0: reached_target = True elif self.rect.left <= self.target_rect.left and self.vector[0] < 0: reached_target = True elif self.rect.right >= self.target_rect.right and self.vector[ 0] > 0: reached_target = True if reached_target: self.step_path() self.rect = self.target_rect # assigning so player is re-centered to tile if gone > boundary. else: self.step_patrol() self.update_path()
def __init__(self, level, patrol): # level is custom object, requiring .format for grid format and .walls for collision_objects # Positioning unit on square of first point on its patrol position = game.get_pixel_position(level, patrol[0]) position = [ position[0] + level.ENEMY_PADDING / 2, position[1] + level.ENEMY_PADDING / 2 ] super().__init__(position, [0, 0], level) self.format = level.format # Used for enemy to find paths. Assumes format never changes! self.speed = 1 self.patrol = patrol # list of key points to visit self.current_target = 0 # index of patrol list self.path = [ ] # list of all points between current patrol point and next point. # path works as stack. index 0 is always next square target, pop off when reached for next target. self.timer = PatrolTimer(0) # timer used to pause for so long on a square # Sprite Image Setup self.image = pygame.image.load(paths.imagePath + "enemy-patrol.png").convert_alpha() self.image = pygame.transform.scale( self.image, [level.ENEMY_SIZE, level.ENEMY_SIZE]) self.rect = self.image.get_rect().move(position) self.target_rect = pygame.Rect([0, 0, 0, 0])
def __init__(self, level, row, col, filename): # filename shows type of tile to be displayed. self.position = [row, col] self.image = pygame.image.load(paths.imagePath + filename).convert_alpha() self.image = pygame.transform.scale(self.image, [level.TILE_SIZE, level.TILE_SIZE]) self.rect = self.image.get_rect().move( game.get_pixel_position(level, [row, col]))
def __init__(self, level, player, spawn): super().__init__(level, [spawn]) self.target_player = player # Sprite Image Setup self.image = pygame.image.load(paths.imagePath + "enemy-seeker.png").convert_alpha() self.image = pygame.transform.scale( self.image, [level.ENEMY_SIZE, level.ENEMY_SIZE]) self.rect = self.image.get_rect().move( game.get_pixel_position(self.linked_level, spawn)) self.rect.move_ip(level.ENEMY_PADDING / 2, level.ENEMY_PADDING / 2) self.active = False # if it can chase after the player