Py Creatures Activity

Build coded creatures that live in a Python world!

Compete against your fellow students to build coded creatures that live in a virtual world. The life of your creatures depends on your coding skills! Design what your creature looks like, what it eats, and how it behaves. Then code this up using Python and unleash your creatures on the virtual world. Whose creatures will thrive and flourish?!


You will need

A computer (Windows, Mac or Linux, but not an iPad or other tablet)

Experience required

You should have a good basic working knowledge of Python.


Making creatures with code

In this workshop, we will create creatures that live in a virtual world.

The life of your creatures depends on your coding skills!


Using Repl.it

We will use an online code editor called Repl.it.

  1. Navigate to repl.it in your browser


  2. Click on the Start coding button on the right:



  3. Click on the Import From GitHub tab:



  4. Type lewfer/gic-pycreatures into the box and then click the Import from GitHub button:



  5. If all goes well you should see the code load:


Running

Let’s run the World! Click on the Run button:

You should see the PyCreatures world appear:

 


Meet the Creatures

The World already comes with some creatures, called Berries and Korubs. You will create the Fleebs. In this World, the Sun is also a creature!


Creating some Fleebs

Currently the World has no Fleebs. Let’s add some.

Expand the creatures folder:

Click on fleeb.py:

If you scroll to the bottom of the file, you will see a for loop that creates 0 Fleebs. Change the number of Fleebs to 10:

Then run the World again:


Examining your Fleeb's data

We can find out a lot of information about our Fleebs (or any other creature). Click on one of them and you see an information panel for the highlighted creature:

Notice how the numbers change as time passes.


Getting the Fleebs to move

You may notice that your Fleebs are not moving. Let’s get then to move. We need to create a function called tick().

The tick() function is called each time the World clock ticks forwards. In the tick() function you can say what the creature should do.

Let’s tell the creature to move.

Add the following lines of code just after the __init__() function:

    def tick(self):
      # Called on each tick of the World clock
      action = "move"
      return action

Controlling the speed of your Fleebs

Your Fleebs are moving very slowly.  You can control the speed of your Fleebs by adjusting the acceleration and max_velocity properties. 

Add the following code in tick(), at the top, just after the comment:

      self.max_velocity = 5   # maximum speed of the creature in pixels per tick
      self.acceleration = 2   # acceleration in pixels per tick per tick

Then run the code again.

Adjust the speed settings to your liking

Keeping your Fleebs in the World

You may have noticed that when your Fleebs reach the edge of the World, they just drop off! Let’s see if we can keep them in the World by bouncing back in. We will write some code to detect when the reach the edge and change their movement angle accordingly.

Add the following code in tick(), just before the return action = “move” line:

      # Take action if we reach the edge of the world
      if self.position.x <= WORLD_LEFT+20:
        self.angle = 0
      elif self.position.x >= WORLD_RIGHT-20:
        self.angle = 180
      elif self.position.y <= WORLD_TOP+20:
        self.angle = 270
      elif self.position.y >= WORLD_BOTTOM-20:
        self.angle = 90

Can you see how this code is working to keep the Fleebs in the World?

Making our Fleebs more friendly

Fleebs are friendly creatures.  Let’s get your Fleebs to say hello to each other.

Enter the following function to recognise a Fleeb and say “hello”:

    def interact(self, other_creature_data):
        # Interact with another creature

        if other_creature_data.type=="Fleeb":
            self.say("hello")

The other_creature_data parameter holds information about the other creature our Fleeb is interacting with.


See if you can make the Fleebs say something when the sun passes over them (hint: the Sun is a creature in this world!).

Are your Fleebs sleepy?

Keeping active is tiring.  Fleebs need to sleep.  If they don’t their alertness levels drop below zero and they just stop moving forever!

Replace the action = “move” line with the following code in the tick() method: 

      # Tell the creature to sleep or move
      if self.alertness < 10:
        action = "sleep"
        self.say("Zzz")
      else:
        action = "move"

What do you think of the way the Fleebs are sleeping? Do you think this could be improved?

Managing energy levels

All creatures need energy to keep them going.  Fleebs are no different!  They will eventually run out of energy and die.  To keep their energy levels up they need to eat.

Add the following highlighted code to eat some Berries:

      if other_creature_data.type=="Fleeb":
         self.say("hello")
      elif other_creature_data.type=="Berry":
         return Interaction("eat")

Make your Fleeb say something appropriate when it is eating

Give your Fleebs a new look

The file fleeb.png is the image that is drawn for your Fleebs.  You can edit this file in an image editing tool such as Paint3D on Windows or Paintbrush on the Mac.  First you need to download the existing image, by clicking in the fleeb.png file and then clicking on “Click here to download”.  

Once you have finished editing the image, you can upload it again. Alternatively, you can find a suitable image on the internet, or use one of the images in the creatures/images directory.


Help!

If your program stops running, it could be that the Repl.it environment has crashed. You can reboot it.

  1. Click on the question mark in the bottom left of the screen
  2. Click on the Workspace shortcuts

  3. Click on the Open shell option

  4. Type busybox reboot into the window that appears:


Backing up your code

If you want a copy of your code to take away, you can download it as a zip file. Click on the three dots near the files list to see this option:


Want to do more?

There are lots more things your creatures can do. For example:

You can also use this project as the basis for learning object oriented programming.

Why not join our coding camp, or get in touch at enquiries@thinkcreatelearn.co.uk