Dark theme

Making the person move randomly.

 

1) So, how do we make the person move randomly? We could change their x and y values randomly, but there's actually a slightly easier way. Each sprite has a direction it faces. If we change this randomly and then get them to move forward a few steps, they'll move around. Here's the code to move once randomly:

Screenshot: One random step

Set this up and run it. Make sure that you understand what it is doing. Note: the wait block is in there because when we take lots of steps it gets a bit hectic unless we slow the sprite down a little.

Note that when coding it is always best to write a little bit of code and then test that it works before going on to the next bit; that way if there's a mistake it is easier to spot where it is.

 

2) Taking one step is all very well, but we need to take lots. In fact, we need to keep moving until all our healthy sprites are infected with the disease. We'll come back to stopping when everyone is infected later. For now, let's make the person move until the person running the model pushes a button.

For this, we'll need a Repeat until block. You put code inside these if you want it to keep repeating until something is true. Grab the wait block and drag it (and everything below it) away from the start block, but not out of the script box (or it'll get deleted). Then grab a Repeat until block and put it in its place:

Screenshot: Repeat until

 

3) Add, as the Repeat until condition (that is, what to check each time when deciding whether to stop), the "sensing" block Key space pressed?. Now drag the wait block and the rest of the movement code inside the Repeat until block. You should have this:

Screenshot: Repeat until

If you run your code now, you should see your sprite wander around until you press the space bar on your keyboard.

Go on to Making lots of people who move around.