Hello
I have used the project of building the snake game. The first part with 11 - 12 years old kids and the full project with 12- 13 years old kids.
There is in this projetct a lot of concepts of OOP.
The first part is a snake , in fact ( just the head) walking and eating stars.
The second part (more difficult) is the snake growing when eating stars.
Here is a description of the project. after the arrow --> , I have added the concept used.
Concepts of OOP seen.
Part 1. 1.- Take a playfield from supply. Take an ellipse from supply and put it in playfield.
Use of an object from supply: -> Getting an Instance of an object
2.- change form to get a long vertical ellipse. Acting on an object using halo:-> Each object has own data like width and height.
3.- Putting another little ellipse to make the head (or two to make eyes) . Composing objects :-> An object can contain other objects (submorphs).
- There is two way to do this . a.- Using menu embedded. -> An Object can see what objects are under it an make a list of these objects.
b.- Using menu and checking accept drop. -> changing the property of an object.
The purpose of making head or eyes are to make front and back different.
From now we are using commands and scripts obtained by the blue eye halo.
Trying forward 5 With exclamation dot.
4.- Letting the snake move.
Then making a script of putins the tile 'forward' in the world.
-> Script , commands , value of parameter.
Click the watch, pause and ticking . -> Process.
5.- Driving the snake.
Making scripts with turn 90 and turn - 90 to drive the snake.
Now there is two ways to make a user interface with buttons to turn.
a- Using the menu in the script an button to fire this script. -> object button
b- Copying the exclamations dots . - > Two objects with same graphic but doing different things. - > Copy of objects acts the same way as the original object.
--> Both introducing the concept of interface to let the user doing things easily.
6.- Eating stars.
This will introduce ->tests ->tile representing an object. ->Which object should contain the script.
The snake can eat stars (script belong to snake) or stars being eaten by snake ( script belong to stars)
As we have one snake and many stars. The solution is script belong to stars. --> owner This way we can copy stars to get other stars without any change to scripts. take a star and get its viewer.
- Get an empty script. - Put a test structure. --> test - Get the test category and the tile overlaps. - You have to change the second name by the name of the snake. -> Parameters and type. We can only substitute an object's name by an object's name.
- in front of yes use the 'hide' tile of stars. just copy some stars -> copy of object behave like original. Not many since we have more scripts to add.
7.- Recovering the hidden stars.
Destroy all stars but one.
It is easier to program one object and then to copy it then to correct all objects.
- Take the tile 'show' and name the script view. copy some stars play and eat the stars.
Get the commands of the playfield and the category 'collection' Adjust the command tellAllContents to show 'view' as script . Try it
-> polymorphism. The same command to many objects, in this case the scripts 'view' are the all the same but they could be different.
(With tellAllContents a test is made and you get no error for objects that doesn't have the script view. (But correct code should give a script 'view' doing nothing to objects of the playfield that should not be showed. (A Sketch with game over for instance.)
8.- Take a star and program the overlapping of snake and star to show all stars. Use only concepts already seen . Could be used to test if kids are understanding what they are doing.
9.- Handling score (counting stars being eated)
Create a variable score for the snake.
--> variable. Destroy all stars but one.
Add incrementation of score after the 'hide' command in the script of the stars where the stars is eated by the snake.
-> incrementation.
Make a script to initialize the game and put 0 in the score.
-> initialization.
Part 2 The growing snake.
1.- Making an ellipse that is following the head.
This ellipse has to go where the head is, then the head goes a step forward. Make the script 'follow' which gives to the x and the y of the ellipse the values of x and y for the snake. This script is called in the snake's script that moves the snake just before the forward tile.
2.- second ellipse . Make a copy of the ellipse. This ellipse should take the place of the first ellipse and then call the script 'follow' of the first ellipse to let it go on the snake.
--> Receiver, we can change who has to do something, by changing the names on the left of the tile .
The script that moves the snake has to be corrected to call the script 'follow' of the last ellipse.
we can add one more ellipse.
Each time we add an ellipse : - we copy the last ellipse. - in the script follow we have to changes the names on the left by the name of the ellipse which is just before. - we have to change the name of the receiver of the call to follow in the snake's script.
3.- Generalization . To generalize the process we need variables to store the names of the receiver.
In the snake we create the variable 'last' of type player which holds the representation of the last ellipse. For that you have to click on the black dot of the gadget and click on the last ellipse.
use the variable last as name of receiver for the x and y affectations.
Keep the last ellipse and destroy the others.
create a variable 'next' of type player in this ellipse.
Change the names of the receiver in the tiles of the script.
Set the value of the variable to snake.
You'll get an error if you try it now, the snake will receive the command follow but doesn't have a script 'follow'
To correct this, just take an empty script in the snake's script viewer and call it follow. --> polymorphism. --> Recursivity The last ellipse tell follow to the next one and the next one tell follow to their next one .... until telling follow the snake which does nothing (end of recursion).
Now you can copy the last ellipse . give to its variable next the last ellipse and give the variable last in the snake the value of this new last ellipse.
Now its very important to report that the interface of E-Toys allow doing things "by hand" and see how things are working before ptograming them.
This concrete level is very important in the construction of thinking.
4.- Programming the creation of a new ellipse. For that we need to keep track of the ellipse which was the last one. In the snake we create another variable 'beforeLast' of type player.
The script 'new' will save the reference to the last ellipse in the variable beforeLast. create a new ellipse by copying the last and store the result in the variable last. Put in the variable next of this new ellipse the reference to the beforeLast. Put this ellipse in the play field . (Object created by copy are born in the world.) Put the reference to the last ellipse in the variable last of the snake.
5.- Add a call to snake's new in the script 'eaten' of the stars.
6.- Destroy all ellipses but the last for a new game. each ellipse tell its next to destroy before destroying itself. --> recursivity.
-----Message d'origine----- De: squeakland-bounces@squeakland.org A: squeakland@squeakland.org Date: 16.02.04 09:21 Objet: [Squeakland] Suggestions for pedagogical examples in Squeak?
Hello all.
If any of you have any helpfull information about my questions below, I will be very gratefull.
squeakland@lists.squeakfoundation.org