Files
arcade/tank.py
T

114 lines
3.2 KiB
Python

import arcade
import math
from bullet import Bullet
from bonus import Bonus
class Tank():
ls = list()
def __init__(self,x,y,type='enemy'):
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 = 5
self.health = 100
self.max_health = 100
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 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 shoot(self):
for gun in self.sp_gun:
Bullet(
gun.center_x,
gun.center_y,
gun.angle,
shift = 50
)
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):
gun.cur = 0
gun.set_texture(gun.cur)
else:
gun.cur += 1
gun.set_texture(gun.cur)
bonus.destroy()
#Столкновения с пулями
colls = Bullet.get_colls(tank.sp_body[0])
for bullet in colls:
tank.health -= 10
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))