def update(self): collision_list = arcade.check_for_collision_with_list( self, self.level.entities) for entity in collision_list: if isinstance(entity, Projectile): self.hurt(entity.damage, entity.change_x) player = self.level.player dist = Maths.manhattan_dist(self.center_x, self.center_y, player.center_x, player.center_y) if dist <= self.range: self.move_to(player) else: self.wander() if self.curr_invis_frame > 0 and self.curr_invis_frame % 12 < 6: self.texture = Textures.get_texture(15, 15) else: self.texture = self.tex if self.intersects(player): player.hurt(self.damage, self.change_x) super().update()
def __init__(self, x, y, keyboard): self.keyboard = keyboard self.movespeed = 2.5 self.jump_height = 4 self.jumping = False self.max_attack_speed = 12 self.curr_attack_speed = 0 self.attack_dir = 0 self.curr_jump_height = 0 self.min_jump_height = 8 self.max_jump_height = 64 self.walk_count = 0 self.walk_frame_speed = 8 self.not_mirrored = True self.curr_dash_frame = 0 self.dash_frame_speed = 12 self.dashing = False self.crawling = False self.curr_crawl_frame = 0 self.crawl_frame_speed = 16 self.health = 9 self.curr_invis_frame = 0 self.invis_frame = 150 # Textures self.idle_texture = Textures.get_texture(0, 4) self.idle_texture_mirrored = Textures.get_texture(0, 5) self.walking_textures = Textures.get_textures(1, 4, 4) self.walking_textures_mirrored = Textures.get_textures(1, 5, 4) self.dash_textures = Textures.get_textures(5, 4, 3) self.dash_textures_mirrored = Textures.get_textures(5, 5, 3) self.crawl_textures = Textures.get_textures(7, 4, 4) self.crawl_textures_mirrored = Textures.get_textures(7, 5, 4) super().__init__(self.idle_texture, x, y)
def __init__(self, camera, keyboard): self.camera = camera self.keyboard = keyboard self.width = WIDTH // TILE_SIZE self.height = HEIGHT // TILE_SIZE self.tile_list = arcade.SpriteList(use_spatial_hash=True, spatial_hash_cell_size=32, is_static=True) self.entities = arcade.SpriteList(use_spatial_hash=True, spatial_hash_cell_size=64) self.particles = arcade.SpriteList(is_static=True) self.tiles = {} self.reset = False self.reset_timer = -1 # for i in range(100): # ball = Ball(Textures.get_texture(2, 5), 128 * random.random(), 128 * random.random()) # ball.change_x = random.randint(-8, 8) # ball.change_y = random.randint(-8, 8) # self.add_entity_to_list(ball, self.entities) self.player = Player(64, 64, self.keyboard) self.add_entity_to_list(self.player, self.entities) self.level_gen = LevelGenerator.LevelGen(self) self.paused = True self.difficulty = 1 self.engine = Engine(self.entities, self.tile_list, self, GRAVITY) self.physics_engine = arcade.PhysicsEnginePlatformer(self.player, self.tile_list, GRAVITY) self.pause_text = Graphics.create_text_list("Paused", 0, 0, True) self.game_over_text = Graphics.create_text_list("Game Over :<", 0, 0, True) self.game_over = False self.game_over_timer = 0 self.curr_health = self.player.health self.health_bar = arcade.SpriteList() for i in range(3): heart = arcade.Sprite() heart.center_x = self.player.center_x - TILE_SIZE + i * TILE_SIZE heart.center_y = self.player.center_y - TILE_SIZE * 1.5 heart.texture = Textures.get_texture(4, 9) self.health_bar.append(heart) self.setup()
def update(self, delta): self.level_gen.update() if not (self.level_gen.generating or self.level_gen.drawing or self.paused or self.game_over): self.engine.update() if self.game_over_timer >= 0: self.game_over_timer -= 1 if self.game_over_timer == 0: self.game_over = False self.reset = True if self.reset_timer >= 0: self.reset_timer -= 1 if self.reset_timer == 0: self.reset = True if self.reset: self.difficulty += 1 self.reset_level() self.generate_level(self.player.center_x, self.player.center_y) self.reset = False remainder = self.player.health if self.player.health > 0 else 0 for i, health in enumerate(self.health_bar): health.center_x = self.player.center_x - TILE_SIZE + i * TILE_SIZE health.center_y = self.player.center_y - TILE_SIZE * 1.5 if self.player.health < self.curr_health: if remainder >= 3: health.texture = Textures.get_texture(4, 9) remainder -= 3 else: health.texture = Textures.get_texture(4 + 3 - remainder, 9) remainder = 0 self.curr_health = self.player.health
def __init__(self, x, y): super().__init__(Textures.get_texture(4, 9), x, y)
def update(self): speed_mult = 1 if self.keyboard.is_pressed("sprint"): speed_mult = 2 if self.keyboard.is_pressed("dash"): if not self.dashing: self.change_y += 2 self.dashing = True if self.keyboard.is_pressed("l"): pass # self.level.reset = True if self.keyboard.is_pressed("down"): self.change_y -= 0.1 self.crawling = True speed_mult *= 0.5 else: self.crawling = False if self.keyboard.is_pressed("attack"): if self.curr_attack_speed == 0: extra_y_dir = 0 if self.keyboard.is_pressed("up"): extra_y_dir = 4 elif self.keyboard.is_pressed("down"): extra_y_dir = -4 attack_x = (self.change_x) * 4 attack_y = (self.change_y + extra_y_dir) * 3 attack_angle = int( math.atan2(attack_y, attack_x) / math.pi * 180) card = Projectile( Textures.SPRITESHEET[3 + int((attack_angle % 360) / 45) + 16], self.center_x, self.center_y, attack_x, attack_y) self.level.add_entity_to_list(card, self.level.entities) self.curr_attack_speed = self.max_attack_speed Sounds.play(Sounds.SHOOT) if self.curr_attack_speed > 0: self.curr_attack_speed -= 1 if self.keyboard.is_pressed("jump"): if self.level.physics_engine.can_jump(1): # if self.level.engine.can_jump(self, 1): if not self.jumping: Sounds.play(Sounds.JUMP) self.level.physics_engine.jump(self.jump_height) self.jumping = True # elif self.level.engine.can_jump(self, -1): elif self.level.physics_engine.can_jump(-1): self.jumping = False self.curr_jump_height = 0 if self.curr_jump_height > self.max_jump_height: self.jumping = False self.curr_jump_height = 0 elif self.curr_jump_height >= self.min_jump_height: self.jumping = False self.curr_jump_height = 0 if self.jumping: self.change_y = self.jump_height self.curr_jump_height += self.jump_height if self.keyboard.is_pressed("left"): self.change_x = -self.movespeed * speed_mult elif self.keyboard.is_pressed("right"): self.change_x = self.movespeed * speed_mult else: if self.change_x > 1: self.change_x -= 1 self.not_mirrored = True elif self.change_x < -1: self.change_x += 1 self.not_mirrored = False else: self.change_x = 0 if self.dashing: if self.change_x > 0: self.change_x = self.movespeed * speed_mult * 1.5 elif self.change_x < 0: self.change_x = -self.movespeed * speed_mult * 1.5 self.curr_dash_frame += 1 if self.curr_dash_frame >= self.dash_frame_speed * len( self.dash_textures): self.curr_dash_frame = 0 self.dashing = False elif self.crawling: self.curr_crawl_frame += 1 if self.curr_crawl_frame >= self.crawl_frame_speed * len( self.crawl_textures): self.curr_crawl_frame = 0 else: self.walk_count += 1 if self.walk_count >= len( self.walking_textures) * self.walk_frame_speed: self.walk_count = 0 if self.curr_invis_frame > 0 and self.curr_invis_frame % 12 < 6: self.texture = Textures.get_texture(15, 15) elif self.change_x > 0: if self.dashing: self.texture = self.dash_textures[self.curr_dash_frame // self.dash_frame_speed] elif self.crawling: self.texture = self.crawl_textures[self.curr_crawl_frame // self.crawl_frame_speed] else: self.texture = self.walking_textures[self.walk_count // self.walk_frame_speed] # self.player_dir = True elif self.change_x < 0: if self.dashing: self.texture = self.dash_textures_mirrored[ self.curr_dash_frame // self.dash_frame_speed] elif self.crawling: self.texture = self.crawl_textures_mirrored[ self.curr_crawl_frame // self.crawl_frame_speed] else: self.texture = self.walking_textures_mirrored[ self.walk_count // self.walk_frame_speed] # self.player_dir = False else: if self.not_mirrored: if self.crawling: self.texture = self.crawl_textures[0] else: self.texture = self.idle_texture else: if self.crawling: self.texture = self.crawl_textures_mirrored[0] else: self.texture = self.idle_texture_mirrored super().update()
def drawRoom(self, room): room_x = int(room.x * ROOM_WIDTH) room_y = int(room.y * ROOM_HEIGHT) for x in range(0, 3): for y in range(0, 3): template = ROOM_TEMPLATES[2] neighbor_rooms = {} for direction in DIRS: neighbor = self.rooms.get((room.x + direction[0], room.y + direction[1])) if neighbor is not None: neighbor_rooms[direction] = neighbor left = neighbor_rooms.get((-1, 0)) is not None bottom = neighbor_rooms.get((0, -1)) is not None top = neighbor_rooms.get((0, 1)) is not None right = neighbor_rooms.get((1, 0)) is not None if y == 0: if x == 1: if bottom: template = getRandTemplate(BOTTOM_OPEN) else: template = getRandTemplate(BOTTOM) elif x == 0: template = getRandTemplate(BOTTOM_LEFT) elif x == 2: template = getRandTemplate(BOTTOM_RIGHT) elif y == 1: if x == 0: if left: template = EMPTY else: template = MIDDLE_LEFT[0] elif x == 1: template = MIDDLE_CENTER[0] elif x == 2: if right: template = EMPTY else: template = MIDDLE_RIGHT[0] elif y == 2: if x == 1: if top: template = getRandTemplate(TOP_OPEN) else: template = TOP[0] elif x == 0: template = TOP_LEFT[0] elif x == 2: template = TOP_RIGHT[0] drawTemplate( self.level, room_x + x * TEMPLATE_WIDTH, room_y + y * TEMPLATE_HEIGHT, template) difficulty = self.level.difficulty while difficulty > 0: if room.type == START_ROOM: break if room.type == BOSS_ROOM: entity_x = (room_x + ROOM_WIDTH / 2) * TILE_SIZE entity_y = (room_y + ROOM_HEIGHT / 2) * TILE_SIZE boss = Boss(entity_x, entity_y, self.level.difficulty) self.level.add_entity_to_list(boss, self.level.entities) difficulty -= 5 else: enemy_type = random.randint(0, 2) entity_x = random.randrange(room_x + 1, room_x + ROOM_WIDTH - 1) entity_y = random.randrange(room_y + 1, room_y + ROOM_HEIGHT - 1) tries = 0 while self.level.get_tile(entity_x, entity_y) or tries < 5: entity_x = random.randrange(room_x + 1, room_x + ROOM_WIDTH - 1) entity_y = random.randrange(room_y + 1, room_y + ROOM_HEIGHT - 1) tries += 1 entity_x *= TILE_SIZE entity_y *= TILE_SIZE if enemy_type == 0: slem = Slime(Textures.get_texture(3, 2), entity_x, entity_y, self.level.difficulty) self.level.add_entity_to_list(slem, self.level.entities) difficulty -= 1 elif enemy_type == 1: enemy = Enemy(Textures.get_texture(0, 2), entity_x, entity_y, self.level.difficulty) self.level.add_entity_to_list(enemy, self.level.entities) difficulty -= 1 else: spiky_ball = SpikyBall(entity_x, entity_y, self.level.difficulty) self.level.add_entity_to_list(spiky_ball, self.level.entities) difficulty -= 1
def __init__(self, x, y, difficulty): super().__init__(Textures.get_texture(1, 1), x, y, difficulty) self.movespeed = 1 + difficulty * 0.2 self.damage = 2