Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A Smooth Snake Game

0.00/5 (No votes)
26 Feb 2007 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here