Snake Game

Submitted by: 
Visitors have accessed this post 40065 times.

This is a short and simple game program based on the mobile Snake Game popularized by the classic Nokia mobile phone models. The program demonstrates the use of Struct and Enum keywords in VB .Net 2010. The game uses the arrow keys on the keyboard.

The snake increases its length and speed by 1 square unit (10 pixels x 10 pixels) and by 4 percent of its initial speed respectively for every additional 5 points garnered in the game. Initially, the time interval for the Timer control is 50 milliseconds and 4 percent of 50 is 2, this is the value we use to decrease further the time interval making the snake to move faster.

Note:
The execution speed for the rendering of the snake does not change no matter how long the snake turns out in the game process. This is because what we do is we only add a new 'head' and remove the current 'tail' by manipulating the collection object that holds the snake's squares (square selections).


Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Comments

Good job. Perfect game. Thanks for sharing your code.

I can't believe this can be done in VB.NET easily.

You're welcome. It took me 3 hours to write the codes. I am sure it is still buggy... :-)

Lao Tzu - “Give a man a fish and you feed him for a day. Teach a man how to fish and you feed him for a lifetime.”

Oelasor

After few minutes of playing, I think the only problem is the snake does not failed when hitting the wall. Is this normal?

I designed it that way :-)... If you wish to have the game behave that way (activating wall collisions), you just have to modify this block of code:

  1. Select Case curDirection
  2. Case Direction.Downward
  3. y = y + 1
  4. If y > rowCount - 1 Then y = 0
  5. Case Direction.Leftward
  6. x = x - 1
  7. If x < 0 Then x = columnCount - 1
  8. Case Direction.Rightward
  9. x = x + 1
  10. If x > columnCount - 1 Then x = 0
  11. Case Direction.Upward
  12. y = y - 1
  13. If y < 0 Then y = rowCount - 1
  14. End Select

into this:

  1. Select Case curDirection
  2. Case Direction.Downward
  3. y = y + 1
  4. If y > rowCount - 1 Then
  5. ' Your wall collision message here
  6. End If
  7. Case Direction.Leftward
  8. x = x - 1
  9. If x < 0 Then
  10. ' Your wall collision message here
  11. End If
  12. Case Direction.Rightward
  13. x = x + 1
  14. If x > columnCount - 1
  15. ' Your wall collision message here
  16. End If
  17. Case Direction.Upward
  18. y = y - 1
  19. If y < 0 Then
  20. ' Your wall collision message here
  21. End If
  22. End Select

I am sorry I should have included some comments on the codes. Anyways, please feel free to post your queries here... I believe this site is very accommodating and always 'active' :-)...On my part... I am most willing to be of help...

Lao Tzu - “Give a man a fish and you feed him for a day. Teach a man how to fish and you feed him for a lifetime.”

Oelasor

Hi,

Thanks for the code. I can't believe you finished the program within 3 hours. I hope I can do the same in the future.

I'm still learning different languages today and I like vb.net.

Trust me...it's not that hard :-) ... maybe my typing speed contributes... hehehe. Boasting aside, I can develop a complete system within a week or so, provided it is well-documented and complete with diagrams and other significant details... OOP (Object-Oriented Programming) and COP (Component-Oriented Programming like .NETs) approaches in programming help a lot...Things become easy when done passionately...

Lao Tzu - “Give a man a fish and you feed him for a day. Teach a man how to fish and you feed him for a lifetime.”

Oelasor

I really wanted to learn OOP thats why I am studying it every day.And by the way what is COP? I just heard it from you...

I did not invent the term COP :).. try Google, there is a lot of info about COP over the Internet... By the way, this small program is not written that way. I chose not to implement the OOP or COP approach since it is just that 'small'... anyways, if one wanted to use the OOP or COP approach regardless how small the project is, it's his personal discretion. We can choose to be 'cheap' sometimes...

Lao Tzu - “Give a man a fish and you feed him for a day. Teach a man how to fish and you feed him for a lifetime.”

Oelasor

Hey, its our first first time jerk.

i think it is a nice game

Hi,
i find it difficult to understand. So i need some comments to undersatnd it.

Can i ask something ? i'm a programmer student , i just want to know how to make a games in VB . Who can give me a code ? tnx :D

how to download ? :(

how to download this game?

Hello, i see in your code we cannot change the snake speed. However, is there a way to make a kind of "level difficulty" by change the speed or it's impossible ?

Note: Sorry for my english, it is not my first language :)

The snake's speed is automatically updated/incremented for every five (5) points you earn in the play. On the other hand, you can design the program to use a difficulty level using the speed, that is, using the Timer's Interval value.

Lao Tzu - “Give a man a fish and you feed him for a day. Teach a man how to fish and you feed him for a lifetime.”

Oelasor

You made all program on 22 lines?

Just as the title says, It crashes at 200% Snake speed. It says TMR.Interval cannot be less than 0.

Thanks for sharing that info... haven't tried the code in that speed. Somehow, to resolve that is to use a variable to update the Timer's Interval value instead of directly modifying it, thus avoiding values less than zero (0).

Lao Tzu - “Give a man a fish and you feed him for a day. Teach a man how to fish and you feed him for a lifetime.”

Oelasor

thanks for source code it is very benifitial for me

You are most welcome... :)

Lao Tzu - “Give a man a fish and you feed him for a day. Teach a man how to fish and you feed him for a lifetime.”

Oelasor

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.