//form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace Snake_Game
{
public partial class Form1 : Form
private int score = 0;
private Keys direction;
private Keys arrow;
private Point LastLocation;
private Food MyFood;
private Snake MySnake;
private Bitmap ScreenBitmap;
private Graphics BitmapPicture;
private Graphics ScreenPage;
public Form1()
direction = Keys.Left;
arrow = direction;
MySnake = new Snake();
MyFood = new Food();
ScreenBitmap = new Bitmap(300, 300);
LastLocation = MySnake.Location[MySnake.Length - 1];
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
private void timer1_Tick(object sender, EventArgs e)
switch (direction)
case Keys.Left:
MySnake.Left();
break;
case Keys.Right:
MySnake.Right();
case Keys.Up:
MySnake.Up();
case Keys.Down:
MySnake.Down();
BitmapPicture.Clear(Color.White);
SolidBrush Mybush = new SolidBrush(Color.Black);
BitmapPicture.FillEllipse(Mybush, (MyFood.Location.X * 10), (MyFood.Location.Y * 10), 10, 10);
bool gameover = false;
for (int i = 0; i < MySnake.Length; i++)
SolidBrush Mybrush = new SolidBrush(Color.Brown);
BitmapPicture.FillRectangle(Mybrush, (MySnake.Location[i].X * 10), (MySnake.Location[i].Y * 10), 10, 10);
if ((MySnake.Location[i] == MySnake.Location[0]) && (i > 0))
gameover = true;
ScreenPage.DrawImage(ScreenBitmap, 0, 0);
CheckSnake();
if (gameover == true)
GameOver();
//Invalidate();
private void StartToolStripMenuItem_Click(object sender, EventArgs e)
StartGame();
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
if (BitmapPicture != null)
BitmapPicture.Dispose();
if (ScreenPage != null)
ScreenPage.Dispose();
Close();
protected override void OnKeyDown(KeyEventArgs e)
if ((e.KeyCode == Keys.Left) ||
(e.KeyCode == Keys.Right) ||
(e.KeyCode == Keys.Up) ||
(e.KeyCode == Keys.Down))
//arrow = e.KeyCode;
base.OnKeyDown(e);
private void CheckSnake()
if (MySnake.Location[0] == MyFood.Location)
MySnake.AddLenght();
MySnake.Location[MySnake.Length - 1] = LastLocation;
score += 10;
ScoreLabel.Text = score.ToString();
CreateFood();
private void CreateFood()
bool fullCell;
do
MyFood.CreateFood();
fullCell = false;
if (MyFood.Location == MySnake.Location[i])
fullCell = true;
while (fullCell == true);
private void StartGame()
MySnake.Reset();
StartToolStripMenuItem.Enabled = false;
//direction = Keys.Left;
timer1.Interval = 100;
ScoreLabel.Text="0";
score = 0;
BitmapPicture = Graphics.FromImage(ScreenBitmap);
ScreenPage = GamePage.CreateGraphics();
timer1.Enabled = true;
public void GameOver()
timer1.Enabled = false;
StartToolStripMenuItem.Enabled = true;
string Message1 = "Your Score:" + score + "\nDo you want to play again?";
if (MessageBox.Show(Message1, "GameOver", MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.Yes)
else
Application.Exit();
public void Winner()
if (score >= 150)
MessageBox.Show("you are wine" + score);
private void pictureBox1_Click(object sender, EventArgs e)
private void fileToolStripMenuItem1_Click(object sender, EventArgs e)
//Class food
class Food
private Point _location;
public Point Location
get { return _location;}
public Food()
_location = new Point();
public void CreateFood()
Random rndFood = new Random();
_location = new Point(rndFood.Next(0, 30), rndFood.Next(0, 30));
//class Snake
class Snake
private int _length;
public int Length
get { return _length; }
set { _length = value; }
private Point[] _location;
public Point[] Location
get { return _location; }
public Snake()
_location = new Point[30 * 30];
Reset();
public void Reset()
_length = 5;
for (int i = 0; i < _length; i++)
_location[i].X = 14;
_location[i].Y = 14;
//for move snake
private void Move()
for (int i = _length - 1; i > 0; i--)
_location[i] = _location[i - 1];
public void Up()
Move();
_location[0].Y--;
if (_location[0].Y < 0)
_location[0].Y += 30;
public void Down()
_location[0].Y++;
if (_location[0].Y > 29)
_location[0].Y -= 30;
public void Left()
_location[0].X--;
if (_location[0].X < 0)
_location[0].X += 30;
public void Right()
_location[0].X++;
if (_location[0].X > 29)
_location[0].X -= 30;
public void AddLenght()
_length++;