Click here to Skip to main content
Click here to Skip to main content

A Smooth Snake Game

, 26 Feb 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

atojha
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 PinmemberZyethetican29-Mar-12 16:11 
Questionreply PinmemberPricess Chissai11-Oct-11 16:00 
Questionrepy PinmemberPricess Chissai11-Oct-11 16:00 
GeneralGood Game PinmemberNeeraj_Saluja26-Feb-07 23:10 
GeneralRe: Good Game Pinmemberatul_m_ojha27-Feb-07 8:59 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150326.1 | Last Updated 27 Feb 2007
Article Copyright 2007 by atojha
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid