PDA

View Full Version : The latest snags in Snake.



Septimus
07-08-2003, 11:22 PM
Sorry if the explainations are hard to understand.

First I'll address the problem with the body disconnecting with the head:-
Reason: The procedure I have at the moment just checks if the current segment is travelling in the same direction as the previous segment. If it isn't then it continues travelling in the direction it's going until it reaches the previous segments X or Y, as required, and then changes the face the same direction as the previous segment.
Problem: If the player turns the snake twice before the next segment in line reaches the X or Y then the next segment will be travelling parallel to it and will not change direction.
What I want: I want to store each turn taken in some sort of list. The X, Y, direction change and a boolean variable so that I don't overwrite a turn that hasn't been taken yet. I was thinking of using an array (length of a hundred or something) of a record with these properties, but I thought I'd better see if there's a better way first.

Next is a problem with the editor. I want to get something kind of similar to the Age of Wonders 2 editor with the map on the left and a tabform of the different tiles to place on the right. I've messed around with a TDrawGrid but I can't figure out how to put 1 tile into each cell.

Finally I'm not sure how I'm going to impliment collision detection. I've divided the mazes up into 20x20 tiles, so I was thinking of somehow checking the kind of tile the snake's head is about to enter. Again, I'm not too sure of the best way to go about this considering the snake moves 1 pixel at a time.

Any help would be appreciated. Thanks in advance.

Sander
08-08-2003, 01:28 PM
1. Now, correct me if I'm wrong, but wouldn't it just be easier to completely ignore the "facing" property and just go for the method I proposed earlier, of going through the snake backwards, after correcting the heads position first? That would eliminate any need for a direction, except for the first head.
If you're not doing this with square segments, it could matter because it might look funny, however, that could be corrected with a small check to the placement of the previous segment to see what the direction would be, without actually storing it every time.
In any case, that should probably eliminate the problem you had, since it ALWAYS follows the next segment, and therefore cannot loose it.

2. I have no experience with DrawGrids, so no help from me here.

3. Collision detection should be done like this:
(This is assuming you have 20x20 tiles)

If (currentx/20=round(currentx/20)) OR (currenty/20=round(currenty/20)) then {you should do this with a directional check, to use the right direction, this one would just use one direction}
collision:=(Map[currentx div 20 +1, currenty div 20 +1].obstacle=true);

Alternatively, you could check whether it is IN something, instead of about to enter something, if you're doing the real snake, this would be best, since it would die if it was in something.

If (currentx/20=round(currentx/20)) OR (currenty/20=round(currenty/20)) then
collision:=(Map[currentx div 20, currenty div 20].obstacle=true);


In case you hadn't noticed yet, there is no need to check for the other segments, since there is no way they can be at a place where the head hasn't been. And you only need to check when entering a new tile, since the tile has already been checked before, thus the whole-tile check. It isn't necessary though, it's just a possibility, you could leave it out, but this would cause for at least 20 results per tile if you don't move in the middle of a tile and don't collide.
EDIT: Made code-blocks into pascal blocks.

Septimus
08-08-2003, 10:07 PM
It would be a lot simpler to work back assigning the X, Y of the previous segment, unfortunately that method doesn't work. I think the reason is because the snake only moves 1 pixel at a time, so the images all end up 1 pixel back from the previous one. A way around this would be to add the height or width of the image to the X or Y, but that would require a direction variable and probably as much headache as I've got now. I might actually work on both methods and see which one gets better results. Thinking of it now it does sound easier to do it your way.

Yeah I realised pretty early that I would only have to check for collision with the head. I'm not sure exactly what points to check tho. I was thinking of the 2 points either side of the front of the head image because just checking the X, Y would allow players to move (image.height/width -1) through walls.

I guess I should develop a little further and get more of an idea of how I'm going to fit it all together before I go asking things I'm not sure will be relevant later in the process.

cairnswm
09-08-2003, 05:15 AM
I'll do a tutorial for you on drawing in a drawgrid. Unfortunatly I dont have time today so I'll hopefully get it to you tomorrow night.

Septimus
11-08-2003, 10:41 AM
That'd be great. Alimonster has informed me of a much easier method of getting the same result from other components, but I haven't taken a good look at it yet and I'd still like to know how to work the drawgrid.