Programming the Environment

  • 0

Programming the Environment

Category : Uncategorized

If STEM is really all about linking Science and Technology, for example biology, the environment and programming, how do your students explore sustainability? If only there were a way to have students create their own virtual environment model with grass that grows and sheep that eat, reproduce and die. It would really help them to appreciate the complex interaction of elements in the environment with every new organism they add.

Years ago I was introduced to the grass-eating colony of turtles exploration on the NetLogo site and it’s a great programming challenge. Unfortunately, the Logo programming language isn’t of much use outside of the classroom. But Python is definitely a useful skill and by using the Processing graphics library we can make a visual, dynamic, interactive (and memorable!) model in maybe a couple of class periods.

We’ll start with a “sheep,” which my students and I simplified to a circle, but you can draw it as realistically as you like. Sheep need an x-y position, a color, a size, an age, and an energy level. When they’re “born” they’re added to the sheep list. So in Python the Sheep object is created like this:

class Sheep:
    def __init__(self,x,y,sList,col):
        self.x = x #x-position
        self.y = y #y-position
        self.col = col #color
        self.age = 0 = 10 #size = 20 #energy level
        #add it to the sheep list

Then you have to tell the program how to update the sheep, to make it walk around (which costs it energy points) and “eat” the grass to increase its energy points. If its energy gets down to 0, it dies and is removed from the sheep list. Every loop the Sheep’s age is incremented, and if it reaches a certain age it dies. Finally you draw the Sheep. That code looks like this:

def update(self):
    #make sheep walk randomly
    move = 10 #the distance they can move
    self.x += random(-move, move)
    self.y += random(-move, move) -= 1
    self.age += 1
    #if the sheep gets enough energy, reproduce
    if > 50:
        #create another Sheep on the same spot
              sheepList,self.col) -= 30 #this costs energy!
    #draw sheep

Compared to Sheep, the patches of grass are pretty simple. They just need an x-y position, a size, an energy content, and an “eaten” property which can be True or False:

class Patch:
    def __init__(self,x,y):
        self.x = x #x-position
        self.y = y #y-position = 2 #energy from eating this patch
        self.eaten = False #hasn't been eaten yet = 10 #size
    def update(self):
        if self.eaten: #if it's been eaten
                       #it can randomly "regrow"
            if random(100)<5:
                self.eaten = False
                fill(BROWN) #eaten grass is brown
            fill(GREEN) #un-eaten grass is green
        #draw the square patch of grass:
        rect(self.x, self.y,,

Now it’s just a matter of creating a field of grass and a bunch of sheep at random locations. I’ve put the entire code on Github. You let the model run and see how it turns out. You can change the rate at which the grass grows, the energy content of the grass, the sheep’s maximum age and many other factors, to see the changes in the environment:


My favorite thing to do with this model is give one species of Sheep a small Evolutionary advantage, like being able to walk just a bit further, like this:

if self.col == PURPLE: 
    move = 12

Students can come up with other advantages, like giving birth to more offspring or getting more energy out of the grass. When you run the model, does the advantage matter? Here’s a screenshot from a model where the advantage certainly did matter and the purple Sheep ended up dominating the environment:

Students love to watch the model run and will come up with all kinds of interesting ideas for expanding on this template. Have fun and Go Sheep!

-Peter Farrell
April 16, 2017

Leave a Reply

Recent Posts