parent
956b8cf83f
commit
0927040d8d
5 changed files with 235 additions and 102 deletions
@ -0,0 +1,57 @@ |
||||
import pygame as pg |
||||
from sprite import Sprite |
||||
from random import randint |
||||
|
||||
|
||||
class Asteroid(Sprite): |
||||
COOLTIME = 15 |
||||
SPEED = 2 |
||||
COLOR = (0,0,255) |
||||
BASE_SIZE = 30 |
||||
N_CHILDREN = 2 |
||||
|
||||
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 |
||||
|
@ -0,0 +1,27 @@ |
||||
import pygame as pg |
||||
from sprite import Sprite |
||||
|
||||
SIZE = 24 |
||||
UNIT = SIZE//4 |
||||
COLOR =(255,0,0) |
||||
C_POINT = (2,2) |
||||
SHAPE = ( |
||||
(0,1), C_POINT, (4,1), |
||||
(3,0), C_POINT, (3,4), |
||||
(4,3), C_POINT, (0,3), |
||||
(1,4), C_POINT, (1,0), |
||||
) |
||||
|
||||
|
||||
class Crosshair(Sprite): |
||||
def __init__(self, scr_rect): |
||||
super().__init__((SIZE,)*2) |
||||
self.rect.x = scr_rect.centerx - self.rect.width / 2 |
||||
self.rect.y = scr_rect.centery - self.rect.height / 2 |
||||
self.image.fill(self.image.get_colorkey()) |
||||
scaled_shape = [(x*UNIT, y*UNIT) for x, y in SHAPE] |
||||
pg.draw.polygon(self.image, COLOR, scaled_shape) |
||||
|
||||
def update(self, new_pos): |
||||
self.rect.x = new_pos.x - self.rect.width / 2 |
||||
self.rect.y = new_pos.y - self.rect.height / 2 |
Loading…
Reference in new issue