четверг, 9 августа 2012 г.

Snake game logic

I have decided to make a snake game as my next project. I started thinking, what do I need to make a snake game work?

We will need some sort of container to keep snake bits. We will need some way to show snake on screen and control it.
So I will have three classes - GameplayLayer, Snake and Grid.
Then I started thinking, how would I keep inner structure of my snake. My first idea was following - make a two-dimensional array, Snake[x][y]. So I will have zeroes in all places where I don't have snake segment and some data (be it char or bit, or int) in the fields where I got stake segment. This seemed like a good idea, and I needed some way to know where the snake head and tail is. So two structures came into play, CGPoint head and CGPoint tail. During snake init I would set those cgpoints to head and tail. We would also need a direction where our snake is going - int direction, with enumerated values for north, south, east and west, being 0, 1, 2, 3;
It all is good and nice before we need to move. Move function will take current head position and direction and via if statements will know where to put new segment.Then we set our head structure pointing at new head. Then we find tail segment and cut it off. But here comes the problem - how do we find new tail? Easy way seems to look at the next segment based on direction, but that only works if the snake is a straight line. If it has any angles it won't work. This was a problem I could not find answer to. Eventually I did, and it was already after I implemented it another way. I would like to know your ideas how to fix that problem in this context, and I will provide answer in the next topic.
After discussing this problem with a friend we came up with a brilliant idea.

We can use NSMutable array of CGPoints. It has a method to retrieve first object and last object, and we won't need obscure struct pointers to head and tail. So it gets really simple.Well in reality you can't make an array of structs, so I wrapped each CGPoint in NSValue and store them in array. Then when I need CGpoints again, convert them back.

Another problem was showing sprites on screen. I use cocos2d, so I decided just to go with array of CCSprites.
Every draw I would take my snake array, iterate over it and for each segment show a segment at certain position.
First I wanted to have snake abstract from drawing, so snake coordinates would be in squares, like
CGPoint(0,0), CGPoint(0,1), ... CGPoint(10, 10); meaning that we will have a grid of 10 by 10 squares.
But further on when I faced difficulties with converting those points to coordinates, I just went quick and dirty way and stored actual screen coordinates, like CGPoint(10,10) incrementing each square by 20 points.
For movement I just change direction with touch on side of the screen, and snake calls move method every 0.3 seconds with given direction.
Collision detection is handled by comparing first snake segment with bounds of the screen, and if I get collision game stops. Self collisions are checked same way but with rectangle intersection.

Next step is to make it grow, and it should be trivial enough.

Комментариев нет:

Отправить комментарий