Computer Science Canada Help with Snake Game |
| Author: | josh [ Tue Jan 11, 2005 6:31 pm ] | ||
| Post subject: | Help with Snake Game | ||
Hey, I am making a snake game for my FP with my friend. I have gotten it so that the snake moves continuoulsy in the direction last hit by the user, it can eat an orange, and get bigger, however it does not get bigger properly and I can't get it so that when you turn just he front of the snake bends and hte back follows the path. Me and my compsci teacher are stumped as to what we need to do. Here is the code:
a few things my teacher helped me with I don't understand 100% but most of it I do. |
|||
| Author: | Cervantes [ Tue Jan 11, 2005 8:13 pm ] |
| Post subject: | |
I hope you realize that's not what Snake is supposed to look like. Are you trying to go for this, new, style of Snake? Also, Snake is supposed to work on a grid, such that each "body part" of the snake is the size of one grid square. Right now, you seem to be doing it without a grid (I guess you could say you're grid size is 1). There's some Snake games on compsci.ca, search for them. There's also a ton of help threads regarding snake right now, they might contain useful inforamtion. If not, let us know |
|
| Author: | josh [ Tue Jan 11, 2005 8:16 pm ] |
| Post subject: | |
It does not look like snake because It is not working yet. it can't get the snake to bend properly and follow the head, and I can't figure out why both sides of the square get larger |
|
| Author: | Cervantes [ Tue Jan 11, 2005 9:09 pm ] | ||
| Post subject: | |||
When in doubt, refer to Zylum's source.
Also, note the use of new. He's using a flexible array (in conjunction with records) to get the job done. You should probably do the same, because, otherwise, you'll get an error when the snake gets more than 15 parts (you've got your arrays 1 .. 15, currently). The whole thread can be found here: |
|||
| Author: | josh [ Tue Jan 11, 2005 9:19 pm ] |
| Post subject: | |
I actually just finished looking at that and I have sent him a PM. I also just read the tutorial on records a few hours ago and was considering htem using them, adnd I also read the one on flexibl arrays. Tomoorow I am gunna try and start from scratch to get one that works on a grid system. I just don't undersntandt he part in zylums codenear the end witht the three drawfillbox's. |
|
| Author: | Amreen [ Wed Jan 12, 2005 9:32 am ] |
| Post subject: | |
kool |
|
| Author: | josh [ Wed Jan 12, 2005 2:09 pm ] |
| Post subject: | |
uh...not really but thanx n e way. I have totally re-wrote it with my partner, it still does not look like the traditional snake (it is not on a grid) but it will be o.k. for handing in. I am definnatly gunna try and make an actual good version of snake when i don't have the time constraints imposed by school. The only thing I am having trouble with is the collision detection because they are circles and they are not on a grid. |
|
| Author: | Cervantes [ Wed Jan 12, 2005 6:45 pm ] | ||||
| Post subject: | |||||
Circle collision is easy.
As for the three drawfillboxes in zylums code: the first is for drawing that green background; the second is for drawing the food; the third is for drawing the snake. What's probably confusing you is all the variables inside the draw commands. Really, it's rather simple. Just understand that the variables for the locations of the snake / food etc. are very small, like 1 to 20 or something. So, he multiplies it by a constant (gridData.width) to, essentially, enlarge it. Take this fully customizable grid:
|
|||||
| Author: | josh [ Wed Jan 12, 2005 6:48 pm ] |
| Post subject: | |
thanks for the help cervantes. It was the variables that where confusing me, what is the advantage though of using smaller variables and multiplying by a constant? I have never used the Math functions before so I was going to use the method that tony wrote about in a tutorial in the turing section, but this looks alot easier |
|
| Author: | Cervantes [ Wed Jan 12, 2005 7:14 pm ] |
| Post subject: | |
The advantage of using smaller variables and multiplying by a constant: 1.) It's easier to understand, for yourself. Would you rather be thinking about 175 being one grid more than 150, or 7 being one grid space more than 6. 2.) It's easier for comparasin's, for collision. Same reason as above. If you are going for the grid style, as opposed to the grid size one style (that you had at first), you're going to either store information in terms of large numbers and divide by a constant to compare them (or compare them as is, but that's annoying too), or store information as small numbers, compare them easily, and then multiply by a constant to draw them to the screen. It's somewhat hard to explain. I hope I did a semi-decent job -Cervantes |
|
| Author: | cool dude [ Wed Jan 12, 2005 7:15 pm ] | ||
| Post subject: | |||
well, well, well, always wanting to go your way josh (lol). i told u thats not how snake looks and we should go my way.
the only problem with this is we can't make it eat the actual orange. i know we have to use a grid but what would be better to use whatdotcolour or collision detection. josh and i are arguing about that. as well how would you use those because we tried both but we can't really get it to work. |
|||
| Author: | Neo [ Wed Jan 12, 2005 7:24 pm ] |
| Post subject: | |
Whatdotcolour is a form of collision detection. If your going to use circles I'd suggest using circle collision detection using Math.Distance(). Get the distance from the food to the snake's head. If the disttance is less than their radiuses combined then eat the food. |
|
| Author: | Cervantes [ Wed Jan 12, 2005 7:34 pm ] |
| Post subject: | |
Neo wrote: If your going to use circles I'd suggest using circle collision detection using Math.Distance()
Actually, I would recommend using location comparisons. It's just as easy, and if you ever decide to use squares instead of ovals, it'll be just a matter of changing the draw commands. |
|
| Author: | josh [ Wed Jan 12, 2005 8:02 pm ] | ||
| Post subject: | |||
as you probobly guessed cooldude is my partner. I tried just changing the draw command but it really messed it up, so there is more to it than that. I like using hte math.function as it seems really simple and it will work. I would use a gird, but we don't have much time to do this and I don't want to go and re-write the whole code again.... Cervantes what do you mena by location comparisons? i you mean like this:
|
|||
| Author: | josh [ Wed Jan 12, 2005 9:25 pm ] | ||
| Post subject: | |||
o.k. so I got the collisoin detectoin working and now the snake grows also but for some reason after it eats an orange the screen sometimes just randomly goes white. Can n e 1 figure out why? here is the new code:
EDIT: I have been looking at this some more and I am wonderng, is it that the new orange is being generated ontop of the new circle being added to the snake? |
|||
| Author: | josh [ Thu Jan 13, 2005 9:10 am ] |
| Post subject: | |
I have figured out that it is not entirely random when it just blanks out. When I grab an orange by turning into it so that at least one circle anywhere on the snakes body is not inline with its head, it whites out. I have no idea why it does this, can some1 please help me with that. THanks a bunch guys |
|