image Computational Thinking -- Lower Level of Abstraction with Variable Attributes

Conventional programming works on a lower level of abstraction. Recall that eating a sandwich can be broken down to:
  1. lift up the sandwich
  2. open mouth
  3. bite into the sandwich
  4. chew
  5. swallow
  6. ...
Similarly, when you use the action "chase" in a game plan, you do not focus on the code that actually implements the action. The following game plan breaks down "chase" into a lower level of abstraction. "position_x" and "position_y" are built-in variable attributes that hold the current position coordinates of a character.

When the position_x of a fox is less than the position_x of the rabbit, the position_x of the fox increments.
Otherwise, the position_x of the fox decrements.
When the position_y of a fox is less than the position_y of the rabbit, the position_y of the fox increments.
Otherwise, the position_y of the fox decrements.
This game plan describes a fox chasing a rabbit. Note that you can only compare attributes in an antecedent. In addition, you cannot use variable attributes in arithmetic expressions.


Try it yourself: How would you make the rabbit flee the fox at a low level of abstraction?


Complete Sample Games for Low Level of Abstraction

// Game #1. Get All Spinstars.

There is a fox. There is a dino. There are 20 spinstars. There are 10 aliens.

You control the fox.
When an arrow key is pressed, the fox moves in the same direction.

The aliens move around.
When the alien reaches the border, the alien reverses direction.
When the fox reaches the border, the fox stops.
When the fox collides with a spinstar, the spinstar disappears.
When the fox collides with a spinstar, the score increases by 1.
When the position_x of a dino is less than the position_x of the fox, the position_x of the dino increments.
Otherwise, the position_x of the dino decrements.
When the position_y of a dino is less than the position_y of the fox, the position_y of the dino increments.
Otherwise, the position_y of the dino decrements.

When the score is equal to 20, you win.
When the fox collides with the dino, it is gameover.
When the fox collides with the alien, it is gameover.

// Game #2. Kitten Surfer.

Wide canvas with 2 frames.
You control the kitten.
When the right arrow is pressed, the kitten moves right direction.
When the left arrow is pressed, the kitten moves left direction.
When the kitten collides with a ball, it bounces.
The kitten falls down.
The speed of the ball is 7.
The speed of the pearl is 7.
When the kitten collides with a brick or pearl, it becomes empowered for 1 second.
When the kitten is empowered and up arrow is pressed, the kitten moves up.

//Ball physics.
The ball starts by moving up.
When position_y of the ball is less than 100, the ball becomes yellow.
When position_y of the ball is greater than 500, the ball becomes blue.
When a ball is blue, it moves up.
When a ball is yellow, it moves down.
When the position_x of the ball is less than 1300, the position_x of the ball increments.
//Clears ball.
When the position_x of the ball is greater than 1300, the ball explodes.

//Pearl physics.
The pearl starts by moving up.
When position_y of the pearl is less than 100, the pearl becomes yellow.
When position_y of the pearl is greater than 500, the pearl becomes blue.
When a pearl is blue, it moves up.
When a pearl is yellow, it moves down.
When the position_x of the pearl is less than 1300, the position_x of the pearl increments.
//Clears ball.
When the position_x of the pearl is greater than 1300, the pearl explodes.

//Ball and pearl spawn.
When the gem is not dead, it becomes yellow.
When the gem is yellow, it inserts ball.
When the topaz is not dead, it becomes yellow.
When the topaz is yellow, it inserts pearl.

//Save pandas.
There is a spinstar.
The speed of spinstar is 10.
The spinstar starts by moving in a random direction.
When the spinstar collides with border, it reverses direction.
When the panda is gone, the spinstar becomes empowered for 0.3 second.
When the spinstar is empowered, it inserts panda.
When the panda collides with kitten, it disappears and scores increases by 1 point.
When the position_y of the spinstar is greater than 500, the position_y of the spinstar decrements by 5.

//Game Over.
When the kitten collides with down border or up border, it explodes.
When the kitten is gone you lose.
When the score equals 10, you win.

Map:

-i
kk
-
-
-
-
-
-
-
GF-----------------ln
-------------------R


Next page

Tutorials Home