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

2016/8/26

What code do I need to reload after 10 seconds?

Dudimus Dudimus

2016/8/26

#
Here's the code for my character which has the subclass of my gun and bullet:
import greenfoot.*;
import java.awt.Color;

/**
 * Class Chased (subclass of Actor): a user-controlled object with a mouse-controlled gun
 */
public class Chased extends Actor
{
    boolean touchingZ = false;
    private int rotation;
    boolean touchingFire = false;
    Gun gun = new Gun(); // the gun for this actor
    Chaser chaser = new Chaser();
    
    int timer=100;
    int ammo=10;
    
   
    /**
     * creates image for actor
     */
    public Chased()
    {
        // the image for this actor
        GreenfootImage image = new GreenfootImage(50, 50);
        image.fillOval(0, 0, 50, 50);
        image.setColor(Color.green);
        image.fillOval(7, 7, 36, 36);
        setImage(image);
    }
    
    /**
     * adds the gun for this actor into the world
     *
     * @param world the world this actor was added into
     */
    public void addedToWorld(World world)
    {
        world.addObject(gun, getX(), getY()); // add the gun belonging to this actor into the world
    }
    

      /**
     * move user-controlled actor and its gun; also, check for game over
     */
    public void act()
    {
        // user-controlled movement
        int dx = 0, dy = 0;
        if (Greenfoot.isKeyDown("a")) dx--;
        if(getOneObjectInFront(Car.class)==null)
        if (Greenfoot.isKeyDown("d")) dx++;
        if(getOneObjectInFront(Car.class)==null)
        if (Greenfoot.isKeyDown("w")) dy--;
        if(getOneObjectInFront(Car.class)==null)
        if (Greenfoot.isKeyDown("s")) dy++;
        if(getOneObjectInFront(Car.class)==null)
        setLocation(getX()+1*dx, getY()+1*dy);
        // limit to world bounds
        if (getX() < 25) setLocation(25, getY());
        if (getY() < 25) setLocation(getX(), 25);
        if (getX() > getWorld().getWidth()-25) setLocation(getWorld().getWidth()-25, getY());
        if (getY() > getWorld().getHeight()-25) setLocation(getX(), getWorld().getHeight()-25);
        // position gun
        gun.setLocation(getX(), getY());
        // check game over
            
         Actor chaser = getOneIntersectingObject(Chaser.class);
       
         
        if (chaser != null)
        
                {
            World world = getWorld();
                    Welt welt = (Welt)world; 
               
                    HealthBar healthbar = welt.getHealthBar();
                    Numberz numberz = welt.getNumberz();
                    if(touchingZ == false)
                    
                    {
                        healthbar.loseHealth();
                        
                        touchingZ = true;
                       
                        if(healthbar.health <=0)
                        {
                         world.removeObjects(world.getObjects(null));
                         Greenfoot.setWorld(new Stats(((Welt)world).score));
                        }
                        
                        
                    }
        }else{
                    touchingZ = false;
                }
               
    }
    public Actor getOneObjectInFront(Class a)
    {
    GreenfootImage myImage=getImage();
    int distanceToFront=myImage.getWidth();
    int xOffset=(int)Math.ceil(distanceToFront*Math.cos(Math.toRadians(getRotation())));
    int yOffset=(int)Math.ceil(distanceToFront*Math.sin(Math.toRadians(getRotation())));
    return(getOneObjectAtOffset(xOffset,yOffset,a));
    
    }
   
    /**
     * Class: Gun (subclass of Actor -- inner class of Chased): the gun for this actor
     */
    private class Gun extends Actor
    {
        /**
         * creates image for actor
         */
        public Gun()
        {
            // the image for this actor
            GreenfootImage image = new GreenfootImage("Picture1.png");
           
            setImage(image);
        }
         
    public boolean Munition(){
        if(ammo>0){
            return true;
        }
        else{
            return false;
        }
    }

    private boolean mouseclick;
    /**
         * responds to mouse movement and mouse button clicks
         */
        public void act()
        {
            // turn towards mouse when mouse moves
            if (Greenfoot.mouseMoved(null) || Greenfoot.mouseDragged(null))
            {
                MouseInfo mouse = Greenfoot.getMouseInfo();
                if (mouse != null) turnTowards(mouse.getX(), mouse.getY());
            }
            // detect mouse clicks to fire shot   
           
            World world = getWorld();
                    Welt welt = (Welt)world;
                    AmmoBar ammobar = welt.getAmmoBar();
                   
            if(!mouseclick && (Greenfoot.mouseClicked(null)))
            {
                if(Munition()==true)
                {
                    mouseclick=true;
                    getWorld().addObject(new Shot(), getX(), getY());
                    ammo--;
                    
                    ammobar.loseAmmo();
                 
                }
            }
            if(mouseclick&& !Greenfoot.isKeyDown("space"))
            {
                mouseclick=false;
            }
            
         
            
        }

       
       
       /**
         * Class Shot (subclass of QActor -- inner class of Chased.Gun): the shots from this gun
         */
        private class Shot extends QActor
        {
            /**
             * sets bounds fields and creates the image for this actor
             */
            public Shot()
            {
                setBoundedAction(QActor.REMOVE, 5); // set bounds fields
                // create image for this actor
                GreenfootImage image = new GreenfootImage("FIRE.png");
                
                setImage(image);

            }
            
            /**
             * initializes rotation and position of actor
             *
             * @param world the world this actor was added into
             */
            public void addedToWorld(World world)
            {
              
                setRotation(Gun.this.getRotation()); // set rotation (to that of gun)
                move(5000); // set position (at end of gun)
                rotation = getRotation();
                setRotation(0);
            }
            
            /**
             * moves actor and checks for removal of actor
             */
            public void act()
            {
                
                setVX(0);
                setVY(0);
                addForce(0, getRotation()*QVAL);
                move(); // moving (equivalent to 'move(5)' for a non-QActor)
                 World world = getWorld();
                    Welt welt = (Welt)world;
                    Numberz numberz = welt.getNumberz();
                if (hits(Chaser.class) || atWorldEdge()){
                    getWorld().removeObject(this); // removing
                    numberz.loseNum();
                   
                }
                
            }
            
            
                  /**
             * internal method to detect object collision; returns true if collision detected, else false
             *
             * @param cls the class of object to check for collision with
             * @return a flag indicating whether an object of given class was detected or not
             */
            private boolean hits(Class cls)
            {
                // get intersecting object and return result
                Actor clsActor = getOneIntersectingObject(cls);
                if (clsActor != null)
                {
                    // remove intersector and bump score
                    getWorld().removeObject(clsActor);
                    
                    return true; 
                }
                return false;
            }
            /**
             * internal method that returns a flag indicating world edge encroachment
             *
             * @return a flag indicating whether the actor has encroached on a world edge or not
             */
            private boolean atWorldEdge()
            {
                // return state of encroachment on world edge
                return getX() <= 0 || getY() <= 0 ||
                    getX() >= getWorld().getWidth()-1 ||
                    getY() >= getWorld().getHeight()-1;
            }
        }
    }
}
danpost danpost

2016/8/26

#
dudimus wrote...
What code do I need to reload after 10 seconds?
First, at line 161 (after expending a round), check to see if any ammo is left; if not, set your timer to about 550 (the number of act cycles that will execute in about ten seconds in a normal speed scenario <speed slider about in the middle>). Next, in the act method, outside of any other code, check to see if the timer has a positive value; if so, decrement it and then check for a value of zero; if zero, add ammo.
Dudimus Dudimus

2016/8/27

#
It works! But how can I make my ammobar be full again (meaning the rectangle on the screen will look like full again)?
Dudimus Dudimus

2016/8/27

#
Here is my ammobar code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class HealthBar here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class AmmoBar extends Actor
{
    int ammo = 10;
    int abw = 80;
    int abh = 15;
    int pixelsPerAmmoPoint = (int)abw/ammo;
    /**
     * Act - do whatever the HealthBar wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public AmmoBar()
    {
        update();
    }
    public void act() 
    {
        update();
    }    
    public void update()
    {
     setImage(new GreenfootImage(abw+2, abh +2));   
     GreenfootImage myImage = getImage();
     myImage.setColor(Color.WHITE);
     myImage.drawRect(0,0,abw+1,abh+1);
     myImage.setColor(Color.GREEN);
     myImage.fillRect(1,1,ammo*pixelsPerAmmoPoint, abh);
    }   
    public void loseAmmo()
    {
        ammo--;
      
    }
     public void addAmmo()
    {
        ammo++;
      
    }
}
Dudimus Dudimus

2016/8/27

#
Here is my updated Chased/Actor code:
import greenfoot.*;
import java.awt.Color;

/**
 * Class Chased (subclass of Actor): a user-controlled object with a mouse-controlled gun
 */
public class Chased extends Actor
{
    boolean touchingZ = false;
    private int rotation;
    boolean touchingFire = false;
    Gun gun = new Gun(); // the gun for this actor
    Chaser chaser = new Chaser();
    
    int timer;
    int ammo=10;
    
   
    /**
     * creates image for actor
     */
    public Chased()
    {
        // the image for this actor
        GreenfootImage image = new GreenfootImage(50, 50);
        image.fillOval(0, 0, 50, 50);
        image.setColor(Color.green);
        image.fillOval(7, 7, 36, 36);
        setImage(image);
    }
    
    /**
     * adds the gun for this actor into the world
     *
     * @param world the world this actor was added into
     */
    public void addedToWorld(World world)
    {
        world.addObject(gun, getX(), getY()); // add the gun belonging to this actor into the world
    }
    

      /**
     * move user-controlled actor and its gun; also, check for game over
     */
    public void act()
    {
        // user-controlled movement
        int dx = 0, dy = 0;
        if (Greenfoot.isKeyDown("a")) dx--;
        if(getOneObjectInFront(Car.class)==null)
        if (Greenfoot.isKeyDown("d")) dx++;
        if(getOneObjectInFront(Car.class)==null)
        if (Greenfoot.isKeyDown("w")) dy--;
        if(getOneObjectInFront(Car.class)==null)
        if (Greenfoot.isKeyDown("s")) dy++;
        if(getOneObjectInFront(Car.class)==null)
        setLocation(getX()+1*dx, getY()+1*dy);
        // limit to world bounds
        if (getX() < 25) setLocation(25, getY());
        if (getY() < 25) setLocation(getX(), 25);
        if (getX() > getWorld().getWidth()-25) setLocation(getWorld().getWidth()-25, getY());
        if (getY() > getWorld().getHeight()-25) setLocation(getX(), getWorld().getHeight()-25);
        // position gun
        gun.setLocation(getX(), getY());
        // check game over
            
         Actor chaser = getOneIntersectingObject(Chaser.class);
  
         
        if (chaser != null)

                {
            World world = getWorld();
                    Welt welt = (Welt)world; 
               
                    HealthBar healthbar = welt.getHealthBar();
                    Numberz numberz = welt.getNumberz();
                    if(touchingZ == false)
                    
                    {
                        healthbar.loseHealth();
                        
                        touchingZ = true;
                       
                        if(healthbar.health <=0)
                        {
                         world.removeObjects(world.getObjects(null));
                         Greenfoot.setWorld(new Stats(((Welt)world).score));
                        }
                        
                        
                    }
        }else{
                    touchingZ = false;
                }
               
    }
    public Actor getOneObjectInFront(Class a)
    {
    GreenfootImage myImage=getImage();
    int distanceToFront=myImage.getWidth();
    int xOffset=(int)Math.ceil(distanceToFront*Math.cos(Math.toRadians(getRotation())));
    int yOffset=(int)Math.ceil(distanceToFront*Math.sin(Math.toRadians(getRotation())));
    return(getOneObjectAtOffset(xOffset,yOffset,a));
    
    }
   
    /**
     * Class: Gun (subclass of Actor -- inner class of Chased): the gun for this actor
     */
    private class Gun extends Actor
    {
        /**
         * creates image for actor
         */
        public Gun()
        {
            // the image for this actor
            GreenfootImage image = new GreenfootImage("Picture1.png");
           
            setImage(image);
        }
         
    public boolean Munition(){
        if(ammo>0){
            return true;
        }
        else{
            return false;
        }
    }

    private boolean mouseclick;
    /**
         * responds to mouse movement and mouse button clicks
         */
        public void act()
        {
            // turn towards mouse when mouse moves
            if (Greenfoot.mouseMoved(null) || Greenfoot.mouseDragged(null))
            {
                MouseInfo mouse = Greenfoot.getMouseInfo();
                if (mouse != null) turnTowards(mouse.getX(), mouse.getY());
            }
            // detect mouse clicks to fire shot   
           
            World world = getWorld();
                    Welt welt = (Welt)world;

                    AmmoBar ammobar = welt.getAmmoBar();
                   
            if(!mouseclick && (Greenfoot.mouseClicked(null)))
            {
                
             
                    if(Munition()==true){
                    mouseclick=true;
                    getWorld().addObject(new Shot(), getX(), getY());
                    ammo--;
                    ammobar.loseAmmo();
                    if (ammo ==0){
                        timer = 2500;
                    }
                
                }
            }
            if(mouseclick&& !Greenfoot.isKeyDown("space"))
            {
                mouseclick=false;
            }
           
             if (timer > 0){
                    timer--;
                    if (timer==0){
                        ammo+=10;
                        
                    }
                }
            
        }

       
       
       /**
         * Class Shot (subclass of QActor -- inner class of Chased.Gun): the shots from this gun
         */
        private class Shot extends QActor
        {
            /**
             * sets bounds fields and creates the image for this actor
             */
            public Shot()
            {
                setBoundedAction(QActor.REMOVE, 5); // set bounds fields
                // create image for this actor
                GreenfootImage image = new GreenfootImage("FIRE.png");
                
                setImage(image);

            }
            
            /**
             * initializes rotation and position of actor
             *
             * @param world the world this actor was added into
             */
            public void addedToWorld(World world)
            {
              
                setRotation(Gun.this.getRotation()); // set rotation (to that of gun)
                move(5000); // set position (at end of gun)
                rotation = getRotation();
                setRotation(0);
            }
            
            /**
             * moves actor and checks for removal of actor
             */
            public void act()
            {
                
                setVX(0);
                setVY(0);
                addForce(0, getRotation()*QVAL);
                move(); // moving (equivalent to 'move(5)' for a non-QActor)
                 
                if (hits(Chaser.class) || atWorldEdge())
  
                {
                    getWorld().removeObject(this); // removing
                 
                   
                }
                
            }
            
            
                  /**
             * internal method to detect object collision; returns true if collision detected, else false
             *
             * @param cls the class of object to check for collision with
             * @return a flag indicating whether an object of given class was detected or not
             */
            private boolean hits(Class cls)
            {
                // get intersecting object and return result
                Actor clsActor = getOneIntersectingObject(cls);
                World world = getWorld();
                    Welt welt = (Welt)world;
                    Numberz numberz = welt.getNumberz();
                if (clsActor != null)
                {
                    // remove intersector and bump score
                    getWorld().removeObject(clsActor);
                    numberz.loseNum();
                    return true; 
                }
                return false;
            }
            /**
             * internal method that returns a flag indicating world edge encroachment
             *
             * @return a flag indicating whether the actor has encroached on a world edge or not
             */
            private boolean atWorldEdge()
            {
                // return state of encroachment on world edge
                return getX() <= 0 || getY() <= 0 ||
                    getX() >= getWorld().getWidth()-1 ||
                    getY() >= getWorld().getHeight()-1;
            }
        }
    }
}
danpost danpost

2016/8/27

#
Dudimus wrote...
how can I make my ammobar be full again (meaning the rectangle on the screen will look like full again)?
Apparently, you have two ammo fields -- one in the class of the ammo bar and the other in the class of the chased. It is almost never a good idea to have two fields for the same data.
Dudimus Dudimus

2016/8/29

#
It works, thanks!
You need to login to post a reply.