def test_full_ammo(self): survivor = Survivor(0, 0) survivor.ammo_count = [0, 0, 0, 0] full_ammo(survivor) self.assertEqual(survivor.ammo_count, list(survivor.init_ammo_count)) survivor.ammo_count[0] += 1 full_ammo(survivor) self.assertNotEqual(survivor.ammo_count, list(survivor.init_ammo_count))
def test_quad_damage(self): survivor = Survivor(0, 0) bullet = Bullet(Vector(0, 0), Vector(3, 0), 0, survivor) bullet.pos += bullet.vel before_4x = bullet.calc_dmg() Drop.actives["4x"] = 1 after4x = bullet.calc_dmg() self.assertEqual(after4x / before_4x, 4.0) self.assertTrue(after4x > before_4x)
def __init__(self): """Constructor function""" # Initialize the pygame module, class instances, and screen/screen_rect size pygame.init() self.settings = Settings() self.screen = pygame.display.set_mode( (self.settings.width, self.settings.height)) self.screen_rect = self.screen.get_rect() self.survivor = Survivor(self) self.zombies = pygame.sprite.Group() self.bullets = pygame.sprite.Group() self._add_zombie() self.slow_mo_reload_event = pygame.USEREVENT + 1 self.spawn_enemy = pygame.USEREVENT + 2 pygame.time.set_timer(self.slow_mo_reload_event, 5000) pygame.time.set_timer(self.spawn_enemy, 5000) self.myFont = pygame.font.SysFont("Times New Roman", 18)
def main(): """initiate tiles, survival, clock and start music and run main loop""" Tile.create() survivor = Survivor(*Tile.random_open_tile()) clock = pygame.time.Clock() logging.debug("options: %s", Options.__dict__) logging.debug("monitor: w=%s, h=%s", Options.monitor_w, Options.monitor_h) pygame.mixer.music.play(loops=-1) main_loop(survivor, clock) game_over(display, Zombie.level)
def popSeek(self, dinner, venom, predator=None): '''Makes every survivor search for the food in the world.''' for surv in self.pop: surv.hunting(dinner, venom) surv.update() if random() < 0.000001 * surv.score: self.pop.append( Survivor(surv.perception[:], surv.atraction[:], self.debug)) if surv.health < 0: self.pop.remove(surv)
def insertSurvivor(self, new_survivor_inf): if 'name' in new_survivor_inf and 'age' in new_survivor_inf and 'gender' in new_survivor_inf: if 'lastLocation' in new_survivor_inf and 'inventory' in new_survivor_inf: new_survivor_obj = Survivor( 0, new_survivor_inf['name'], new_survivor_inf['age'], new_survivor_inf['gender'], new_survivor_inf['lastLocation']['x'], new_survivor_inf['lastLocation']['y'], new_survivor_inf['inventory']) new_survivor_obj = self.insert(new_survivor_obj) return new_survivor_obj return None
def run_game(): # initializing pygame, settings, screen object pygame.init() settings = Settings() screen = pygame.display.set_mode((settings.width, settings.height)) pygame.display.set_caption('Zombies Game') # initiate data on status and scoreboard stats = GameStats(settings) sb = Scoreboard(settings, screen, stats) # initiate the play button play_button = Button(settings, screen, "Play") # initialize the survivor,bullets,and zombies survivor = Survivor(settings, screen) bullets = Group() zombies = Group() # create the horde gf.create_horde(settings, screen, zombies, survivor) while True: gf.check_events(settings, screen, stats, play_button, sb, survivor, zombies, bullets) if stats.game_active: survivor.update() gf.update_bullets(settings, screen, stats, sb, survivor, bullets, zombies) gf.update_zombie(settings, stats, screen, survivor, zombies, bullets) gf.update_screen(screen, settings, sb, survivor, stats, bullets, zombies, play_button) survivor.blitme() survivor.update() pygame.display.flip()
def addSurvivor(self): '''Adds a new survivor to the population.''' self.pop.append(Survivor(debug=self.debug))
def makePopulation(self): '''Creates a list of survivors with lenght of maxpop.''' self.pop = [Survivor(debug=self.debug) for _ in range(self.maxpop)]
class Zombify: """Class that manages game's overall functionality.""" def __init__(self): """Constructor function""" # Initialize the pygame module, class instances, and screen/screen_rect size pygame.init() self.settings = Settings() self.screen = pygame.display.set_mode( (self.settings.width, self.settings.height)) self.screen_rect = self.screen.get_rect() self.survivor = Survivor(self) self.zombies = pygame.sprite.Group() self.bullets = pygame.sprite.Group() self._add_zombie() self.slow_mo_reload_event = pygame.USEREVENT + 1 self.spawn_enemy = pygame.USEREVENT + 2 pygame.time.set_timer(self.slow_mo_reload_event, 5000) pygame.time.set_timer(self.spawn_enemy, 5000) self.myFont = pygame.font.SysFont("Times New Roman", 18) def run_game(self): """Runs main loop for the game""" # Main loop for the game while 1: self._event_handler() self.survivor.update_survivor() self.bullets.update() self.zombies.update(self) self._remove_bullets() self._update_screen() def _update_screen(self): """Draw and update the screen""" self.screen.fill(self.settings.color) self.screen.blit(self.survivor.image_copy, self.survivor.image_rect) #self.screen.blit(self.zombie.image, self.zombie.image_rect) self._onscreen_text() self.screen.blit( self.label1, (self.screen_rect.left + 20, self.screen_rect.top + 20)) self.screen.blit( self.label2, (self.screen_rect.left + 170, self.screen_rect.top + 20)) self.screen.blit( self.label3, (self.screen_rect.right - 175, self.screen_rect.top + 20)) self.screen.blit( self.label4, (self.screen_rect.right - 50, self.screen_rect.top + 20)) for bullet in self.bullets.sprites(): bullet.draw_bullet() #self.zombies.draw(self.screen) for zombie in self.zombies.sprites(): zombie.draw_zombie() pygame.display.flip() def _add_bullet(self): """Adds bullet to screen.""" # Makes sure there is no more than one bullet on screen if len(self.bullets) < self.settings.bullets_allowed: new_bullet = Bullet(self) self.bullets.add(new_bullet) self.settings.bullet_available = False def _add_zombie(self): """Adds zombie to screen.""" zombie = Zombie(self) self.zombies.add(zombie) def _remove_bullets(self): """Removes bullet object if it leaves bounds of the screen.""" for bullet in self.bullets.copy(): if bullet.rect.bottom <= self.screen_rect.top or bullet.rect.right <= self.screen_rect.left or bullet.rect.top >= self.screen_rect.bottom or bullet.rect.left >= self.screen_rect.right: self.bullets.remove(bullet) self.settings.bullet_available = True collisions = pygame.sprite.groupcollide(self.bullets, self.zombies, True, True) #print(len(self.bullets)) def _event_handler(self): """Manages events such as keystrokes or mouse clicks.""" for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == self.slow_mo_reload_event: self.survivor.slow_mo_available = True elif event.type == self.spawn_enemy: self._add_zombie() # Handles events when key is pressed elif event.type == pygame.KEYDOWN: if event.key == pygame.K_p: sys.exit() if event.key == pygame.K_d: self.survivor.moving_right = True if event.key == pygame.K_a: self.survivor.moving_left = True if event.key == pygame.K_SPACE or event.key == pygame.K_w: self.survivor.is_jumping = True if event.key == pygame.K_LSHIFT: self.survivor.slow_mo = True #self.test = pygame.get_ticks() # Handles events when key is released elif event.type == pygame.KEYUP: if event.key == pygame.K_d: self.survivor.moving_right = False if event.key == pygame.K_a: self.survivor.moving_left = False if event.key == pygame.K_LSHIFT: self.survivor.slow_mo = False self.survivor.slow_mo_available = False elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: self._add_bullet() def _onscreen_text(self): """Renders text onto player's screen""" self.label1 = self.myFont.render("Slow-Mo Available: ", 1, (0, 0, 0)) if self.survivor.slow_mo_available: self.label2 = self.myFont.render( f"{self.survivor.slow_mo_available}", 1, (0, 200, 0)) else: self.label2 = self.myFont.render( f"{self.survivor.slow_mo_available}", 1, (200, 0, 0)) self.label3 = self.myFont.render("Bullet Available: ", 1, (0, 0, 0)) if self.settings.bullet_available: self.label4 = self.myFont.render( f"{self.settings.bullet_available}", 1, (0, 200, 0)) else: self.label4 = self.myFont.render( f"{self.settings.bullet_available}", 1, (200, 0, 0))
def dataToSurvivor(self, survData): return Survivor(survData['_id'], survData['name'], survData['age'], survData['gender'], survData['lastLocation']['x'], survData['lastLocation']['y'], survData['inventory'], survData['infectionReports'])