image Computational Thinking - How Many Rabbits are in the Top Half?

The following game plan uses variable attributes to keep track of the number of rabbits in the top half of the canvas. Recall that "position_x" and "position_y" are built-in variable attributes that hold the current position coordinates of a character. Every pixel on the canvas has an x, y-coordinate, like points on a graph. One frame of the canvas is 600x600 pixels. However (0,0) is in the upper left corner of the canvas, instead of the center. As we move right, the x-coordinate values increase. As we move down, the y-coordinate values increase. Using these x, y coordinates, we can position the pixels of each video game character on a specific point on the canvas.

Back to our original task: How would you keep track of the number of rabbits in the top half of the canvas? Consider the following game plans:

There are 10 rabbits.
The rabbits wander around.
When a rabbit reaches a border, it turns around.
If the position_y of a rabbit is less than 300, the rabbit is in_the_top_half.
Otherwise, it is not in_the_top_half.
Since the canvas is 600 pixels tall, rabbits in the top half will have a position_y less than 300. These rabbits are given the Boolean attribute "in_the_top_half".
When a rabbit is in_the_top_half, it turns yellow.
Otherwise, it returns to original color.
A color attribute is also added to the rabbits in the top half. Now, when the wandering rabbits cross from top to bottom, or from bottom to top, they change color.
If a rabbit that is not yellow is in_the_top_half, score increments.
If a yellow rabbit is not in_the_top_half, score decrements.
Note that that when the rabbits cross over, they become "in_the_top_half" before they turn yellow in the next frame. Score is used to keep count of how many rabbits enter the top half of the canvas.

Try it yourself: Instead of using score, we can replace score with an variable attribute num_in_top of another single-instance object, say a diamond. If the num_in_top of the diamond is greater than 5, you win. You can display the value of variable attributes by "Display num_in_top of the diamond." Try that out and see!


Complete Sample Games for Computational Thinking

// Game #1. 7 Up.

You control the pointer.

When you press the left arrow, the pointer moves left.
When you press the right arrow, the pointer moves right.
When you press the spacebar on the keyboard, the pointer shoots up.

When the pointer touches the border, it wraps around.
The speed of the pointer is 6.
Topaz is invisible.
The pearls move down.
When the topaz collides with a pearl, the pearl reverses direction.
If the position_y of a pearl is less than 300, the pearl is in_the_top_half.
Otherwise, it is not in_the_top_half.
If a pearl is in_the_top_half, score increments.
If a pearl is not in_the_top_half, score decrements.
When a pearl is shot, the pearl turns red for 3 seconds.
When a pearl is red, the pearl moves up.
When a bullet collides with a wall, the bullet disappears.

When score is equal to 7, you win.

Map:

-
-
-
-
-
-
-
-
-
sssss----------sssss
----w----------w
----w----------w
----w----------w
----w----------w
----wRRRRRRRRRRw
----w----------w
----w----------w
----w----------w
wwwwwFFFFFFFFFFwwwww
----------p

// Game #2. Fishing birds.

You control the kitten.
When the right arrow is pressed, the kitten moves right.
When the left arrow is pressed, the kitten moves left.
When space bar is pressed, the kitten inserts a diamond.

//Hook movement.
There are 0 diamonds initially.
The size of diamonds is 15.
If a diamond whose the position_y is less than 590 is not red, the position_y of the diamond increments by 2.
If the position_y of a diamond greater than 580, the diamond becomes red.
If a diamond is red, the angle of diamond is 180.
If a diamond is red, the position_y of a diamond decrements by 2.
If a diamond whose the position_y is less than 300 is red, it explodes.
When a diamond collides with a bird, it becomes red.

//Fishes movement.
30 birds are scattered near the bottom.
The birds wander around.
When a bird reaches a border, it turns around.
If the position_y of a bird is less than 300, the position_y of a bird increments.
If the position_y of a bird is greater than 300, the bird is in_the_bottom_half.
Otherwise, it is not in_the_bottom_half.
When a bird collides with a diamond that is not red, it explodes and score decreases by 1.

//Turtle movement.
7 turtles are scattered near the bottom.
The turtles wander around.
When a turtle reaches a border, it turns around.
If the position_y of a turtle is less than 300, the position_y of a turtle increments.

//Score control.
If a bird is in_the_bottom_half, score increments.

//Game over.
When score is less than 1, you win.
When a diamond that is not red collides with a turtle, you lose.

Map:

-
-
-
-
-
-
-
-
----------i
FFFFFFFFFFFFFFFFFFFF
hhhhhhhhhhhhhhhhhhhh
hhhhhhhhhhhhhhhhhhhh
hhhhhhhhhhhhhhhhhhhh
hhhhhhhhhhhhhhhhhhhh
hhhhhhhhhhhhhhhhhhhh
hhhhhhhhhhhhhhhhhhhh
hhhhhhhhhhhhhhhhhhhh
mmmhhhhhhhhhhhhhhhhh
mmmmhhhhhhhhhhhhhhhh
mmmmLLLhhhLhhhhhhhLL


Next page

Tutorials Home