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

2013/10/29

Space invaders enemy AI

benskii93 benskii93

2013/10/29

#
Heya, I'm trying to make a space invaders style game for a University assignment and can't seem to replicate the invader movements. I tried just creating if statements that access the objects position on the stage (when the invaders are to the right of the stage as the condition and the resulting code body just being move(-2). However the invaders just reach the right side and stop moving, I think this may be because the if statements conditions are met and then the if's code body only iterates once instead of constantly. I tried to fix this by making the if's body execute a while loop that stopped once the invaders had hit the left side and this didn't work either. My assignment is due in 10 days and I'm completely stuck, any help at all would be really appreciated :)
I would keep track of this stuff inside your world, instead of in the actors. You can find one of the most far right, or far left invader, and keep track of it until it hits the wall, or is dead. If it's dead, just find one of the most far right/left invaders again. If it's far enough to the right or left, you have a method that tells ALL the space invaders to start moving the other way. I would accomplish this by keeping a boolean variable inside the world, that keeps track if it's going right or left (Call it goingRight or something like that, false if going left). Then have a method inside your world that returns that boolean so the living invaders can access it at any time, unless you make it public, which you shouldn't do. Then you can find the most right/left (based off your boolean) by going through all the actors and finding out which actor has the highest, or least x. Keep track of this invader until its x becomes too big, or too small, or if it dies. You should have a method in your world that the space invaders call when they die, that tells the world to try again, without changing the boolean. If it's the x that has changed, make sure to change the boolean, then call the method to find the most left/right invader again. Don't make a loop for the whole proccess! Just wait until certain conditions are met, and then call the methods required. Use a loop, when looking through all the invaders, to see which one has the highest/lowest x. Hope this helps!
danpost danpost

2013/10/30

#
@FRUP, that would be a lot to deal with (finding the left-most and right-most invaders and keeping track of them). Keeping the direction of movement in the world is fine; however, having the invaders access that field is not as easy as just passing the value to the invader when it is time for it to move. Again, using a loop to find the highest/lowest x'd invader is not necessary. You can have the invader checks its location when it is moved a return a boolean value to indicate whether it has reached the edge or not. Retain the value in the world until all invaders have completed that set of movements, then shift them all down and change the direction field. I will upload a basic 're-make' of space invaders (without bunkers or scores), but the code will not be published. I will, however, explain how I did it. I created an actor called 'Row' to track (by using 'getIntersectingObjects') the invaders on each row and had the world tell each row object when and in which direction to move the invaders in that row. The row object, in turn, moves each invader in that row, getting back the state of each one being on the edge or not. That state is then returned back to the world, which will then, if necessary, shift all invaders down and change the direction AFTER all rows have completed that sequence of moves. Further information will be supplied when asked for it (except for the code itself). The scenario is located here.
benskii93 benskii93

2013/10/31

#
This is all good information, its given me some good ideas, thanks a lot both of you.
You need to login to post a reply.