This site requires JavaScript, please enable it in your browser!
Greenfoot back
greencarsonfoot
greencarsonfoot wrote ...

2019/6/15

Help me with my code!

greencarsonfoot greencarsonfoot

2019/6/15

#
So I am new to Greenfoot and I am having a problem with a actor. Basically, all I need is for the program to quit when the value of the fuel hits zero.
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)
public class shuttle extends Actor
{
    //variables
    private static final int EAST = 0;
    private static final int WEST = 1;
    private static final int NORTH = 2;
    private static final int SOUTH = 3;
    public Flag F1;
    public Home H1;
    public Explosion E1;
    private int direction;
    GoingToMoon world = (GoingToMoon)getWorld(); //tells the program that space is the same as the word world
    
    public Counter FCounter;
    public void setDirection(int direction)    //constructor
    {
        this.direction = direction;
        switch(direction)
        {
            case SOUTH:
            setRotation(180); // points down
            break;
            case EAST:
            setRotation(90); // points right
            break;
            case NORTH:
            setRotation(0); // points up
            break;
            case WEST:
            setRotation(270); // points left
            break;
            default: // execution continues without change
            break;
        }

    }

    public void FuelDisplay()
    {
        int Fuel = 5;
        if (FCounter == null)
        {
            FCounter = new Counter ("Fuel: ", Fuel);
            getWorld().addObject(FCounter, 720, 280);
        }
        FCounter.Decrement();
        if (Fuel == 0)
        {
            GoingToMoon world = (GoingToMoon)getWorld();
            Greenfoot.stop();
        }
    }

    public shuttle()
    {
        setDirection(NORTH); //sets to the default direction
    }

    public void move()
    {
        switch(direction)
        {
            case SOUTH:
            setLocation(getX(), getY() + 1);
            break;
            case EAST:
            setLocation(getX() + 1, getY());
            break;
            case NORTH:
            setLocation(getX(), getY() - 1);
            break;
            case WEST:
            setLocation(getX() - 1, getY());
            break;
        }
    }

    public void act() 
    {
        if(FoundMoon())
        {
            setDirection(SOUTH); //points ship down
            move(); //asks ship to move
        } else if (FoundEarth())
        {
            setDirection(NORTH); //points ship up
            Greenfoot.stop(); //ends game
        } else if (FoundRockC())
        {
            explode();
            Greenfoot.stop();
        } else if (FoundRockR())
        {

            explode();

            Greenfoot.stop();
        } else if (FoundRockL())
        {
            explode();
            Greenfoot.stop();
        }
        move();
        processKeys();
        FuelDisplay();

    }

    public boolean FoundMoon()
    {
        Actor MoonL = getOneObjectAtOffset(0, -15, MoonLand.class); //detects moon
        if (MoonL != null)
        {
            F1 = new Flag(); //creates new flag
            getWorld().addObject(F1, 450, 50); //changes location of F1
            return true;
        } else {
            return false;
        }
    }

    public boolean FoundEarth()
    {
        Actor EarthL = getOneObjectAtOffset(0, 5, Earth.class); //detects moon
        if (EarthL != null)
        {
            H1 = new Home(); //creates new landing message
            getWorld().addObject(H1, 550, 750); //changes location of H1
            return true;
        } else {
            return false;
        }
    }

    private void processKeys()
    {
        if(Greenfoot.isKeyDown("down"))
        {
            setDirection(SOUTH);
            move();
        }
        if(Greenfoot.isKeyDown("up"))
        {
            setDirection(NORTH);
            move();
        }
        if(Greenfoot.isKeyDown("right"))
        {
            setDirection(EAST);
            move();
        }        
        if(Greenfoot.isKeyDown("left"))
        {
            setDirection(WEST);
            move();
        }        
    }

    private boolean FoundRockC()
    {
        Actor RockC = getOneObjectAtOffset(0, -15, RockCentre.class); //detects rock centre
        Actor RockL = getOneObjectAtOffset(0, -15, RockLeft.class); //detects rock left
        Actor RockR = getOneObjectAtOffset(0, -15, RockRight.class); //detects rock right
        if (RockC != null)
        {
            return true;
        } else {
            return false;
        }
    }

    private boolean FoundRockR()
    {
        Actor RockR = getOneObjectAtOffset(0, -15, RockRight.class); //detects rock right
        if (RockR != null)
        {
            return true;
        } else {
            return false;
        }
    }

    private boolean FoundRockL()
    {
        Actor RockL = getOneObjectAtOffset(0, -15, RockLeft.class); //detects rock left
        if (RockL != null)
        {
            return true;
        } else {
            return false;
        }

    }

    public void explode()
    {
        E1 = new Explosion(); //creates new flag
        getWorld().addObject(E1, getX(), getY()); //changes location of F1

    }
    public boolean FoundSpaceStation()
    {
        Actor SpaceS = getOneObjectAtOffset(+15, 0, spacestation.class);
        if (SpaceS != null)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}
greencarsonfoot greencarsonfoot

2019/6/15

#
Also, here is the code for the counter if needed. P.S I am doing this for a school project, I did not write or modify this code for the counter as it was given.
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

/**
 * Counter is an Actor in the Going to the Moon Scenario
 *
 * @author: Randy Gallant
 * @School: University of Guelph/Humber
 * @version: 1.0 Nov. 2007
 */

public class Counter extends Actor
{
    private int value;;
    private String text;
    private int stringLength;

    public Counter()
    {
        text = "";
        stringLength = (text.length() + 2) * 10;
        updateImage();
        
    }

    public Counter(String prefix, int fuel)
    {
        value = fuel;
        text = prefix;
        stringLength = (text.length() + 2) * 10;

        setImage(new GreenfootImage(stringLength, 16)); // sets up an image using stringLength which is 16 pixels above the image
        updateImage();
    }

    public void Reset(int valuenew)
    {
        value = valuenew;
        updateImage();
    }

    public void Decrement()
    {
        value--; //decreases the value
        updateImage();
    }

    /**
     * Create and update the image
     */
    private void updateImage()
    {
        GreenfootImage image = getImage();
        image.clear();
        image.setColor(Color.WHITE); //sets the text colour to white
        image.drawString(text + value, 1, 12);
    }
}
You need to login to post a reply.