Introduction: (Very Simple) Disease Modeling (using Scratch)

About: Musician, developer, and hobbyist. Write articles regularly on JS, Python, and other programming related things.

Today, we'll be simulating a disease outbreak, with it being any disease, not necessarily COVID-19. This simulation was inspired by a video by 3blue1brown, which I'll link to. Since this is drag and drop, we can't do as much as we can with JS or Python, but there are benefits to using scratch too, so, if you want to see how to model an outbreak with JS, you can view my article which I wrote on modeling disease outbreaks here. Now, let's get started!

Note: The image above is from a simulation by quantum9innovation (not an Instructables user) which you can view here.


You'll need:

  • A Scratch Account (you can sign up here
  • Basic knowledge of drag and drop (but I'll still go through the code)
  • A computer or device you can program with (which you probably have, since you're reading this)
  • Optional - Watch the 3b1b video, it'll give you some idea of what we're programming.

Step 1: Basic Setup

First, let's create a project, title it whatever you want, and then delete the existing sprite on the canvas. You can do this by clicking the trash button on the sprite. Now we have a blank canvas, and you can change the background color to whatever you want.

Next, create a new sprite, and instead of using an existing sprite, paint your own. Make it a blue dot. This sprite represents the susceptible population of the community, and we'll also have a recovered/removed and infected population, which is where the name of the model, SIR (susceptible, infected, recovered/removed), comes from. Make sure to name the sprite "uninfected".

Now, create a new sprite (again), and title it sprite1, which we'll also paint ourselves. Title it "Sprite1" and create 2 costumes, one should be a red dot, and the other a grey dot. Make them costume1 and costume2, respectively. These two represent the infected (the red dot) and recovered/removed (the grey dot) populations.

Step 2: Setting Up the Code for the Susceptible Population

We now set up the susceptible population code. We first create 2 variables: people and infected. The "People" variable represents the population and can be changed according to how many people we want in our simulation, and the simulation will change accordingly. We also create an infected variable, and this represents the population that has/had the disease. Both of these variables should be global variables, meaning they can be used in all sprites.

Next, copy the code above into the uninfected sprite. Let's walk through what it does. When the program starts, the sprite is hidden, and this is so we don't have to put the same code for clones into the sprite itself, which helps. Then, we set the two variables (infected and people) to what we want them to be, in this case, we set the infected to 1 and people to 100. This means we start with one person who is infected and 100 total people, not including the infected person. We then run a loop, which is run the amount that the people variable is, in this case, 100. We go to a random position and then create a clone of the sprite. We go to a random position because this way we don't have a line of dots moving in one direction, and instead are spawning in random positions.

Step 3: Setting Up the Code for the Infected and Removed Sprite

Now, switch to the "Sprite1" sprite and then copy the code above. Let's go through it. When the program starts, the sprite is hidden, and then runs a loop for the amount that infected is set to. It goes to a random position and creates a clone of itself.

Step 4: Completing the Susceptible Population Code

Let's go through the things we need to do:

  • Infect
  • Move

Switch to the Uninfected sprite, copy the code above, and let's go through how it completes the infecting and moving. First, it goes to costume one, and this is really not necessary, but we have it there so if we decide to add more, we don't have to worry about it changing costumes to the new ones we added. Next, it shows itself. If you remember, we hid the original sprite, so the clones will also be hidden, which we don't want. We then run a forever loop, which will run the whole program until someone clicks the stop sign on scratch. We glide to a random position for 1 second, and then check if we're on the edge, in which case we bounce of it. Next, if we are touching the color red, we then clone Sprite1 (the infected/removed population) and increase the infected variable by 1, followed by deleting our sprite.

Step 5: Completing the Infected/Removed Code

Switching to Sprite1, we create a new list, timer. This list will keep track of how long a dot is infected, and after a certain amount of time, it either dies or recovers, become part of the removed/recovered population, and is represented by a grey dot, which cannot be reinfected.

Copy the code above and let's go through it. When we start as a clone we insert the total amount of seconds the program has been running for in the timer list, and we'll check this to see how long it's been infected and change it to be recovered accordingly. We hide the timer list and then switch the clone costume to the infected costume just in case, and then show our sprite. We now run a forever loop, in which multiple things happen: we tell the clone to glide to a random position every one second, check if the infected variable is greater than the population itself, in which case we set it to the population, and finally, we go and check the timer list's first item to see if it's been more than 5 seconds, and if true we switch the costume to the recovered costume so we can't infect, and then delete the item from the timer.

Block Code Contest

Participated in the
Block Code Contest