📜 ⬆️ ⬇️

Learning programming through the game or how to quickly collect all the honey

A few years ago I started teaching my favorite python language to schoolchildren. And there was such a task: to tell about the object model, but whatever it was not boring and as clearly as possible. And then I did not immediately, but it dawned - the bees!



The idea is simple: everything in the world is the essence of objects (and subjects, but this is not about that now). Programmers are lazy people, they did not invent something special and invented their analogs of real objects. All invented (virtual, in other words) objects are programmers grouped into classes and in the class they describe what is common for objects of the same type. Then programmers create instances of virtual objects and run them to work. That is, in the programs the programmers work with the objects, not the programmers themselves. Objects live their virtual life, push and pull each other, give and take something (data) from each other. The task of the programmer is to make the objects not just push and jerk, but accomplish the task the programmer needs.

We will also make an apiary model and practice managing objects-bees. By the time of the beginning of beekeeping, students should know the basics of the language, its basic structures and operators, and have a basic understanding of classes.
')
So - the bee:
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) 

In addition to the bee in our virtual world there are flowers and a hive. There is honey in the flowers, it must be harvested and taken to the hive. A bee can carry up to 100 units of honey, while a flower contains from 100 to 200 units. The hive keeps all the honey brought into it, but not more than a certain limit (depending on the number of flowers in the apiary). The amount of honey that the object contains is displayed through the property of the object and is called honey. To see the amount of honey in a bee, you need to write like this - bee.honey (at the flower - flower.honey, at the hive - beehive.honey). Object properties are often called attributes. Our bee has more attributes: flowers - a list of all the flowers in the apiary and my_beehive - the native hive.

Using attributes, we can view the state of the object. But you can change the state of the object using the methods. The methods are such levers, yanked for one - the object flew, yanked for another - stopped. How can we change the state of our bee?
Taking and loading honey, for example:
 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)

How does the world interact with our bee? He sends signals to her, pushes her in the side: “Look what happened!” Signals are the methods that the world around us causes for a bee object. The signals on our apiary are:
  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 woman in red with the desired flower. Please note: the return of the self.move_at (finded_flower) command will not send the bee immediately to the flower, it's just a note “fly there and then after exiting the event.” A subsequent call to self.move_at (other_flower) will change the mark and the bee will fly to the last specified object.
It is in the body of the signal methods that the artificial intelligence of the bee is realized. For example, the behavior code of a bee from a cartoon (file my_bee.py)
 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() #      ... 

But just collecting honey alone is not interesting, let's go with friends
 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() #   -  

And then we will compete with another hive.

On the left - worker bees, each processing its own flower. On the right - greedy, fly to the most obese. Greed is losing, why is it interesting? ...

I suggest the community to take the checkers in hand and do a warm-up before the new year - who will collect the honey faster and more than anyone? Moreover, there is a sign: to set releases before the new year - to make the authorities angry, to start a new one - not to love yourself, to rule the old one - to die from boredom.

The project code can be downloaded from the gitkhab beegarden. To start, you need python older than 2.6 and Pygame older than 1.9, tested under Ubuntu and Windows 7 / Vista. For Ubuntu, you can install Pygame like this
 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.msi
You can take checkers in your hands like this: clone a project into a separate folder (or just download and unzip the archive) and view / edit my_bee.py and run.py files there. You can compete with each other, as well as with the algorithms of Working Bee and the Greedy Bee written by me.

Good luck in the coming!

Source: https://habr.com/ru/post/164229/


All Articles