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

2021/12/20

Improve crab and worm game

fdl._.11 fdl._.11

2021/12/20

#
Hi, can anyone help me?? i want to make the worm move randomly when the crab approaches the worm at range 50... *use getObjectsInRange to check the range and use Greenfoot.addRandomNumber to move the worm randomly.. sorry for my bad english, i hope you understand what i mean...
danpost danpost

2021/12/20

#
fdl._.11 wrote...
i want to make the worm move randomly when the crab approaches the worm at range 50... *use getObjectsInRange to check the range and use Greenfoot.addRandomNumber to move the worm randomly..
I presume you mean !AAARGH!! add getRandomNumber to move the worm randomly. Also, I presume that the random part of moving is for the direction of movement, not speed or distance. There are a couple of things to consider here. First, the worm will probably want to remain upright while moving is some direction (a rotating worm would look off). Second, to avoid the randomness having the worm just jiggle around in the same general area, the initial direction of movement will need to be retained, so that the movement can continue along the same general direction (varied by a small rotation, which may also be retained to avoid moving generally along a straight line). Movement similar to the Smileys in my Space Smileys scenario might be considered. Differences may include a constant speed, in lieu of speeding up and slowing down) and a way to alter direction each time the crab comes within range (while keeping the direction while the crab remains in range). The direction field might be better off being of type Integer, rather than int. That way, it can have a null value for while crab is out of range and be set to some random value once the crab comes within range. A boolean may be sufficient in lieu of the Integer. Really depends on how the movement is coded. You could even consider the worm moving away from the crab to begin with, then do some turning.
fdl._.11 fdl._.11

2021/12/21

#
danpost wrote...
fdl._.11 wrote...
i want to make the worm move randomly when the crab approaches the worm at range 50... *use getObjectsInRange to check the range and use Greenfoot.addRandomNumber to move the worm randomly..
I presume you mean !AAARGH!! add getRandomNumber to move the worm randomly. Also, I presume that the random part of moving is for the direction of movement, not speed or distance. There are a couple of things to consider here. First, the worm will probably want to remain upright while moving is some direction (a rotating worm would look off). Second, to avoid the randomness having the worm just jiggle around in the same general area, the initial direction of movement will need to be retained, so that the movement can continue along the same general direction (varied by a small rotation, which may also be retained to avoid moving generally along a straight line). Movement similar to the Smileys in my Space Smileys scenario might be considered. Differences may include a constant speed, in lieu of speeding up and slowing down) and a way to alter direction each time the crab comes within range (while keeping the direction while the crab remains in range). The direction field might be better off being of type Integer, rather than int. That way, it can have a null value for while crab is out of range and be set to some random value once the crab comes within range. A boolean may be sufficient in lieu of the Integer. Really depends on how the movement is coded. You could even consider the worm moving away from the crab to begin with, then do some turning.
can you help to change my code?? here is my code:
    public void randomMove()
    {
        Crab crab = new Crab();
        List<Crab> actors = getObjectsInRange(50, Crab.class);
        
        for(Crab i : actors)
        {
            if(i != null)
            {
                int a = Greenfoot.getRandomNumber(200);
                int b = Greenfoot.getRandomNumber(200);
                setLocation(a,b);
            }
        }
    }
it worked that the worm will move randomly... but crab can't eat it. I want that the crab only can eat the worm with dash( if i press the up key, crab will move more fast) here is the code
        if (Greenfoot.isKeyDown("up"))
        {
            speed = speed*3;
            move(speed);
        }
danpost danpost

2021/12/21

#
fdl._.11 wrote...
here is my code for dash
if (Greenfoot.isKeyDown("up"))
{
    speed = speed*3;
    move(speed);
}
Whoa. I do not think you want to multiply speed by 3 there. That would be a "permanent" change and in less than one second, a move will cause your actor to "dash" to the edge of the world. Use this, instead:
if (Greenfoot.isKeyDown("up")) move(speed*3);
(so the value of speed, itself, does not change)
Can you help me to code it??? i have write some code, but i'm still confused... Here is my code: << Code Omitted >> (randomMove method)
Your simplified code would look like this:
public void randomMove()
{
    if (!getObjectsInRange(50, Crab.class).isEmpty())
    {
        move(Greenfoot.getRandomNumber(550));
    }
}
(everything else in your original code had absolutely no purpose) With the simplified code, using 550 on line 5 will teleport the worm pretty much anywhere in your world, provided your world is not extremely huge. Also, without any rotation, using move will always make your worm move to the right. If the worm is to remain along the path originally taken (after rotation is used), then that rotation will need to be retained -- saved in a field. Also, if the movement direction is to change each time the crab comes within range, then a boolean will be needed to indicate whether the crab was last in range or not. So, something like this:
private boolean crabInRange;
private int moveAngle;

public void randomMove()
{
    // test if crab changes its state of being in range to control retained state and set move angle
    if (crabInRange == getObjectsInRange(50, Crab.class).isEmpty())
    {
        // record change in state
        crabInRange = ! crabInRange; // state changed
        // set new move direction (set whether changing states one way or the other, as it is irrelevant when not moving)
        moveAngle = Greenfoot.getRandomNumber(360); // new move direction
    }
    // test if crab in range to move
    if (crabInRange)
    {
        // turn toward moving direction
        setRotation(moveAngle);
        // move
        move(Greenfoot.getRandomNumber(50));
        // reset rotation to zero to upright actor image
        setRotation(0);
    }
}
danpost danpost

2021/12/21

#
I do understand that more work will need done with this to make it do what you want. With what was given by me, the crab may have trouble getting to the worm.
fdl._.11 fdl._.11

2021/12/21

#
danpost wrote...
I do understand that more work will need done with this to make it do what you want. With what was given by me, the crab may have trouble getting to the worm.
don't worry, it's already solved XD here is my code
public void randomMove()
    {
        Crab crab = new Crab();
        List<Crab> actors = getObjectsInRange(50, Crab.class);
        
        for(Crab i : actors)
        {
            if(i != null)
            {
                if(Greenfoot.isKeyDown("up"))
                {
                    move(0);
                }
                else
                {
                    Integer a = Greenfoot.getRandomNumber(550);
                    Integer b = Greenfoot.getRandomNumber(550);
                    setLocation(a,b);  
                }
            }
        }          
    }
and thanks for your advce
danpost danpost

2021/12/21

#
fdl._.11 wrote...
here is my code << Code Omitted >>
More simply:
private void randomMove()
{
    if (Greenfoot.isKeyDown("up")) return;
    if (getObjectsInRange(50, Crab.class).isEmpty()) return;
    int a = Greenfoot.getRandomNumber(550);
    int b = Greenfoot.getRandomNumber(550);
    setLocation(a, b);
}
You need to login to post a reply.