-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
87 lines (66 loc) · 2.85 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import pygame as pg
from constants import WINDOW_WIDTH, WINDOW_HEIGHT, ENEMY_SPAWN_EVENT, CLOCK_TICK
from models.player import Player
from models.map import Map
from engine.control import handle_key_up, handle_key_down
from models.space_ship import GalaxyFighter
from math import pi
def print_game_over(main_surface):
myfont = pg.font.SysFont("monospace", 100)
label = myfont.render("GAME OVER!", 1, (255, 0, 0))
main_surface.blit(label, (WINDOW_WIDTH / 2 - label.get_width() / 2 , WINDOW_HEIGHT / 2 - label.get_height() / 2))
pg.display.flip()
def reset_game():
space_ship = GalaxyFighter(50, 30, (255, 255, 255))
player = Player(space_ship)
game_map = Map(player)
return player, game_map
def main():
""" Set up the game and run the main game loop """
pg.init() # Prepare the pygame module for use
surface_height = WINDOW_HEIGHT # Desired physical surface size, in pixels.
surface_width = WINDOW_WIDTH # Desired physical surface size, in pixels.
clock = pg.time.Clock()
# Create surface of (width, height), and its window.
main_surface = pg.display.set_mode((surface_width, surface_height))
bg_color = (0, 0, 0) # A color is a mix of (Red, Green, Blue)
player, game_map = reset_game()
while True:
clock.tick(CLOCK_TICK)
ev = pg.event.poll() # Look for any event
keys = pg.key.get_pressed()
if ev.type == pg.QUIT or keys[pg.K_ESCAPE]: # Window close button clicked?
break # . .. leave game loop
if player == None and keys[pg.K_RETURN]:
player, game_map = reset_game()
elif ev.type == ENEMY_SPAWN_EVENT:
game_map.spawn_enemies()
# This block should contain key mapping that requires Player object
if player != None:
if keys[pg.K_LEFT]:
player.change_direction(-0.075)
if keys[pg.K_RIGHT]:
player.change_direction(0.075)
if keys[pg.K_UP]:
game_map.move_objects((player.direction_angle + pi) % (2*pi),
player.get_speed())
if ev.type == pg.KEYDOWN:
if ev.key == pg.K_SPACE:
game_map.bullets.append(player.fire())
game_map.update()
# We draw everything from scratch on each frame.
# So first fill everything with the background color
main_surface.fill(bg_color)
game_map.draw()
if player == None:
game_map.hud.print_game_over()
else:
player.draw()
# Now the surface is ready, tell pygame to display it!
pg.display.flip()
# This block must be after all drawing
if player != None and player.health <= 0:
player = None
continue
pg.quit() # Once we leave the loop, close the window.
main()