snake game with arrays - help help

Started by
1 comment, last by dmatter 17 years ago
Hey there... I just made a snake game, where each "part" of the snake is a glRect with the coordinates: (snake.x,snake.y) where i is the number of the "part". Since the screen is 30 "pieces" wide and 20 high, I will require an array of 600 (struct snake snakes[600]), where the snake struct holds both x and y. What I want to know is if there is a uppersize limit to what size an array can be. If snakes[1000] will work? And is there a better way of handling this? thanks..
Advertisement
In practice, it is not possible to have all 600 tiles of the gameboard filled up with snake at the same time. The player dies long before this happens.
I guess in theory, a very good player could weave the snake into a giant double spiral or other looped shape that covers the whole board, but at this point its not much of a game anymore...
Most Snake games usually have a set limit for the longest the snake ever grows to keep things reasonable.


Also, if you want a more memory effient way of doing things.
... how are you representing your gameboard? because right now your snake is the same size as the whole board... you could combine the snake and board into a single data structure if you wanted

Or, rather than using an array for the snake you could consider a linkedlist
As far as im aware when you define an array the size of the array is usually a 4-byte unsigned int which (i think) has a maximum upper value of 4294967295 so therefore that would be the largest size array you could allocate?!

If you allocate more memory than can fit into your RAM then your operating system should page memory out to your harddrive, so in theory you could allocate pretty much as much as you want [smile].
There may well be a maximum memory footprint that the operating system will permit a program to have, but i expect most programs/programmers will never get anywhere near that.

snakes[1000] really should be fine, if struct snake contains only two floats then thats probably only
(2 * 4bytes) * 1000 = 8000 bytes or 8000/1024 = 7.8 Kb

This topic is closed to new replies.

Advertisement