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

2019/1/19

How to check if you have detected a different color

pablo0826 pablo0826

2019/1/19

#
Hi, I'm creating a Lunar Lander game, in which I would like the rocket to explode if it touches the mountains. Originally the problem I was having was that the image couldn't be made so that the rocket explodes only when it touches the mountain, since it was a png image with a background (even if the background was removed). If anyone can give me a solution to that, that would honestly be great because it can make it much easier. Otherwise, I would like to know how to write the program to check if the rocket has touched a different colour, that's all, Thanks :)
danpost danpost

2019/1/20

#
Would help if your png was made available for viewing, as well as the code for the lander and your world.
pablo0826 pablo0826

2019/1/20

#
Above are images of my world before and after running the program. And below is the code for the lander.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Rocket here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Rocket extends Actor
{
    private final double GRAVITY = 0.1;
    private final double THRUST = 0.15;
    private double speed;
    
    
    public GreenfootImage rocket1 = new GreenfootImage("rocketWithoutThrust.png");
    public GreenfootImage rocket2 = new GreenfootImage("rocketWithThrust.png");
    public Rocket()
    {
        setRotation(270);
        speed = 0;
        
    }
    public void act() 
    {
        processKeys();
        fall();
        land();
        touchingAsteroid();
        //System.out.println(speed);
    }
    public void touchingAsteroid()
    {
        if(isTouching(Asteroid.class))
        {
         Greenfoot.playSound("Explosion.mp3");
         getWorld().addObject(new Explosion(), getX(), getY());
         getWorld().removeObject(this);
        }
    }
    public void processKeys()
    {
        if (Greenfoot.isKeyDown("up"))
        {
            fireEngine();
            setImage(rocket2);
        } else {
            setImage(rocket1);
        }
        if (Greenfoot.isKeyDown("left"))
        {
            setLocation(getX() - 1, getY());
        }
        if (Greenfoot.isKeyDown("right"))
        {
            setLocation(getX() + 1, getY());
        }
        if (this.getRotation() > 0)
        {
            if (Greenfoot.isKeyDown("up"))
            {
                
            }
        }
    }
    public void fireEngine()
    {
        speed = speed - THRUST; 
    }
    public void fall()
    {
        setLocation(getX(), (int)(getY() + speed));
        speed = speed + GRAVITY;
    }
    public void land()
    {
       if(isTouching(Mountains.class))
       {
         if(speed < 3.5)
         {
            getWorld().addObject(new Label("Safe Landing", 30), getWorld().getWidth()/2, getWorld().getHeight()/2);           
        } else {
         Greenfoot.playSound("Explosion.mp3");
         getWorld().addObject(new Explosion(), getX(), getY());
         getWorld().removeObject(this);
         Greenfoot.stop();
        }
      }
    }
    public void printScore()
    {
        getWorld().addObject(new Score(Integer.toString((int)speed), 100), 200, 100);
    }
}
danpost danpost

2019/1/20

#
pablo0826 wrote...
<< Images Omitted >> Above are images of my world before and after running the program. And below is the code for the lander. << Code Omitted >>
What is the size of your world? what is the size of the image assigned to the Mountains class? and, where was your Mountains object addied into the world at? Do you have scrolling implemented? You may want to track the location coordinates of your Rocket object as double values so as not to lose precision (makes for smoother acceleration/deceleration).
You need to login to post a reply.