Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Snake help

Author
Message
Eisan
14
Years of Service
User Offline
Joined: 18th Sep 2010
Location:
Posted: 26th Feb 2011 04:56
Hi again everyone. Just started to code again and was having a bit of trouble trying to get a sprite to continue moving in a direction after a keypress.

For example, I press the "W" key and it keeps going up until i press ASD.

Here is my current code




Also could some one give me a quick pointer on how I would be able to continue to attach more body segments after the snake eats a "dot" and of course have the parts move in the old snake fashion?

Not asking for an entire code, just a general idea of what I should be looking at =)

Thanks!
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 26th Feb 2011 09:44
Quote: "Hi again everyone. Just started to code again and was having a bit of trouble trying to get a sprite to continue moving in a direction after a keypress."


To do that you need to separate the input from the keyboard and the movement of the player. When the player presses a key we use a variable to store the direction the player wants to go then check the direction the player is currently going and move the player in that direction.

In the following code snip the movement is only by 1 pixel at a time since I don't know exactly how big the sprite is and so you can see it before it flys off the screen.



Quote: "Also could some one give me a quick pointer on how I would be able to continue to attach more body segments after the snake eats a "dot" and of course have the parts move in the old snake fashion?

Not asking for an entire code, just a general idea of what I should be looking at =)"


A snake game usually is done in a grid with an array to store the players current position within the grid. In the grid the players starting position is the snakes current length. Anytime a new segment is added the length number is increased in the new position. When the grid is shown all numbers higher than zero within the grid are lowered. When it's done right you'll see the snake magically move it's tail around perfectly.

Eisan
14
Years of Service
User Offline
Joined: 18th Sep 2010
Location:
Posted: 3rd Mar 2011 06:44
Thanks for the reply. Sorry I didn't reply sooner, wanted to try to work it out before I posted again.

Well... I am stuck =( I tried messing around with some arrays, but I really have no idea what direction to take with it. Currently tried to see if I could get another sprite to load after it reached the food. It worked with some success, but the extra body part would appear before it even reached the food location.

As for the image sizes, everything is 16x16

Here is my current code. I added the game kill commands at the end so it would feel like i was accomplishing something! lol.



Another question as well. How would I go about having the snake move in a snapped grid fashion. Currently it is just moving the 8 pixels (Just realized I need to update that) over a continuous period, not every .x seconds.

Thanks for the help!
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 6th Mar 2011 16:52
And I'm sorry for not replying sooner... I've been sick lately. I'm still kinda sick so I'll be brief (and no code).

If the images are 16x16 you don't need an array the same size as the screen. You just need to divide the screen up x 16s to determine how big of an array you need. 800 / 16 = 50 600 / 16 = 37.5 (rounding down to a whole number 36). Make the array 49x35 (because arrays include zeros) and you'll have all the room you need for the snake game.

In a snake game it's easier to use the array to display the entire screen rather than using pasted sprites or regular sprites. Also the player doesn't actually move based on coordinates on the screen but rather moves within the array. If the player starts in the middle of the grid 25x18 and starts going up the position in the grid changes to 25x17, 25x16, 25x15, 25x14, and so on. The entire screen redrawing each step showing the players progress up the screen.

Eisan
14
Years of Service
User Offline
Joined: 18th Sep 2010
Location:
Posted: 8th Mar 2011 01:17
Starting to make a bit more sense, thanks!

Buuuut, stuck again =(. Can't seem to figure out how to proportionally get the array to fit the screen, or get the sprite to move within it

Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 8th Mar 2011 04:57
Actually to fit the 800x600 screen better it's best to reduce the images by one pixel vertically so their 16x15. If you do that the grid array should be 49x39 (50x40 adding the zeros).

It's best not to use sprites at all and use PASTE IMAGE instead to show the screen. You show the array using two FOR/NEXT loops... one for the grids vertical coordinates and one for the horizontal coordinates.

Now I don't mean to just throw this code at you but here it is... it's kinda hard to tell you the concept without revealing the entire process in code. I remed off your media to make it easier to work with but you can delete the "make media" area and unrem your loaded images.



This uses the same concept I explained earlier. The array is used to store the locations of everything and used to show the screen. When the user moves their location in the array changes and any change in the array is shown on the next loop. Wherever the player was is replaced by the number of Parts. When a user eats food the Parts variable increases. Close to the end of the loop the whole array is checked and any numbers between 1 and 999 are reduced by one so if the tail is 7 pieces long in the array it'll look like "1000 7 6 5 4 3 2 1". 1000 is the head and 7-1 is the tail. On the next loop the 1000 will turn into the total number of parts (another 7) and because the array is constantly being reduced the other numbers will be reduced and the one will disappear giving the illusion of movement with a tail.

Login to post a reply

Server time is: 2024-09-29 02:20:15
Your offset time is: 2024-09-29 02:20:15