Hello,
From what I can tell from your code, your concept seems sound: check the position of the centipede, if at a certain x location, replot y and change the x direction etc.
That all makes sense, but I think there needs to be a larger over-all plan.
My approach would be to treat ANY area occupied on the board as a potential "collision" area (something that can be bumped into and thus change the direction of the centipede). That would mean defining the sides of the screen as collidable.
This would allow you to apply one general algorithm to any of the centipede segments and they would behave the same no matter what they collided with (expect when getting shot) including the edges of the screen. I will call the balls that make up the centipede segments or sections. So a whole centipede might be 15 sections long for example.
But how do you do this? The centipede can collide with spiders, and mushrooms and other weird creatures or the centipede segment in front of it can be shot etc., etc. How do we manage all the possible collisions? Aren't they all different?
The first thing to do is divide your screen into a grid. Each grid space will be the maximum size it takes to draw a single centipede section. For the sake of argument, we'll say a centipede section can be drawn in a 16x16 pixel box. On a 640x480 screen, that would be a 40x30 grid.
Most of the other objects on the screen are the same size as a centipede section, except, I think, the spider and the snail. I think those are 2x1 sections big. Whatever the case, when something is inside a grid square on the screen, that grid is considered collidable, and if a centipede section tries to move there, that's when the "bump" algorithm has to activate.
Every object can be identified by a number to tell what type it is. For example:
0 nothing at this location
1 could be a mushroom
2 could be a snail
3 could be a centipede section
4 could be a spider
5 could be an invisible barrier (to use to block the sides of the screen)
Let's talk about number 5 for a second. If we assign the grid squares on each side of the screen to number 5, then when the centipede tries to move there, it will think it's a collidable area. You don't even need to create a graphic. Just having the grid squares holding the number 5 should be enough.
This same logic applies to any other number that is considered impassable.
How do I divide the screen up into a grid? What does that even mean?
Well, there is a type of variable that lends itself rather nicely to this task. It is called an array. An array is a group of like variables that are stored in sequence under one name. For example, I could make an array that holds the screen grid I've been talking about. Making an array is called DIMensioning it:
dim screengrid(40,30)
What I'm doing is creating a table, a 2 dimensional array that represents a 40x30 grid. There are many tutorials about arrays on the forums so if you aren't familiar with them, you should check them out.
Each square in our grid is represented by a horizontal and a vertical position screengrid(horizontal,vertical) . Every square is 16x16 pixels on the screen. So the actual grid a centipede segment is on can be easily calculated by dividing the sements current x and y position by 16 and adding 1. At each of the positions in the grid, it can either have number that represents the presence of an object, or it can be 0 which means it is passable. In the top left corner of the screen, that would be an impassable area so:
screengrid(1,1)=5
And that would be the same for every grid cell for the entire left side of the screen:
horizontal=1
for vertical=1 to 30
screengrid(horizontal,vertical)=5
next vertical
You could do something similar to the right side of the screen.
Now, as you centipede moves across the screen, you test the grid square directly in front of each section, one at a time. If the area is free, you move that section forward. If the grid square has something in it, then you use your the change direction algorithm on that section of the centipede.
Once you have this working smoothly, your centipede should behave as you expect, even if it gets shot in the middle splitting it in two. Once a section is shot, it turns into a mushroom. The next section behind it would run the grid test and see that there is a mushroom on that grid square and change direction. The other sections behind it would do the same. The sections in front of the one that was shot, would not be affected and keep going in the direction they were.
Enjoy your day.