def __init__(self, snake, previous_body_piece, x, y): super().__init__() self.snake = snake self.previous_body_piece = previous_body_piece self.direction = consts.DIRECTION_RIGHT self.rect = False self.rect_old = False # Snake's body piece if not SnakeBodyPiece.sprite_images.get(self.snake.color): snake_type_short = 'body' image_quality = '_md' if Settings.cell_size >= consts.CELL_SIZE_MIN_FOR_IM_MD else '' image = pg.image.load( resources.file_name_get(name='im_snake_', subname=snake_type_short, quality=image_quality, num=self.snake.color, subnum=1)).convert() image = pg.transform.smoothscale( image, (Settings.cell_size, Settings.cell_size)) image.set_colorkey(Color.BLACK) self.image = image SnakeBodyPiece.sprite_images[self.snake.color] = self.image else: self.image = SnakeBodyPiece.sprite_images[self.snake.color] self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y self.rect_old = self.image.get_rect() self.rect_old.x = self.rect.x self.rect_old.y = self.rect.y
def __init__(self, x, y, cartridge_type, game): super().__init__() self.images_sprite_no = 1 self.frame = 0 self.rect = None self.cartridge_type = cartridge_type self.game = game self.cartridge_type_txt = None self.bullet_type = None # the kind of bullet it replenishes self.cartridge_type_txt = self.cartridge_type.value if self.cartridge_type == CartridgeType.T1_LASER1: cartridge_type_short = 't1' self.bullet_type = BulletType.T1_LASER1 self.qty = randint(5, 15) elif self.cartridge_type == CartridgeType.T2_LASER2: cartridge_type_short = 't2' self.bullet_type = BulletType.T2_LASER2 self.qty = randint(4, 15) elif self.cartridge_type == CartridgeType.T3_PHOTONIC: cartridge_type_short = 't3' self.bullet_type = BulletType.T3_PHOTONIC self.qty = randint(2, 5) elif self.cartridge_type == CartridgeType.T4_NEUTRONIC: cartridge_type_short = 't4' self.bullet_type = BulletType.T4_NEUTRONIC self.qty = randint(2, 5) if not Cartridge.sprite_images.get(self.cartridge_type): image = pg.image.load( resources.file_name_get(name='im_cartridge_', subname=cartridge_type_short, num=1)).convert() image = pg.transform.smoothscale(image, Cartridge.size) image.set_colorkey(Color.BLACK) Cartridge.sprite_images[self.cartridge_type] = image else: image = Cartridge.sprite_images[self.cartridge_type] self.image = image self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y # Add cartridge to the active sprite list self.game.active_sprites.add(self) self.game.cartridges.add(self) self.game.normal_items.add(self)
def __init__(self, x, y, mine_type, game): super().__init__() self.images_sprite_no = 1 self.frame = 0 self.rect = None self.mine_type = mine_type self.game = game self.mine_type_txt = None self.attack_power = None self.health_total = None self.health = None if self.mine_type == MineType.T1_AQUA: self.mine_type_txt = 'mines_t01' mine_type_short = 't1' self.health_total = 12 self.attack_power = MINE_T01_ATTACK_POWER elif self.mine_type == MineType.T2_LILAC: self.mine_type_txt = 'mines_t02' mine_type_short = 't2' self.health_total = 25 self.attack_power = MINE_T02_ATTACK_POWER if not Mine.sprite_images.get(self.mine_type): image_quality = '_md' if Settings.cell_size >= consts.CELL_SIZE_MIN_FOR_IM_MD else '' image = pg.image.load(resources.file_name_get(name='im_mine_', subname=mine_type_short, quality=image_quality, num=1)).convert() image = pg.transform.smoothscale(image, Mine.size) image.set_colorkey(Color.BLACK) Mine.sprite_images[self.mine_type] = image else: image = Mine.sprite_images[self.mine_type] self.image = image self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y self.health = self.health_total # Add mine to the active sprite list self.game.active_sprites.add(self) self.game.mines.add(self)
def __init__(self, x, y, apple_type, game): super().__init__() self.images_sprite_no = 1 self.frame = 0 self.rect = None self.apple_type = apple_type self.game = game self.apple_type_txt = None if self.apple_type == AppleType.T1_RED: self.apple_type_txt = 'apples_t01' apple_type_short = 't1' elif self.apple_type == AppleType.T2_GREEN: self.apple_type_txt = 'apples_t02' apple_type_short = 't2' elif self.apple_type == AppleType.T3_YELLOW: self.apple_type_txt = 'apples_t03' apple_type_short = 't3' if not Apple.sprite_images.get(self.apple_type): image_quality = '_md' if Settings.cell_size >= consts.CELL_SIZE_MIN_FOR_IM_MD else '' image = pg.image.load(resources.file_name_get(name='im_apple_', subname=apple_type_short, quality=image_quality, num=1)).convert() image = pg.transform.smoothscale(image, Apple.size) image.set_colorkey(Color.BLACK) Apple.sprite_images[self.apple_type] = image else: image = Apple.sprite_images[self.apple_type] self.image = image self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y # Add apple to the active sprite list self.game.active_sprites.add(self) self.game.apples.add(self) self.game.normal_items.add(self)
def __init__(self, x, y, rec_potion_type, game): super().__init__() self.images_sprite_no = 1 self.frame = 0 self.rect = None self.rec_potion_type = rec_potion_type self.game = game self.rec_potion_type_txt = None self.qty = randint(15, 100) self.stat_type = None # the kind of stat. it replenishes self.rec_potion_type_txt = self.rec_potion_type.value self.stat_type = self.rec_potion_type if self.rec_potion_type == RecoveryPotionType.T1_HEALTH: rec_potion_type_short = 't1' elif self.rec_potion_type == RecoveryPotionType.T2_POWER: rec_potion_type_short = 't2' if not RecoveryPotion.sprite_images.get(self.rec_potion_type): image = pg.image.load(resources.file_name_get(name='im_rec_potion_', subname=rec_potion_type_short, num=1)).convert() image = pg.transform.smoothscale(image, RecoveryPotion.size) image.set_colorkey(Color.BLACK) RecoveryPotion.sprite_images[self.rec_potion_type] = image else: image = RecoveryPotion.sprite_images[self.rec_potion_type] self.image = image self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y # Add rec_potion to the active sprite list self.game.active_sprites.add(self) self.game.rec_potions.add(self) self.game.normal_items.add(self)
def __init__(self, color, x, y, game): super().__init__() self.color = color self.is_alive = True self.change_x = 0 self.change_y = 0 self.game = game self.snake = self self.attack_power = SNAKE_DEFAULT_ATTACK_POWER self.other_snakes = pg.sprite.Group() self.body_length = Snake.body_len_start = Settings.snake_body_len_start self.speed = Settings.cell_size self.body_pieces = [] self.rip_frames = [] self.direction = consts.DIRECTION_RIGHT self.direction_old = self.direction self.images_sprite_no = 1 self.frame = 0 self.rect = False self.rect_old = False self.body_len_start = None self.stats = { 'score': 0, 'lives': SNAKE_LIVES_DEFAULT, 'health': SNAKE_HEALTH_DEFAULT, 'power': SNAKE_POWER_DEFAULT, 'speed': self.speed, 'health_max': SNAKE_HEALTH, 'power_max': SNAKE_POWER, 'bullets_t01': SNAKE_BULLETS_T1_DEFAULT, 'bullets_t01_shot': 0, 'bullets_t02': SNAKE_BULLETS_T2_DEFAULT, 'bullets_t02_shot': 0, 'bullets_t03': SNAKE_BULLETS_T3_DEFAULT, 'bullets_t03_shot': 0, 'bullets_t04': SNAKE_BULLETS_T4_DEFAULT, 'bullets_t04_shot': 0, 'apples': 0, 'apples_t01': 0, 'apples_t02': 0, 'apples_t03': 0, 'mines': 0, 'mines_t01': 0, 'mines_t02': 0, 'bats': 0, 'bats_t01': 0, 'bats_t02': 0, 'bats_t03': 0, } self.stats_old = { 'score': None, 'lives': None, 'health': None, 'power': None, 'speed': None, 'bullets_t01': None, 'bullets_t02': None, 'bullets_t03': None, 'bullets_t04': None, 'apples': None, 'mines': None, } # Snake's head by direction if not Snake.sprite_images.get((self.color, consts.DIRECTION_RIGHT)): snake_type_short = 'head' image_quality = '_md' if Settings.cell_size >= consts.CELL_SIZE_MIN_FOR_IM_MD else '' image = pg.image.load( resources.file_name_get(name='im_snake_', subname=snake_type_short, quality=image_quality, num=self.color, subnum=1)).convert() image = pg.transform.smoothscale( image, (Settings.cell_size, Settings.cell_size)) image.set_colorkey(Color.BLACK) Snake.sprite_images[(self.color, consts.DIRECTION_RIGHT)] = image image = pg.image.load( resources.file_name_get(name='im_snake_', subname=snake_type_short, quality=image_quality, num=self.color, subnum=1)).convert() image = pg.transform.flip(image, True, False) image = pg.transform.smoothscale( image, (Settings.cell_size, Settings.cell_size)) image.set_colorkey(Color.BLACK) Snake.sprite_images[(self.color, consts.DIRECTION_LEFT)] = image image = pg.image.load( resources.file_name_get(name='im_snake_', subname=snake_type_short, quality=image_quality, num=self.color, subnum=1)).convert() image = pg.transform.rotate(image, 90) image = pg.transform.smoothscale( image, (Settings.cell_size, Settings.cell_size)) image.set_colorkey(Color.BLACK) Snake.sprite_images[(self.color, consts.DIRECTION_UP)] = image image = pg.image.load( resources.file_name_get(name='im_snake_', subname=snake_type_short, quality=image_quality, num=self.color, subnum=1)).convert() image = pg.transform.rotate(image, 270) image = pg.transform.smoothscale( image, (Settings.cell_size, Settings.cell_size)) image.set_colorkey(Color.BLACK) Snake.sprite_images[(self.color, consts.DIRECTION_DOWN)] = image self.image = Snake.sprite_images[(self.color, consts.DIRECTION_RIGHT)] self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y self.rect_old = self.image.get_rect() self.rect_old.x = self.rect.x self.rect_old.y = self.rect.y # Snake's body and tail previous_body_piece = self for i in range(self.body_length): self.body_pieces.append( SnakeBodyPiece(snake=self, previous_body_piece=previous_body_piece, x=self.rect.x - ((i + 1) * Settings.cell_size), y=self.rect.y)) previous_body_piece = self.body_pieces[i] # Add snake's head and body to the active sprite list self.game.active_sprites.add(self) for body_piece in self.body_pieces: self.game.active_sprites.add(body_piece) self.game.snakes.add(body_piece)
def __init__(self, x, y, bat_type, game, change_x=0, change_y=0): super().__init__() self.images_sprite_no = 4 self.frame = randint(0, self.images_sprite_no - 1) self.rect = None self.bat_type = bat_type self.game = game self.bat_type_txt = None self.attack_power = None self.health_total = None self.health = None self.boundary_left = None self.boundary_right = None self.boundary_top = None self.boundary_bottom = None self.change_x = change_x self.change_y = change_y self.direction = consts.DIRECTION_RIGHT self.animate_timer = pg.time.get_ticks() self.drop_item_pct = 100 walking_frames = [] if self.bat_type == BatType.T1_BLUE: self.bat_type_txt = 'bats_t01' bat_type_short = 't1' self.attack_power = 10 self.health_total = 90 self.drop_item_pct = 100 elif self.bat_type == BatType.T2_LILAC: self.bat_type_txt = 'bats_t02' bat_type_short = 't2' self.attack_power = 13 self.health_total = 120 self.drop_item_pct = 100 elif self.bat_type == BatType.T3_RED: self.bat_type_txt = 'bats_t03' bat_type_short = 't3' self.attack_power = 18 self.health_total = 240 self.drop_item_pct = 100 if not Bat.sprite_images.get(self.bat_type): image_quality = '_md' if Settings.cell_size >= consts.CELL_SIZE_MIN_FOR_IM_MD else '' for i in range(self.images_sprite_no): image = pg.image.load(resources.file_name_get(name='im_bat_', subname=bat_type_short, quality=image_quality, num=i+1)).convert() image = pg.transform.smoothscale(image, Bat.size) image.set_colorkey(Color.BLACK) walking_frames.append(image) Bat.sprite_images[self.bat_type] = (image, walking_frames) else: image = Bat.sprite_images[self.bat_type][0] if self.change_x > 0: self.direction = consts.DIRECTION_RIGHT elif self.change_x < 0: self.direction = consts.DIRECTION_LEFT self.image = Bat.sprite_images[self.bat_type][1][0] self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y self.initialize_boundaries() self.health = self.health_total # Add bat to the active sprite list self.game.active_sprites.add(self) self.game.bats.add(self)
def __init__(self, x, y, bullet_type, game, owner, change_x=0, change_y=0): super().__init__() self.images_sprite_no = 1 self.frame = randint(0, self.images_sprite_no - 1) self.rect = None self.bullet_type = bullet_type self.game = game self.owner = owner self.bullet_type_txt = None self.health_total = 100 self.health = self.health_total self.attack_power = None self.bullet_range = BULLET_STD_RANGE self.boundary_left = None self.boundary_right = None self.boundary_top = None self.boundary_bottom = None self.change_x = change_x self.change_y = change_y self.direction = None self.animate_timer = self.game.current_time self.bullet_type_txt = self.bullet_type.value if self.bullet_type == BulletType.T1_LASER1: bullet_type_short = 't1' self.attack_power = BULLET_POWER_BASE self.bullet_range = int(self.bullet_range * 1.25) elif self.bullet_type == BulletType.T2_LASER2: bullet_type_short = 't2' self.attack_power = BULLET_POWER_BASE * 1.4 self.bullet_range = int(self.bullet_range * 1.15) elif self.bullet_type == BulletType.T3_PHOTONIC: bullet_type_short = 't3' self.attack_power = BULLET_POWER_BASE * 3.4 elif self.bullet_type == BulletType.T4_NEUTRONIC: bullet_type_short = 't4' self.attack_power = BULLET_POWER_BASE * 4.5 if not Bullet.sprite_images.get( (self.bullet_type, consts.DIRECTION_RIGHT)): image_quality = '_md' if Settings.cell_size >= consts.CELL_SIZE_MIN_FOR_IM_MD else '' image = pg.image.load( resources.file_name_get(name='im_bullet_', subname=bullet_type_short, quality=image_quality, num=1)).convert() image = pg.transform.smoothscale(image, Bullet.size) image.set_colorkey(Color.BLACK) Bullet.sprite_images[(self.bullet_type, consts.DIRECTION_RIGHT)] = image image = pg.image.load( resources.file_name_get(name='im_bullet_', subname=bullet_type_short, quality=image_quality, num=1)).convert() image = pg.transform.flip(image, True, False) image = pg.transform.smoothscale(image, Bullet.size) image.set_colorkey(Color.BLACK) Bullet.sprite_images[(self.bullet_type, consts.DIRECTION_LEFT)] = image image = pg.image.load( resources.file_name_get(name='im_bullet_', subname=bullet_type_short, quality=image_quality, num=1)).convert() image = pg.transform.rotate(image, 90) image = pg.transform.smoothscale(image, Bullet.size) image.set_colorkey(Color.BLACK) Bullet.sprite_images[(self.bullet_type, consts.DIRECTION_UP)] = image image = pg.image.load( resources.file_name_get(name='im_bullet_', subname=bullet_type_short, quality=image_quality, num=1)).convert() image = pg.transform.rotate(image, 270) image = pg.transform.smoothscale(image, Bullet.size) image.set_colorkey(Color.BLACK) Bullet.sprite_images[(self.bullet_type, consts.DIRECTION_DOWN)] = image if self.change_x > 0: self.direction = consts.DIRECTION_RIGHT elif self.change_x < 0: self.direction = consts.DIRECTION_LEFT elif self.change_y > 0: self.direction = consts.DIRECTION_DOWN elif self.change_y < 0: self.direction = consts.DIRECTION_UP self.image = Bullet.sprite_images[(self.bullet_type, consts.DIRECTION_RIGHT)] self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y self.initialize_boundaries() # Add bullet to the active sprite list self.game.active_sprites.add(self) self.game.bullets.add(self)