Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ verify_ssl = true

[dev-packages]
autopep8 = "*"
snakeviz = "*"

[packages]
pygame = "*"
Expand Down
24 changes: 23 additions & 1 deletion Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@ Dev Setup
2. Run `pip install pipenv` to install pipenv
3. Run `pipenv install` to bring in dependencies
4. Launch the game by running `pipenv run python main.py`

####Profiling
* To enable profiling, launch the game with the `--profile` option.
e.g.
`pipenv run python main.py --profile`
* To view a profile result, run the following command:
`pipenv run snakeviz <.prof file>`
10 changes: 5 additions & 5 deletions gamemaster.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@
from maincharacter import MainCharacter
from not_so_basic_grunt import NotSoBasicGrunt

class GameMaster():

def __init__(self, universe, spawn_controller, score_counter):
class GameMaster:
def __init__(self, universe, spawn_controller, score_counter, screen_size):
self.universe_ = universe
self.spawn_controller = spawn_controller
self.score_counter_ = score_counter
self.main_character_spawn_height_ = self.universe_.height_*0.9
self.spawn_main_character()
self.spawn_main_character(screen_size)

def draw(self, screen):
self.universe_.main_character_.draw_view(screen)
self.universe_.main_character_.draw_ui(screen)
self.score_counter_.draw(screen)

def spawn_main_character(self):
def spawn_main_character(self, screen_size):
starting_position = [
self.universe_.width_/2, self.main_character_spawn_height_]
the_main_character = MainCharacter(
starting_position, self.universe_)
starting_position, self.universe_, screen_size)
self.universe_.create_main_character(the_main_character)

def update(self, events):
Expand Down
62 changes: 32 additions & 30 deletions homing_missiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,21 @@
pygame.font.init()

BLACK = 0, 0, 0
DARK_GREEN = 0, 100, 0
GREEN = 0, 255, 0
RED = 255, 0, 0
WHITE = 255, 255, 255
YELLOW = 255, 255, 0
GRAY = 88, 88, 88
DARK_GREEN = 0, 100, 0

EVENT_KEY_a = 97
EVENT_KEY_BACKSPACE = 8
EVENT_KEY_ENTER = 13
EVENT_KEY_z = 122


class HomingMissilesTargetingSystem():

def __init__(self, universe):
class HomingMissilesTargetingSystem:
def __init__(self, universe, screen_size):
self.universe_ = universe

self.current_text_ = ""
Expand All @@ -45,6 +44,30 @@ def __init__(self, universe):
self.word_generator_ = WordGenerator()
self.word_length_min_ = 5
self.word_length_range_ = 3
self.grid = self._initialize_grid(screen_size[0], screen_size[1])

@staticmethod
def _initialize_grid(width, height):
grid_surface = pygame.Surface((width, height))

line_separation = 25
line_width = 1

for i in range(line_separation, width, line_separation):
pygame.draw.line(grid_surface,
DARK_GREEN,
(i, 0),
(i, height),
line_width)

for i in range(line_separation, height, line_separation):
pygame.draw.line(grid_surface,
DARK_GREEN,
(0, i),
(width, i),
line_width)

return grid_surface

def get_target_id(self, terminal_input):
for enemy in self.universe_.enemies():
Expand Down Expand Up @@ -103,15 +126,14 @@ def update(self, events):
"""

def draw(self, screen):
self.draw_background(screen)
self.draw_grid(screen)
self.draw_entities(screen)
self.draw_target_tags(screen)
self.draw_targets(screen)
self.targeting_terminal_.draw_terminal(screen)

def draw_background(self, screen):
pygame.draw.rect(screen, BLACK, pygame.Rect((0, 0), screen.get_size()))
def draw_grid(self, screen):
screen.blit(self.grid, (0, 0))

def draw_entities(self, screen):
self.draw_friendly_projectiles(screen)
Expand All @@ -137,26 +159,6 @@ def draw_main_character(self, screen):
main_character.size_)
pygame.draw.rect(screen, self.main_character_color_, main_character_rect)

def draw_grid(self, screen):
height = screen.get_height()
width = screen.get_width()
line_separation = 25
line_width = 1

for i in range(line_separation, width, line_separation):
pygame.draw.line(screen,
DARK_GREEN,
(i, 0),
(i, height),
line_width)

for i in range(line_separation, height, line_separation):
pygame.draw.line(screen,
DARK_GREEN,
(0, i),
(width, i),
line_width)

def draw_target_tags(self, screen):
for enemy in self.universe_.enemies():
enemy_color = self.enemy_colors_.get(enemy.get_type(), self.default_enemy_color_)
Expand Down Expand Up @@ -267,13 +269,13 @@ def report_destroyed(self):
def register(self, listeners):
self.listeners_.append(listeners)

class HomingMissiles():

def __init__(self, universe):
class HomingMissiles:
def __init__(self, universe, screen_size):
self.universe_ = universe

self.NAME_ = "Homing Missiles"
self.targeting_system = HomingMissilesTargetingSystem(universe)
self.targeting_system = HomingMissilesTargetingSystem(universe, screen_size)

def update(self, events):
self.targeting_system.update(events)
Expand Down
49 changes: 33 additions & 16 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,51 @@
import cProfile
import sys

import time
import argparse
import pygame

from main_menu import main_menu
from terminalfighter import terminalfighter

pygame.init()
GAME_WIDTH = 1000
GAME_HEIGHT = 700

PROFILE_FILE_NAME = "terminalfighter"


def exit_game(screen):
sys.exit()

GAME_WIDTH = 1000
GAME_HEIGHT = 700

# pygame ticks, one tick is 1/1000 second
# 16 pygame ticks per update is approximately 62.5 updates per second
FRAME_LENGTH_TICKS = 16

screen = pygame.display.set_mode(
(int(GAME_WIDTH), int(GAME_HEIGHT)))

run_game_state = {
"MENU" : main_menu,
"PLAY" : terminalfighter,
"QUIT" : exit_game
"MENU": main_menu,
"PLAY": terminalfighter,
"QUIT": exit_game
}

gamestate = "MENU"


while True:
gamestate = run_game_state.get(gamestate, exit_game)(screen, FRAME_LENGTH_TICKS)

if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("-p", "--profile", action='store_true',
help="Enabling profiling. Profiling output is dumped into terminalfighter_<timestamp>")
args = parser.parse_args()
pr = cProfile.Profile()
if args.profile:
# Start profiling
print("Starting with profiling enabled")
pr.enable()
try:
pygame.init()
screen = pygame.display.set_mode((int(GAME_WIDTH), int(GAME_HEIGHT)))
gamestate = "MENU"
while True:
gamestate = run_game_state.get(gamestate, exit_game)(screen, FRAME_LENGTH_TICKS)
finally:
if args.profile:
pr.disable()
prof_file = "%s_%d.prof" % (PROFILE_FILE_NAME, int(time.time()))
print("Dumping profiling results to %s" % prof_file)
pr.dump_stats(prof_file)
24 changes: 12 additions & 12 deletions main_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@

pygame.init()

GAME_WIDTH = 1000
GAME_HEIGHT = 700

WHITE = 255, 255, 255
BLACK = 0, 0, 0

HIGH_SCORE_FILE = "HighScore.txt"


class SelectionListener(DestroyListener):

def __init__(self):
Expand All @@ -36,11 +34,13 @@ def reported_destroyed(self, type_gameobject):
if not self.selected_:
self.selected_ = self.selections_[type_gameobject.id_]

def spawn_main_character(universe):
the_main_character = MainCharacter([universe.width_/2, universe.height_*0.9], universe)

def spawn_main_character(universe, screen_size):
the_main_character = MainCharacter([universe.width_/2, universe.height_*0.9], universe, screen_size)
the_main_character.weapons_ = the_main_character.weapons_[:1]
universe.create_main_character(the_main_character)


def spawn_selection(starting_pos, name, universe, selection_listener):
the_selection = BasicGrunt(starting_pos, universe)
the_selection.speed_ = 0
Expand All @@ -50,14 +50,14 @@ def spawn_selection(starting_pos, name, universe, selection_listener):


def main_menu(screen, frame_length_ticks):
universe = Universe((GAME_WIDTH, GAME_HEIGHT))
universe = Universe(screen.get_size())
selection_listener = SelectionListener()
ui_font_ = pygame.font.SysFont("monospace", 30)
LABEL_SPACING = 50
play_position = [GAME_WIDTH*(1/3), GAME_HEIGHT*(1/4)]
quit_position = [GAME_WIDTH*(2/3), GAME_HEIGHT*(1/4)]
label_spacing = 50
play_position = [screen.get_width() * (1/3), screen.get_height() * (1/4)]
quit_position = [screen.get_width() * (2/3), screen.get_height() * (1/4)]

spawn_main_character(universe)
spawn_main_character(universe, screen.get_size())
spawn_selection(play_position, "PLAY", universe, selection_listener)
spawn_selection(quit_position, "QUIT", universe, selection_listener)
highscore = highscore_service.get_highscore()
Expand Down Expand Up @@ -86,12 +86,12 @@ def main_menu(screen, frame_length_ticks):
play_label = ui_font_.render("PLAY", 1, WHITE)
screen.blit(play_label,
(play_position[0] - (play_label.get_width()/2),
play_position[1] - LABEL_SPACING))
play_position[1] - label_spacing))

quit_label = ui_font_.render("QUIT", 1, WHITE)
screen.blit(quit_label,
(quit_position[0] - (quit_label.get_width()/2),
quit_position[1] - LABEL_SPACING))
quit_position[1] - label_spacing))

instruction_label = ui_font_.render("TYPE TO SHOOT!", 1, WHITE)
screen.blit(instruction_label,
Expand Down
7 changes: 3 additions & 4 deletions maincharacter.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@

class MainCharacter(GameObject):

def __init__(self, starting_position, universe):
def __init__(self, starting_position, universe, screen_size):
self.position_ = starting_position
self.universe_ = universe

self.font_size_ = 22
self.id_ = self.create_ID()
self.max_health_ = 100
Expand All @@ -31,8 +30,8 @@ def __init__(self, starting_position, universe):
self.ui_font_ = pygame.font.SysFont("monospace", self.font_size_)
self.weapon_label_x_spacing_ = 5
self.weapon_label_y_spacing_ = 10
self.weapons_ = [Rifle(self.universe_),
HomingMissiles(self.universe_)]
self.weapons_ = [Rifle(self.universe_, screen_size),
HomingMissiles(self.universe_, screen_size)]

self.current_weapon_ = self.weapons_[self.selected_weapon_index_]
self.health_ = self.max_health_
Expand Down
Loading