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

2016/9/28

Object Interaction Based on Image Size

zmcclary1205 zmcclary1205

2016/9/28

#
I am trying to make a green foot game similar to Agar.io and am having difficulty with some of the object interaction. The code syntax is compiling, but Greenfoot says there is an error in the program when I attempt to run it. The error is in the first if statement in the eat enemy or eat ball method. Ball Code
public class Ball extends Actor
{
    GreenfootImage image = getImage();
    public int x = image.getWidth();
    public int y = image.getHeight();
    
    public Enemy enemy;
    
    /**
     * Constructors
     */
    public Ball(Enemy enemy1)
    {
        enemy = enemy1;
        
        GreenfootImage img = new GreenfootImage(50,50);
        int r = Greenfoot.getRandomNumber(256);
        int g = Greenfoot.getRandomNumber(256);
        int b = Greenfoot.getRandomNumber(256);
        img.setColor(new Color(r,g,b,255));
        img.fillOval(0,0,50,50);
        setImage(img);
    }
    
    /**
     * Act - do whatever the Ball0 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        eatBerries();
        movement();
        changeImage();
        eatEnemy();
    }
    
    /**
     * Change Image Color.
     */
    public void changeImage()
    {
        if (x == 100)
        {
            GreenfootImage img = new GreenfootImage(101,101);
            int r = Greenfoot.getRandomNumber(256);
            int g = Greenfoot.getRandomNumber(256);
            int b = Greenfoot.getRandomNumber(256);
            img.setColor(new Color(r,g,b,255));
            img.fillOval(0,0,101,101);
            setImage(img);
        }
        
        if (x == 200)
        {
            GreenfootImage img = new GreenfootImage(201,201);
            int r = Greenfoot.getRandomNumber(256);
            int g = Greenfoot.getRandomNumber(256);
            int b = Greenfoot.getRandomNumber(256);
            img.setColor(new Color(r,g,b,255));
            img.fillOval(0,0,201,201);
            setImage(img);
        }
        
        if (x == 300)
        {
            GreenfootImage img = new GreenfootImage(301,301);
            int r = Greenfoot.getRandomNumber(256);
            int g = Greenfoot.getRandomNumber(256);
            int b = Greenfoot.getRandomNumber(256);
            img.setColor(new Color(r,g,b,255));
            img.fillOval(0,0,301,301);
            setImage(img);
        }
        
        if (x == 400)
        {
            GreenfootImage img = new GreenfootImage(401,401);
            int r = Greenfoot.getRandomNumber(256);
            int g = Greenfoot.getRandomNumber(256);
            int b = Greenfoot.getRandomNumber(256);
            img.setColor(new Color(r,g,b,255));
            img.fillOval(0,0,401,401);
            setImage(img);
        }
        
        if (x == 500)
        {
            GreenfootImage img = new GreenfootImage(501,501);
            int r = Greenfoot.getRandomNumber(256);
            int g = Greenfoot.getRandomNumber(256);
            int b = Greenfoot.getRandomNumber(256);
            img.setColor(new Color(r,g,b,255));
            img.fillOval(0,0,501,501);
            setImage(img);
        }
    }
    
    /**
     * Eat Berries
     */
    public void eatBerries()
    {
        Actor berry;
        berry = getOneIntersectingObject(Berry.class);
        
        if (berry != null)
        {
            image.scale(x+1,y+1);
            
            World world;
            world = getWorld();
            world.removeObject(berry);
        }
    }
    
    public void movement()
    {
        
        if (x <= 100)
        {
            if(Greenfoot.isKeyDown("up"))
            {
                move(10);
            }
        
            if(Greenfoot.isKeyDown("down"))
            {
                move(-10);
            }
            
            if(Greenfoot.isKeyDown("left"))
            {
                turn(-6);
            }
            
            if(Greenfoot.isKeyDown("right"))
            {
                turn(6);
            }
        }
        
        if (x >= 100)
        {
            if (x <= 200)
            {
                if(Greenfoot.isKeyDown("up"))
                {
                    move(8);
                }
        
                if(Greenfoot.isKeyDown("down"))
                {
                    move(-8);
                }
                
                if(Greenfoot.isKeyDown("left"))
                {
                    turn(-5);
                }
            
                if(Greenfoot.isKeyDown("right"))
                {
                    turn(5);
                }
            }
        }
            
        if (x >= 200)
        {
            if (x <= 300)
            {
                if(Greenfoot.isKeyDown("up"))
                {
                    move(6);
                }
        
                if(Greenfoot.isKeyDown("down"))
                {
                    move(-6);
                }
                
                if(Greenfoot.isKeyDown("left"))
                {
                    turn(-4);
                }
            
                if(Greenfoot.isKeyDown("right"))
                {
                    turn(4);
                }
            }
        }
        
        if (x >= 300)
        {
            if (x <= 400)
            {
                if(Greenfoot.isKeyDown("up"))
                {
                    move(4);
                }
        
                if(Greenfoot.isKeyDown("down"))
                {
                    move(-4);
                }
                
                if(Greenfoot.isKeyDown("left"))
                {
                    turn(-3);
                }
            
                if(Greenfoot.isKeyDown("right"))
                {
                    turn(3);
                }
            }
        }
        
        if (x >= 400)
        {
            if (x <= 500)
            {
                if(Greenfoot.isKeyDown("up"))
                {
                    move(2);
                }
        
                if(Greenfoot.isKeyDown("down"))
                {
                    move(-2);
                }
                
                if(Greenfoot.isKeyDown("left"))
                {
                    turn(-2);
                }
            
                if(Greenfoot.isKeyDown("right"))
                {
                    turn(2);
                }
            }
        }
        
        if (x >= 500)
        {
            if(Greenfoot.isKeyDown("up"))
            {
                move(2);
            }
        
            if(Greenfoot.isKeyDown("down"))
            {
                move(-2);
            }
            
            if(Greenfoot.isKeyDown("left"))
            {
                turn(-1);
            }
            
            if(Greenfoot.isKeyDown("right"))
            {
                turn(1);
            }
        }
    }
    
    /**
     * Eat Enemy
     */
    public void eatEnemy()
    {
        Actor enemy2;
        enemy2 = getOneObjectAtOffset(0,0,Enemy.class);
        
        if (enemy2 != null)
        {
            if(x>enemy.x1)
            {
                image.scale(x+enemy.x1,y+enemy.y1);
            
                World world;
                world = getWorld();
            
                world.removeObject(enemy);
            }
        }
    }
}
Enemy Code
public class Enemy extends Actor
{
    GreenfootImage image = getImage();
    public int x1 = image.getWidth();
    public int y1 = image.getHeight();
    
    public Ball ball;
    
    /**
     * Constructors
     */
    public Enemy(Ball ball1)
    {
        ball = ball1;
        
        GreenfootImage img = new GreenfootImage(50,50);
        int r = Greenfoot.getRandomNumber(256);
        int g = Greenfoot.getRandomNumber(256);
        int b = Greenfoot.getRandomNumber(256);
        img.setColor(new Color(r,g,b,255));
        img.fillOval(0,0,50,50);
        setImage(img);
    }
    
    /**
     * Act - do whatever the Ball0 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        eatBerries();
        movement();
        changeImage();
        eatBall();
    }
    
    /**
     * Change Image Color.
     */
    public void changeImage()
    {
        if (x1 == 100)
        {
            GreenfootImage img = new GreenfootImage(101,101);
            int r = Greenfoot.getRandomNumber(256);
            int g = Greenfoot.getRandomNumber(256);
            int b = Greenfoot.getRandomNumber(256);
            img.setColor(new Color(r,g,b,255));
            img.fillOval(0,0,101,101);
            setImage(img);
        }
        
        if (x1 == 200)
        {
            GreenfootImage img = new GreenfootImage(201,201);
            int r = Greenfoot.getRandomNumber(256);
            int g = Greenfoot.getRandomNumber(256);
            int b = Greenfoot.getRandomNumber(256);
            img.setColor(new Color(r,g,b,255));
            img.fillOval(0,0,201,201);
            setImage(img);
        }
        
        if (x1 == 300)
        {
            GreenfootImage img = new GreenfootImage(301,301);
            int r = Greenfoot.getRandomNumber(256);
            int g = Greenfoot.getRandomNumber(256);
            int b = Greenfoot.getRandomNumber(256);
            img.setColor(new Color(r,g,b,255));
            img.fillOval(0,0,301,301);
            setImage(img);
        }
        
        if (x1 == 400)
        {
            GreenfootImage img = new GreenfootImage(401,401);
            int r = Greenfoot.getRandomNumber(256);
            int g = Greenfoot.getRandomNumber(256);
            int b = Greenfoot.getRandomNumber(256);
            img.setColor(new Color(r,g,b,255));
            img.fillOval(0,0,401,401);
            setImage(img);
        }
        
        if (x1 == 500)
        {
            GreenfootImage img = new GreenfootImage(501,501);
            int r = Greenfoot.getRandomNumber(256);
            int g = Greenfoot.getRandomNumber(256);
            int b = Greenfoot.getRandomNumber(256);
            img.setColor(new Color(r,g,b,255));
            img.fillOval(0,0,501,501);
            setImage(img);
        }
    }
    
    /**
     * Eat Berries
     */
    public void eatBerries()
    {
        Actor berry;
        berry = getOneIntersectingObject(Berry.class);
        
        if (berry != null)
        {
            image.scale(x1+1,y1+1);
            
            World world;
            world = getWorld();
            world.removeObject(berry);
        }
    }
    
    public void movement()
    {
        if (x1 <= 100)
        {
            move(Greenfoot.getRandomNumber(21)-10);
            turn(Greenfoot.getRandomNumber(13)-6);
        }
        
        if (x1 >= 100)
        {
            if (x1 <= 200)
            {
                move(Greenfoot.getRandomNumber(17)-8);
                turn(Greenfoot.getRandomNumber(11)-5);
            }
        }
            
        if (x1 >= 200)
        {
            if (x1 <= 300)
            {
                move(Greenfoot.getRandomNumber(13)-6);
                turn(Greenfoot.getRandomNumber(9)-4);
            }
        }
        
        if (x1 >= 300)
        {
            if (x1 <= 400)
            {
                move(Greenfoot.getRandomNumber(9)-4);
                turn(Greenfoot.getRandomNumber(7)-3);
            }
        }
        
        if (x1 >= 400)
        {
            if (x1 <= 500)
            {
                move(Greenfoot.getRandomNumber(5)-2);
                turn(Greenfoot.getRandomNumber(5)-2);
            }
        }
        
        if (x1 >= 500)
        {
            move(Greenfoot.getRandomNumber(5)-2);
            turn(Greenfoot.getRandomNumber(3)-1);
        }
    }
    
    /**
     * Eat Ball
     */
    public void eatBall()
    {
        if (x1>ball.x)
        {
            Actor ball2;
            ball2 = getOneObjectAtOffset(0,0,Ball.class);
            
            if(ball2 != null)
            {
                image.scale(x1+ball.x,ball.y);
            
                World world;
                world = getWorld();
                world.removeObject(ball);
                
                Greenfoot.stop();
            }
        }
    }
}
danpost danpost

2016/9/29

#
I would like to first suggest not to have fields for the image and its width and height. These can be acquired on the fly. For example:
if (this.getImage().getWidth() > enemy.getImage().getWidth())
That way, you are ensured to get the actual current values for it size and do not have to worry about keeping track of them (which you really are not doing; you are re-scaling the images, but not adjusting the value of the fields for their height and width.
You need to login to post a reply.