Click here to Skip to main content
Licence CPOL
First Posted 26 Feb 2007
Views 39,828
Downloads 1,771
Bookmarked 16 times

A Smooth Snake Game

By | 26 Feb 2007 | Article
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)

About the Author

atul_m_ojha

Web Developer

India India

Member

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 PinmemberZyethetican15:11 29 Mar '12  
Questionreply PinmemberPricess Chissai15:00 11 Oct '11  
Questionrepy PinmemberPricess Chissai15:00 11 Oct '11  
GeneralGood Game PinmemberNeeraj_Saluja22:10 26 Feb '07  
GeneralRe: Good Game Pinmemberatul_m_ojha7:59 27 Feb '07  

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.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 27 Feb 2007
Article Copyright 2007 by atul_m_ojha
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid