Cheese Code
PepperJack Cheese Code
SmoothMover
I can't get the Vector to work.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Cheese here.
*
* @author (your name)
* @version (a version number or a date)
*/
public abstract class Cheese extends Animal
{
/**
* Act - do whatever the Cheese wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public int speed;
public void fly()
{
speed = 25;
setLocation(getX(), getY() - speed);
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class PepperJackCheese here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class PepperJackCheese extends Cheese
{
/**
* Act - do whatever the PepperJackCheese wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public int x;
private int size;
private int stability;
public void act()
{
fly();//this is the moving method
//breakUp();
if (getY() <= 0 && getWorld()!= null)//disappear at top of world
{
getWorld().removeObject(this);
return;
}
}
public PepperJackCheese()
{
this(50);
}
public PepperJackCheese(int size)
{
super(new Vector(Greenfoot.getRandomNumber(360), 2));
setSize(size);
}
public PepperJackCheese(int size, Vector speed)
{
super(speed);
setSize(size);
}
public void setSize(int size)
{
stability = size;
this.size = size;
GreenfootImage image = getImage();
image.scale(size, size);
}
public int getStability()
{
return stability;//<LEFT OFF HERE
}
private void breakUp()
{
CheeseWorld space = (CheeseWorld) getWorld();
Greenfoot.playSound("Explosion.wav");
space.countScore();
if(size <= 16)
{
getWorld().removeObject(this);
space.addAsteroids(1);
space.countScore();
space.countAsteroids();
}
else
{
int r = getMovement().getDirection() + Greenfoot.getRandomNumber(45);
double l = getMovement().getLength();
Vector speed1 = new Vector(r + 60, l * 1.2);
Vector speed2 = new Vector(r - 60, l * 1.2);
Asteroid a1 = new Asteroid(size/2, speed1);
Asteroid a2 = new Asteroid(size/2, speed2);
getWorld().addObject(a1, getX(), getY());
getWorld().addObject(a2, getX(), getY());
a1.move();
a2.move();
getWorld().removeObject(this);
}
}
}import greenfoot.*;
public abstract class SmoothMover extends Actor
{
private Vector movement;
private double exactX;
private double exactY;
public SmoothMover()
{
this(new Vector());
}
public SmoothMover(Vector movement)
{
this.movement = movement;
}
public void move()
{
exactX = exactX + movement.getX();
exactY = exactY + movement.getY();
if(exactX >= getWorld().getWidth()) {
exactX = 0;
}
if(exactX < 0) {
exactX = getWorld().getWidth() - 1;
}
if(exactY >= getWorld().getHeight()) {
exactY = 0;
}
if(exactY < 0) {
exactY = getWorld().getHeight() - 1;
}
super.setLocation((int) exactX, (int) exactY);
}
public void setLocation(double x, double y)
{
exactX = x;
exactY = y;
super.setLocation((int) x, (int) y);
}
public void setLocation(int x, int y)
{
exactX = x;
exactY = y;
super.setLocation(x, y);
}
public double getExactX()
{
return exactX;
}
public double getExactY()
{
return exactY;
}
public void addForce(Vector force)
{
movement.add(force);
}
public void accelerate(double factor)
{
movement.scale(factor);
if (movement.getLength() < 0.15) {
movement.setNeutral();
}
}
public double getSpeed()
{
return movement.getLength();
}
public void stop()
{
movement.setNeutral();
}
public Vector getMovement()
{
return movement;
}
}
