Logotron educational software. Partners with the teaching profession - Pioneers in Learning
menubar search this site my shopping cart support products about home
 

Catch the Fly

Designing the Project | Programming
Improving the Project
| Download the Project

Task

  • to create a simple game where moving objects (i.e. flies) are caught by clicking on (or swatting) them.
You need to know
  • how to set events for a turtle, and how to work with buttons
You will learn
  • how to name, run and stop processes, and how to change a turtle's shape in the process

Designing the Project

On the page there is a turtle with the image of a fly. It can move around the screen freely until it is clicked on by a mouse i.e. swatted. The graphics needed for this project, therefore, are:

  • an image of the fly
  • an image of a swatted fly
  • an image of a fly-swatter

You can draw these yourself in LogoMotion or download them as a zipped file by clicking here (34 KB). The graphics are named fly.lgf, trapped.lgf and flytrap.lgf. Unzip them into

C:\Program Files\Imagine Logo\image

   
Take a look at fly.lgf in particular. You will see that the .LGF has eight frame items so that the fly appears to flutter when the animation is run. Also, in order that the .LGF responds to commands that you will give it (to make it change direction on the screen) the Heading Mode has been set. This setting means that the frames appear as segments of a circle with values in degrees.

Programming

Load all the images into your project by typing the following into the command line of Imagine Logo:

? make "fly loadImage "fly.lgf 
? make "trapped loadImage "trapped.lgf 
? make "flytrap loadImage "flytrap.lgf 

You can check that these images are now in your project by viewing them in the Explore Project Window. Clicking F4 on your keyboard will open this window to the right of your screen. The Explore Project Window makes it possible for you to explore the structure of your project: its pages and turtles, all building blocks (panes, text boxes, buttons, sliders…) are displayed here with all their settings.

You will see that, at present, your project consists of a single page (screen) and your three Global Variables. Double click on any of them now, and a dialog window will appear displaying the three graphics.

   

Change your turtle's shape into that of the fly.

Right click on turtle and choose Change t1 from the dropdown menu. Then click on Set Shape... and browse until you find your fly.lgf graphic.

 
   

In the command line at the bottom of the Imagine Logo screen type the following run process and see what happens to your turtle:

? every 60 [fd 2] every 800 [rt any]

The fly is moving forward two pixels every 60 milliseconds and then turning a random amount (any) to the right every 800 milliseconds. Stop the turtle by clicking F12 on your keyboard.

You can now specify an event for your turtle. Right click on it again ad choose Change t1. Select the Events tab. Clicking on the Add button at the bottom of the dialog window will give you a list of events to choose from. Select onLeftDown.

Where your cursor is flashing next to the onLeftDown event, type the following:

setShape :trapped stopAll

This tells your turtle that, if it is clicked on with the left mouse button, it must change its shape to trapped.lgf and stop moving.

See if your event works. Run your previous process, by typing

? every 60 [fd 2] every 800 [rt any]

into the command line. Now click on the fly.

To finish your game, you need to add a New Game button that will start, or reset Catch a Fly.

To create the button, go to the Main toolbar, click on New Button, and then place your button called b1 anywhere on the screen.

The properties of your button will need to be changed. Right click on the button and select Change b1. A dialog window will appear.

Firstly, you will need to change the caption, or words that will appear on the button. Next to Caption, type 'New game'. Then give your button a command in the onPush line. This command will run every time the button is clicked:

setMouseCursor :flytrap 
setShape :fly 
every 60 [fd 2]
every 800 [rt any]

NB: Although these commands have been split into four lines so that they are easier to read, in the event onPush they must be typed as one line.

Click on OK when you have done this. Play your game to see if it works.


Improving the Project

You can add other flies by copying the first one. For now, just add one more (so that you have two turtles).

Right click on your turtle and choose Copy to Clipboard from the dropdown menu. Then right click anywhere on the screen and choose Paste from Clipboard. A 'clone' of your first turtle should appear. It will have, not only the same appearance, but also the same properties of movement.

Click on the New Game button. You will see that the new fly you have created does not move. This is because you need to make changes to the commands you have given to the turtles.

In the command line at the bottom of the Imagine Logo screen type:

? tell all

If you now click on the New Game button again all of your flies should be moving. However, if you swat one, then you will find that the others have also stopped.

   

You can correct this:
  • Right click on the button and choose Change t1.
  • Change the event onPush to say just newGame
  • Click on the Procedures tab. Then click on the Add button. You are going to create a new procedure called newGame. Type the following:

    to newGame
     
    setMouseCursor :flytrap
     setShape :fly
     t1'every 60 [fd 2]
     t1'every 800 [rt any]
     t2'every 60 [fd 3]
     t2'every 800 [rt 10+random 100]
    end

NB: This command does not have to be typed into the Procedures for the button. It could also be typed into the onPush command line. However, it is a very long command, and is more easily read under Procedures.

   
Click on the New Game button. When the flies start moving type

? sh allProcesses

This command makes Imagine list all the current processes. The result will be:

[@Commander [fd 2] [rt any] [fd 3] [rt 10 + random 100]]

This shows there are four processes running, two for each fly. We can correct the event onLeftDown of both flies so that each of them does not stop allProcesses but only those ones running for it.

Right click on the first fly and choose Change t1 from the dropdown menu. Select the Events tab and change the onLeftDown event to stop only processes [fd 2] and [rt any]

Similarly for t2, change the onLeftDown event to stop only processes [fd 3] and [rt 10 + random 100] .

Try the project again– now, if we trap one fly, it stops but the other one continues to move.

We can now add new flies, create new processes for them and then stop them with the onLeftDown event.
 
Another possibility for stopping a specific process is to name it at its running.

Correct the procedure newGame of the button b1:

to newGame
 setMouseCursor :flytrap
 setShape :fly
 (t1'every 60 [fd 2] "t1h)
 (t1'every 800 [rt any] "t1o)
 (t2'every 60 [fd 2] "t2h)
 (t2'every 800 [rt any] "t2o)
end

Click on the New Game button. When the flies start moving type

? sh allProcesses

This time the result will be:

[@Commander t1h t1o t2h t2o]

The same processes are running as before, but now they each have a name, i.e.t1h is actually [fd 2]. Now we can change the event onLeftDown for each turtle and stop just its two processes:

setShape :trapped cancel "t1h cancel "t1o 

Repeat with the other two processes for t2.

However, this way we still have to copy and paste the specific process names for each new turtle. More experienced programmers have probably already realised that if we choose appropriate names for the processes (e.g. ones depending on the names of the turtle itself), we will not have to modify the event for each new turtle.

Edit the procedure newGame so it runs as many processes as there are turtles on the page and give them appropriate names:

to newGame
 setMouseCursor :flytrap
 tell all
 setShape :fly
 each
  [(every 60 [fd 2] word who "h)
   (every 800 [rt any] word who "o)]
end

What we did:

  • we changed the mouse's shape to flytrap
  • we addressed all turtles by using the basic command tell
  • we changed the shape of all turtles to fly
  • the each command goes through all of the turtles and runs the processes of moving and turning for them; at the same time it names them by attaching the letter h or o to the name of the turtle (the name of the turtle is found by the who command ).
Delete the turtle t2.

For t1 correct the event onLeftDown:
setShape :trapped cancel word myName "h cancel word myName "o

First the command changes the turtle's shape and then stops both processes that are run for it – it creates the names of the processes by composing its name and the letter "h and "o into single word.

We can now copy the turtle t1 through the Clipboard and insert any number of turtles. The button b1 will run and name the processes for them and each of them will stop only its own two processes.

Download the Project

You can download an example of the finished project by clicking here

 
   
  back to top