class MyBee(Bee): pass
It is not just by itself, it belongs to the general class "Bees". In python, this is implemented by inheritance: the parent class is specified in parentheses. In the Bee class there is everything that is necessary for the life of a virtual bee - it is born, it can move and carry honey. class MyBee(Bee): def __init__(self): Bee.__init__(self) self.move_at(Point(200,300)) def on_stop_at_flower(self, flower): self.load_honey_from(flower) def on_honey_loaded(self): if self.honey <= 100: self.move_at(Point(300,400)) else: self.move_at(self.my_beehive)
self.load_honey_from(source) # self.unload_honey_to(target) #
You can take honey from any object and upload it to any object too: a flower, a beehive, and even a bee. We write self in front of each method, because, as it were, we give the command “Load honey!” To ourselves (self - eng. “I myself”) from within the class code. But you can give the command to another object: other_bee.load_honey_from(self)
unless of course he obeys us (we are authorized to give him commands). Similarly, the movement command is implemented: self.move_at(target) #
as a target, you can specify a point on the screen or another object (the coordinates of the object are taken at the time the command is given) def on_stop_at_flower(self, flower): """ """ pass def on_stop_at_beehive(self, beehive): """ """ pass def on_honey_loaded(self): """ """ pass def on_honey_unloaded(self): """ """ pass
And just at these hubs for the life of a bee, she can decide what to do next. At these moments, the game stops, as in a matrix, and you can calmly think things through, view all available objects from all sides and find a from beegarden import Bee # class MyBee(Bee): def __init__(self): """ """ Bee.__init__(self) self.flower = self.flowers.pop() # self.move_at(self.flower) # def on_stop_at_flower(self, flower): """ """ if flower.honey > 0: # self.load_honey_from(flower) # else: # self.go_next_flower() # , def on_honey_loaded(self): """ """ if self.honey == 100: # ? self.move_at(self.my_beehive) # , else: # self.go_next_flower() # , def on_stop_at_beehive(self, beehive): """ """ self.unload_honey_to(beehive) # def on_honey_unloaded(self): """ """ self.go_next_flower() # , def go_next_flower(self): """ """ if not self.flower.honey: # if not self.flowers: # if self.honey: # self.move_at(self.my_beehive) # return # , else: # self.flower = self.flowers.pop() # self.move_at(self.flower) #
Bees have to live in the game world, let's create it from beegarden import GameEngine, Scene # from my_bee import MyBee # game = GameEngine("My little garden") # scene = Scene(flowers_count=3) # bee = MyBee() # - ! game.go() # ...
from beegarden import GameEngine, Scene from my_bee import MyBee game = GameEngine("My little garden") scene = Scene(flowers_count=20) # bees = [] # for i in range(5): # 5 bee = MyBee() # bees.append(bee) # game.go() # -
sudo apt-get install python-pygame
For Windows 7 / Vista: Python itself needs to be installed python-2.7.3.msi and then pygame pygame-1.9.1.win32-py2.7.msiSource: https://habr.com/ru/post/164229/
All Articles