Greenfoot Flashcards
How do you make an actor turn and move randomly?
move(Greenfoot.getRandomNumber(2));
turn(90*Greenfoot.getRandomNumber(3));
How do you make actor move in the direction of the arrow keys when pressed?
if (Greenfoot.isKeyDown(“left”))
{
setLocation(getX()-1,getY());
}
if (Greenfoot.isKeyDown(“right”))
{
setLocation(getX()+1,getY());
}
if (Greenfoot.isKeyDown(“up”))
{
setLocation(getX(),getY-1());
}
if (Greenfoot.isKeyDown(“down”))
{
setLocation(getX(),getY+1());
}
How to edit actor(1) code to make an actor(1) remove another actor(2) when it collided with it?
Actor collided = getOneIntersectingObject(actor(2).class);
if (collided != null)
{
getWorld().removeObject(collided);
}
How to add a sound every time actor(1) collides with actor(2)?
Greenfoot.playSound(“pop.wav”);
How to edit actor(1) code so the counter displays how many actor(2) it’s collided with?
((Counter)getWorld().getObjects(Counter.class).get(0)).bumpCount(1);
What is the correct counter code?
private int totalCount = 0;
public Counter()
{
setImage(new GreenfootImage(“0”, 20, Color.WHITE, Color.BLACK));
}
public void bumpCount(int amount)
{
totalCount += amount;
setImage(new GreenfootImage(“” + totalCount, 20, Color.WHITE, Color.BLACK));
How to edit actor(2) code so it removes itself if it collides with actor(3)?
Actor collided = getOneIntersectingObject(actor(3).class);
if (collided != null)
{
getWorld().removeObject(this);
}
How to edit actor(2) code so that 1 point is deducted from the counter if actor(2) is removed?
((Counter)getWorld().getObjects(Counter.class).get(0)).bumpCount(-1);
How to make the world class a square
Change the numbers in the world class’s method into:
super(9, 9, 60);
How to make actor(2) move randomly left to right?
move(Greenfoot.getRandomNumber(2));
turn(180*Greenfoot.getRandomNumber(3));
What does the term ‘superclass’ mean and examples?
A class which other classes inherit from, e,g. World & Actor
What does the term ‘property’ mean?
A variable associated with a class, e.g. private xyz
What does the term ‘class’ mean?
Representation of a real world object, e.g. cat, mouse & ground
What does the term ‘method’ mean?
A function associated with a class, e.g. public xyz
What does the term ‘comment’ mean?
An additional bit of information that explains what some code is doing, e.g.
/**
*xyz
*/