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

2017/8/18

Catching Game. Needs minor adjustments

MOChibi MOChibi

2017/8/18

#
Hey guys! I need help. I'm making a catching game in Greenfoot ver. 3.1.0. It went fine, which surprised me because I am a beginner and don't know anything else much but the basics. However, my basket keeps going off the screen. This is the code of basket:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class cb here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class cb extends Actor
{
    /**
     * Act - do whatever the cb wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        checkKeys();
        AppleCrunch();
    }
    public void AppleCrunch()
    {
        if(isTouching(apple.class))
        {
            getOneObjectAtOffset(0,0,apple.class);
            ((counter2)getWorld().getObjects(counter2.class).get(0)).bumpCount(15);
        }
        if(isTouching(banana.class))
        {
            getOneObjectAtOffset(0,0,banana.class);
            ((counter2)getWorld().getObjects(counter2.class).get(0)).bumpCount(8);
        }
        if(isTouching(cherry.class))
        {
            getOneObjectAtOffset(0,0,cherry.class);
            ((counter2)getWorld().getObjects(counter2.class).get(0)).bumpCount(5);
        }
        if(isTouching(grape.class))
        {
            getOneObjectAtOffset(0,0,grape.class);
            ((counter2)getWorld().getObjects(counter2.class).get(0)).bumpCount(20);
        }
    }
    public void checkKeys()
    {
        if(Greenfoot.isKeyDown("left"))
        {
            move(-10);
        }
        if(Greenfoot.isKeyDown("right"))
        {
            move(10);
        }
    }
}
Also, I want the fruits and bomb to fall slower and more random. Which in these ff. codes should I change? code of world (park1):
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class park1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class park1 extends World
{
    /**
     * Constructor for objects of class park1.
     * 
     */
    public park1()   
    {
       super(600, 400, 1);
       AppleCrunch();
       prepare();
       BananaSplit();
       BoingBoing();
       CherryBomb();
       Kaboom();
    }
    public void act()
    {
        if(getObjects(apple.class).isEmpty())AppleCrunch();
        if(getObjects(banana.class).isEmpty())BananaSplit();
        if(getObjects(grape.class).isEmpty())BoingBoing();
        if(getObjects(cherry.class).isEmpty())CherryBomb();
        if(getObjects(bomb.class).isEmpty())Kaboom();
    }
    public void prepare()
    {
        cb cb=new cb();
        addObject(cb,618,100);
        counter2 counter2=new counter2();
        addObject(counter2,57,69);
        score score=new score();
        addObject(score,64,44);
        score.setLocation(59,44);
        cb.setLocation(373,403);
    }
    public void AppleCrunch()
    {
        if(Greenfoot.getRandomNumber(2)<50)
        {
            addObject(new apple(),Greenfoot.getRandomNumber(519),20);
        }
    }
    public void BananaSplit()
    {
        if(Greenfoot.getRandomNumber(2)<50)
        {
            addObject(new banana(),Greenfoot.getRandomNumber(519),20);
        }
    }
    public void BoingBoing()
    {
        if(Greenfoot.getRandomNumber(2)<50)
        {
            addObject(new grape(),Greenfoot.getRandomNumber(519),20);
        }
    }
    public void CherryBomb()
    {
        if(Greenfoot.getRandomNumber(2)<50)
        {
            addObject(new cherry(),Greenfoot.getRandomNumber(519),20);
        }
    }
    public void Kaboom()
    {
        if(Greenfoot.getRandomNumber(2)<50)
        {
            addObject(new bomb(),Greenfoot.getRandomNumber(519),20);
        }
    }
}
banana code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class banana here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class banana extends fruits
{
    /**
     * Act - do whatever the banana wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setLocation(getX(),getY()+10);
        BananaSundae();
    }    
    public void BananaSundae()
    {
        if(isTouching(cb.class))
        {
            ((counter2)getWorld().getObjects(counter2.class).get(0)).bumpCount(8);
        }
        if(isAtEdge())
        {
            getWorld().removeObject(this);
        }
    }  
}
apple code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class apple here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class apple extends fruits
{
    /**
     * Act - do whatever the apple wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setLocation(getX(),getY()+10);
        AppleCider();
    }    
    public void AppleCider()
    {
        if(isTouching(cb.class))
        {
            ((counter2)getWorld().getObjects(counter2.class).get(0)).bumpCount(15);
        }
        if(isAtEdge())
        {
            getWorld().removeObject(this);
        }
    }        
}
cherry code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class cherry here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class cherry extends fruits
{
    /**
     * Act - do whatever the cherry wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setLocation(getX(),getY()+10);
        CherrySplat();
    }    
    public void CherrySplat()
    {
        if(isTouching(cb.class))
        {
            ((counter2)getWorld().getObjects(counter2.class).get(0)).bumpCount(5);
        }
        if(isAtEdge())
        {
            getWorld().removeObject(this);
        }
    }   
}
grape code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class grape here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class grape extends fruits
{
    /**
     * Act - do whatever the grape wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setLocation(getX(),getY()+10);
        GrapeJuice();
    }    
    public void GrapeJuice()
    {
        if(isTouching(cb.class))
        {
            ((counter2)getWorld().getObjects(counter2.class).get(0)).bumpCount(20);
        }
        if(isAtEdge())
        {
            getWorld().removeObject(this);
        }
    }   
}
bomb code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class bomb here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class bomb extends fruits
{
    /**
     * Act - do whatever the bomb wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setLocation(getX(),getY()+10);
        Boombayah();
    }    
    public void Boombayah()
    {
        if(isTouching(cb.class))
        {
            ((counter2)getWorld().getObjects(counter2.class).get(0)).bumpCount(-10);
        }
        if(isAtEdge())
        {
            getWorld().removeObject(this);
        }
    }
}
This is all. Please help me and state what is wrong specifically because like I said, I am just a beginner and don't know much yet. I need help as soon as possible. THANK YOU FOR ALL THOSE WHO WILL HELP ME!!! =.=
MOChibi MOChibi

2017/8/18

#
It also seems to be in instant win because I only caught one fruit and it is already in 100+ score. How do I change this?
Super_Hippo Super_Hippo

2017/8/18

#
getOneObjectAtOffset(0,0,apple.class);
A line like this is not doing anything. You should remove the fruit when the cb collects it or you will keep generating points as long as it is touching the fruit. The fruit itself should not check if it is touching the cb, it is enough when the code is in one class. Moving 10 each act cycle (cb and fruits) also seems a bit fast but maybe this is wanted.
if(Greenfoot.getRandomNumber(2)<50)
getRandomNumber(2) returns either 0 or 1 randomly, so it will always be below 50 and spawn a new fruit whenever the one was collected.
MOChibi MOChibi

2017/8/22

#
Ohhhhh...... Ok, I get it. Thanks Super_Hippo.
MOChibi MOChibi

2017/8/22

#
Okay. I have the right score, but how about the others that cb did not touch? How will they go back to the top?
Super_Hippo Super_Hippo

2017/8/22

#
I thought they would just be removed from the world and the world adds a new one because it sees that none is in the world. Maybe decreasing score if the user failed to catch it.
You need to login to post a reply.