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

2012/10/25

Coding a moving Centipede

Kevin_Bacon Kevin_Bacon

2012/10/25

#
Please Answer within the next 24 hours if possible! So the title is pretty self explanatory. I have to make a moving centipede for an assignment that can do this: 1.) Centipede comes in from the top of the screen. It is 12 segments long. 2.) Centipede descends from the center of the board and immediately begins moving towards the right of the board. 3.) When the centipede hits the right hand wall, it will move into the next row towards the bottom and start moving left. When it hits the left wall, it will move into the next row toward the bottom and start moving right. I can make one object move no problem, but now I have to have 11 others play "follow the leader". I know I need an array to do this, and a boolean condition that checks for turns. But I have no idea how to approach this or where to put certain parts of code in. My only experience with arrays thus far is with making a proton wave image grow; that's images, not actual objects. Any help please? I have no idea where to start. Also, I currently have each body part as a separate actor. Should each part instead be a subclass of Head (which is a subclass of Actor)? EDIT: Here is some code I have so far. It's pretty unpolished, but the basic idea is correct, isn't it? public void act() { move(15); //if it hits wall, turn 90 degrees if(getRotation() == 90) { isBugTurning = true; //if it is: //check next element //get element to turn } }
danpost danpost

2012/10/25

#
All parts of the centepede can be of the same sub-class of actor (call it 'Segment'). You will need to add an instance Segment variable to the world class (private Segment head;) to hold a reference to the first Segment created. In the Segment class, you can have two instance int variables to hold the previous location of that segment. They can be used both to find the segment behind this one and to control adding another segments as the centepede comes down from the top. You can have an instance int variable in the world class to control the direction of the head of the centepede. With both a reference to the head and the direction it is moving, the world class can control the movement of the head, and repeatedly find the next segment from where the previous segment has been and move it. After moving all segments in the world, check to see if the number of segment is twelve yet. And, if not, add another segment. If it is the first segment added, save it in the 'head' variable.
danpost danpost

2012/10/25

#
Download my Snake Demo scenario to get an idea of how to play follow the leader. In this scenario, each segment has a reference to the segment that follow it. But, I felt it would probably be easier for a less experienced programmer to just have the segment keep track of the location that it has moved from (which would be the location of the segment following it, if there is one).
You need to login to post a reply.