THIS IS MY SPACE CLASS:
THIS IS MY OBSTACLE CLASS:
public class Space extends World
{
private String[] soundFiles =
{"3c", "3d", "3e", "3f", "3g", "3a", "3b", "4c", "4d","4e", "4f", "4g"};
/**
* Create space.
*/
public Space()
{
super(960, 620, 1);
makeObstacles();
randomBodies(5);
// Uncomment one of the following method calls if you want the objects created automatically:
//sunAndPlanet();
}
/**
* Set up the universe with a sun and a planet.
*/
public void sunAndPlanet()
{
removeAllObjects();
addObject (new Body (50, 240.0, new Vector(270, 0.03), new Color(255, 216, 0)), 460, 270);
addObject (new Body (20, 4.2, new Vector(90, 2.2), new Color(0, 124, 196)), 695, 260);
}
/**
* Set up the universe with a sun and two planets.
*/
public void sunAndTwoPlanets()
{
removeAllObjects();
addObject (new Body (50, 240.0, new Vector(270, 0.0), new Color(255, 216, 0)), 460, 310);
addObject (new Body (20, 4.2, new Vector(90, 2.2), new Color(0, 124, 196)), 695, 300);
addObject (new Body (24, 4.6, new Vector(270, 1.8), new Color(248, 160, 86)), 180, 290);
}
/**
* Set up the universe with a sun, a planet, and a moon.
*/
public void sunPlanetMoon()
{
removeAllObjects();
addObject (new Body (50, 240.0, new Vector(270, 0.0), new Color(255, 216, 0)), 460, 270);
addObject (new Body (20, 4.2, new Vector(90, 2.2), new Color(0, 124, 196)), 720, 260);
addObject (new Body (5, 0.8, new Vector(90, 3.25), new Color(240, 220, 96)), 748, 260);
}
/**
* Remove all objects currently in the world.
*/
private void removeAllObjects()
{
removeObjects (getObjects(Actor.class));
}
/**
* It will create the obtacles in the world.
*/
public void makeObstacles()
{
int i = 0;
for (i = 0; i < soundFiles.length; i++)
{
//addObject (new Obstacle (soundFiles [i] + ".wav"), 150 + i*60, 310);
addObject (new Obstacle (soundFiles [i] + ".wav"), 100 + i*70, 5);
addObject (new Obstacle (soundFiles [i] + ".wav"), 100 + i*70, 615);
addObject (new Obstacle (soundFiles [i] + ".wav"), 15 + 10, i*55);//vertical walls
addObject (new Obstacle (soundFiles [i] + ".wav"), 925 + 10, i*55);//vertical walls
}
}
/**
*
*/
public void randomBodies (int n)
{
removeObjects(getObjects(Body.class));
for (int i = 0; i < n; i++)
{
int s = 20 + Greenfoot.getRandomNumber(30);
double m = s * 7.0;
double speed = Greenfoot.getRandomNumber(150) / 100.0;
int d = Greenfoot.getRandomNumber(360);
int x = Greenfoot.getRandomNumber(getWidth());
int y = Greenfoot.getRandomNumber(getHeight());
int r = Greenfoot.getRandomNumber(255);
int g = Greenfoot.getRandomNumber(255);
int b = Greenfoot.getRandomNumber(255);
addObject (new Body (s, m, new Vector(d, speed), new Color(r, g, b)), x, y);
}
}
}public class Obstacle extends Actor
{
private String sound;
private boolean touched;
/**
* Create an obstacle with a sound for each, sounds are from our piano project
*/
public Obstacle (String soundFile)
{
sound = soundFile;
}
/**
* Act - do whatever the Obstacle wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
Object body = getOneIntersectingObject(Body.class);
if (body == null && touched) //we did this in our piano project but not with null, null means..
{
setImage ("block.png");
touched = false;
}
if (body !=null && !touched)
{
setImage ("block-light.png");
Greenfoot.playSound(sound);
touched = true;
}
}
}

