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

2013/11/25

Game Design One(BVW)

csr1010 csr1010

2013/11/25

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

/**
 * Write a description of class Zombie here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Zombie extends Actor
{

    /**
    * Act - do whatever the Zombie wants to do. This method is called whenever
    * the 'Act' or 'Run' button gets pressed in the environment.
    */
   
  
    public void act() 
    {
        
       followSurvivor();
       int num = 0;
       lookForSurvivor();

    }
    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);
            
            
            
        }
    }
    public void lookForSurvivor()
    {
            if (canSee(Survivor.class) )
            {
                eat(Survivor.class);
            }
    }
    public boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0,0, clss);
        return actor != null;
    }
    public void eat(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0,0, clss);
        if(actor != null) {
            getWorld().removeObject(actor);
            Greenfoot.stop();
        }
    }
}
bubaishungry56 bubaishungry56

2013/11/25

#
Thank you this has helped me a lot
csr1010 csr1010

2013/11/25

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Font;
import java.awt.Color;
/**
 * Write a description of class Survivor here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Survivor extends Actor
{
    boolean ifKeyIsStillDown = false;
    public static int score = 0;
    public int direction = 0;
    //int shottimer = 0;
    int mX;
    int mY;
    /**
     * Act - do whatever the Survivor wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        checkForArrowKeys(3);
        getMouse();
        //shottimer--;
        setRotation((int)getAngle());
    }

    public void checkForArrowKeys( int adjust)
    {
        if ( Greenfoot.isKeyDown("a") )
        {
            setLocation(getX() - adjust, getY() );
            //setRotation(0);
        }  
        if ( Greenfoot.isKeyDown("d") )
        {
            setLocation(getX() + adjust, getY() );
            //setRotation(180);
        }
        if ( Greenfoot.isKeyDown("w") )
        {
            setLocation(getX(), getY() -adjust );
            //setRotation(90);
        }
        if ( Greenfoot.isKeyDown("s") )
        {
            setLocation(getX() , getY() + adjust );
            //setRotation(270);
        }
        if (! Greenfoot.isKeyDown("space") )
        {
            ifKeyIsStillDown = false;
        }
        if ( Greenfoot.isKeyDown("space") )
        {
            shoot();
            ifKeyIsStillDown = true ;
        }
    }   

    public void shoot()  
    {  

        {  
            if (!ifKeyIsStillDown)
            //if (shottimer < 1)
            {

                Bullet b = new Bullet(getRotation());  
                b.setRotation(getRotation());  
                getWorld().addObject(b, getX(), getY());  

                //shottimer = 10;
            }
        }  
    }  

    public void getMouse()
    {
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if (mouse != null)
        {
            mX = mouse.getX();
            mY = mouse.getY();
            if (mouse.getButton() == 1)
            {
                shoot();
            }
        }
        
    }

    public double getAngle()
    {
        //double hyp = Math.sqrt(Math.pow(getX() - mX, 2) + Math.pow(getY() - mY, 2));
        //double opp = Math.abs(getX() - mX);
        //System.out.println(hyp / opp);
        return Math.atan2(getY() - mY, getX() - mX) * 180.0 / Math.PI;
    }
   
}
csr1010 csr1010

2013/11/26

#
IMPROVED SURVIVOR CODE
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Font;
import java.awt.Color;
/**
 * Write a description of class Survivor here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Survivor extends Actor
{
    boolean ifKeyIsStillDown = false;
    public static int score = 0;
    public int direction = 0;
    //int shottimer = 0;
    int mX;
    int mY;
    /**
     * Act - do whatever the Survivor wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        checkForArrowKeys(3);
        getMouse();
        //shottimer--;
        setRotation((int)getAngle());
    }

    public void checkForArrowKeys( int adjust)
    {
        if ( Greenfoot.isKeyDown("a") )
        {
            setLocation(getX() - adjust, getY() );
            //setRotation(0);
        }  
        if ( Greenfoot.isKeyDown("d") )
        {
            setLocation(getX() + adjust, getY() );
            //setRotation(180);
        }
        if ( Greenfoot.isKeyDown("w") )
        {
            setLocation(getX(), getY() -adjust );
            //setRotation(90);
        }
        if ( Greenfoot.isKeyDown("s") )
        {
            setLocation(getX() , getY() + adjust );
            //setRotation(270);
        }
        //if (! Greenfoot.isKeyDown("space") )
        //{
        //    ifKeyIsStillDown = false;
        // }
        //if ( Greenfoot.isKeyDown("space") )
        //{
        // shoot();
        //ifKeyIsStillDown = true ;
        //}
    }   

    public void shoot()  
    {  

        {  
            if (!ifKeyIsStillDown)
            //if (shottimer < 1)
            {

                Bullet b = new Bullet(getRotation());  
                b.setRotation(getRotation());  
                getWorld().addObject(b, getX(), getY());  

                //shottimer = 10;
            }
        }  
    }  

    public void getMouse()
    {
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if (mouse != null)
        {
            mX = mouse.getX();
            mY = mouse.getY();
            if (mouse.getButton() == 1)
            {
                shoot();
                ifKeyIsStillDown = true;
            }
            else
            {
                ifKeyIsStillDown = false;    
            }
        }
        
    }

    public double getAngle()
    {
        //double hyp = Math.sqrt(Math.pow(getX() - mX, 2) + Math.pow(getY() - mY, 2));
        //double opp = Math.abs(getX() - mX);
        //System.out.println(hyp / opp);
        return Math.atan2(getY() - mY, getX() - mX) * 180.0 / Math.PI;
    }
   
}
csr1010 csr1010

2013/11/26

#
IMPROVED BULLET CODE
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;  
    } 
    
}   
You need to login to post a reply.