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

2015/3/11

How can I make my actors to automatically try to reach specific objects, which they should eat to win the game?

CSBossmann CSBossmann

2015/3/11

#
Hi, i´m making a greenfoot game for school. The actors have to eat objects, if they ate enough they win the game. I won´t the actor woh is controlled by the KI to try to go automatically to the objects, it should eat. Thank you!
danpost danpost

2015/3/11

#
Show what you tried, code-wise. Cannot help without seeing code.
Qiniso Qiniso

2015/3/11

#
HELLO THERE NEED HELP im trying to create a game that deals with numbers (MATHS). It an assignment due end of this week........and I don't even know where to start
CSBossmann CSBossmann

2015/3/11

#
So this is the code in which I think i have to insert the methods. I read something about a "turntowards ()" method, but I don´t really know how and where to use it... "SchweinchenBopps" should try to go to the "Robbe" instead of moving randomly.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Biene here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class SchweinchenBopps extends Troll
{
    private int timer = 0;
    private Counter2 counter2;
    private int stunTimer;
    public SchweinchenBopps (Counter2 pointCounter2)
    {
        counter2 = pointCounter2;
    }
 
    public void act()
    {
        if (stunTimer > 0)
        {
            stunTimer--;
            return; // this forces an immediate exit from the method so no further code within the method is executed
        }
        checkKeypress();
        lookForRobbe();
        shoot();
        lookForBulletH();
        if (getY() < 80) setLocation(getX(), 80);
 
    }
 
    public void lookForBulletH ()
    {
        if ( canSee(BulletH.class))
            stunTimer = 250;
 
    }
 
    private void checkKeypress()
    {
        if (Greenfoot.isKeyDown("left"))
        {
            turn(-4);
        }
        if (Greenfoot.isKeyDown("right"))
        {
            turn(4);
        }
        if (Greenfoot.isKeyDown("up"))
        {
            move(4);
        }
        if (Greenfoot.isKeyDown("down"))
        {
            move(-4);
        }
    }
 
    public void lookForRobbe ()
    {
        if ( canSee(Robbe.class) )
        {
            eat(Robbe.class);
            counter2.add(5);
 
        }
    }
 
    public  void shoot ()
    {
        if(timer>0)timer--;
        if(timer==0 && Greenfoot.isKeyDown("space"))
        {
            BulletS bullets= new BulletS();
            getWorld().addObject(bullets, getX()+25, getY());
            timer=60;
            bullets.setRotation(getRotation());
        }
    }
 
 
}
danpost danpost

2015/3/11

#
The SchweinchenBopps class looks like it creates user controlled objects that do not move randomly. Anyway, instead of 'checkKeypress', you should have 'move' which will locate, turn and move toward a Robbe object. How many Robbe object will there be in the world at any given time? The answer to that will determine what is needed in coding the movement.
CSBossmann CSBossmann

2015/3/11

#
Oh sorry! I copied the code of the wrong class... The right one is here: The Robben are spawning randomly in the whole world, the whole time.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Biene here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class SharkEins extends Troll
{
    private int RobbenEaten;
    private int timer=0;
    public void act()
    {
        move();
        turnAtEdge();
        randomTurn(); 
        shoot();
        lookForRobbe();
    }
 
    public void move()
    {
        move (5);
    }
        public void lookForRobbe ()
    {
        if ( canSee(Robbe.class) )
        {
            eat(Robbe.class);
        }
    }
 
    public  void shoot ()
    {
        if(timer>0)timer--;
        if(timer==0)
        {
            BulletH bulleth= new BulletH();
            getWorld().addObject(bulleth, getX()+25, getY());
            timer=60;
            bulleth.setRotation(getRotation());
        }
    }
 
    public void randomTurn()
    {
        if ( Greenfoot.getRandomNumber(100) < 10 )
        {
            turn( Greenfoot.getRandomNumber(40)-20 );
        }       
    }
 
    public void turnAtEdge()
    {
        if (atWorldEdge())
        {
            turn(7);
        }
    }
}
danpost danpost

2015/3/11

#
What are the SharkEins objects shooting at -- I mean, if it takes one to 'see' a Robbe to 'eat' one? Anyway, you need to basically change what is in your 'move' method. You probably want that the closest Robbe object be the one the actor moves toward. That will require determining which one is closest, turning toward its location and moving. See what you can come up with, a continue discussion with issues.
You need to login to post a reply.