You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
125 lines
3.1 KiB
125 lines
3.1 KiB
#!/usr/bin/env python3
|
|
from sys import argv
|
|
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 = (1280, 960)
|
|
MAX_FPS = 60
|
|
|
|
|
|
# Game constants
|
|
|
|
# Sprite positions
|
|
start = (0,0)
|
|
|
|
# Setup
|
|
pg.init()
|
|
clock = pg.time.Clock()
|
|
|
|
font = pg.font.SysFont('Courier',24)
|
|
|
|
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, 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()
|
|
|
|
# Game loop
|
|
carry_on = True
|
|
win = False
|
|
cooling = 0
|
|
|
|
while carry_on:
|
|
|
|
# Quitting states
|
|
for event in pg.event.get():
|
|
if event.type == pg.QUIT:
|
|
carry_on = False
|
|
elif event.type==pg.KEYDOWN:
|
|
if event.key==pg.K_ESCAPE:
|
|
carry_on = False
|
|
|
|
### Game Logic ###
|
|
|
|
mousev = pg.math.Vector2(pg.mouse.get_pos())
|
|
mousev -= scr_rect.center
|
|
|
|
keyboard_events = pg.key.get_pressed()
|
|
|
|
# ship direction
|
|
if mousev.length() > the_ship.SIZE/2:
|
|
the_ship.direction += mousev.normalize()
|
|
the_ship.direction.normalize_ip()
|
|
|
|
if keyboard_events[pg.key.key_code('w')]:
|
|
the_ship.velocity += the_ship.direction / 2**0.5
|
|
|
|
|
|
# Move ship
|
|
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.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, (map_rect.w, map_rect.h))
|
|
|
|
disp_sprites.update(map_rect=map_rect)
|
|
disp_sprites.draw(screen)
|
|
|
|
the_ship.update(rot_vector= the_ship.direction)
|
|
screen.blit(the_ship.image, the_ship.rect)
|
|
|
|
if True:
|
|
elapsed_time = pg.time.get_ticks()/1000
|
|
elapsed_minutes = int(elapsed_time // 60)
|
|
elapsed_seconds = int(elapsed_time // 1) % 60
|
|
|
|
time_string = f"{elapsed_minutes:d}:{elapsed_seconds:02d}"
|
|
time_label = font.render(time_string, 1, WHITE)
|
|
time_pos = (screen.get_width()-10-time_label.get_width(),10)
|
|
screen.blit(time_label, time_pos)
|
|
|
|
gem_str = f"Level Complete!"
|
|
gem_label = font.render(gem_str,True, WHITE)
|
|
screen.blit(gem_label, (10, 10))
|
|
|
|
pg.display.flip()
|
|
clock.tick(MAX_FPS)
|
|
|
|
pg.quit()
|
|
|