def from_json(self, json): ''' Restore this object from the passed in json object @param json - the json object ''' CreatureSprite.from_json(self, json) self.health = json['health']
def __init__(self, position=(0, 0), size=(0, 0), direction=Direction.down, json=None): CreatureSprite.__init__(self, PLAYER_IMAGE, position, size, direction) if json: self.inventory = {} self.final_inventory = [] self.from_json(json) else: self.lives = self.std_lives self.health = self.std_health self.inventory = {MagicShoes : 2, Item : 1, Potion : 2, Crystal : 3} self.final_inventory = [] self.count = 0 self.bullets = 1 self.weapon_tier = 0 self.fire_sound = pymix.Sound(LASER) self.laser = 1
def to_json(self): ''' Serialize the important members of this class as a json object ''' json = CreatureSprite.to_json(self) json['health'] = self.health return json
def test_foreground(self): creature = CreatureSprite(PLAYER_IMAGE, (0, 0), (60, 60), Direction.up) tile = Tile(None, None) tile.height = 2 tile.width = 2 tile.background.append([0, 0]) tile.background.append([0, 0]) tile.foreground.append([0, 0]) tile.foreground.append([1, 0]) tile.top.append([0, 0]) tile.top.append([0, 0]) creature.move(Direction.right, tile) self.assertEqual((0, 0), creature.coords) creature.move(Direction.down, tile) self.assertEqual((0, 1), creature.coords)
def from_json(self, json): ''' Restore this object from the passed in json object @param json - the json object ''' CreatureSprite.from_json(self, json) self.health = json['health'] self.count = json['count'] self.weapon_tier = json['wep_tier'] self.lives = json['lives'] self.bullets = json['bullets'] items = get_items() for name, num in json['inventory'].items(): for i in range(0, num): self.addItemToInventory(items[name]) for name in json['final inventory']: self.addItemToInventory(items[name])
def to_json(self): ''' Serialize the important members of this class as a json object ''' json = CreatureSprite.to_json(self) json['health'] = self.health json['count'] = self.count json['wep_tier'] = self.weapon_tier json['inventory'] = {} for item, num in self.inventory.items(): json['inventory'][item.name] = num json['final inventory'] = [] for item in self.final_inventory: json['final inventory'].append(item.name) json["bullets"] = self.bullets json['lives'] = self.lives return json
class TestCreature(unittest.TestCase): creature = None screen = pygame.display.set_mode((600, 600)) clock = pygame.time.Clock() BLACK = (0,0,0) background = pygame.Surface(screen.get_size()) background = background.convert() background.fill(BLACK) def setUp(self): self.creature = CreatureSprite(PLAYER_IMAGE, (5, 5), (60, 60), Direction.up) self.tile = Tile(None, None) def test_move_up(self): self.creature.move(Direction.up, self.tile) self.assertEqual((5, 4), self.creature.coords) def test_move_down(self): self.creature.move(Direction.down, self.tile) self.assertEqual((5, 6), self.creature.coords) def test_move_left(self): self.creature.move(Direction.left, self.tile) self.assertEqual((4, 5), self.creature.coords) def test_move_right(self): self.creature.move(Direction.right, self.tile) self.assertEqual((6, 5), self.creature.coords) def test_foreground(self): creature = CreatureSprite(PLAYER_IMAGE, (0, 0), (60, 60), Direction.up) tile = Tile(None, None) tile.height = 2 tile.width = 2 tile.background.append([0, 0]) tile.background.append([0, 0]) tile.foreground.append([0, 0]) tile.foreground.append([1, 0]) tile.top.append([0, 0]) tile.top.append([0, 0]) creature.move(Direction.right, tile) self.assertEqual((0, 0), creature.coords) creature.move(Direction.down, tile) self.assertEqual((0, 1), creature.coords) def test_top_edge(self): self.creature.move(Direction.up, self.tile) self.creature.isOutOfBounds(10, 10, -1, -2, -1, -2) self.creature.move(Direction.up, self.tile) self.creature.isOutOfBounds(10, 10, -1, -2, -1, -2) self.creature.move(Direction.up, self.tile) self.creature.isOutOfBounds(10, 10, -1, -2, -1, -2) self.creature.move(Direction.up, self.tile) self.creature.isOutOfBounds(10, 10, -1, -2, -1, -2) self.creature.move(Direction.up, self.tile) self.creature.isOutOfBounds(10, 10, -1, -2, -1, -2) self.creature.move(Direction.up, self.tile) self.creature.isOutOfBounds(10, 10, -1, -2, -1, -2) self.creature.move(Direction.up, self.tile) self.creature.isOutOfBounds(10, 10, -1, -2, -1, -2) self.assertEqual((5, 0), self.creature.coords) def test_bottom_edge(self): self.creature.move(Direction.down, self.tile) self.creature.isOutOfBounds(10, 10, -1, -2, -1, -2) self.creature.move(Direction.down, self.tile) self.creature.isOutOfBounds(10, 10, -1, -2, -1, -2) self.creature.move(Direction.down, self.tile) self.creature.isOutOfBounds(10, 10, -1, -2, -1, -2) self.creature.move(Direction.down, self.tile) self.creature.isOutOfBounds(10, 10, -1, -2, -1, -2) self.creature.move(Direction.down, self.tile) self.creature.isOutOfBounds(10, 10, -1, -2, -1, -2) self.creature.move(Direction.down, self.tile) self.creature.isOutOfBounds(10, 10, -1, -2, -1, -2) self.creature.move(Direction.down, self.tile) self.creature.isOutOfBounds(10, 10, -1, -2, -1, -2) self.assertEqual((5, 9), self.creature.coords) def test_right_edge(self): self.creature.move(Direction.right, self.tile) self.creature.isOutOfBounds(10, 10, -1, -2, -1, -2) self.creature.move(Direction.right, self.tile) self.creature.isOutOfBounds(10, 10, -1, -2, -1, -2) self.creature.move(Direction.right, self.tile) self.creature.isOutOfBounds(10, 10, -1, -2, -1, -2) self.creature.move(Direction.right, self.tile) self.creature.isOutOfBounds(10, 10, -1, -2, -1, -2) self.creature.move(Direction.right, self.tile) self.creature.isOutOfBounds(10, 10, -1, -2, -1, -2) self.creature.move(Direction.right, self.tile) self.creature.isOutOfBounds(10, 10, -1, -2, -1, -2) self.creature.move(Direction.right, self.tile) self.creature.isOutOfBounds(10, 10, -1, -2, -1, -2) self.assertEqual((9, 5), self.creature.coords) def test_left_edge(self): self.creature.move(Direction.left, self.tile) self.creature.isOutOfBounds(10, 10, -1, -2, -1, -2) self.creature.move(Direction.left, self.tile) self.creature.isOutOfBounds(10, 10, -1, -2, -1, -2) self.creature.move(Direction.left, self.tile) self.creature.isOutOfBounds(10, 10, -1, -2, -1, -2) self.creature.move(Direction.left, self.tile) self.creature.isOutOfBounds(10, 10, -1, -2, -1, -2) self.creature.move(Direction.left, self.tile) self.creature.isOutOfBounds(10, 10, -1, -2, -1, -2) self.creature.move(Direction.left, self.tile) self.creature.isOutOfBounds(10, 10, -1, -2, -1, -2) self.creature.move(Direction.left, self.tile) self.creature.isOutOfBounds(10, 10, -1, -2, -1, -2) self.assertEqual((0, 5), self.creature.coords) def test_can_take_action_true(self): self.creature.iters_until_action = 0 self.assertTrue(self.creature.can_take_action()) def test_can_take_action_false(self): self.creature.iters_until_action = 10 self.assertFalse(self.creature.can_take_action()) def test_action_taken(self): self.creature.action_taken() self.assertEqual(self.creature.iters_until_action, 12)
def setUp(self): self.creature = CreatureSprite(PLAYER_IMAGE, (5, 5), (60, 60), Direction.up) self.tile = Tile(None, None)
def __init__(self, position=(0, 0), size=(0, 0), direction=Direction.down, json=None): CreatureSprite.__init__(self, ENEMY_IMAGE, position, size, direction) if json: self.from_json(json) else: self.health = 3