Introduction to snake game:
Snake game is a computer video game and a classic one in which a player moves a snake whose food is displayed at various points on screen and if snake hits boundary wall of screen game overs or even if snake hits itself making a closed loop.
Our snake game will look something like this
C# Snake Game:
Now to make a snake game in C# we need to know following things.
1). How to draw on form in C#
2). How to move a drawn object in C#
3). How to detect,access and process what key user hits.
4). Game logic
Step 1: How to Draw on form in C#:
You can draw on form in C# very easily first you need to declare a graphics object in form designer under public class partial form: form1 as shown below
namespace snake_game
{
public partial class Form1 : Form
{
Graphics paper;
Here we defined a graphic object named paper. Now this paper is our canvas basically on which we are going to draw our snake and its food.
Drawing Snake:
Snake can be thought of as a rectangle shaped object with some width and height. Its width changes but its height remains same now to do it we draw on our paper named graphic object or canvas a rectangle giving it some width and some height. We draw our snake and its food by calling some functions of drawing also made by us in paint event of a form as shown below.
private void Form1_Paint(object sender, PaintEventArgs e)
{
paper = e.Graphics;
food.drawFood(paper);
snakes.drawSnake(paper);
}
Now their are two methods that we made and gave our paper as their arguments real drawing of objects is done in these methods. We have two classes one for each(food and snake). In food class we draw food using a random number generator and calling fillrectangle() function.
public class Food
{
private int x, y, width, height;
private SolidBrush brush;
public Rectangle foodRec;
public Food(Random randFood)
{
x = randFood.Next(0, 29) * 10;
y = randFood.Next(0, 29) * 10;
brush = new SolidBrush(Color.Black);
width = 10;
height=10;
foodRec = new Rectangle(x, y, width, height);
}
public void foodlocation(Random randFood)
{
x = randFood.Next(0, 29) * 10;
y = randFood.Next(0, 29) * 10;
}
public void drawFood(Graphics paper)
{
foodRec.X = x;
foodRec.Y = y;
paper.FillRectangle(brush,foodRec);
}
}
Similarly snake is drawn using drawSnake() function we made in snake class.
namespace snake_game
{
public class snake
{
private Rectangle[] snakeRec;
private SolidBrush brush;
private int x, y, width, height;
public Rectangle[] SnakeRec
{
get { return snakeRec; }
}
public snake()
{
snakeRec = new Rectangle[3];
brush = new SolidBrush(Color.Black);
x = 20;
y = 0;
width = 10;
height = 10;
for (int i = 0; i < snakeRec.Length; i++)
{
snakeRec[i] = new Rectangle(x, y, width, height);
x -= 10;
}
}
public void drawSnake(Graphics paper)
{
foreach (Rectangle rec in snakeRec)
{
paper.FillRectangle(brush, rec);
}
}
public void drawSnake()
{
for (int i = snakeRec.Length - 1; i > 0; i--)
{
snakeRec[i] = snakeRec[i - 1];
}
}
public void moveDown()
{
drawSnake();
snakeRec[0].Y += 10;
}
public void moveUp()
{
drawSnake();
snakeRec[0].Y -= 10;
}
public void moveLeft()
{
drawSnake();
snakeRec[0].X -= 10;
}
public void moveRight()
{
drawSnake();
snakeRec[0].X += 10;
}
public void growSnake()
{
List
rec.Add(new Rectangle(snakeRec[snakeRec.Length-1].X,snakeRec[snakeRec.Length-1].Y,width,height));
snakeRec = rec.ToArray();
}
}
}
This was all about drawing now we turn to Step 2.
Step 2: How to move a drawn object in C#
For moving we need a timer which can be found in the inventory or toolbox on left side drag it and place it on form to get a timer. Now we want to move our snake at each timer's tick we do this as shown below.
private void timer1_Tick(object sender, EventArgs e)
{
snakeScoreLabel.Text = Convert.ToString(score);
if (down) { snakes.moveDown(); }
if (up) { snakes.moveUp(); }
if (right) { snakes.moveRight(); }
if (left) { snakes.moveLeft(); }
this.Invalidate();
collision();
for (int i = 0; i < snakes.SnakeRec.Length; i++)
{
if (snakes.SnakeRec[i].IntersectsWith(food.foodRec))
{
score += 1;
snakes.growSnake();
food.foodlocation(randFood);
}
}
}
Here we made a timer tick event and set its time and redrawn snake to its new location by just adjusting and processing coordinates.
Step 3: How to detect,access and process what key user hits
To detect and access what user press for moving snake like up key down key left or right key we go to our form and go to its events window on right side and double click on "key down event" and we entered in to event coding as shown below and code following code.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Space)
{
timer1.Enabled = true;
codesmeshlabel.Text= "";
spaceBarLabel.Text = "";
down = false;
up = false;
left = false;
right = true;
}
if (e.KeyData == Keys.Down && up == false)
{
down = true;
up = false;
right = false;
left = false;
}
if (e.KeyData == Keys.Up && down == false)
{
down = false;
up = true;
right = false;
left = false;
}
if (e.KeyData == Keys.Left && right == false)
{
down = false;
up = false;
right = false;
left = true;
}
if (e.KeyData == Keys.Right && left == false)
{
down = false;
up = false;
right = true;
left = false;
}
}
Step 4: Game Logic
Now we move to game logic. So far we have discussed the drawing of snake and its moving now we need to set this that if snake hits any of the four walls of form its should say "game over". To do this we made a collision function as shown below.
public void collision()
{
for (int i = 1; i < snakes.SnakeRec.Length; i++)
{
if (snakes.SnakeRec[0].IntersectsWith(snakes.SnakeRec[1]))
{
restart();
}
}
if (snakes.SnakeRec[0].X < 0 || snakes.SnakeRec[0].X > 290)
{
restart();
}
if (snakes.SnakeRec[0].Y < 0 || snakes.SnakeRec[0].Y > 290)
{
restart();
}
}
That's it! So you made a snake game of your own make it play it and enjoy it. For any further queries feel free to comment below it will be appreciated. To download a complete working snake game code click here