Show the fly
Now we have the spider moving it’s going to need a target — the fly. We can get this working over a few steps, starting with getting the fly drawing, then adding code to time its appearance.
Drawing the fly
What will we need in the digital reality to help us create the fly?
- It will need a position:
flyX
andflyY
. - It will need a constant size: let’s stick with circles, so
FLY_RADIUS
.
That should be enough to get something to appear. You should be able to plan this out yourself. Review the details on how we added the spider as this will be very similar. Pick a different color, so that you can tell the fly and spider apart.
The one thing that will need some thought is how to position the fly.
We don’t want the fly always being in the center of the screen — we need to randomise its location. To do this you can use the Rnd
method from SplashKit. For example, Rnd(SCREEN_WIDTH)
will give you a random value from 0 to SCREEN_WIDTH - 1.
Timing Appearing
One of the game features we wanted was that the fly should not appear straight away, but appear after a short delay (lets say between 1 and 3 seconds). In order to hide the fly, we need to add an if statement around the drawing code so that we only draw the fly if it has appeared. We can track if the fly has appeared using a boolean variable. This can start false
, change to true
when sufficient time has passed.
To get this working we need to think a little about how we can work with time.
We cannot control time — computers are great, but we still don’t have this capability. However, much like other external events, we can observe the passing of time. The computer keeps track of the time, so we can use that to check how long has elapsed.
SplashKit provides timer functionality that we can use to track time for now. In general, we can create one game timer, start this at the beginning of the game, and then use it to find out how many milliseconds have passed.
Method | Required Arguments | Description |
---|---|---|
CreateTimer | A name for the timer. | Creates a timer. |
StartTimer | The name of the timer to use | Records the current time, so that it can work out how much time has passed. |
StopTimer | The name of the timer to use. | Resets the timer so that it is not recording time. |
TimerTicks | The name of the timer to use. | Returns you the time that has passed in milliseconds since StartTimer was called. |
We will need to code these steps somewhere within the event loop. They are not really about handing input, so we can create a new logical group for the steps related to updating the game. This can be the code for the fly appearing, escaping, and being eaten.
The general logic for how this new group of instructions will fit into the program’s sequence this is shown below.
Putting this together
To make this work we will need:
- A game timer — we can create a constant to keep track of its name
GAME_TIMER
. - A variable to track how long needs to pass before we show the fly:
appearAtTime
. - A boolean to capture if the fly has appeared:
flyAppeared
.
These can be initialised as shown below. We can also change the initialisation of flyX
and flyY
, as these values should now be set when the fly appears.
With these new variables added, we can make use of them in our steps.
- We need to create and start the timer before the event loop. This way we are tracking time from the start of the game.
- In update game we will need to:
- Check if the fly has not appeared, and if the required time is elapsed. If this is true, we then:
- set
flyAppeared
to be true. - assign
flyX
a random x value (Rnd(SCREEN_WIDTH)
). - assign
flyY
a random y value (Rnd(SCREEN_HEIGHT)
).
- set
- Check if the fly has not appeared, and if the required time is elapsed. If this is true, we then:
- In draw game we can:
- Only draw the fly if
flyAppeared
is true.
- Only draw the fly if
These ideas translate into pseudocode as follows:
Code this up and test the game a few times. The fly should appear after a short period of time. The amount of time will differ between executions, based on the random value.