Click here to Skip to main content
15,882,017 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi, I'm working on a test for a snake game. To make the body effect for the snake I want to assign a value for every body gained after eating the point. For example I eat a point I gain a body, that body is assigned a value like 1,2,3 and so forth. Then I want to make the newly gained body follow the value 1 less then it's own. So this hopefully gives it the effects that a typical snake game has. Anyone knows how to do this?

NOTE: I'm not using XNA

thanks.
Posted
Comments
Sergey Alexandrovich Kryukov 9-Jul-12 17:19pm    
Look, this is your own design. You are explaining a part of it and ask to fix... who knows what. It does not look productive. There is no special technical "secrets" to share or hidden API to reference, this is just work to be done. If there was something you do not understand or misunderstand, or a bug, we would gladly help you, but here..? I don't think we should make this work for you. Please find out what are your concerns, then ask a question.
--SA
MR. AngelMendez 9-Jul-12 17:34pm    
ok, my question is, how can I make a custom value to a control? So If I have a picturebox how can I set some sort of value like picturebox(1). Not sure how to explain it that's why I posted my snake example.
[no name] 9-Jul-12 17:50pm    
"make a custom value to a control"... you make a custom control.
"value like picturebox(1)"... that makes no sense unless you are referring to an array element in Visual Basic or calling a function named picturebox.
85% of the narrative that you posted makes little or no sense. A snake game like you describe would lengthen the body by x amount not assign anything like 1,2,3 to anything.
MR. AngelMendez 9-Jul-12 17:55pm    
how can I lengthen the body by the x amount like you said?
[no name] 9-Jul-12 18:41pm    
How would I know that? The only thing any of us know about your project is that it's a game, you are not using XNA and it's C#. Other than that, we know absolutely nothing about what it is that you are doing.

1 solution

There are many solutions to this problem.

You want to keep it simple and easy to understand!

Try writing the pseudo code first then walk through it on paper before you code it to see if you really understand how it works. If your pseudo code does not work, then clean it up until it does work on paper. Then once you have a clear understanding on paper then you will have a better idea how to do it when you write the actual code.

* Note * If your pseudo code is wrong, then your actual code will be wrong too!

So take your time to understand the problem fully first.

---

Try defining the problem such as this:

1. Keep it simple by only allowing 4 directions. Left, Right, Up, and Down which would be designated as L, R, U, and D respectively.

2. The tail would not have a letter since it is only following.

3. If you just moved left you cannot immediately go right because you would bump into your next segment of the body part. Same goes for right to left, up to down, and down to up.

4. Use a simple array of letters in a string. Such as "ULDR" for the first 4 parts of the trail for example.

5. Limit the Snakes' length to say 50 so your character string would only allow 50 characters maximum.

Then it is just a matter of moving the head in the next direction such as Left. Then updating your character string as follows: First character in the New Direction String would be "L" then say that the previous 4 directions were "ULDR" so you would shift each one of those 1 character to the right in your character string then dropping the last character which then you would update the 4 positions to "LULD".

Lastly you would use a "loop" from 1 to last position to move less 1. (Or perchance you wouldn't need to use -1 on the move string... You will have to determine this yourself!)

Or at least that is one way to imagine doing it... Lastly you would then have to code the program.

Another good way would be to FIND an existing Snake Program and see how they did it so that you can learn something new!

Enjoy!

Bill G.
Computer Programmer / Systems Analyst / Senior Software Systems Engineer for over 34 years.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900