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

2016/12/12

Smoothmover

georgette georgette

2016/12/12

#
Hello, I'm at a loss. What type of method would I use to add a horizontal line near the bottom of the screen that will simulate the surface of the planet. The ship should stop when it runs into the ground/surface.

public class Ship extends SmoothMover
{
    private GreenfootImage Ship;
    private static final double GRAVITY = 0.5;
    //private int reloadDelayCount;
    private double vSpeed;   
    private double mass;
    private GreenfootImage ship = new GreenfootImage("ship.png");    
    private GreenfootImage shipWithThrust = new GreenfootImage("shipWithThrust.png");    

    /**
     * Construct a ship with default size, mass, velocity and color.
     */
    public Ship()
    {
        Vector initial = new Vector(Greenfoot.getRandomNumber(360), .6);
        setRotation(270);
        //this (20, 300, new Vector(0, 0.0), defaultColor);

    }

    /**
     * Act - do whatever the Ship wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 

    {
        move();
        checkKeys();
        vSpeed += GRAVITY; //add constant acceleration due to gravity.
        setLocation(getX(), getY()+vSpeed);

        //applyGravity();
    }

    /**
     * Check whether there are any keys pressed.
     */
    public void checkKeys()
    {
        ignite(Greenfoot.isKeyDown("up"));         
        if(Greenfoot.isKeyDown("left"))
        {
            turn(-5);
        }
        if(Greenfoot.isKeyDown("right"))
        {
            turn(5);
        }

    }

    /**
     * Are there flames coming out from behind the ship?
     */
    private void ignite(boolean thrust)
    {
        if (thrust == true)
        {
            Vector power = new Vector(getRotation(), .2);
            addToVelocity(power);
            setImage("shipWithThrust.png");
        }
        else
        {
            setImage("ship.png");
        }
    }

    /**
     * Add new ship to the center of world
     */
    public void shipScenario()
    {
        World myWorld = getWorld();
        Ship ship = new Ship();

        myWorld.addObject(ship, 40, 30);       

    }    

}
danpost danpost

2016/12/12

#
Create an Actor subclass for the ground. It image can be transparent, if you wish (created programmatically in the constructor of the new class. Have the world add an object of the class to itself. Have the ship look for it and not proceed below it.
georgette georgette

2016/12/14

#
Hello. I added a method to create an image at the bottom of the world to simulate the surface of the planet. However, I need the image to be fixed as well as the full length of the world. Any help will be greatly appreciated!

public class Space extends World
{
    /**
     * Constructor for objects of class Space.
     * 
     */
    public Space()
    {    
        super(960, 620, 1);
        createShip();
        createSurface();  
        setPaintOrder(Surface.class); 

        //drawLine();                                 
        
    }    
     
    /**
     * Add horizontal line near bottom of the screen. Bonus - use an image to simulate the surface of the planet.
     */
    public void createSurface()
    {
        Surface surface =  new Surface();
        addObject (surface, 959, 619);
        World world;    

    }


public class Surface extends SmoothMover
{
    /**
     * Act - do whatever the Surface wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
        setLocation(getX()-1, getY());        
    }    
    
    /**
     * Constructor: Initialize surface
     */
    public Surface()
    {
        getImage().mirrorHorizontally();
    }
}
danpost danpost

2016/12/14

#
georgette wrote...
I added a method to create an image at the bottom of the world to simulate the surface of the planet. However, I need the image to be fixed as well as the full length of the world.
Actually, you did not create an image -- you created an object to be the surface, Unless you set a default image for the class, its image will be a greenfoot icon; and when mirrored horizontally (not what you want to do here), it will be a backward greenfoot icon. Since the surface will be as long as the width of the world and it will not be moving, the Surface class does not need to extend the SmoothMover class; extending the Actor class is sufficient. Except that now I see that you are having the surface move to the left as the scenario runs (I do not think that is what you want, either). I also noticed that you placed the Surface object at the far right of the world viewer, which does not make much sense; if it is to cover the width of the world, it should be placed in the horizontal center of the world (near the bottom, since it is a ground surface object). The Surface class can simply be this:
import greenfoot.*;

public class Surface extends Actor
{
    public Surface()
    {
        setImage(new GreenfootImage(960, 1));
    }
}
If a Surface object is placed close enough to the bottom edge of the world (less than the shortest dimension of any image of the ship), then the ship is guaranteed to detect it after moving to the bottom of the screen. If you want the surface further from the bottom of the screen, increase the height of the image of the Surface object accordingly and adjust its vertical positioning (again, as long as bottom edge of Surface object is as low as the single line surface, you will be save to presume the ship will detect the surface).
georgette georgette

2016/12/15

#
Thanks, that helps! (I really do have a png image for my surface object :-)
danpost danpost

2016/12/15

#
georgette wrote...
Thanks, that helps! (I really do have a png image for my surface object :-)
I had said this:
danpost wrote...
If a Surface object is placed close enough to the bottom edge of the world (less than the shortest dimension of any image of the ship), then the ship is guaranteed to detect it after moving to the bottom of the screen.
but I missed a word. It should read:
If a Surface object is placed close enough to the bottom edge of the world (less than half the shortest dimension of any image of the ship), then the ship is guaranteed to detect it after moving to the bottom of the screen.
georgette georgette

2017/1/3

#
Thanks again danpost and others who responded to my requests for help. There is absolutely no way I would have passed this class with a b+ without you!!
You need to login to post a reply.