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

2019/1/18

I have a question

imdojangbob imdojangbob

2019/1/18

#
I have a question I am programming a game called box head Unfortunately, the game gets slower or lags as I am playing it. How can I fix this problem? I know that the problem is caused because too many methods are compiling at once.
imdojangbob imdojangbob

2019/1/18

#
My laptop is MacBook, it has 8 GB Ram
imdojangbob imdojangbob

2019/1/18

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

/**
 * Write a description of class Human here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Human extends Animal
{
    int health = 50;
    int healthrestore = 0;
    int pistol = 0;
    int SMG = 0;
    int ShotGun = 0;
    int Barrel = 0;
    int ammo = 100;
    int SMGammo = 150;
    int Shotgunammo = 20;
    
    private int direction;
    /**
     * Act - do whatever the Human wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        moveAround();
        health();
        healthrestore();   
        chooseGun();
        showHealth();
    }    
    
    public void moveAround()
    {
        if(Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(), getY()-2);
            setRotation(270);
        }
        
        if(Greenfoot.isKeyDown("down"))
        {
            setLocation(getX(),getY()+2);
            setRotation(90);
        }
        
        if(Greenfoot.isKeyDown("right"))
        {
            setLocation(getX()+2, getY());
            setRotation(0);
        }
        
        if(Greenfoot.isKeyDown("left"))
        {
            setLocation(getX()-2, getY());
            setRotation(180);
        }
        
        if(Greenfoot.isKeyDown("up") && Greenfoot.isKeyDown("right"))
        {
            setLocation(getX()+2, getY()-2);
            setRotation(315);
        }
        
        if(Greenfoot.isKeyDown("up") && Greenfoot.isKeyDown("left"))
        {
            setLocation(getX()-2, getY()-2);
            setRotation(225);
        }
        
        if(Greenfoot.isKeyDown("down") && Greenfoot.isKeyDown("left"))
        {
            setLocation(getX()-2, getY()+2);
            setRotation(135);
        }
        
        if(Greenfoot.isKeyDown("down") && Greenfoot.isKeyDown("right"))
        {
            setLocation(getX()+2, getY()+2);
            setRotation(45);
        }
    }
    
    public void health()
    {
        if(canSee(Zombie.class) || canSee(Fireball.class))
        {
            if(canSee(Zombie.class))
            {
                health = health - 10;
                clear();
            }
            else if (canSee(Fireball.class))
            {
                health = health - 25;
                clear();
                eat(Fireball.class);
            }
        }
    }
    
    public void healthrestore()
    {
        healthrestore = healthrestore + 1;
        if (healthrestore == 1000 && health < 50)
        {
            health = 50;
            healthrestore = 0;
            clear();
        }
    }
    
    public void chooseGun()
    {
        displayMessage("Pistol: " + ammo, 30, 30, black, 15);
        displayMessage("SMG: " + SMGammo, 30, 50, black, 15);
        displayMessage("Shotgun: " + Shotgunammo, 30, 70, black, 15);
        pistol = pistol + 1;
        SMG = SMG + 1;
        ShotGun = ShotGun + 1;
        if(pistol > 30 && Greenfoot.isKeyDown("space"))
        {
            getWorld().addObject(new Bullet(), getX(),getY());
            ammo = ammo - 1;
            pistol = 0;
            clear();
            if(ammo <= 0)
            {
                clear();
                displayMessage("reload the ammo", 30,30, black, 15);
            }
        }
            
        if(SMG > 5 && Greenfoot.isKeyDown("z"))
        {
            getWorld().addObject(new Bullet(), getX(),getY());
            SMGammo = SMGammo - 1;
            SMG = 0;
            clear();
        }
        
        if(ShotGun > 50 && Greenfoot.isKeyDown("x"))
        {
            getWorld().addObject(new Bullet(), getX(),getY());
            Shotgunammo = Shotgunammo - 1;
            ShotGun = 0;
            clear();
        }
        
        if(Greenfoot.isKeyDown("r"))
        {
            ammo = 100;
            SMGammo = 150;
            Shotgunammo = 20;
            clear();
        }
    }    
    
    public void showHealth()
    {
        displayMessage("Health: " + health, 1000, 50, black, 15);
        if (health != health)
        {
            clear();
        }
    }
}
danpost danpost

2019/1/18

#
imdojangbob wrote...
the game gets slower or lags as I am playing it. How can I fix this problem? I know that the problem is caused because too many methods are compiling at once.
"too many methods are compiling at once": not sure what you mean by that. The problem does not appear to come from this class, unless it has something to do with the clear method, which I presume is in the Animal class.
imdojangbob imdojangbob

2019/1/18

#
Thank you for advice, What should I do with my animal class?
imdojangbob imdojangbob

2019/1/18

#
import greenfoot.*;
import java.awt.*;// add so text can be added
import java.util.List;
import java.util.ArrayList;

/**
 * ASD Revised Animal class
 * last revision - Fall 2017
 * 
 * ASD revised the code with updates for Color and Font classes
 * This revision updates the code so that all Color and Font are updated with greenfoot.Color
 * and greenfoot.Font per latest version (3.1.0) specifications
 * 
 * Also for bold-ing the text, revisions had to be made:
 * instead of Font font = new Font("bold", 1, size); 
 * --> changed to --> greenfoot.Font font = new greenfoot.Font("b",true,false, size);
 */

/**
 * ASD Revised Animal class
 * last revision - 12/13
 * 
 * ASD has revised the Animal class to add additional functionality to Animal
 * This revision has added methods for:
 * 1) Having objects removed from the world by a different object.
 * 2) Adding text to the screen by one or more actors:
 */

/**
 * Animal. This is the base class for all animals. In addition to the standard Actor
 * methods, it provides the ability to move and turn.
 * 
 * @author Michael Kolling
 * @version 1.0
 */
public class Animal extends Actor
{
    private static final double WALKING_SPEED = 5.0;
    
    // Below are variables added for the new text method - one GreefootImage variable (image) and color variables
    public GreenfootImage image = null;
    public greenfoot.Color black = new greenfoot.Color(0, 0, 0);
    public greenfoot.Color white = new greenfoot.Color(255, 255, 255);
    public greenfoot.Color red = new greenfoot.Color(255, 0, 0);
    public greenfoot.Color blue = new greenfoot.Color(0, 0, 255);
    public greenfoot.Color green = new greenfoot.Color(0, 255, 0);
    public greenfoot.Color orange = new greenfoot.Color(255, 100, 0);
    public greenfoot.Color lightBlue = new greenfoot.Color(0, 255, 255);
    public greenfoot.Color pink = new greenfoot.Color(255, 0, 255);
    public greenfoot.Color yellow = new greenfoot.Color(255, 255, 0);
    public greenfoot.Color brown = new greenfoot.Color(100, 0, 0);
    
    /**
     * Constructor for Animal - nothing to do.
     */
    public Animal()
    {
    }

    /**
     * Act - empty method. Animals have no default action.
     */
    public void act() 
    {
    } 
    
    /**
     * Turn 'angle' degrees towards the right (clockwise).
     */
    public void turn(int angle)
    {
        setRotation(getRotation() + angle);
    }
    
    /**
     * 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);
    }

    /**
     * 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;
    }
    
    /**
     * Return true if we can see an object of class 'clss' right where we are. 
     * False if there is no such object here.
     */
    public boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss); // This is the original canSee() method collision command
        //Actor actor = getOneIntersectingObject(clss); // ASD changed collision command so any part of objects will see or eat the other.
        return actor != null;        
    }
    
    /**
     * Return true if we can see an object of class 'clss' right where we are. 
     * False if there is no such object here.
     */
    public boolean canSee2(Class clss)
    {
        //Actor actor = getOneObjectAtOffset(0, 0, clss); // This is the original canSee() method collision command
        Actor actor = getOneIntersectingObject(clss); // ASD changed collision command so any part of objects will see or eat the other.
        return actor != null;        
    }
    
    /**
     * Try to eat an object of class 'clss'. This is only successful if there
     * is such an object where we currently are. Otherwise this method does
     * nothing.
     */
    public void eat(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss); //This is the original eat() method command
        //Actor actor = getOneIntersectingObject(clss); // ASD changed collision command so any part of objects will see or eat the other.
        if(actor != null) {
            getWorld().removeObject(actor);
        }
    }
    
        /**
     * Try to eat an object of class 'clss'. This is only successful if there
     * is such an object where we currently are. Otherwise this method does
     * nothing.
     */
    public void eat2(Class clss)
    {
        //Actor actor = getOneObjectAtOffset(0, 0, clss); //This is the original eat() method command
        Actor actor = getOneIntersectingObject(clss); // ASD changed collision command so any part of objects will see or eat the other.
        if(actor != null) {
            getWorld().removeObject(actor);
        }
    }
    
    /**
     * ASD Remove Object Method added here.
     * Remove one actor of a particular class. If there are multiple
     * actors in the world from that class, it will remove the 1st actor
     * in that class that was placed.
     */
    public void removeObject(Class clss)
    {
        List<Actor> actors = getWorld().getObjects(clss);
        int num = 0;
        for(Actor c: actors) {
            if(num == 0) {
                num = 1;
                getWorld().removeObject(c);
            }
        }
    }
    
    /**
     * ASD Text Addition Method added here:
     */
        public Actor access(java.lang.Class clss)
        {
        List<Actor> list = getWorld().getObjects(clss);
        int num = 0;
        Actor actor = null;
        for(Actor a: list)
        {
            if(num == 0) {
                num = 1;
                actor = a;
            }
        }
        return actor;
    }
    
    /**
     * There are 2 different methods below both called dispalyMessage().
     * Which one is used depends on the type of parameters 
     * that are typed in the actor with this method.
     * The first one has a string as the first parameter. 
     * The second one has a number (int) has the first parameter.
     * Both methods will need to have an x, y location, color and size.
     * 
     * Example 1 starting with a string: displayMessage("Counter = " + count, 100, 50, black, 10);
     * This example assumes that you have a int variable called count 
     * which is counting somewhere.
     * 
     * Example 2 starting with an integer: displayMessage(count, 100, 50, black, 10);
     * This example assumes that you have a int variable called count 
     * which is counting somewhere.
     */
    
    /**
     * This method displays the String "message" at the
     * ints ("x", "y"), using existing Color variables,
     * using the parameters in the method.
     */
    public void displayMessage(String message, int x, int y, greenfoot.Color c, int size)
    {
        if(image == null)
        {
            image = new GreenfootImage(getWorld().getBackground());    
        }
        
        GreenfootImage img = new GreenfootImage(getWorld().getBackground());
        img.setColor(c); //This sets the drawing color of the image "img".
        greenfoot.Font font = new greenfoot.Font("b",true,false, size);
        img.setFont(font); //This sets the font of the image "img".
        img.drawString(message, x, y); //This draws the string "message" onto the image "img".
        getWorld().setBackground(img); //This sets the background of the world to "img".
    }
    
    /**
     * This method calls the other displayMessage() method,
     * turning the int "num" in the parameter into a String.
     */
    public void displayMessage(int num, int x, int y, greenfoot.Color c, int size)
    {
        displayMessage("" + num, x, y, c, size);
    }
    
    /**
     * This method sets the background of the world
     * to the GreenfootImage "image" if it is not null.
     * Basically this method clears all the text on the screen.
     * Then new text can be written without writing over the old text.
     * If multiple actors are writing text on the screen this method
     * is used only once with the 1st actor that is writing.
     */
    public void clear()
    {
        if(image != null) {
            getWorld().setBackground(image);
        }
    }
    
}
danpost danpost

2019/1/18

#
imdojangbob wrote...
What should I do with my animal class?
Personally, I would totally remove the Animal class from the project. I see only one "possibly" useful method in it (the move method) and it is not used in the Human class. All the others either have a method now in the Actor class that can be used in lieu of it (canSee, eat, turn, atWorldEdge); or, are badly written (access, removeObject); or, are related to poor design (displayMessage, clear). From a design standpoint, it would be better to use an actor, or actors, to display messages. That way, you are not constantly messing with the background image of the world, You may want to check out my Value Display Tutorial scenario.
You need to login to post a reply.