My world code
My Rock code
My Counter code
import greenfoot.*;
public class MyWorld extends World
{
public int a = 0;
private int threshold = 100; //increase to make the time until a random thing is added longer...
private int thresholdWave = 3; //increase to make the time until a random thing is added longer...
/**
* Creates a world with the size of 1100x500 cells, where every cell is just 1 pixel
*/
public MyWorld()
{
super(1100, 500,1);
addObject(new Counter(), 50, 25);
}
public void act()
{
a++;
if(a == threshold)
{
RandomPlacement();
a = 0;
}
}
public void RandomPlacement()
{
Counter counter = new Counter();
int x = Greenfoot.getRandomNumber(getWidth());
int y = 10;
addObject (new Rock(counter),x,y);
}
{
if (Greenfoot.mouseClicked(this)){ // Hvis der er trykket med musen på objektet sker følgende
int x= Greenfoot.getRandomNumber (getWidth());//generer tilfældigt nummer
int y= Greenfoot.getRandomNumber (getHeight());//generer tilfældigt nummer
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Rock here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Rock extends Mover
{
private static final int NUM_FRAGMENTS = 50;
private int rotate = Greenfoot.getRandomNumber(9)+1;
private Counter counter;
public Rock(Counter pointCounter)
{
counter = pointCounter;
}
/**
* Act - do whatever the Rock wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
setLocation(getX(), getY()+1);// the actor's y coordinate placement is added by one each time the act button is pressed.
setRotation(getRotation () +rotate);// The rotation of Rock is increasing by randomly generated number(1-10) each time act is pressed.
move();//the rotation affects the x coordinate of Rock.
if (Greenfoot.mouseClicked(this)){
explode();
counter.rockExploded();
}
}
public void explode()
{
placeDebris (getX(), getY(),NUM_FRAGMENTS);
getWorld(). removeObject(this);
}
private void placeDebris(int x, int y, int numFragments)
{
for (int i=0; i < numFragments; i++) {
getWorld().addObject (new Debris(), x, y );
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
* A simple counter with graphical representation as an actor on screen.
*
* @author mik
* @version 1.0
*/
public class Counter extends Actor
{
private static final Color transparent = new Color(0,0,0,0);
private GreenfootImage background;
private int value;
private int target;
/**
* Create a new counter, initialised to 0.
*/
public Counter()
{
background = getImage(); // get image from class
value = 0;
target = 0;
updateImage();
}
/**
* Animate the display to count up (or down) to the current target value.
*/
public void act()
{
if (value < target) {
value++;
updateImage();
}
else if (value > target) {
value--;
updateImage();
}
}
/**
* Add a new score to the current counter value.
*/
public void add(int score)
{
target += score;
}
/**
* Return the current counter value.
*/
public int getValue()
{
return value;
}
/**
* Set a new counter value.
*/
public void setValue(int newValue)
{
target = newValue;
value = newValue;
updateImage();
}
public void rockExploded()
{
add(5);
}
/**
* Update the image on screen to show the current value.
*/
private void updateImage()
{
GreenfootImage image = new GreenfootImage(background);
GreenfootImage text = new GreenfootImage("" + value, 22, Color.BLACK, transparent);
image.drawImage(text, (image.getWidth()-text.getWidth())/2,
(image.getHeight()-text.getHeight())/2);
setImage(image);
}
}
