Jump to content
  • Advertisement
Sign in to follow this  
madsravn

snake game with arrays - help help

This topic is 4254 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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..

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!