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

2013/11/15

beginner help

1
2
csr1010 csr1010

2013/12/3

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bullet extends Actor
{
    /**
     * Act - do whatever the Bullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */

    public boolean exsists = true;
    public static int kills;
    private int gunCoolDown;
    private int speed = -10;
    public Bullet(int direction)
    {
        setRotation(direction);    
    }

    public void act() 
    {
        move(speed);
        if (canSee (Zombie.class)) 
        {
            kill(Zombie.class);
            exsists = false;
        }
        if (exsists)
        {
            checkEdges();
        }
    }

    public void kill(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0,0, clss);
        if(actor != null) {
            getWorld().removeObject(actor);
            getWorld().removeObject(this);
        }
    }

    public boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0,0, clss);
        return actor != null;
    }

    public void checkEdges()
    {
        if (atWorldEdge())
        {
            getWorld().removeObject(this);
        }
    }

    public boolean atWorldEdge()  
    {  
        if(getX() < 10 || getX() > getWorld().getWidth() - 10)  
            return true;  
        if(getY() < 10 || getY() > getWorld().getHeight() - 10)  
            return true;  
        else  
            return false;  
    } 
    
}   
danpost danpost

2013/12/3

#
Is there a question about the Bullet class given above Other than the fact that line 18 can be removed from the code and that the code could be cleaned up a bit, I do not see any problems with it.
csr1010 csr1010

2013/12/13

#
public void followSurvivor()  
    {  
        int dist = 325;  
        Actor closest = null;  
    
        if(!getObjectsInRange(dist, Survivor.class).isEmpty())  
        {  
        for (Object obj: getObjectsInRange(dist, Survivor.class))  
        {  
            Actor guy = (Actor) obj;  
            int guyDist = (int) Math.hypot(guy.getX() - getX(), guy.getY() - getY()); 
            if (closest == null || guyDist< dist)  
            {  
                closest = guy;  
                dist = guyDist;
                move(2);
            }  
        }  
            turnTowards(closest.getX(),closest.getY());  
        }
            else 
            {
            if ( Greenfoot.getRandomNumber (100) < 10)
            
            turn(Greenfoot.getRandomNumber(90)-45);
            move(1);
            
            
            
        }
    }
You need to login to post a reply.
1
2