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!


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()); } } } |
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 ); } } } |