Next Generation Emulation banner

1 - 10 of 10 Posts

·
Shadow of nothing...
Joined
·
6,071 Posts
Discussion Starter #1
can someone please explain the lgic of a simple game of the snake ( the one where goes around eating apples). My college project is going to be a simple snake game. :)
 

·
Registered
Joined
·
1,577 Posts
You can't figure that out yourself? -_-
 

·
"Insert witty title here"
Joined
·
1,049 Posts
Piece of cake. Basically you adjust the snake's parts by changing them to the piece that comes before them. You wait for a keypress, then move the snake's head once in that direction. You would then change the coordinates of the part behind the head to the old head coordinates, change the third part to the second part's old coordinates, and so on. It'd probably be easier to do it backwards, starting with the last part first. You just wait for a delay period, and then repeat the process until a key is pressed again. Of course there's more to the game than that, but that's the basic logic for moving the snake.

Edit: Scott, I also had trouble figuring this out until a fellow programmer explained it. I then wanted to slap myself in the forehead for not realizing something so simple -_-.
 

·
Registered
Joined
·
1,577 Posts
Yup, and if the snake hits an apple, don't move the tail (allow it to stretch by one square).
 

·
Shadow of nothing...
Joined
·
6,071 Posts
Discussion Starter #5
scottlc said:
You can't figure that out yourself? -_-
I know how it's done, but I can never get it to work. I've made 4 pevious attempts, but I could never get the snake parts to follow the head :mad:
I know what to do when it comes to setting apples on the screen and turning directions and stuff, but making the snake move around like a snake is the part I have so much trouble with. It's sounds easy, but I could NEVER get it right :mad:
 

·
Shadow of nothing...
Joined
·
6,071 Posts
Discussion Starter #7
I still don't get it (i'm a stupid moron) :(
 

·
"Insert witty title here"
Joined
·
1,049 Posts
Ok, let's see...

Decide a starting point for the head of the snake. Have a number of starting pieces for the snake, something like 3. Have a variable hold the current number of snake pieces. Make an array(or whatever it's called now :p) that can hold the x and y coordinates for any number of pieces the snake could have, if the game continues until the player hits an obstacle, then something like 500. Have a variable hold something that will tell you the snake's current direction, maybe the letters "U", "D", "L", "R". While there is no input, have the game move the snake in that direction. It's easier to do this backwards, you won't need a temporary variable. Change the last snake piece's coordinates to the second to last, change the second to last to the third to last's coordinates. After you are down to the first piece(the head), move it once in the current direction. So, starting with the last piece, make the pieces follow the one that is in front of them. If the player presses up, down, left, or right, change the current direction variable. Use that to decide whether to add or subtract from the x or y coordinates of the snake's head. I hope that's understandable, I can't really say it any simpler :/.
 

·
Shadow of nothing...
Joined
·
6,071 Posts
Discussion Starter #9
Ok, the thing that i didn't understand was how to move the snake (btw I get it now, thanks). I understand the part about when to move it (ie if no input, keep moving in that direction). Anyway, my problem was I was doing it foreward, not backwards like you said. Thanks alot. :)
 
1 - 10 of 10 Posts
Top