Source for sprites

master
mian 2 years ago
parent 3357520c87
commit 33a2342985
  1. 38
      block.py
  2. 25
      gem.py
  3. 38
      ship.py
  4. 15
      sprite.py

@ -0,0 +1,38 @@
import pygame as pg
from sprite import Sprite
class Block(Sprite):
COLOR = (0,0,255)
def __init__(self, rect_t):
super().__init__(rect_t[2:4])
self.map_rect = pg.Rect(rect_t)
self.image.fill(self.COLOR)
def update(self,map_rect):
self.rect.x = self.map_rect.x - map_rect.x
self.rect.y = self.map_rect.y - map_rect.y
def collide_ship(self, ship):
if self.map_rect.colliderect(ship.map_rect):
ship.velocity = 0
if ship.map_rect.centery <= self.map_rect.top:
ship.map_pos.y -= ship.map_rect.bottom - self.map_rect.top
elif ship.map_rect.centery >= self.map_rect.bottom:
ship.map_pos.y += self.map_rect.bottom - ship.map_rect.top
if ship.map_rect.centerx <= self.map_rect.left:
ship.map_pos.x -= ship.map_rect.right - self.map_rect.left
elif ship.map_rect.centerx >= self.map_rect.right:
ship.map_pos.x += self.map_rect.right - ship.map_rect.left
if __name__=='__main__':
from ship import Ship
block = Block((0,0, 10, 20))
a_ship = Ship((10+Ship.SIZE/4, 20+Ship.SIZE/4), pg.Rect(0,0,100,100))
block.collide_ship(a_ship)
assert a_ship.map_pos.x == 10 + Ship.SIZE/2 and a_ship.map_pos.y == 20 + Ship.SIZE/2, "bad collision"
block = Block((0,0, 10, 40))
a_ship = Ship((10+Ship.SIZE/4, 20+Ship.SIZE/4), pg.Rect(0,0,100,100))
block.collide_ship(a_ship)
assert a_ship.map_pos.x == 10 + Ship.SIZE/2, "bad collision"

@ -0,0 +1,25 @@
import pygame as pg
from sprite import Sprite
GREEN = (0,255,0)
class Gem(Sprite):
RADIUS = 10
def __init__(self, pos):
size = (2*self.RADIUS,)*2
super().__init__(size)
rpos = (pos[0]- self.RADIUS, pos[1] - self.RADIUS)
self.map_rect = pg.Rect(rpos, size)
self.pos = pg.math.Vector2(pos)
pg.draw.circle(self.image, GREEN, (self.RADIUS,)*2, self.RADIUS)
def update(self, map_rect):
self.rect.x = self.pos.x - map_rect.x - self.RADIUS
self.rect.y = self.pos.y - map_rect.y - self.RADIUS
def collide_ship(self, ship):
rel_pos = self.pos - ship.map_rect.center
if rel_pos.length() < ship.SIZE/2 + self.RADIUS:
self.kill()

@ -0,0 +1,38 @@
import pygame as pg
from sprite import Sprite
SHAPE = [(6,3), (1,5), (2,3), (1,1)]
class Ship(Sprite):
SIZE = 40
COLOR =(255,0,0)
def __init__(self, pos, scr_rect):
super().__init__((self.SIZE,)*2)
self.map_pos = pg.math.Vector2(pos)
u = self.SIZE//6
self.shape = [pg.math.Vector2(p[0]*u,p[1]*u) for p in SHAPE]
self.rect.x = scr_rect.centerx - self.rect.width / 2
self.rect.y = scr_rect.centery - self.rect.height / 2
self.velocity = 0
self.deceleration = 0
self.direction = pg.math.Vector2((1,0))
self.change = pg.math.Vector2((0,0))
def update(self, rot_vector):
center = self.SIZE/2
def rotate(p):
x0 = p[0]-center
y0 = p[1]-center
x1 = x0 * rot_vector.x - y0 * rot_vector.y
y1 = x0 * rot_vector.y + y0 * rot_vector.x
return (x1 + center, y1 + center)
new_shape = list(map(rotate, self.shape))
self.image.fill(self.image.get_colorkey())
pg.draw.polygon(self.image, self.COLOR, new_shape)
@property
def map_rect(self):
return pg.Rect(
(self.map_pos.x-self.SIZE/2+1, self.map_pos.y-self.SIZE/2+1),
(self.SIZE-2, self.SIZE-2))

@ -0,0 +1,15 @@
import pygame as pg
PINK = (255,0,255)
class Sprite(pg.sprite.Sprite):
def __init__(self, size):
super().__init__()
self.image = pg.Surface(size)
self.image.fill(PINK)
self.image.set_colorkey(PINK)
self.rect = self.image.get_rect()
Loading…
Cancel
Save