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?!
A computer (Windows, Mac or Linux, but not an iPad or other tablet)
You should have a good basic working knowledge of Python.
In this workshop, we will create creatures that live in a virtual world.
The life of your creatures depends on your coding skills!
We will use an online code editor called Repl.it.
Let’s run the World!Click on the Run button:
You should see the PyCreatures world appear:
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!
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:
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.
You may notice that your Fleebs are not moving. Let’s get then to move. We need to create a function called
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
def tick(self): # Called on each tick of the World clock action = "move" return action
Your Fleebs are moving very slowly. You can control the speed of your Fleebs by adjusting the
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.
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
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")
Keeping active is tiring. Fleebs need to sleep. If they don’t their alertness levels drop below zero and they just stop moving forever!
action = "move" line with the following code in the
# Tell the creature to sleep or move if self.alertness < 10: action = "sleep" self.say("Zzz") else: action = "move"
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")
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.
If your program stops running, it could be that the Repl.it environment has crashed. You can reboot it.
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:
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 firstname.lastname@example.org