image Computational Thinking - Infinite Number of Carrots

Suppose you wish to write a game in which the player rabbit goes out to find and collect carrots. Initially, there might be 30 carrots on the field. However, we want to keep adding new carrots to the field. How could we do that?

There is one rabbit and 30 carrots.
When an arrow key is pressed, the rabbit moves in the same direction.
When the rabbit touches a carrot, the carrot is eaten.
This simple game plan describes a game in which a player rabbit goes out to find and collect carrots. Every time the rabbit touches a carrot, the carrot disappears forever. No replenishing of carrots thus far.
When a carrot disappears, it inserts[4] a carrot. Here, it says that whenever a carrot is eaten, it will insert a new carrot at a random location. The random location is indicated by the [4] parameter. Please refer to the previous tutorial lesson for other parameters.

Because insertions can be made approximately once every half second, sometimes you will not see a new carrot inserted if the rabbit eats two carrots closely together.

Alternative method

When the rabbit touches a carrot, the rabbit inserts[4] a carrot.

Instead of using the carrot to insert another carrot, you can use the rabbit to insert a new carrot.
Alternative method 2

There is a turtle. The turtle is invisible.
When the rabbit touches a carrot, the turtle becomes notified.
Otherwise, the turtle is not notified.
When the turtle is notified, it inserts[4] a carrot.

Here, you can also use another character (like an arbiter) to make the insertions. With a clever use of Boolean attributes ("notified"), the invisible character (the turtle) randomly moving around on the field. Whenever the rabbit eats a carrot, the invisible turtle inserts a new carrot at a random location (with [4]). Notice the sentence starting with "Otherwise," - without this important sentence, the turtle will be notified forever once the rabbit touches a carrot.
Alternative method 3

There is a turtle. The turtle is invisible.
The turtle starts out moving in a random direction.
When the turtle reaches a border, it reverses direction.
When the rabbit touches a carrot, the turtle becomes notified.
Otherwise, the turtle is not notified.
When the turtle is notified, it inserts a carrot.

This is similar to the example above, except that the invisible turtle now moves around. Whenever the rabbit eats a carrot, the invisible turtle inserts a new carrot wherever it happens to be at that time.
Combine some of the sentences above and you have the complete game plan where carrots are replenished!


Quiz:
Complete Sample Games for Infinite Number of Objects

// Game #1. Get the Bones.

There is one dino and 10 bones.
There are 5 aliens, a pearl, and a ball.
The player controls the dino.
When an arrow key is pressed, the dino moves in the same direction.

When the dino touches a bone, the bone is eaten and the score increases by 1.
The aliens move around.
The ball moves around.
The pearl is invisible.
The pearl moves around.
When the pearl reaches a border, it reverses direction.
When the alien reaches a border, it reverses direction.
When the ball reaches a border, it reverses direction.
When the dino reaches a border, it stops.

When the dino touches a bone, the pearl becomes notified.
Otherwise, the pearl is not notified.
When the dino touches a bone, the ball becomes notified.
Otherwise, the ball is not notified.
When the pearl is notified, the pearl inserts a bone.
When the ball is notified, the ball inserts a rock.
The speed of the pearl is 10 pixel per frame.
The speed of the ball is 5 pixel per frame.

When the dino collides with a rock, gameover.
When the dino collides with an alien, gameover.
When the dino collides with a ball, gameover.
When the score is equal to 25, you win.

// Game #2. Juggling Breakout.

The player controls the kitten.
When the right arrow is pressed, the kitten moves right.
When the left arrow is pressed, the kitten moves left.
When the space is pressed, the kitten becomes empowered.
When the kitten is empowered, the speed of kitten is 9.
When the kitten is not empowered, the speed of kitten is 4.
When the kitten collides with border, it reverses direction.
The speed of the ball is 3.

//Ball spawn.
When a sapphire sees ball, it becomes red for 2 seconds.
When a sapphire is not red, it inserts a ball.
The balls start by moving in a random direction.
When a ball collides with topaz, it reverses direction.
When a ball collides with border, it reverses direction.
When a ball collides with kitten, it reverses direction and score increases by 1 point.

//Ground control.
When a topaz collides with a ball, it becomes pink.
When a pink topaz collides with a ball, it becomes red.
When a red topaz collides with a ball, it explodes.

//Game over.
When a topaz is gone, you lose.
When the score equals 40, you win.

Map:

----------h
----------l
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
----------i
FFFFFFFFFFFFFFFFFFFF

// Game #3. Pick the Stars.

The rock is invisible.
The player controls the rock.
When the left arrow key is pressed, the rock becomes rotating_ccw.
When the left arrow key is pressed, the rock becomes not stationary.
When the right arrow key is pressed, the rock becomes rotating_cw.
When the right arrow key is pressed, the rock becomes not stationary.

//Stationary Rock Conditions.
When the rock is not rotating_ccw, it is n_rotating_ccw.
When the rock is rotating_ccw, it is not n_rotating_ccw.
When the n_rotating_ccw rock is not rotating_cw, it is stationary.

//Player character Pointer.
There is a pointer.
The speed of the pointer is 2.7.
When a pointer touches a border, it explodes.

//Counter Clockwise Rotation for Pointer.
When the rock is rotating_ccw and the pointer is not ccw_locked, the pointer becomes rotating_ccw.
When the pointer is ccw_locked, the pointer becomes not rotating_ccw.
When the rock is rotating_cw, the pointer becomes not rotating_ccw.
When the pointer is rotating_ccw, the angle of the pointer decreases by 4.
When the pointer is rotating_ccw, the tracked_angle of the pointer decreases by 4.

//Clockwise Rotation for Pointer.
When the rock is rotating_cw and the pointer is not cw_locked, the pointer becomes rotating_cw.
When the pointer is cw_locked, the pointer becomes not rotating_cw.
When the rock is rotating_ccw, the pointer becomes not rotating_cw.
When the pointer is rotating_cw, the angle of the pointer increases by 4.
When the pointer is rotating_cw, the tracked_angle of the pointer increases by 4.

//Pointer left/right movement.
The tracked_angle of the pointer is 90.
When the tracked_angle of the pointer is less than 72, the pointer moves left.
When the tracked_angle of the pointer equals 70, the pointer becomes ccw_locked.
When the tracked_angle of the pointer is greater than 70, the pointer becomes not ccw_locked.
When the tracked_angle of the pointer is greater than 108, the pointer moves right.
When the tracked_angle of the pointer equal 110, the pointer becomes cw_locked.
When the tracked_angle of the pointer is less than 110, the pointer becomes not cw_locked.

//Inserting spinstars with an apricot.
The apricot is invisible.
When the rock is rotating_ccw, the apricot moves left at 2 pixels per frame.
When the rock is rotating_cw, the apricot moves right at 2 pixels per frame.
When the rock is stationary, the apricot becomes active.
When the rock is stationary, the apricot chases the rock at 1 pixel per frame.
Otherwise, the apricot is not active.
When the apricot is active, it becomes ripe for 0.2 second.
When the ripe apricot is not active, it inserts a spinstar.
Spinstars move down at 3 pixels per frame.

//Bamboo boundary.
When a bamboo reaches a border, it wraps around.
When a pointer touches a bamboo, it explodes.
When the rock is stationary, the bamboo moves down at 3.5 pixels per frame.
When the rock is not stationary, the bamboo moves down at 3 pixels per frame.

//Scoring points.
When a pointer touches a spinstar, the score adds 1.
When a pointer touches a spinstar, the pointer destroys the spinstar.
When a spinstar touches a border, it explodes.

//Game win/lose conditions.
When the pointer explodes, the game is over.
When the score equals 24, the player wins.

Map:

-=----m-@o---m
------m------m
-----m------m
------m------m
-----m------m
------m------m
------m------m
-------m------m
-------m------m
------m------m
-----m------m
----m------m
----m------m
---m------m
----m------m
----m------m
-----m---p--m
------m------m
-----m------m
------m------m
------m------m


Next Page

Tutorials Home