import arcade import math from bullet import Bullet from bonus import Bonus class Tank(): ls = list() def __init__(self,x,y,type='enemy',max_health=100,speed = 5): self.sp_body = arcade.SpriteList() self.sp_gun = arcade.SpriteList() self.type = type self.x = x self.y = y self.gun_angle = 0 self.speed = speed self.max_health = max_health self.health = self.max_health self.power = 10 if type == 'player': body = arcade.Sprite( './assets/tankBody_blue.png', scale=1 ) gun = arcade.Sprite() gun.append_texture(arcade.load_texture('./assets/tankBlue_barrel2.png')) gun.append_texture(arcade.load_texture('./assets/tankBlue_barrel3.png')) gun.append_texture(arcade.load_texture('./assets/tankBlue_barrel1.png')) gun.cur = 0 gun.set_texture(gun.cur) else: body = arcade.Sprite( './assets/tankBody_dark.png', scale=1 ) gun = arcade.Sprite( './assets/tankBlue_barrel2.png', scale=1 ) body.center_x = x body.center_y = y self.sp_body.append(body) gun.center_x = x gun.center_y = y self.sp_gun.append(gun) Tank.ls.append(self) def get_sprite(self): return self.sp_body[0] def get_sprite_list(self): sp = arcade.SpriteList() for sprite in self.sp_body: sp.append(sprite) for sprite in self.sp_gun: sp.append(sprite) return sp def gun_to_xy(self,x,y): for gun in self.sp_gun: gun.angle = math.atan2(x-gun.center_x,y-gun.center_y) * 180 / math.pi def move(self,dx,dy): for body in self.sp_body: body.change_x = dx*self.speed body.change_y = dy*self.speed if dx != 0 or dy != 0: body.angle = math.atan2(dx,dy) * 180 / math.pi for gun in self.sp_gun: gun.change_x = dx*self.speed gun.change_y = dy*self.speed def move_engine(self,engine,dx,dy): for sprite in self.sp_body:#,*self.sp_gun]: engine.apply_force(sprite,(dx*self.speed,dy*self.speed)) def shoot(self): for gun in self.sp_gun: Bullet( gun.center_x, gun.center_y, gun.angle, shift = 20+self.power*0.6, power = self.power, owner=self ) def destroy(self): Tank.ls.remove(self) @classmethod def update(cls): for tank in cls.ls: tank.sp_body.update() tank.x = tank.sp_body[0].center_x tank.y = tank.sp_body[0].center_y tank.sp_gun.update() #Подбор усилителей colls = Bonus.get_colls(tank.sp_body[0]) for bonus in colls: if bonus.type == 'star': for gun in tank.sp_gun: if gun.cur < len(gun.textures)-1: gun.cur += 1 gun.set_texture(gun.cur) tank.power *= 2 bonus.destroy() #Столкновения с пулями colls = Bullet.get_colls(tank.sp_body[0]) for bullet in colls: if tank == bullet.owner: continue tank.health -= bullet.power bullet.destroy() if tank.health <= 0: tank.destroy() @classmethod def draw(cls): for tank in cls.ls: tank.sp_body.draw() tank.sp_gun.draw() heal_max_width = 40 heal_width = tank.health/tank.max_health * heal_max_width arcade.draw_polygon_filled([ (tank.x-heal_max_width//2,tank.y+25), (tank.x-heal_max_width//2+heal_width,tank.y+25), (tank.x-heal_max_width//2+heal_width,tank.y+20), (tank.x-heal_max_width//2,tank.y+20), ],(255,0,0)) arcade.draw_polygon_outline([ (tank.x-20,tank.y+25), (tank.x+20,tank.y+25), (tank.x+20,tank.y+20), (tank.x-20,tank.y+20), ],(0,0,0))