Click here to Skip to main content
13,559,184 members
Click here to Skip to main content
Add your own
alternative version


16 bookmarked
Posted 26 Feb 2007
Licenced CPOL

A Smooth Snake Game

, 26 Feb 2007
Rate this:
Please Sign up or sign in to vote.
SIngle Player Game in VB.NET
Screenshot - screenshot.gif


EveryOne must have played this Game .I made this in 2 days in office during free time.Its a simple Game in VB.NET which uses all beginer level classes only.No Special Information is required prior to running Game


I saw many version before making this one.Most them use Rectangle Blocks or Circles for movement,Thats not the standard way.SO i used DrawLine() with pixel accuracy of 1,to get such smoothness

Input & Outputs

No special Input and Output is required,Users can change the Level of playing by selecting Levels,also Soon new version with Snake Color and Width selection facility"ll also be available

Output is shown directly on screen as per the KeyPress() movements

<pre lang=vbnet>

If Not (MenuItem3.Text = "Resume") Then

Timer1.Interval = int_timer1 : Timer2.Interval = int_timer2 : Timer1.Start()

If (e.KeyCode = Keys.Down) And ((prev = "up") Or (prev = "right") Or (prev = "left") Or (prev = "")) Then keypressed = "down"If (e.KeyCode = Keys.Up) And ((prev = "down") Or (prev = "") Or (prev = "right") Or (prev = "left")) Then keypressed = "up"If (e.KeyCode = Keys.Right) And ((prev = "left") Or (prev = "") Or (prev = "up") Or (prev = "down")) Then keypressed = "right"If (e.KeyCode = Keys.Left) And ((prev = "right") Or (prev = "") Or (prev = "up") Or (prev = "down")) Then keypressed = "left"

End If


I use the DrawLine()function for drawing snake.Also a second line starts immediately from behind after sometime.This line is used for erasing the Snake from rear end

<pre lang=vbnet>

Public snake_erase As Pen = New Pen(Color.Black, 2)

Me.CreateGraphics.DrawLine(snake_erase, X(value), Y(value), X(value + 1), Y(value + 1))



Food :

Food generation was the easier portion,I have used System.Random() for generating Random Food particles as per Form Width and Height so One can play this with any Size.

<pre lang=vbnet>
foodx = RandomNumber.Next(20, Me.Width - 60)
foody = RandomNumber.Next(20, Me.Height - 60)
Me.CreateGraphics.DrawRectangle(food, foodx, foody, 2, 2)

I have used accuracy of +-1 & +-2 so no problem should be there

Increasing Length:

Length is increased juz by decreasing length of the erasing snake coming from rear end,I have used a valriable value for that.


Major constraint is the Array size of X() and Y() presently I have set them to 5,00,000 (Enuf for playing upto 5-6 hrs) But one can write his own Array erasing fucntion

Collision Detection :

Collision Detection between Food and Snake has been done by matching the Snake(x,y) and Food (x,y) co_ordinates,If Both are equal then Length of snake is increased and New Food is shown Randomly

Conclusion :

It was a nice experience making this Game.If You have any trouble One can contact me at



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


About the Author

Web Developer
India India
Atul Ojha is currently Completing his Computer Engineering (8th Sem),Has Worked on ASP,ASP.NET,VB.NET,C#,WML/WMLS,Shell Scripting,SQL Server,MySQL,JSP,

Somewhat in Assembly Language also.
Interests : working for Long Hours on PC,Playing PC Games,Watching F1 and ofcourse sleeping Smile | :)

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
Zyethetican29-Mar-12 15:11
memberZyethetican29-Mar-12 15:11 
Questionreply Pin
Pricess Chissai11-Oct-11 15:00
memberPricess Chissai11-Oct-11 15:00 
Questionrepy Pin
Pricess Chissai11-Oct-11 15:00
memberPricess Chissai11-Oct-11 15:00 
GeneralGood Game Pin
Neeraj_Saluja26-Feb-07 22:10
memberNeeraj_Saluja26-Feb-07 22:10 
GeneralRe: Good Game Pin
atul_m_ojha27-Feb-07 7:59
memberatul_m_ojha27-Feb-07 7:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01-2016 | 2.8.180515.1 | Last Updated 27 Feb 2007
Article Copyright 2007 by atojha
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid