Vraag 2:
Level 1:
Onder public class lvl 1 extend world
private int flowers=10;
private int margin=50;
Onder public lvl 1
makeFlowers();
private void makeFlowers()
{
for (int teller = 0;teller<flowers;teller++)
{
for (int teller2 =0;teller2<flowers;teller2++)
{
if(teller % 2 ==0)
{
addObject (new Flower(true), margin+(teller*30), margin+(teller2*30));
}else
{
addObject (new Flower(false), margin+(teller*30), margin+(teller2*30));
}
}
}
}
Flower class:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Flower here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Flower extends BaseClass
{
public void act()
{
// Add your action code here.
}
public Flower(boolean poisonous)
{
if (poisonous==true)
{
setImage("sm-flower-blue.gif");
}else
{
setImage("sm-flower-pink.gif");
}
}
}
Vraag 3
Wolf class:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class wolf here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class wolf extends LittleRedCap
{
private int marge=90;
private int randomWalk = 5;
public void act()
{
move(10);
turnAtEdge();
walkRandom();
eatLittleRedCap();
}
/**
* turns the wolf unpredictable at the edge in another rotation.
*
*
*/
public void turnAtEdge()
{
int min=0;
int max=90;
if(atWorldEdge())
{
if (getX()>570)
{
max=getRandomNumber(100,200);
}
if (getY()>570)
{
max=getRandomNumber(180,240);
}
if (getX()<30)
{
max=getRandomNumber(230,400);
}
if (getY()<30)
{
max=getRandomNumber(30,160);
}
setRotation(max);
}
}
/**
* causes a random walk
*/
public void walkRandom()
{
//we have to set +1 for the max because the max variable will be excluded
int randomNumber = getRandomNumber(-randomWalk,randomWalk+1);
setRotation(getRotation()+randomNumber);
}
public void eatLittleRedCap()
{
if(canSee(LittleRedCap.class))
{
removeObject(LittleRedCap.class);
}
}
}
Baseclass:
import greenfoot.*;
import java.util.Random;
import java.util.List;
import java.util.ArrayList;
/**
* helpClass. This is the base class for all animals. In addition to the standard Actor
* methods, it provides the ability to move and turn.
*
* @author Michael Kolling
* @version 1.0
*/
public class BaseClass extends Actor
{
private static final double WALKING_SPEED = 1.0;
/**
* Turn 'angle' degrees towards the right (clockwise).
*/
public void turn(int angle)
{
setRotation(getRotation() + angle);
}
/**
* Move forward in the current direction.
*/
public void move()
{
double angle = Math.toRadians( getRotation() );
int x = (int) Math.round(getX() + Math.cos(angle) * WALKING_SPEED);
int y = (int) Math.round(getY() + Math.sin(angle) * WALKING_SPEED);
setLocation(x, y);
}
/**
* Test if we are close to one of the edges of the world. Return true is we are.
*/
public boolean atWorldEdge()
{
if(getX() < 20 || getX() > getWorld().getWidth() - 20)
return true;
if(getY() < 20 || getY() > getWorld().getHeight() - 20)
return true;
else
return false;
}
/**
* Return true if we can see an object of class 'clss' right where we are.
* False if there is no such object here.
*/
public boolean canSee(Class clss)
{
Actor actor = getOneObjectAtOffset(0, 0, clss);
return actor != null;
}
/**
* Try to eat an object of class 'clss'. This is only successful if there
* is such an object where we currently are. Otherwise this method does
* nothing.
*/
public void removeObject(Class clss)
{
Actor actor = getOneObjectAtOffset(0, 0, clss);
if(actor != null) {
getWorld().removeObject(actor);
}
}
public int getRandomNumber(int min, int max)
{
Random random = new Random();
int n = random.nextInt(max-min) + min;
return n;
}
}