![]() |
| click to enlarge |
DOWNLOAD the .nlogo file HERE.
Implementation
The most important part of a game is that the user can interact with the computer. Here, the objective is to swat flies by clicking on them. This happens during the go procedure while the game is running. The netlogo dictionary tells us that there is a function mouse-down? that reports true when the mouse is clicked inside the current view (the field where the turtles move around). It also gives a bit of demo code how to find out the coordinates where the mouse was clicked. We use that code and change it a bit: we only make the patch red if there was a fly on it (so, that's the splat a killed fly leaves behind) and also kill all the flies that sit on that patch (actually, we ask them to kill themselves ;-) :if mouse-down? [ ask patch mouse-xcor mouse-ycor [ if count turtles-here > 0 [ set pcolor red ] ask turtles-here [die] ] ]
After that, we check if there are no more turtles (flies) in the game (=all the flies have been killed) and if yes, the game is finished and we call 2 more procedures to save our highscore, draw the "WIN !" lettering on the background and stop our game:
if count turtles = 0 [ save-highscore finish-game stop ]
If, on the other hand, there are still turtles in the game, then we let them all move with a move procedure that is similar to the one we used in the Rabbits on grass tutorial; only now we use the level set from the slider as stepsize:
to move fd random level ifelse (random 2) = 0 [ rt random 90 ] [ lt random 90 ] endOur highscore is saved in a file called "flygame-highscore.txt" in the same directory as the .nlogo file. If the file exists the new data is appended to the end; if no file exists yet it is created. This happens automatically when you open a file and then write to it. Note that the file-type command does not determine the type of the file but rather types (like a typewriter) to a file.
to save-highscore file-open "flygame-highscore.txt" file-type player-name file-type ": " file-type ticks file-type " TICKS (" file-type number-of-flies ifelse number-of-flies = 1 [ file-type " FLY / LEVEL " ] [ file-type " FLIES / LEVEL " ] file-type level file-type ")" file-print " " file-close end
The file then looks like this:
PLAYER 1: 49 TICKS (5 FLIES / LEVEL 2)
PLAYER 1: 64 TICKS (5 FLIES / LEVEL 2)
PLAYER 1: 190 TICKS (5 FLIES / LEVEL 4)
tina: 42 TICKS (3 FLIES / LEVEL 2)
tina: 27 TICKS (1 FLY / LEVEL 2)
tina: 11 TICKS (1 FLY / LEVEL 2)
To write "WIN !" on the background we use the finish-game procedure. The x and y coordinates for each patch to be colored are stored in the xlist and ylist lists and then we cycle through the lists and color the respective patches green:
to finish-game ; output "WIN !" on the background ; letters coordinates: W I let xlist [ -10 -10 -9 -9 -8 -8 -7 -7 -6 -6 -5 -5 -4 -4 -2 -2 -1 -1 -1 -1 -1 ... ] let ylist [ 2 1 0 -1 -2 -3 0 -1 -2 -3 0 -1 2 1 2 -3 2 1 0 -1 -2 ... ] ( foreach xlist ylist [ ask patch ?1 ?2 [ set pcolor green ] ] ) end
Here, a little trick in our setup procedure: if for some reason the player name would be empty, we set it to "PLAYER 1" but if you enter your name it will not be overwritten if you play multiple rounds pressing setup, then go:
if player-name = "" [ set player-name "PLAYER 1" ]
And finally, the turtle shape (icon) - we used Tools -> Turtle Shapes Editor -> Import from Library and imported the "bee" shape, then edited it and saved it under the name "housefly". You can use this shape in other netlogo programs with Import from model and then selecting the flygame .nlogo file.


Keine Kommentare:
Kommentar veröffentlichen