forked from amalkhatib90/Gibbonga-Part1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
154 lines (121 loc) · 4.44 KB
/
game.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
## @file game.py
# Source file for game class
#
# Project: Gallaga Clone
# Author: Py Five
# Created: 10/14/19
import pygame
import os.path
import sys
import random
from player import Player
from enemy import Enemy
from shot import Shot
from pygame.locals import *
import constants as const
## @class Game
# @brief Runs the game session and manages all actors
class Game:
## Constructor
# @post: Game components have been initialized
def __init__(self):
# Initialize pygame
pygame.init()
# Initialize member variables
self.screen = pygame.display.set_mode(const.SCREENRECT.size, 0)
self.clock = pygame.time.Clock()
self.quit = False
# Setup Game Window
icon = pygame.image.load('assets/images/player_ship.png')
icon = pygame.transform.scale(icon, (60, 80))
pygame.display.set_icon(icon)
pygame.display.set_caption('Gallaga Clone')
pygame.mouse.set_visible(0)
## Loads and scales object/game image
# @author: Kristi
# @pre: image exists
# @param: filename, name of image to be loaded
# @param: width, desired width of image
# @param: height, desired height of image
# @returns: Surface object
def load_image(self, filename, file_width, file_height):
# Load image
filename = os.path.join('assets/images', filename)
img = pygame.image.load(filename)
# Scale image
img = pygame.transform.scale(img, (file_width, file_height))
# Make transparent
img.set_colorkey(img.get_at((0,0)), RLEACCEL)
return img.convert()
## Runs the game session
# @pre: Game components have been initialized
# @post: Game has been exited properly
def run(self):
# Load Images
background_img = pygame.image.load('assets/images/space.jpg')
player_img = self.load_image('player_ship.png', 45, 65)
enemy_img = self.load_image('enemy_spaceship.png', 26, 26)
shot_img = self.load_image('missile1.png', 10, 24)
# Load Background
background = pygame.Surface(const.SCREENRECT.size)
for x in range(0, const.SCREENRECT.width, background_img.get_width()):
background.blit(background_img, (x, 0))
self.screen.blit(background, (0, 0))
pygame.display.flip()
# Initialize Starting Actors
player = Player(player_img)
enemies = [Enemy(enemy_img)]
shots = []
actors = []
# Game loop
while player.alive and not self.quit:
self.clock.tick(const.FPS)
# Call event queue
pygame.event.pump()
# Process input
key_presses = pygame.key.get_pressed()
right = key_presses[pygame.K_RIGHT]
left = key_presses[pygame.K_LEFT]
shoot = key_presses[pygame.K_SPACE]
exit = key_presses[pygame.K_q]
# Check for quit conditions
if pygame.event.peek(QUIT) or exit:
self.quit = True
break
# Update actors
for actor in [player] + enemies + shots:
render = actor.erase(self.screen, background)
actors.append(render)
actor.update()
# Remove out-of-frame shots
for shot in shots:
if shot.rect.top <= 0:
shots.remove(shot)
# Move the player
x_dir = right - left
player.move(x_dir)
# Create new shots
if not player.reloading and shoot and len(shots) < const.MAX_SHOTS:
shots.append(Shot(shot_img, player))
player.reloading = shoot
# Create new alien
if not int(random.random() * const.ENEMY_ODDS):
enemies.append(Enemy(enemy_img))
# Check for collisions
for enemy in enemies:
if enemy.collision_check(player):
player.alive = False
for shot in shots:
if shot.collision_check(enemy):
enemies.remove(enemy)
# Draw actors
for actor in [player] + enemies + shots:
render = actor.draw(self.screen)
actors.append(render)
# Update actors
pygame.display.update(actors)
actors = []
# Exit game and system
pygame.display.quit()
pygame.quit()
sys.exit()