def test_add_more_items_than_limit(self): for i in range(8): self.inventory.inventory_manager.add_item( gameobjects.Item((0, 0), (0, 0), 'test-item-{}'.format(i), 1, True, None, self.sprites, 'test')) item_9 = gameobjects.Item((0, 0), (0, 0), 'test-item-9', 1, True, None, self.sprites, 'test') self.inventory.inventory_manager.add_item(item_9) self.assertNotIn(item_9, self.inventory.inventory_manager.items) self.assertEqual(len(self.inventory.inventory_manager.items), 8)
def test_items_with_quantity_zero_are_removed_from_inventory(self): item_1 = gameobjects.Item((0, 0), (0, 0), 'test-item-1', 1, True, None, self.sprites, 'test') item_2 = gameobjects.Item((0, 0), (0, 0), 'test-item-2', 1, True, None, self.sprites, 'test') self.inventory.inventory_manager.add_item(item_1) self.inventory.inventory_manager.add_item(item_2) item_2.quantity = 0 self.inventory.inventory_manager.remove_spent_items() self.assertIn(item_1, self.inventory.inventory_manager.items) self.assertNotIn(item_2, self.inventory.inventory_manager.items)
def test_item_does_not_become_equipped_when_another_item_is_equipped(self): item_1 = gameobjects.Item((0, 0), (0, 0), 'test-item-1', 1, True, None, self.sprites, 'test') item_2 = gameobjects.Item((0, 0), (0, 0), 'test-item-2', 1, True, None, self.sprites, 'test') self.inventory.inventory_manager.add_item(item_1) self.inventory.inventory_manager.add_item(item_2) self.inventory.inventory_manager.equip_item(item_1) self.inventory.inventory_manager.equip_item(item_2) self.assertIsNot(item_2, self.inventory.inventory_manager.equipped_item)
def test_add_item_already_in_inventory(self): item_1 = gameobjects.Item((0, 0), (0, 0), 'test-item', 1, True, None, self.sprites, 'test') item_2 = gameobjects.Item((0, 0), (0, 0), 'test-item', 1, True, None, self.sprites, 'test') self.inventory.inventory_manager.add_item(item_1) self.inventory.inventory_manager.add_item(item_2) self.assertIn(item_1, self.inventory.inventory_manager.items) self.assertNotIn(item_2, self.inventory.inventory_manager.items) self.assertEqual(len(self.inventory.inventory_manager.items), 1) self.assertEqual(self.inventory.inventory_manager.items[0].quantity, 2)
def test_dropping_item_not_in_inventory_has_no_effect(self): drop_location = (64, 64) item_1 = gameobjects.Item((0, 0), (0, 0), 'test-item-1', 1, True, None, self.sprites, 'test') item_2 = gameobjects.Item((0, 0), (0, 0), 'test-item-2', 1, True, None, self.sprites, 'test') self.inventory.inventory_manager.add_item(item_1) i = self.inventory.inventory_manager.drop_item_at_location( item_2, drop_location) self.assertEqual(self.inventory.inventory_manager.items, self.inventory.inventory_manager.items) self.assertIs(i, None)
def test_cannot_unequip_item_if_inventory_is_full(self): item = gameobjects.Item((0, 0), (0, 0), 'test-item', 1, True, None, self.sprites, 'test') self.inventory.inventory_manager.add_item(item) self.inventory.inventory_manager.equip_item(item) for i in range(8): self.inventory.inventory_manager.add_item( gameobjects.Item((0, 0), (0, 0), 'test-item-{}'.format(i), 1, True, None, self.sprites, 'test')) self.inventory.inventory_manager.unequip_item(item) self.assertNotIn(item, self.inventory.inventory_manager.items) self.assertIs(self.inventory.inventory_manager.equipped_item, item)
def test_cannot_unequip_item_that_is_not_equipped(self): item_1 = gameobjects.Item((0, 0), (0, 0), 'test-item-1', 1, True, None, self.sprites, 'test') item_2 = gameobjects.Item((0, 0), (0, 0), 'test-item-2', 1, True, None, self.sprites, 'test') self.inventory.inventory_manager.add_item(item_1) self.inventory.inventory_manager.add_item(item_2) self.inventory.inventory_manager.equip_item(item_1) self.inventory.inventory_manager.unequip_item(item_2) self.assertIs(self.inventory.inventory_manager.equipped_item, item_1) self.assertIn(item_2, self.inventory.inventory_manager.items) self.assertEqual(len(self.inventory.inventory_manager.items), 1)
def test_item_only_equips_if_it_is_equippable(self): item = gameobjects.Item((0, 0), (0, 0), 'test-item', 1, False, None, self.sprites, 'test') self.inventory.inventory_manager.add_item(item) self.inventory.inventory_manager.equip_item(item) self.assertIn(item, self.inventory.inventory_manager.items) self.assertIsNot(item, self.inventory.inventory_manager.equipped_item)
def test_dropped_item_is_returned(self): item = gameobjects.Item((0, 0), (0, 0), 'test-item', 1, True, None, self.sprites, 'test') self.inventory.inventory_manager.add_item(item) i = self.inventory.inventory_manager.drop_item_at_location( item, (0, 0)) self.assertIs(item, i)
def test_item_becomes_equipped(self): item = gameobjects.Item((0, 0), (0, 0), 'test-item', 1, True, None, self.sprites, 'test') self.inventory.inventory_manager.add_item(item) self.assertIsNot(item, self.inventory.inventory_manager.equipped_item) self.inventory.inventory_manager.equip_item(item) self.assertIs(item, self.inventory.inventory_manager.equipped_item)
def test_unequip_item(self): item = gameobjects.Item((0, 0), (0, 0), 'test-item', 1, True, None, self.sprites, 'test') self.inventory.inventory_manager.add_item(item) self.inventory.inventory_manager.equip_item(item) self.inventory.inventory_manager.unequip_item(item) self.assertIn(item, self.inventory.inventory_manager.items) self.assertIs(self.inventory.inventory_manager.equipped_item, None)
def test_dropped_item_is_in_proper_location(self): drop_location = (64, 64) item = gameobjects.Item((0, 0), (0, 0), 'test-item', 1, True, None, self.sprites, 'test') self.inventory.inventory_manager.add_item(item) self.inventory.inventory_manager.drop_item_at_location( item, drop_location) self.assertEqual(item.rect.topleft, drop_location)
def test_equipped_item_is_in_proper_location(self): item = gameobjects.Item((0, 0), (0, 0), 'test-item', 1, True, None, self.sprites, 'test') self.inventory.inventory_manager.add_item(item) self.inventory.inventory_manager.equip_item(item) self.inventory.inventory_manager.update_item_locations() self.assertEqual( item.rect.topleft, self.inventory.inventory_manager.equipped_item_location)
def test_added_items_are_in_proper_locations(self): for i in range(8): self.inventory.inventory_manager.add_item( gameobjects.Item((0, 0), (0, 0), 'test-item-{}'.format(i), 1, True, None, self.sprites, 'test')) self.inventory.inventory_manager.update_item_locations() for item, location in zip( self.inventory.inventory_manager.items, self.inventory.inventory_manager.item_locations): self.assertEqual(item.rect.topleft, (self.inventory.rect.x + location[0], self.inventory.rect.y + location[1]))
def main(): pygame.init() pygame.display.set_caption('Horror Game Dev!') screen_size = (640, 480) displaysurf = pygame.display.set_mode(screen_size) fps_clock = pygame.time.Clock() fps = 30 #-----# #ITEMS# #-----# key = gameobjects.Item((160, 160), (32, 32), 'key', 1) #-----# #-------# #WEAPONS# #-------# pistol = gameobjects.Weapon((96, 96), (32, 32), 'pistol', 'ammo-pistol', 25, 20) axe = gameobjects.Weapon((160, 96), (32, 32), 'axe', None, 10, 1) #-------# #----# #AMMO# #----# pistol_ammo = gameobjects.Item((96, 96), (32, 32), 'ammo-pistol', 15) #----# #------# #PLAYER# #------# player = gameobjects.Player((0, 0), (32, 32), 'player', DOWN, 2) player_interactor = gameobjects.Interactor((player.rect.x - 32, player.rect.y - 32), (96, 96), 'player-interactor') player_moveable_interactor = gameobjects.Interactor((player.rect.x - 4, player.rect.y - 4), (40, 40), 'player-moveable-interactor') player_inventory = gameobjects.Inventory((0, 0), (0, 0), 'inventory-player', [pistol_ammo, key], 8, pistol) #------# blockers = [ # gameobjects.Prop((96, 128), (32, 32), 'lock'), #gameobjects.Prop((64, 128), (32, 32), 'lock'), gameobjects.MoveableProp((128, 256), (32, 32), 'moveable') ] moveables = [blocker for blocker in blockers if blocker.tag == 'moveable'] enemies = [ #gameobjects.Enemy((128, 128), (32, 32), 'zombie', LEFT, 2, 100), #gameobjects.Enemy((160, 128), (32, 32), 'zombie', LEFT, 2, 50) ] bullets = [] while True: pygame.event.pump() ##-----## ##INPUT## ##-----## keyboard = pygame.key.get_pressed() ##-----## ##------## ##UPDATE## ##------## #--------------# #SET COLLISIONS# #--------------# player_interactor.collision_handler.set_collisions(['blockers'], [blockers]) player_moveable_interactor.collision_handler.set_collisions(['moveables'], [moveables]) #--------------# if keyboard[pygame.K_UP]: player.direction = UP player.mover.move(0, -player.move_speed) if player.rect.topleft[1] < 0: player.mover.move(0, player.move_speed) elif keyboard[pygame.K_DOWN]: player.direction = DOWN player.mover.move(0, player.move_speed) if player.rect.bottomright[1] > screen_size[1]: player.mover.move(0, -player.move_speed) elif keyboard[pygame.K_LEFT]: player.direction = LEFT player.mover.move(-player.move_speed, 0) if player.rect.topleft[0] < 0: player.mover.move(player.move_speed, 0) elif keyboard[pygame.K_RIGHT]: player.direction = RIGHT player.mover.move(player.move_speed, 0) if player.rect.bottomright[0] > screen_size[0]: player.mover.move(-player.move_speed, 0) for event in pygame.event.get(): if event.type == pygame.QUIT: myutil.terminate() elif event.type == pygame.KEYDOWN: #-----# #SHOOT# #-----# if event.key == pygame.K_SPACE: bullet = player.shooter.shoot(player_inventory) if bullet is not None: bullets.append(bullet) #-----# elif event.key == pygame.K_e: pass elif event.type == pygame.MOUSEBUTTONDOWN: #----------# #OPEN LOCKS# #----------# if event.button == 1: try: for blocker in player_interactor.collision_handler.collisions['blockers']: if blocker.rect.collidepoint(event.pos): if blocker.tag == 'lock' and 'key' in player_inventory: item = player_inventory.inventory_manager.get_item_by_tag('key') ### item.quantity -= 1 ### blockers.remove(blocker) except KeyError: pass #----------# #=---------------------------------------------# #MOVE OR REMOVE BULLETS, DAMAGE OR KILL ENEMIES# #----------------------------------------------# for bullet in bullets: index = bullet.rect.collidelist(enemies) if index != -1: enemies[index].health -= bullet.damage if enemies[index].health <= 0: enemies.remove(enemies[index]) ### bullets.remove(bullet) elif (bullet.distance == 0 or bullet.rect.x < 0 or bullet.rect.x > screen_size[0] or bullet.rect.y < 0 or bullet.rect.y > screen_size[1] or bullet.rect.collidelist(blockers) != -1): bullets.remove(bullet) else: if bullet.direction == UP: bullet.mover.move(0, -32) if bullet.direction == DOWN: bullet.mover.move(0, 32) if bullet.direction == LEFT: bullet.mover.move(-32, 0) if bullet.direction == RIGHT: bullet.mover.move(32, 0) bullet.distance -= 1 #----------------------------------------------# #--------------# #SET COLLISIONS# #--------------# player.collision_handler.set_collisions(['blockers', 'moveables'], [blockers]) #--------------# #---------------------# #COLLIDE WITH BLOCKERS# #---------------------# try: ###FIX USE TRY EXCEPT ELSEon == DOWN: if player.rect.centerx < collider.centerx - (collider.centerx - collider.topleft[0]): player.mover.move(-player.move_speed, 0) elif player.rect.centerx > collider.centerx + (collider.topright[0] - collider.centerx): player.mover.move(player.move_speed, 0) elif player.direction == LEFT or player.direction == RIGHT: if player.rect.centery < collider.centery - (collider.centery - collider.topleft[1]): player.mover.move(0, -player.move_speed) elif player.rect.centery > collider.centery + (collider.bottomright[1] - collider.centery ): player.mover.move(0, player.move_speed) except KeyError: pass #---------------------# ##------## ##----## ##DRAW## ##----## displaysurf.fill(myutil.Colors.BLACK) pygame.draw.rect(displaysurf, myutil.Colors.PINK, player_moveable_interactor) player.renderer.draw(displaysurf, myutil.Colors.GREEN) for enemy in enemies: enemy.renderer.draw(displaysurf, myutil.Colors.RED) for blocker in blockers: blocker.renderer.draw(displaysurf, myutil.Colors.GRAY) for bullet in bullets: bullet.renderer.draw(displaysurf, myutil.Colors.BLUE) ##----## #------# #UPKEEP# #------# player_interactor.rect.topleft = (player.rect.x - 32, player.rect.y - 32) player_moveable_interactor.rect.topleft = (player.rect.x - 4, player.rect.y - 4) player_inventory.inventory_manager.clean_up() ### player.collision_handler.collisions = {} player_interactor.collision_handler.collisions = {} pygame.display.update() fps_clock.tick(fps)
def test_equipped_item_is_removed_from_items(self): item = gameobjects.Item((0, 0), (0, 0), 'test-item', 1, True, None, self.sprites, 'test') self.inventory.inventory_manager.add_item(item) self.inventory.inventory_manager.equip_item(item) self.assertNotIn(item, self.inventory.inventory_manager.items)
def test_dropped_item_is_removed_from_inventory(self): item = gameobjects.Item((0, 0), (0, 0), 'test-item', 1, True, None, self.sprites, 'test') self.inventory.inventory_manager.add_item(item) self.inventory.inventory_manager.drop_item_at_location(item, (0, 0)) self.assertNotIn(item, self.inventory.inventory_manager.items)
def test_full_returns_false_when_inventory_is_not_full(self): for i in range(self.inventory.inventory_manager.item_limit - 1): self.inventory.inventory_manager.add_item( gameobjects.Item((0, 0), (0, 0), 'test-item-{}'.format(i), 1, True, None, self.sprites, 'test')) self.assertFalse(self.inventory.inventory_manager.is_full())
def test_add_item_not_in_inventory(self): item = gameobjects.Item((0, 0), (0, 0), 'test-item', 1, True, None, self.sprites, 'test') self.inventory.inventory_manager.add_item(item) self.assertIn(item, self.inventory.inventory_manager.items) self.assertEqual(len(self.inventory.inventory_manager.items), 1)