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

2023/10/22

Need Help with Creating a Method

bearpants bearpants

2023/10/22

#
So I'm trying to create a game for a project I have that's due tomorrow. The game and code is relatively simple (it's a WIP). I am trying to create a method that will increase the 'moveAmount' (how quickly the Apple falls) variable by 1 every 20 apples eaten, but I can't seem to get anything to work. Any ideas? (NOTE: I mean the moveAmount variable in the Apple class NOT the one in the Dinosaur class.) Also, I apologize if the code is not formatted correctly as this is my first time using this forum. Here's the code for the Apple class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Apple extends Animal
{
    int moveAmount = 3;
    
    public Apple()
    {
        super();
    }
    
    public void act()
    {
        setLocation(getX(), getY() + moveAmount);
        gameOver();
    }
    
    public void gameOver()
    {
        if(getY() > 599)
        {
            Greenfoot.stop();   
        }
    }
}
Here's the code for the Counter class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Counter extends Actor
{
    private static final Color transparent = new Color(0,0,0,0);
    private GreenfootImage background;
    private int value;
    private int target;
    private String prefix;
    
    public Counter()
    {
        this(new String());
    }

    /**
     * Create a new counter, initialised to 0.
     */
    public Counter(String prefix)
    {
        background = getImage();  // get image from class
        value = 0;
        target = 0;
        this.prefix = prefix;
        updateImage();
    }
    
    /**
     * Animate the display to count up (or down) to the current target value.
     */
    public void act() 
    {
        if (value < target) {
            value++;
            updateImage();
        }
        else if (value > target) {
            value--;
            updateImage();
        }
    }

    /**
     * Add a new score to the current counter value.  This will animate
     * the counter over consecutive frames until it reaches the new value.
     */
    public void add(int score)
    {
        target += score;
    }

    /**
     * Return the current counter value.
     */
    public int getValue()
    {
        return target;
    }

    /**
     * Set a new counter value.  This will not animate the counter.
     */
    public void setValue(int newValue)
    {
        target = newValue;
        value = newValue;
        updateImage();
    }
    
    /**
     * Sets a text prefix that should be displayed before
     * the counter value (e.g. "Score: ").
     */
    public void setPrefix(String prefix)
    {
        this.prefix = prefix;
        updateImage();
    }

    /**
     * Update the image on screen to show the current value.
     */
    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(background);
        GreenfootImage text = new GreenfootImage(prefix + value, 22, Color.BLACK, transparent);
        
        if (text.getWidth() > image.getWidth() - 20)
        {
            image.scale(text.getWidth() + 20, image.getHeight());
        }
        
        image.drawImage(text, (image.getWidth()-text.getWidth())/2, 
                        (image.getHeight()-text.getHeight())/2);
        setImage(image);
    }
}
Here's the code for the MyWorld class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class MyWorld extends World
{
    Counter Counter = new Counter("Score: ");
    
    public MyWorld()
    {    
        // Create a new world with 830x600 cells with a cell size of 1x1 pixels.
        super(830, 600, 1);
        setBackground("forest.jpg");
        populate();
    }
    
    public void populate()
    {
        Dinosaur Dinosaur = new Dinosaur();
        addObject(Dinosaur, getWidth() / 2, getHeight() - 200);
        
        Apple Apple = new Apple();
        addObject(Apple, Greenfoot.getRandomNumber(729) + 51, 0);
        
        addObject(Counter, 750, 50);
    }
    
    public void countApples()
    {
        Counter.add(1);
    }
}
And (lastly) here's the code for Dinosaur class (which is meant to eat the apples):
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Dinosaur extends Animal
{
    private int moveAmount;
    private boolean edge;
    private String dinosaurFaceTowards;
    private int timer = 0;
    
    public Dinosaur()
    {
        moveAmount = 5;
        edge = false;
    }
    
    public void act()
    {
        checkKeys();
        eatApples();
        animateDinosaur();
    }
    
    public void checkKeys()
    {
        if(Greenfoot.isKeyDown("left") || Greenfoot.isKeyDown("a"))
        {
            setLocation(getX() - moveAmount, getY());
        }
        else if(Greenfoot.isKeyDown("right") || Greenfoot.isKeyDown("d"))
        {
            setLocation(getX() + moveAmount, getY());
        }
    }
    
    public void eatApples()
    {
        Actor Apple = getOneIntersectingObject(Apple.class);
        
        if(Apple != null)
        {
            getWorld().removeObject(Apple);
            timer = 15;
            ((MyWorld) getWorld()).countApples();
            ((MyWorld) getWorld()).addObject(new Apple(), Greenfoot.getRandomNumber(729) + 51, 0);
        }
    }
    
    public void animateDinosaur()
    {
        if(timer <= 15 && timer > 0)
        {
            if(Greenfoot.isKeyDown("left") || Greenfoot.isKeyDown("a"))
            {
                dinosaurFaceTowards = "left";
                setImage("dinosaur3-left.png");
            }
            else if(Greenfoot.isKeyDown("right") || Greenfoot.isKeyDown("d"))
            {
                dinosaurFaceTowards = "right";
                setImage("dinosaur3-right.png");
            }
            else if(dinosaurFaceTowards == "left")
            {
                setImage("dinosaur3-left.png");
            }
            else if(dinosaurFaceTowards == "right")
            {
                setImage("dinosaur3-right.png");
            }
            timer--;
        }
        else if(Greenfoot.isKeyDown("left") || Greenfoot.isKeyDown("a"))
        {
            dinosaurFaceTowards = "left";
            setImage("dinosaur2-left.png");
        }
        else if(Greenfoot.isKeyDown("right") || Greenfoot.isKeyDown("d"))
        {
            dinosaurFaceTowards = "right";
            setImage("dinosaur2-right.png");
        }
        else
        {
            if(dinosaurFaceTowards == "left")
            {
                setImage("dinosaur1-left.png");
            }
            else if(dinosaurFaceTowards == "right")
            {
                setImage("dinosaur1-right.png");
            }
        }
    }
}
bearpants bearpants

2023/10/23

#
Disregard this. I got it to work.
You need to login to post a reply.