mian 2 years ago
parent bd6a3f7492
commit 956b8cf83f
  1. 50
      main.py
  2. 21
      ship.py
  3. 10
      starfield.py
  4. 31
      torpedo.py

@ -4,16 +4,17 @@ import pygame as pg
# locals
from ship import Ship
from starfield import Starfield
from torpedo import Torpedo
# Colors
BLACK = (0,)*3
WHITE = (255,)*3
# Graphical Environment
WIN_SIZE = (640, 480)
WIN_SIZE = (1280, 960)
MAX_FPS = 60
# Game constants
# Sprite positions
@ -29,24 +30,28 @@ pg.display.set_caption(f"Space Collector")
screen = pg.display.set_mode(WIN_SIZE)
scr_rect = screen.get_rect()
pg.mouse.set_cursor(pg.SYSTEM_CURSOR_CROSSHAIR)
map_rect = pg.Rect(
(start[0]-scr_rect.width/2,
start[1]-scr_rect.height/2),
scr_rect.size
)
the_starfield = Starfield(screen);
the_starfield = Starfield(screen, grid_step=40, paralax=1);
the_ship = Ship(start,scr_rect)
the_ship.rect.x = scr_rect.centerx - the_ship.rect.width / 2
the_ship.rect.y = scr_rect.centery - the_ship.rect.height / 2
disp_sprites = pg.sprite.Group()
#moving = pg.sprite.Group(gems,blocks)
moving = pg.sprite.Group()
# Game loop
carry_on = True
win = False
cooling = 0
while carry_on:
@ -60,40 +65,39 @@ while carry_on:
### Game Logic ###
mouse_events = pg.mouse.get_pressed()
if mouse_events[0]:
the_ship.deceleration = 0
the_ship.velocity += 1
if mouse_events[2]:
the_ship.deceleration = 1
mousev = pg.math.Vector2(pg.mouse.get_pos())
mousev -= scr_rect.center
# ship movement
keyboard_events = pg.key.get_pressed()
# ship direction
if mousev.length() > the_ship.SIZE/2:
the_ship.direction += mousev.normalize()/(the_ship.velocity+1)
the_ship.direction += mousev.normalize()
the_ship.direction.normalize_ip()
the_ship.velocity = max(the_ship.velocity - the_ship.deceleration, 0)
if the_ship.velocity == 0:
the_ship.deceleration = 0
if keyboard_events[pg.key.key_code('w')]:
the_ship.velocity += the_ship.direction / 2**0.5
# Move ship
the_ship.change = (the_ship.velocity*the_ship.direction)
the_ship.map_pos.x += round(the_ship.change.x)
the_ship.map_pos.y += round(the_ship.change.y)
the_ship.map_pos.x += round(the_ship.velocity.x) % map_rect.w
the_ship.map_pos.y += round(the_ship.velocity.y) % map_rect.h
# Move camera to follow ship
map_rect.x = the_ship.map_pos.x - map_rect.w/2
map_rect.y = the_ship.map_pos.y - map_rect.h/2
map_rect.x = (the_ship.map_pos.x - map_rect.w/2) % map_rect.w
map_rect.y = (the_ship.map_pos.y - map_rect.h/2) % map_rect.h
# Torpedoes
if keyboard_events[pg.key.key_code("space")] and cooling <= 0:
disp_sprites.add(Torpedo(scr_rect, the_ship.map_pos, the_ship.direction, the_ship.velocity))
cooling = Torpedo.COOLTIME
elif cooling > 0:
cooling -= 1
### Display Logic ###
screen.fill(BLACK)
the_starfield.draw(screen, map_rect)
the_starfield.draw(screen, map_rect, (map_rect.w, map_rect.h))
disp_sprites.update(map_rect=map_rect)
disp_sprites.draw(screen)

@ -1,23 +1,24 @@
import pygame as pg
from sprite import Sprite
COLOR =(0,255,0)
SIZE = 40
U = SIZE // 6
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)
super().__init__((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.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.velocity = pg.math.Vector2((0,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
@ -29,10 +30,10 @@ class Ship(Sprite):
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)
pg.draw.polygon(self.image, 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))
(self.map_pos.x-SIZE/2+1, self.map_pos.y-SIZE/2+1),
(SIZE-2, SIZE-2))

@ -24,15 +24,17 @@ class Starfield:
self.star_color = star_color
self.star_size = star_size
def draw(self, screen, map_pos):
def draw(self, screen, map_pos, map_period):
sky_pos = pg.math.Vector2((map_pos.x, map_pos.y))/self.paralax
xstart = snap(sky_pos.x, self.grid_step)
ystart = snap(sky_pos.y, self.grid_step)
xstop = xstart + screen.get_rect().width
ystop = ystart + screen.get_rect().width
srect = screen.get_rect()
xstop = xstart + srect.width
ystop = ystart + srect.height
for x in range(xstart, xstop, self.grid_step):
for y in range(ystart, ystop, self.grid_step):
self.rng.seed(hash((x,y)))
self.rng.seed(hash((
x % map_period[0], y % map_period[1])))
if self.rng.randint(0, self.inv_density) == 0:
xoff = self.rng.randint(0, self.grid_step)
yoff = self.rng.randint(0, self.grid_step)

@ -0,0 +1,31 @@
import pygame as pg
from sprite import Sprite
SPEED = 4
COLOR = (255,0,0)
SIZE = 12
LIFETIME = 60
class Torpedo(Sprite):
COOLTIME = 15
def __init__(self, scr_rect, map_pos, direction, velocity):
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())
pg.draw.circle(self.image, COLOR, self.image.get_rect().center, self.image.get_rect().width/2)
self.map_pos = pg.math.Vector2(map_pos)
self.velocity = pg.math.Vector2(direction) * SPEED + velocity
self.lifetime = LIFETIME
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
self.lifetime -= 1
if self.lifetime == 0:
self.kill()
Loading…
Cancel
Save