Exercise

Star Money, Star Thalers by the Grimm Brothers
There was once upon a time a little girl whose father and mother were dead, and she was so poor that she no longer had a room to live in, or bed to sleep in, and at last she had nothing else but the clothes she was wearing and a little bit of bread in her hand which some charitable soul had given her. She was good and pious, however. And as she was thus forsaken by all the world, she went forth into the open country, trusting in the good God.

Then a poor man met her, who said, “Ah, give me something to eat, I am so hungry.”

She handed him the whole of her piece of bread, and said, “May God bless you,” and went onwards.

Then came a child who moaned and said, “My head is so cold, give me something to cover it with.”

So she took off her hood and gave it to him. And when she had walked a little farther, she met another child who had no jacket and was frozen with cold. Then she gave it her own, and a little farther on one begged for a frock, and she gave away that also.

At length she got into a forest and it had already become dark, and there came yet another child, and asked for a shirt, and the good little girl thought to herself, “It is a dark night and no one sees you, you can very well give your shirt away,” and took it off, and gave away that also.

And as she so stood, and had not one single thing left, suddenly some stars from heaven fell down, and they were nothing else but hard smooth pieces of money, and although she had just given her shirt away, she had a new one which was of the very finest linen. Then she put the money into it, and was rich all the days of her life.

Exercise 9.1.   Develop a game program based on the story of “Star Money, Star Thalers.”

Add the teachpack
draw.ss
The main function of the program should consume a natural number and drop that many thalers (from the top of the world) on the girl (at the bottom of the world), one at a time. The thaler should move randomly to the left or right and downwards, but should always stay within the boundaries of the world (canvas). The girl should react to 'left and 'right keystrokes, moving a moderate number of pixels in reaction but always staying completely within the boundaries of the world.

Hints and suggestions:

  1. Represent the girl visually as a container that is open at the top (the outline of a rectangle without top boundary). Show the number of thalers it has caught inside of the container.

    Design a function for drawing the girl.

  2. Represent the thaler visually as a yellow disk.

    Design a function for drawing the thaler.

  3. The world of StarThalers contains two physical objects: the girl and one flying thaler. It also contains two abstract objects: the number of thalers yet to come and the number of thalers that the girl caught so far.

    Pick physical dimensions of the world (width, height, background color).

    Design a function for drawing the world, assuming that the world’s canvas is visible.

  4. Define the function tock, which refreshes the visible world in reaction to time events:

    (start WORLD-WIDTH WORLD-HEIGHT)
    (big-bang .1 ... some initial world ...)
    (on-tick-event tock)
    

  5. Design a function for moving the world and another one for moving a thaler. Then modify tock so it too can move the world.

  6. Design a function for moving the girl in reaction to a keystroke. The function consumes a girl and a keystroke. Its result is the girl in the new position.

    Define the function react, which uses this new function to move the girl in the world:

    (start WORLD-WIDTH WORLD-HEIGHT)
    (big-bang .1 ... some initial world ...)
    (on-tick-event tock)
    (on-key-event react)
    

  7. Make sure that the world reacts (at tick events) to the landing of a thaler, to the girl catching a thaler, and to the world running out of thalers in addition to moving the thaler as needed.

Good luck and enjoy!