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

2019/6/2

Actor Position - Random but Restrictive

centipede123 centipede123

2019/6/2

#
Hey guys, we need your help! Does anyone know how to set a random, but also restrictive value for the Position of actors? We want to set Mushrooms into the world randomly, but between the value 0 and 20. So they can be set everywhere except on the Position (y = 0). This is our current source code: for(int i=0; i<37; i++) { Mushroom leaf = new Mushroom(); int x = Greenfoot.getRandomNumber(getWidth()); int y = Greenfoot.getRandomNumber(getHeight()); addObject(leaf, x, y); }
danpost danpost

2019/6/2

#
Use
int y = 1+Greenfoot.getRandomNumber(getHeight()-1);
centipede123 centipede123

2019/6/2

#
Thank you very much for your quick response. And a new question appeared: If there´s a mushroom infront of the Actor, the Actor should turn and move one cell down. How do we make him move one cell?
danpost danpost

2019/6/2

#
centipede123 wrote...
If there´s a mushroom infront of the Actor, the Actor should turn and move one cell down. How do we make him move one cell?
move(1);
centipede123 centipede123

2019/6/2

#
ok thank you, but we want the Actor move one cell down and then turn 180° so it looks in the other direction. so is it possible to give the command "move one cell down"? Or do you have an other idea?
danpost danpost

2019/6/2

#
centipede123 wrote...
ok thank you, but we want the Actor move one cell down and then turn 180° so it looks in the other direction. so is it possible to give the command "move one cell down"? Or do you have an other idea?
setLocation(getX(), getY()+1);
centipede123 centipede123

2019/6/4

#
Thank you a lot, that really helped us. But our problem now is that we have lost the survay of our programm. We don´t know if it´s possible but if anyone is here to help us, we would really appreciate it. We made the code for the snake (centipede) but it doesn´t move. What´s the problem? Thank you very much.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Class Snake:
 * the head and the body segments are both of this class
 */
public class Snake extends Actor
{
    private Snake follower = null; // holds a reference to the body segment behind this one, if there is one
    
    /** Size of this Snake */
    private int size;
    
    /** When the stability reaches 0 the snake will break up */
    private int stability;

    
    /**
     * Act - do whatever the centipede wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move();
        AbprallenMushroom ();
        turnAtEdge();
    }    
    
    private static final double WALKING_SPEED = 1.0;
   
    /**
     * Move forward in the current direction.
     */
    public void move()
    {
        double angle = Math.toRadians( getRotation() );
        int x = (int) Math.round(getX() + Math.cos(angle) * WALKING_SPEED);
        int y = (int) Math.round(getY() + Math.sin(angle) * WALKING_SPEED);
        
        setLocation(x, y);
    }
    
    /**
     * Make the snake sequence disappear
     */
    private void breakUp() 
    {
        Greenfoot.playSound("Explosion.wav");
        
        if (size <= 16) {
            // if we are already very small, just disappear
            getWorld().removeObject(this);
            return;
        }
    }
    
    /**
     * Return true if we can see an object of class 'class' right where we are. 
     * False if there is no such object here.
     */
    public boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        return actor != null;        
    }
    
    /**
     * Snake should turn, if running into a mushroom
     */
    public void AbprallenMushroom()
    {
        if (canSee(Mushroom.class))
        {
            setLocation(getX(), getY()+1);
        } 
    }
    
    /**
     * Test if we are close to one of the edges of the world. Return true is we are.
     */
    public boolean atWorldEdge()
    {
        if(getX() < 20 || getX() > getWorld().getWidth() - 20)
            return true;
        if(getY() < 20 || getY() > getWorld().getHeight() - 20)
            return true;
        else
            return false;
    }
    
   /**
    * This method works to make the snake turn at the edge
    */
   public void turnAtEdge()
   {
    if(atWorldEdge())
     {
           setLocation(getX(), getY()+1);

        } 
    }

     /**
     * Hit this snake dealing the given amount of damage.
     */
    public void hit(int damage) 
    {
        stability = stability - damage;
        if (stability <= 0) {
            breakUp();       
        }
    }
   
    /**
     * Create an asteroid with default size and speed.
     */
    public Snake()
    {
        this(15);
    }
    
    /**
     * Create an snake with a given size size, direction, and speed.
     */
    private Snake(int size)
    {
        setSize(size);
    }
    
    /**
     * Set the size of this asteroid. Note that stability is directly
     * related to size. Smaller asteroids are less stable.
     */
    public void setSize(int size) 
    {
        this.size = size;
        GreenfootImage image = getImage();
        image.scale(size, size);
    }
    
    /**
     * Method setFollower
     * allows the world to inform a segment which segment is following it
     * 
     * @param follows A parameter to recieve a reference to the body segment that follows this one
     */
    public void setFollower(Snake follows)
    {
        follower = follows;
    }

}
danpost danpost

2019/6/4

#
I believe the problem is in line 40. It will place your actor within one cell of the top left corner -- every time. The x and y values previously calculated are offset values, not coordinate values.
danpost danpost

2019/6/4

#
I do not see anything in your code that controls the direction of the snake. If it is controlled by its rotation, there is no command to turn or change its rotation. With the rotation being used in its movement code, it will only move one way, except when hitting a mushroom or edge.
centipede123 centipede123

2019/6/5

#
Ok thanks, we will try!!This is our rocket that should damage the snake and mushrooms with its bullets, that we have programmed extra, but it won´t work. The rocket doesn´t shoot the bullets. Do you know why?
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

/**
 * A rocket that can be controlled by the arrowkeys: up, left, right.
 * The gun is fired by hitting the 'space' key.
 * 
 * @version 2.0
 */
public class Rocket extends Mover
{
    private int gunReloadTime;              // The minimum delay between firing the gun.
    private int reloadDelayCount;           // How long ago we fired the gun the last time.
    private Vector acceleration;            // A vector used to accelerate when using booster.
    private int shotsFired;                 // Number of shots fired.
    
    private GreenfootImage rocket = new GreenfootImage("rocket.png");
    //private GreenfootImage rocketWithThrust = new GreenfootImage("rocketWithThrust.png");

    /**
     * Initialise this rocket.
     */
    public Rocket()
    {
        gunReloadTime = 0;
        reloadDelayCount = 0;
        acceleration = new Vector(0, 0.3);    // used to accelerate when thrust is on
        shotsFired = 0;
    }
    
    /**
     * Do what a rocket's gotta do. (Which is: mostly flying about, and turning,
     * accelerating and shooting when the right keys are pressed.)
     */
    public void act()
    {
        move();
        checkKeys();
        reloadDelayCount++;
    }
    
    /**
     * Return the number of shots fired from this rocket.
     */
    public int getShotsFired()
    {
        return shotsFired;
    }
    
    /**
     * Set the time needed for re-loading the rocket's gun. The shorter this time is,
     * the faster the rocket can fire. The (initial) standard time is 20.
     */
    public void setGunReloadTime(int reloadTime)
    {
        gunReloadTime = reloadTime;
    }
    
    /**
     * Check whether there are any key pressed and react to them.
     */
    private void checkKeys() 
    {
        if(Greenfoot.isKeyDown("left")) {
            move(-1);
        }        
        if(Greenfoot.isKeyDown("right")) {
            move(1);
        }
        if(Greenfoot.isKeyDown("space")) {
            fire();
        }        
    }
    
    /**
     * Fire a bullet if the gun is ready.
     */
    private void fire() 
    {
        if (reloadDelayCount >= gunReloadTime) {
            Bullet b = new Bullet(getMovement().copy(), getRotation());
            getWorld().addObject(b, getX(), getY());
            b.move();
            shotsFired++;
            reloadDelayCount = 0;   // time since last shot fired
        }
    }
}
Scorcher Scorcher

2019/6/6

#
I don't know why it won't shoot, but there are things in your code I'd like to bring attention to:
  • You have in the constructor "gunReloadTime = 0;" but your comment (line 51) says it should be 20. I don't think that's causing any problems though.
  • Do you have something covering the background that's not part of the background? If so, the bullets might be there, just hidden behind it.
  • I assume that the Bullet is also a subclass of Mover. You have the method "move()" in the Rocket's act method; the Bullet also has it, but it's only called once. I'm not sure if the Bullet moves by itself, so it could be behind the Rocket.
Super_Hippo Super_Hippo

2019/6/6

#
You could add this line into the if-block in the fire method to make sure it is executed:
System.out.println("Bullet shot");
If it is printing it, show your Bullet class code. Maybe it is moving too fast. Also make sure that it has an image and (nothing is in front of it as mentioned above).
centipede123 centipede123

2019/6/8

#
Thank you for your help!! We will try everything out and check the mentioned points.
You need to login to post a reply.