import pygame as pg from random import randint from .sprite import Sprite class Asteroid(Sprite): COOLTIME = 15 SPEED = 2 COLOR = (0,0,255) BASE_SIZE = 30 N_CHILDREN = 3 def __init__(self, map_rect=None, map_pos=(0,0), velocity=(0, SPEED), size_lev=3): self.size = size_lev * self.BASE_SIZE self.size_lev = size_lev super().__init__((self.size,)*2) self.image.fill(self.image.get_colorkey()) pg.draw.circle( self.image, self.COLOR, self.image.get_rect().center, self.image.get_rect().width/2, ) self.map_pos = pg.math.Vector2(map_pos) if map_rect is not None: if randint(0,1)==0: self.map_pos.x = 0 if randint(0,1)==0 else map_rect.width -1 self.map_pos.y = randint(0,map_rect.height-1) else: self.map_pos.y = 0 if randint(0,1)==0 else map_rect.height -1 self.map_pos.x = randint(0,map_rect.width-1) self.velocity = pg.math.Vector2(velocity) self.velocity.rotate_ip(randint(0,359)) def update(self, map_rect): self.map_pos += self.velocity self.map_pos.x %= map_rect.w self.map_pos.y %= map_rect.h self.rect.x = (self.map_pos.x - self.rect.w/2 - map_rect.x) % map_rect.w self.rect.y = (self.map_pos.y - self.rect.w/2 - map_rect.y) % map_rect.h def breakup(self): new_size = self.size_lev-1 self.kill() children = list() if new_size > 0: for i in range(self.N_CHILDREN): children.append(Asteroid( map_pos=self.map_pos.copy(), velocity=self.velocity.copy()*1.2, size_lev=new_size, )) return children