image Tutorial on Probabilistic Events

So far, all actions are carried out as long as the antecedent is satisfied. However, sometimes we would like an action to only happen some of the time, you can use probability. Consider game plan below.

There are 2 rabbits and 4 foxes.
The rabbit starts out moves right.
When a rabbit reaches a border, it turns around.
The foxes wander around.
When a fox sees a rabbit, it chases the rabbit with 5 percent probability.

What do you think would happen? When you run the above game plan, you will see that the fox chases the rabbit more than 5 percent of the time. Why is that? First, remember that we are describing the game plan for every frame of the video game. Therefore, the game is evaluating the game plan 50 times a second. So whenever a fox sees a rabbit, it has 50 chances per second to decide whether or not it wants to chase the rabbit!


Try it yourself: Now try changing the 5 percent to 50 percent. You should see that the fox is almost always chasing the rabbit.
Complete Sample Games for Probabilistic Events

// Game #1. Blow up the Rocks to Get the Pearl.

There is an alien near the bottom.
There are 20 rocks.
There is 0 pearl.
There are 10 dinos near the top.

You control the alien.
When an arrow key is pressed, the alien moves in the same direction.
When you press W on the keyboard, the alien shoots up.
When you press A on the keyboard, the alien shoots left.
When you press S on the keyboard, the alien shoots down.
When you press D on the keyboard, the alien shoots right.

The dinos move around.
When the rock is shot, the rock explodes.
The rocks move around.
When the rock explodes, the rock inserts a pearl with a 15 percent probability.
When the rock explodes, the rock inserts a rock with a 40 percent probability.

When the rock reaches the border, the rock reverses direction.
When the dino reaches the border, the dino reverses direction.
When the alien reaches the border, the alien stops.
When the dino is shot, the dino is empowered.
When the dino is empowered, the dino chases the alien.

When the alien collides with the dino, gameover.
When the alien collides with the rock, gameover.
When the alien collides with the pearl, you win.

// Game #2. Spaceship with broken engine.

Wide canvas with 2 frames.
There are 0 emeralds.
The size of emerald is 15.
The speed of pointer is 7.
The speed of gem is 5.
The speed of sapphire is 5.
The speed of emerald is 10.
The speed of dino is 2.
The speed of coyote is 2.
The speed of spinstar is 2.
The angle of pointer is 90.
The angle of emerald is 90.

//Player Control.
You control the pointer.
When the up arrow is pressed, the pointer moves up.
When the down arrow is pressed, the pointer moves down.
When the space is pressed, the pointer becomes empowered.
When the pointer is empowered, it inserts emerald.
The emerald moves right.

//Spaceship with Broken Engine.
There is a bird.
The bird is invisible.
When the bird is not dead, it becomes facing_down for 0.5 second with 20 percent probability.
When the bird is facing_down, the pointer moves down.
When the bird is not facing_down, the pointer moves up.

//Spaceship collision.
When the pointer collides with a topaz, it explodes.
When the pointer collides with a dino, it explodes.
When the pointer collides with a coyote, it explodes.

// Spawner movement.
The sapphire starts by moving up.
When the sapphire collides with topaz, it reverses direction.
When the sapphire is not dead, it inserts a dino with 20 percent probability.
The gem starts by moving up.
When the gem collides with a topaz, it reverses direction.
When the gem is not dead, it inserts a coyote with 20 percent probability.
The pearl starts by moving up.
When the pearl collides with topaz, it reverses direction.
When the pearl is not dead, it inserts a spinstar with 2 percent probability.

// Enemy movement.
When a dino is not dead, it moves left.
When a coyote is not dead, it moves left.
When a dino collides with an emerald, it explodes.
When a coyote collides with an emerald, it explodes.
When a spinstar is not dead, it moves left.
When a spinstar collides with emerald, it explodes and the score adds 1 point.

//Game Over.
When the pointer is gone, you lose.
When the score equals 20, you win.

Map:

FFFFFFFFFFFFFFFFFFFFF
-=------------------
--------------------
--------------------
-------------------QG
--------------------
--------------------
--------------------
----p--------------sR
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
-------------------Dh
--------------------
--------------------
--------------------
FFFFFFFFFFFFFFFFFFFFF


Next page

Tutorials Home