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