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

2011/3/29

Cpu LightBike (again)

m.legere1323 m.legere1323

2011/3/29

#
So I've tried and tried and tried, but can't get a successful cpu with some basic AI for my lightBike game...For 3.0 thats what I'm trying to do...Those of you who havent seen my lightbike game its at <http://greenfootgallery.org/scenarios/2538> ....My grid for 3.0 is (100,100,5) and Here's my code that won't seem to work: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.List; import java.util.ArrayList; /** * Cpu guy that tries to avoid your bike. * * @author (Michael Legere) * @version (3.0) */ public class RedEnemy extends Mover { private static final int EAST = 0; private static final int WEST = 1; private static final int NORTH = 2; private static final int SOUTH = 3; private int direction; public RedEnemy() { setDirection(WEST); } public boolean canMove() { World myWorld = getWorld(); int x = getX(); int y = getY(); switch(direction) { case SOUTH : y -= 1; break; case EAST : x -= 1; break; case NORTH : y += 1; break; case WEST : x += 1; break; } List line = myWorld.getObjectsAt(x, y, BlueLine.class); if(line.isEmpty()) { return true; } else { return false; } } public void setDirection(int direction) { this.direction = direction; } public void turnLeft() { setRotation(getRotation() + 90); } public void turnRight() { setRotation(getRotation() - 90); } public void think() { if(!canMove()) { turnRight(); } } public void act() { think(); wrap(); generateRedLine(); move(-1); } } If anyone was able to trudge through this could you give me some tips? The Cpu just runs straight and crashes into the player's line when its in front of it.
m.legere1323 m.legere1323

2011/3/29

#
Also, BlueLine is the player's line
davmac davmac

2011/3/30

#
Sure. A couple of problems. 1. When the bike turns, it doesn't call setDirection(), so dreiction is always going to be WEST. 2. The lightpath uses getOneIntersectingObject to check if a bike crashed into it, but the bikes are bigger than one unit in size, so the bike will intersect the lightpath before it sees the lightpath (the bike only looks one unit ahead).
m.legere1323 m.legere1323

2011/3/30

#
Ill try that thank you very much!
m.legere1323 m.legere1323

2011/3/30

#
I tried this, but it still doesn't like me =/ import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.List; import java.util.ArrayList; /** * Cpu guy that tries to avoid your bike. * * @author (Michael Legere) * @version (3.0) */ public class RedEnemy extends Mover { private static final int EAST = 0; private static final int WEST = 1; private static final int NORTH = 2; private static final int SOUTH = 3; private int direction; public RedEnemy() { setDirection(WEST); } public boolean canMove() { World myWorld = getWorld(); int x = getX(); int y = getY(); switch(direction) { case SOUTH : y -= 3; break; case EAST : x -= 3; break; case NORTH : y += 3; break; case WEST : x += 3; break; } List line = myWorld.getObjectsAt(x, y, BlueLine.class); if(line.isEmpty()) { return true; } else { return false; } } public void setDirection(int direction) { this.direction = direction; } public void turn() { switch(this.direction) { case SOUTH : setRotation(getRotation() - 90); setDirection(WEST); break; case EAST : setDirection(NORTH); setRotation(getRotation() + 90); break; case NORTH : setDirection(EAST); setRotation(getRotation() - 90); break; case WEST : setDirection(SOUTH); setRotation(getRotation() + 90); break; } } public void think() { if(!canMove()) { turn(); } } public void act() { think(); wrap(); generateRedLine(); move(-1); } }
davmac davmac

2011/3/30

#
For one thing, SOUTH and NORTH are mixed up (the direction is SOUTH when the bike is heading up the screen, surely that is meant to be NORTH). Just to be clear: in Greenfoot, and in many computer graphics APIs, postitive Y values are further *down* the screen (not like in a conventional graph). Also, in the canMove method, the offsets are negative. Where you are adding 3 you should be subtracting 3 and vice versa - because you want to be looking *ahead* of the bike, not behind it. Finally, I'm not sure if 3 is big enough an offset. Try 10 or even 20, then whittle it down.
m.legere1323 m.legere1323

2011/3/30

#
Oh haha, sorry, I completely overlooked that (the north south thing) ...guess I was tired? Thanks though..again!
You need to login to post a reply.