This repository has been archived by the owner on Oct 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_manager.py
109 lines (87 loc) · 4.26 KB
/
game_manager.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
from agent import Agent
from red_ai_pilot import RedAiPilot
from blue_player_pilot import BluePlayerPilot
from map_builder import MapBuilder
from environment import Environment
import pygame
# event values for handling during game play
SHOOTING = pygame.USEREVENT + 1
class GameManager(Agent):
"""The game_manager handles all agents responsible for making the game run"""
def __init__(self):
"""sets up the game variables and then initializes its employee agents"""
super(GameManager, self).__init__("game_manager")
self.play_window = Environment()
self.screen = None
# screen size
self.screen_height = 444
self.screen_width = 1116
self.running_game = True
# player list
self.player_list = pygame.sprite.Group()
self.play_window.add_object("player_list", self.player_list)
# holds bullet list
self.bullet_list = pygame.sprite.Group()
self.play_window.add_object("bullet_list", self.bullet_list)
# holds the sprites that make up the wall
self.wall_list = pygame.sprite.Group()
self.play_window.add_object("wall_list", self.wall_list)
# initialize agents and place them in the environment
self.map_builder = MapBuilder(self.play_window)
self.red_ai_pilot = RedAiPilot(self.play_window)
self.blue_player_pilot = BluePlayerPilot(self.play_window)
def build_walls(self):
"""calls the map builder agent to parse through the level file and create the map of the game"""
self.map_builder.build_arena()
self.wall_list = self.play_window.get_object("wall_list")
def check_bullet_collisions(self):
"""checks if any bullets have collided with objects and need to be removed"""
for bullet in self.bullet_list:
bullet.update_movement()
self.check_if_bullet_is_in_boundaries(bullet)
self.check_player_bullet_collision(bullet)
self.check_wall_bullet_collision(bullet)
def check_if_bullet_is_in_boundaries(self, bullet):
"""removes the bullet if it is no longer on the map"""
if bullet.rect.x < 0 or bullet.rect.x > 1116 or bullet.rect.y < 0 or bullet.rect.y > 444:
self.bullet_list.remove(bullet)
def check_player_bullet_collision(self, bullet):
"""checks if the bullet has collided with a player"""
for player in self.player_list:
if bullet.rect.colliderect(player):
player.hit_points -= 10
self.bullet_list.remove(bullet)
def check_wall_bullet_collision(self, bullet):
for wall_block in self.wall_list:
if bullet.rect.colliderect(wall_block):
self.bullet_list.remove(bullet)
def check_pygame_events(self):
"""checks any for events such as keys pressed or A.I. actions that change the state of the game"""
for e in pygame.event.get():
if e.type == pygame.QUIT:
self.running_game = False
if e.type == pygame.KEYDOWN and e.key == pygame.K_ESCAPE:
self.running_game = False
def draw(self):
"""displays the game images on the screen"""
self.screen.fill((0, 0, 0))
self.player_list.draw(self.screen)
self.wall_list.draw(self.screen)
self.bullet_list.draw(self.screen)
pygame.display.flip()
def initialize_screen(self):
# Set up the display
pygame.display.set_caption("BOT ARENA 3.0!")
self.screen = pygame.display.set_mode((self.screen_width, self.screen_height))
def run_game(self):
"""calls the player agent's to perform their moves and check's for bullet movement"""
self.check_pygame_events()
self.red_ai_pilot.make_decision()
self.blue_player_pilot.check_input_for_actions()
self.check_bullet_collisions()
self.draw()
def setup_players(self):
"""adds the player sprites to the list of players for reference and sets up the bots in their environment"""
self.player_list.add(self.red_ai_pilot.bot, self.blue_player_pilot.bot)
self.blue_player_pilot.setup_bot_map()
self.red_ai_pilot.setup_bot_map()