Transcript Document

Greenfoot
Lesson 3
Created by NW 2012 – please note all copyright on images used is property of copyright holder. Note: some of the more complicated descriptions are taken
from http://www.greenfoot.org/doc/ so credit to them too!
Objectives
• Understand what our code actually means!
(learn a new language).
• Be able to identify rules and variables within
our code.
• Test predictions by varying the rules in our
program
Created by NW 2012 – please note all copyright on images used is property of copyright holder. Note: some of the more complicated descriptions are
taken from http://www.greenfoot.org/doc/ so credit to them too!
Let’s look at the code we wrote last lesson
Move() – move the actor a
certain speed.
isKeyDown – check whether a
given key is currently pressed
down
Turn() the actor a certain
amount of degrees.
Let’s look at the code we wrote last lesson
This area of code looks to see if
our actor (the Ant) is actually
touching a hamburger.
The first points are the X and Y
offset from the ants current
position. We used 0 to know
that it (the hamburger) is
directly under our object.
We use the “null” value as is
there is no hamburger touching
an ant then there is nothing to
remove!
These three lines of code
instruct what should happen if
our actor (Ant) does actually
touch a hamburger.
Refactoring the code
Lets see if we can get our ant to
move in a different manner.
Change your code to look like the
following.
Once you have, RUN your game and
see what has changed.
This is the area of code we
have changed – see if you
can work out what it is
going to do.
Following the mouse
Lets see if we can get our ant to
follow the mouse. Change your
code to look like the following.
Once you have, RUN your game and
see what has changed.
This area of code we have
added now asks greenfoot
to see if the mouse is
moved. (remember null is
needed as a command if
nothing is happening!)