def destroy(self, direction=None): sprite_v = "./textures/sprites/breaks_v.png" sprite_g = "./textures/sprites/breaks_g.png" if self.is_broken: game_data.for_destroy[self.id] = self return size_v = Size(40, 20) size_g = Size(20, 40) if direction == Direction.Up: self.sprite = sprite_g self.size = size_g self.is_broken = True elif direction == Direction.Down: self.position = Vector(self.position.x, self.position.y + size_g.height) self.sprite = sprite_g self.size = size_g self.is_broken = True elif direction == Direction.Left: self.sprite = sprite_v self.size = size_v self.is_broken = True elif direction == Direction.Right: self.position = Vector(self.position.x + size_v.width, self.position.y) self.sprite = sprite_v self.size = size_v self.is_broken = True
def fire(enemy, direction): if enemy.bullet is None: size_by_direction = { Direction.Up: Size(12, 10), Direction.Right: Size(10, 13), Direction.Down: Size(13, 10), Direction.Left: Size(10, 13) } size = size_by_direction[direction] position_by_direction = { Direction.Up: Vector( enemy.position.x + enemy.size.width / 2 - size.width / 2, enemy.position.y - enemy.size.height / 2), Direction.Down: Vector( enemy.position.x + enemy.size.width / 2 - size.width / 2, enemy.position.y + 3 * enemy.size.height / 2), Direction.Left: Vector( enemy.position.x - enemy.size.width / 2, enemy.position.y + enemy.size.height / 2 - size.height / 2), Direction.Right: Vector( enemy.position.x + 3 * enemy.size.width / 2, enemy.position.y + enemy.size.height / 2 - size.height / 2) } position = position_by_direction[direction] enemy.bullet = Bullet(position.x, position.y, size, direction, True, enemy=enemy)
def fire(player): if player.bullet is None: direction_by_sprite = { player.sprite_u_1: Direction.Up, player.sprite_r_1: Direction.Right, player.sprite_d_1: Direction.Down, player.sprite_l_1: Direction.Left, player.sprite_u_2: Direction.Up, player.sprite_r_2: Direction.Right, player.sprite_d_2: Direction.Down, player.sprite_l_2: Direction.Left } direction = direction_by_sprite[player.sprite] size_by_direction = { Direction.Up: Size(12, 10), Direction.Right: Size(10, 13), Direction.Down: Size(13, 10), Direction.Left: Size(10, 13) } size = size_by_direction[direction] position_by_direction = { Direction.Up: Vector( player.position.x + player.size.width / 2 - size.width / 2, player.position.y - player.size.height / 2), Direction.Down: Vector( player.position.x + player.size.width / 2 - size.width / 2, player.position.y + 3 * player.size.height / 2), Direction.Left: Vector( player.position.x - player.size.width / 2, player.position.y + player.size.height / 2 - size.height / 2), Direction.Right: Vector( player.position.x + 3 * player.size.width / 2, player.position.y + player.size.height / 2 - size.height / 2) } position = position_by_direction[direction] player.bullet = Bullet(position.x, position.y, size, direction, False, player=player) game_data.is_space_pressed = False
def test_destroy(self): cords = Vector(10, 20) size = Size(40, 40) breaks = Breaks(cords.x, cords.y, size) sprite_v = "./textures/sprites/breaks_v.png" sprite_g = "./textures/sprites/breaks_g.png" size_v = Size(40, 20) size_g = Size(20, 40) # u breaks.destroy(Direction.Up) self.assertEqual(breaks.position.x, cords.x) self.assertEqual(breaks.position.y, cords.y) self.assertEqual(breaks.sprite, sprite_g) self.assertEqual(breaks.size.height, size_g.height) self.assertEqual(breaks.size.width, size_g.width) # d breaks = Breaks(cords.x, cords.y, size) breaks.destroy(Direction.Down) self.assertEqual(breaks.position.x, cords.x) self.assertEqual(breaks.position.y, cords.y + size_g.height) self.assertEqual(breaks.sprite, sprite_g) self.assertEqual(breaks.size.height, size_g.height) self.assertEqual(breaks.size.width, size_g.width) # r breaks = Breaks(cords.x, cords.y, size) breaks.destroy(Direction.Right) self.assertEqual(breaks.position.x, cords.x + size_v.width) self.assertEqual(breaks.position.y, cords.y) self.assertEqual(breaks.sprite, sprite_v) self.assertEqual(breaks.size.height, size_v.height) self.assertEqual(breaks.size.width, size_v.width) # l breaks = Breaks(cords.x, cords.y, size) breaks.destroy(Direction.Left) self.assertEqual(breaks.position.x, cords.x) self.assertEqual(breaks.position.y, cords.y) self.assertEqual(breaks.sprite, sprite_v) self.assertEqual(breaks.size.height, size_v.height) self.assertEqual(breaks.size.width, size_v.width) id = breaks.id breaks.destroy() self.assertIn(id, game_data.for_destroy)
def __init__(self): self.size = 40 self.size_player = Size(30, 30) self.game_objects = { "4": lambda x, y, size: breaks.Breaks(x, y, size), "9": lambda x, y, size: wall.Wall(x, y, size), "A": lambda x, y, size: water.Watter(x, y, size), "B": lambda x, y, size: grass.Grass(x, y, size), "P": lambda x, y, size: player.Player(x, y, size), "E": lambda x, y, size: base.Base(x, y, size) }
def test_init(self): cords = Vector(10, 20) size = Size(40, 40) water = Watter(cords.x, cords.y, size) self.assertEqual(water.position.x, cords.x) self.assertEqual(water.position.y, cords.y) self.assertEqual(water.size.width, size.width) self.assertEqual(water.size.height, size.height) self.assertEqual(water.speed, 0) self.assertEqual(water.sprite, "./textures/sprites/water.png") self.assertIsNone(water.upd) self.assertEqual(water.name, "Water") self.assertEqual(water.drawing_priority, 5)
def __init__(self, pos): self.size_v = Size(35, 30) self.size_g = Size(30, 35) super().__init__(pos.x, pos.y, self.size_v, "Enemy", "./textures/sprites/enemy_u_1.png", 3, 1, Enemy.upd) self.sprite_u_1 = "" self.sprite_r_1 = "" self.sprite_d_1 = "" self.sprite_l_1 = "" self.sprite_u_2 = "" self.sprite_r_2 = "" self.sprite_d_2 = "" self.sprite_l_2 = "" type_enemy = self.choose_type_enemy() if type_enemy == 1: self.set_first_type_enemy() else: self.set_second_type_enemy() self.last_step = "" self.bullet = None self.is_frozen = False self.freeze_time = 0
def test_init(self): cords = Vector(10, 20) size = Size(40, 40) grass = Grass(cords.x, cords.y, size) self.assertEqual(grass.position.x, cords.x) self.assertEqual(grass.position.y, cords.y) self.assertEqual(grass.size.width, size.width) self.assertEqual(grass.size.height, size.height) self.assertEqual(grass.speed, 0) self.assertEqual(grass.sprite, "./textures/sprites/grass.png") self.assertIsNone(grass.upd) self.assertEqual(grass.name, "Grass") self.assertEqual(grass.drawing_priority, 1)
def test_destroy(self): game_data.game_objects.clear() cords = Vector(10, 20) size_p = Size(30, 30) player = Player(cords.x, cords.y, size_p) Player.fire(player) bullet = self.find_bullet() self.assertIsNotNone(bullet) self.assertIsNotNone(player.bullet) bullet.destroy() self.assertEqual(game_data.game_objects[-1].name, "Animation") self.assertIn(bullet.id, game_data.for_destroy) self.assertIsNone(player.bullet)
def test_init(self): cords = Vector(10, 20) size = Size(12, 10) bullet = Bullet(cords.x, cords.y, size, Direction.Up, False) self.assertEqual(bullet.position.x, cords.x) self.assertEqual(bullet.position.y, cords.y) self.assertEqual(bullet.size.width, size.width) self.assertEqual(bullet.size.height, size.height) self.assertEqual(bullet.speed, 3) self.assertEqual(bullet.sprite, "./textures/sprites/bullet_u.png") self.assertIsNotNone(Bullet.upd) self.assertEqual(bullet.name, "Bullet") self.assertEqual(bullet.drawing_priority, 4)
def __init__(self, pos, pos_enemy): game_data.count_enemies_in_game += 1 sprites = [ "./textures/animations/spawn_enemy_1.png", "./textures/animations/spawn_enemy_2.png", "./textures/animations/spawn_enemy_3.png" ] self.init_pos = Vector(pos.x + 5, pos.y + 5) super().__init__(self.init_pos.copy(), sprites, -1, AnimationSpawnEnemy.upd) self.size = Size(40, 40) self.circles = 0 self.position_enemy = pos_enemy self.name = "AnimationSpawnEnemy"
def create_game_object_by_symbol(self, symbol, x, y): if symbol not in self.game_objects: return if symbol == "P": if game_data.player is not None: raise SystemError("Player have already exist") obj = self.game_objects[symbol]( game_data.game_place_offset + x * self.size + self.size_player.width / 4, game_data.game_place_offset + y * self.size + self.size_player.height / 4, self.size_player) game_data.player = obj return obj = self.game_objects[symbol]( game_data.game_place_offset + x * self.size, game_data.game_place_offset + y * self.size, Size(self.size, self.size)) if symbol == "E": if game_data.base is not None: raise SystemError("Base have already exist") game_data.base = obj
def test_fire(self): game_data.game_objects.clear() game_data.for_destroy.clear() cords = Vector(55, 55) enemy = Enemy(cords) Enemy.fire(enemy, Direction.Up) objs = ["Enemy", "Bullet"] i = 0 size_bullet_u = Size(12, 10) id = 0 for game_object in game_data.game_objects: self.assertEqual(game_object.name, objs[i]) if game_object.name == "Bullet": self.assertEqual(Direction.Up, game_object.direction) self.assertEqual(game_object.size.height, size_bullet_u.height) self.assertEqual(game_object.size.width, size_bullet_u.width) self.check_obj_position(game_object, Vector(65, 37.5)) id = game_object.id i += 1 self.assertEqual(enemy.bullet.id, id)
def test_upd(self): game_data.game_objects.clear() game_data.for_destroy.clear() game_data.map_width = 500 game_data.map_height = 500 cords_p = Vector(50, 50) size_p = Size(30, 30) player = Player(cords_p.x, cords_p.y, size_p) cords = Vector(200, 200) size = Size(12, 10) bullet = Bullet(cords.x, cords.y, size, Direction.Up, False, player=player) player.bullet = bullet BonusClock() Bullet.upd(bullet) self.check_obj_position(bullet, Vector(200, 197)) self.assertEqual(len(game_data.for_destroy), 0) wall = Wall(200, 196, Size(40, 40)) Bullet.upd(bullet) self.check_obj_position(bullet, Vector(200, 194)) self.assertIn(bullet.id, game_data.for_destroy) self.assertNotIn(wall.id, game_data.for_destroy) game_data.game_objects.pop() game_data.for_destroy.clear() player.position = Vector(200, 192) Bullet.upd(bullet) self.check_obj_position(bullet, Vector(200, 191)) self.assertIn(bullet.id, game_data.for_destroy) self.assertIn(player.id, game_data.for_destroy) size_by_direction = { Direction.Up: Size(12, 10), Direction.Right: Size(10, 13), Direction.Down: Size(13, 10), Direction.Left: Size(10, 13) } # r bullet.direction = Direction.Right bullet.size = size_by_direction[bullet.direction] game_data.game_objects.pop(0) game_data.for_destroy.clear() Bullet.upd(bullet) self.check_obj_position(bullet, Vector(203, 191)) # d bullet.direction = Direction.Down bullet.size = size_by_direction[bullet.direction] Bullet.upd(bullet) self.check_obj_position(bullet, Vector(203, 194)) # l bullet.direction = Direction.Left bullet.size = size_by_direction[bullet.direction] Bullet.upd(bullet) self.check_obj_position(bullet, Vector(200, 194)) bullet.position = Vector(26, 26) bullet.direction = Direction.Up bullet.size = size_by_direction[bullet.direction] Bullet.upd(bullet) self.assertIn(bullet.id, game_data.for_destroy)
def __init__(self): super().__init__( random.randint(game_data.game_place_offset, game_data.map_width), random.randint(game_data.game_place_offset, game_data.map_height), Size(15, 16), "./textures/bonuses/clock.png", 0)
def test_init(self): size = Size(500, 600) self.assertEqual(size.height, 500) self.assertEqual(size.width, 600)