Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / Centipede

Author
Message
Saml92
13
Years of Service
User Offline
Joined: 10th Mar 2011
Location: swansea
Posted: 16th Mar 2011 14:44
I am creating centipede for an assignment but am unsure how to get the centipede to hit the side of the screen move down and then carry on moving in the opposite direction i have got it to move down and change direction but i want to do it the way the centipede game does this.

This is what i have so far



speed = 0.02
Left = 0
Right = 0

DirCX = 0

centipedexpos=centipedexpos+(Speed*DirCX)
centipedexpos=centipedexpos+(Speed*DirCXR)

if centipedexpos <= 312
centipedeypos = centipedeypos + 30
centipedexpos = centipedexpos + 30
Speed = speed + 0.5
dirCXR =2
DirCX = 0
Left = 0
Right = 1
endif
if centipedexpos>= 769
centipedeypos = centipedeypos + 30
centipedexpos = centipedexpos - 30
Speed = speed + 1
Left = 1
Right = 0
dirCXR =0
DirCX =-2
endif

if left = 1
sprite 987,centipedexpos-154 ,centipedeypos,5
sprite 997,centipedexpos-11,centipedeypos,15
endif
if Right = 1
sprite 997,centipedexpos-11,centipedeypos,25
sprite 987,centipedexpos-154 , centipedeypos,15
endif
if SPRITE EXIST(987) = 0 and left = 1
sprite 988,centipedexpos-138,centipedeypos,5
else
sprite 988,centipedexpos-138,centipedeypos,6
endif

sprite 989,centipedexpos-124,centipedeypos,7
sprite 990,centipedexpos-109,centipedeypos,8
sprite 991,centipedexpos-95,centipedeypos,9
sprite 992,centipedexpos-81,centipedeypos,10
sprite 993,centipedexpos-67,centipedeypos,11
sprite 994,centipedexpos-53,centipedeypos,12
sprite 995,centipedexpos-39,centipedeypos,13
sprite 996,centipedexpos-25,centipedeypos,14
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 19th Mar 2011 12:37 Edited at: 19th Mar 2011 12:48
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:



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.
Saml92
13
Years of Service
User Offline
Joined: 10th Mar 2011
Location: swansea
Posted: 30th Mar 2011 16:57
Thanks for your reply it helped me a lot

Login to post a reply

Server time is: 2024-03-29 08:37:33
Your offset time is: 2024-03-29 08:37:33