Click here to Skip to main content
15,884,298 members
Articles / Programming Languages / Visual Basic

A Smooth Snake Game

Rate me:
Please Sign up or sign in to vote.
2.59/5 (11 votes)
26 Feb 2007CPOL2 min read 89.6K   5.1K   17   5
SIngle Player Game in VB.NET
Screenshot - screenshot.gif

Introduction

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

Purpose

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

</pre>

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))

value=value+1

</pre>

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)
</pre>

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.

Constraints:

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 atul_m_ojha@yahoo.co.in

Thanks,

License

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


Written By
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 | :)

Comments and Discussions

 
GeneralMy vote of 1 Pin
Zyethetican29-Mar-12 15:11
Zyethetican29-Mar-12 15:11 
Questionreply Pin
Pricess Chissai11-Oct-11 15:00
Pricess Chissai11-Oct-11 15:00 
Questionrepy Pin
Pricess Chissai11-Oct-11 15:00
Pricess Chissai11-Oct-11 15:00 
GeneralGood Game Pin
Neeraj Saluja26-Feb-07 22:10
Neeraj Saluja26-Feb-07 22:10 
GeneralRe: Good Game Pin
atojha27-Feb-07 7:59
atojha27-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.