class TestFish(unittest.TestCase): def setUp(self): self.constraint = (1000, 1000) self.fish = Fish(self.constraint, 100, 100, Directions.left, 1) def test_init_no_params(self): fish = Fish(self.constraint) self.assertEqual(fish.x, 0) self.assertEqual(fish.y, 0) self.assertEqual(fish.direction, (1, 0)) self.assertEqual(fish.size, 0) self.assertEqual(fish.image_size, 80) radius = (fish.size * 15 + 41) / 2 self.assertEqual(fish.radius, radius) self.assertFalse(fish.hungry) self.assertFalse(fish.dead) def test_init(self): self.assertEqual(self.fish.x, 100) self.assertEqual(self.fish.y, 100) self.assertEqual(self.fish.direction, Directions.left) self.assertEqual(self.fish.size, 1) radius = (self.fish.size * 15 + 41) / 2 self.assertEqual(self.fish.radius, radius) def test_starve(self): self.fish.starve() self.assertTrue(self.fish.dead) self.assertEqual(self.fish.state, 'die') self.assertFalse(self.fish.mirrored) self.assertEqual(self.fish.frame, 0) self.fish.mirrored = True self.fish.starve() self.assertEqual(self.fish.frame, 720) # # How to test time()??? works every 10 seconds.. # def test_hungry_check(self): # self.fish.last_fed = 9 # self.fish.hungry_check() # self.assertTrue(self.fish.hungry) def test_eat(self): food = 5 growth = self.fish.growth size = self.fish.size self.fish.eat(food) self.assertFalse(self.fish.hungry) self.assertEqual(self.fish.growth, growth + food) self.assertEqual(self.fish.size, size) self.fish.eat(food) self.assertNotEqual(self.fish.size, size)
class FredLifeGame(Widget): ships = ListProperty([]) fish = ObjectProperty(None) start_time = ObjectProperty(None) def __init__(self, *args, **kwargs): self.size = (Window.width, Window.height) super(FredLifeGame, self).__init__(*args, **kwargs) self.victory_screen = FredLifeScore() self.manufacture_ships(3) self.fish = Fish( box=[self.game_area.x, self.game_area.y + 65, self.game_area.width, self.game_area.height - 175]) self.fish.bind(pos=lambda instance, value: self.check_for_smthing_to_eat(value)) self.fish.bind(calories=self.update_calories_bar) self.fish.bind(on_death=self.the_end) def play(self, *largs): for ship in self.ships: self.drop_ship_onto_sea(ship) self.game_area.add_widget(self.fish, index=1) self.fish.active = True Clock.schedule_interval(self.drop_food, 4) Clock.schedule_interval(self.sail_ships, 4) Clock.schedule_interval(self.check_for_smthing_to_eat, 0.2) self.start_time = datetime.now() def pause(self): Clock.unschedule(self.drop_food) Clock.unschedule(self.sail_ships) Clock.unschedule(self.check_for_smthing_to_eat) def the_end(self, instance): self.pause() self.victory_screen.calories_score.text = str(self.fish.total_calories) self.victory_screen.junk_score.text = str(self.fish.junk_swallowed) self.victory_screen.open() def manufacture_ships(self, count=1): for n in range(0, count): ship = Ship(horison=self.horison) self.ships.append(ship) self.ships[0].bind(on_start_sailing=lambda instance: Clock.schedule_interval(self.drop_junk, 1)) self.ships[0].bind(on_stop_sailing=lambda instance: Clock.unschedule(self.drop_junk)) def drop_ship_onto_sea(self, ship): try: if not ship: ship = self.ships.pop() self.game_area.add_widget(ship) ship.center_x = randint(0, Window.width - ship.width / 4) ship.y = self.game_area.height ship.active = True except IndexError: Logger.debug("No ships left in dock.") def check_for_smthing_to_eat(self, dt): """Collision detection""" to_eat = [] for stuff in self.game_area.children: if stuff.collide_widget(self.fish): if isinstance(stuff, Food) or isinstance(stuff, Junk): to_eat.append(stuff) for shit in to_eat: self.game_area.remove_widget(shit) self.game_area.add_widget(FoodScoreFeedback(calories=shit.calories, center=shit.center)) self.fish.eat(shit) def drop_food(self, td): """Periodically drop food from the ships""" for ship in self.ships: food = Food(what="bottle", lvl=self.fish.obese_lvl, x=ship.center_x + randint(-50, 50), y=ship.y + randint(-5, 5)) def really_drop_food(food, td): self.game_area.add_widget(food) food.active = True Clock.schedule_once(partial(really_drop_food, food), random() * 2) def drop_junk(self, *args): """Periodically drop junk from the ships""" for ship in self.ships: junk = Junk(lvl=self.fish.obese_lvl, x=ship.center_x + randint(-50, 50), y=ship.y + randint(-5, 5)) self.game_area.add_widget(junk) junk.active = True def sail_ships(self, timer): for ship in self.ships: ship.sail() def update_calories_bar(self, instance, new_value): self.calories_bar.value = new_value
class FishLifeGame(Widget): ships = ListProperty([]) fish = ObjectProperty(None) start_time = ObjectProperty(None) def __init__(self, *args, **kwargs): self.size = (Window.width, Window.height) super(FishLifeGame, self).__init__(*args, **kwargs) self.victory_screen = FishLifeScore() self.manufacture_ships(3) self.fish = Fish(box=[self.game_area.x, self.game_area.y + 65, self.game_area.width, self.game_area.height - 175]) self.fish.bind(pos=lambda instance, value: self.check_for_smthing_to_eat(value)) self.fish.bind(calories=self.update_calories_bar) self.fish.bind(on_death=self.the_end) def play(self, *largs): for ship in self.ships: self.drop_ship_onto_sea(ship) self.game_area.add_widget(self.fish, index=1) self.fish.active = True # Tick tock, the food is dropped \o ooops Clock.schedule_interval(self.drop_food, 2) # SAIL AWAY! Clock.schedule_interval(self.sail_ships, 5) # Lets try and not overheat the CPU ;) Clock.schedule_interval(self.check_for_smthing_to_eat, 0.4) self.start_time = datetime.now() def pause(self): Clock.unschedule(self.drop_food) Clock.unschedule(self.sail_ships) Clock.unschedule(self.check_for_smthing_to_eat) def the_end(self, instance): self.pause() self.victory_screen.calories_score.text = str(self.fish.total_calories) self.victory_screen.junk_score.text = str(self.fish.junk_swallowed) self.victory_screen.total_score.text = "On %s a fish was caught, size of %s, which well fed the people of the world for %s days straight!" % (datetime.now().strftime("%B %d, %Y"), self.fish.rank[self.fish.obese_lvl - 1], (datetime.now() - self.start_time).seconds ) self.add_widget(self.victory_screen) def manufacture_ships(self, count = 1): """Next batch coming for Somalia""" for n in range(0, count): ship = Ship(horison=self.horison) self.ships.append(ship) # *cough*workaround*cough* bind on first ship self.ships[0].bind(on_start_sailing=lambda instance: Clock.schedule_interval(self.drop_junk, 0.4)) self.ships[0].bind(on_stop_sailing=lambda instance: Clock.unschedule(self.drop_junk)) def drop_ship_onto_sea(self, ship): """Randomly throw away the dead meat! BUahaha""" try: if not ship: ship = self.ships.pop() self.game_area.add_widget(ship) ship.center_x = randint(0, Window.width - ship.width/4) ship.y = self.game_area.height ship.active = True except IndexError: Logger.debug("No ships left in dock.") def check_for_smthing_to_eat(self, dt): """Collision detection: leFish vs Food'n'Junk""" to_eat = [] for stuff in self.game_area.children: if stuff.collide_widget(self.fish): if isinstance(stuff, Food) or isinstance(stuff, Junk): to_eat.append(stuff) for shit in to_eat: self.game_area.remove_widget(shit) self.game_area.add_widget(FoodScoreFeedback(calories=shit.calories, center=shit.center)) self.fish.eat(shit) def drop_food(self, td): """Periodicaly drop food from the ships""" for ship in self.ships: food = Food(what="bottle", lvl=self.fish.obese_lvl, x = ship.center_x + randint(-50,50), y = ship.y + randint(-5,5)) def really_drop_food(food, td): self.game_area.add_widget(food) food.active = True Clock.schedule_once(partial(really_drop_food, food), random() * 2) def drop_junk(self, *args): """Feels sooooOOo goood yeaaahhhh""" for ship in self.ships: junk = Junk(lvl=self.fish.obese_lvl, x = ship.center_x + randint(-50,50), y = ship.y + randint(-5,5)) self.game_area.add_widget(junk) junk.active = True def sail_ships(self, timer): """I wonder, wheres the captain?""" for ship in self.ships: ship.sail() def update_calories_bar(self, instance, new_value): self.calories_bar.value = new_value
class FishLifeGame(Widget): ships = ListProperty([]) fish = ObjectProperty(None) start_time = ObjectProperty(None) def __init__(self, *args, **kwargs): self.size = (Window.width, Window.height) super(FishLifeGame, self).__init__(*args, **kwargs) self.victory_screen = FishLifeScore() self.manufacture_ships(3) self.fish = Fish(box=[ self.game_area.x, self.game_area.y + 65, self.game_area.width, self.game_area.height - 175 ]) self.fish.bind( pos=lambda instance, value: self.check_for_smthing_to_eat(value)) self.fish.bind(calories=self.update_calories_bar) self.fish.bind(on_death=self.the_end) def play(self, *largs): for ship in self.ships: self.drop_ship_onto_sea(ship) self.game_area.add_widget(self.fish, index=1) self.fish.active = True # Tick tock, the food is dropped \o ooops Clock.schedule_interval(self.drop_food, 2) # SAIL AWAY! Clock.schedule_interval(self.sail_ships, 5) # Lets try and not overheat the CPU ;) Clock.schedule_interval(self.check_for_smthing_to_eat, 0.4) self.start_time = datetime.now() def pause(self): Clock.unschedule(self.drop_food) Clock.unschedule(self.sail_ships) Clock.unschedule(self.check_for_smthing_to_eat) def the_end(self, instance): self.pause() self.victory_screen.calories_score.text = str(self.fish.total_calories) self.victory_screen.junk_score.text = str(self.fish.junk_swallowed) self.victory_screen.total_score.text = "On %s a fish was caught, size of %s, which well fed the people of the world for %s days straight!" % ( datetime.now().strftime("%B %d, %Y"), self.fish.rank[self.fish.obese_lvl - 1], (datetime.now() - self.start_time).seconds) self.victory_screen.open() def manufacture_ships(self, count=1): """Next batch coming for Somalia""" for n in range(0, count): ship = Ship(horison=self.horison) self.ships.append(ship) # *cough*workaround*cough* bind on first ship self.ships[0].bind(on_start_sailing=lambda instance: Clock. schedule_interval(self.drop_junk, 0.4)) self.ships[0].bind( on_stop_sailing=lambda instance: Clock.unschedule(self.drop_junk)) def drop_ship_onto_sea(self, ship): """Randomly throw away the dead meat! BUahaha""" try: if not ship: ship = self.ships.pop() self.game_area.add_widget(ship) ship.center_x = randint(0, Window.width - ship.width / 4) ship.y = self.game_area.height ship.active = True except IndexError: Logger.debug("No ships left in dock.") def check_for_smthing_to_eat(self, dt): """Collision detection: leFish vs Food'n'Junk""" to_eat = [] for stuff in self.game_area.children: if stuff.collide_widget(self.fish): if isinstance(stuff, Food) or isinstance(stuff, Junk): to_eat.append(stuff) for shit in to_eat: self.game_area.remove_widget(shit) self.game_area.add_widget( FoodScoreFeedback(calories=shit.calories, center=shit.center)) self.fish.eat(shit) def drop_food(self, td): """Periodicaly drop food from the ships""" for ship in self.ships: food = Food(what="bottle", lvl=self.fish.obese_lvl, x=ship.center_x + randint(-50, 50), y=ship.y + randint(-5, 5)) def really_drop_food(food, td): self.game_area.add_widget(food) food.active = True Clock.schedule_once(partial(really_drop_food, food), random() * 2) def drop_junk(self, *args): """Feels sooooOOo goood yeaaahhhh""" for ship in self.ships: junk = Junk(lvl=self.fish.obese_lvl, x=ship.center_x + randint(-50, 50), y=ship.y + randint(-5, 5)) self.game_area.add_widget(junk) junk.active = True def sail_ships(self, timer): """I wonder, wheres the captain?""" for ship in self.ships: ship.sail() def update_calories_bar(self, instance, new_value): self.calories_bar.value = new_value