Test clicking on the screen

This commit is contained in:
Yohann D'ANELLO 2020-12-11 18:33:47 +01:00
parent d9912cacad
commit f453b82a58
3 changed files with 31 additions and 3 deletions

View File

@ -28,7 +28,7 @@ class Item(Entity):
"""
if self.held:
self.held_by.inventory.remove(self)
self.map.add_entity(self)
self.held_by.map.add_entity(self)
self.move(self.held_by.y, self.held_by.x)
self.held = False
self.held_by = None
@ -49,7 +49,7 @@ class Item(Entity):
"""
self.held = True
self.held_by = player
self.map.remove_entity(self)
self.held_by.map.remove_entity(self)
player.inventory.append(self)
def save_state(self) -> dict:

View File

@ -68,7 +68,8 @@ class Map:
"""
Unregister an entity from the map.
"""
self.entities.remove(entity)
if entity in self.entities:
self.entities.remove(entity)
def find_entities(self, entity_class: type) -> list:
return [entity for entity in self.entities

View File

@ -216,6 +216,33 @@ class TestGame(unittest.TestCase):
self.game.handle_key_pressed(KeyValues.SPACE)
self.assertEqual(self.game.state, GameMode.MAINMENU)
def test_mouse_click(self) -> None:
"""
Simulate mouse clicks.
"""
self.game.state = GameMode.MAINMENU
# Settings menu
self.game.display_actions(DisplayActions.MOUSE, 25, 21)
self.assertEqual(self.game.main_menu.position, 4)
self.assertEqual(self.game.state, GameMode.SETTINGS)
bomb = Bomb()
bomb.hold(self.game.player)
bomb2 = Bomb()
bomb2.hold(self.game.player)
self.game.state = GameMode.INVENTORY
# Click nowhere
self.game.display_actions(DisplayActions.MOUSE, 0, 0)
self.assertEqual(self.game.state, GameMode.INVENTORY)
# Click on the second item
self.game.display_actions(DisplayActions.MOUSE, 8, 25)
self.assertEqual(self.game.state, GameMode.INVENTORY)
self.assertEqual(self.game.inventory_menu.position, 1)
def test_new_game(self) -> None:
"""
Ensure that the start button starts a new game.