Snake in Mono Winforms

Course- C# >

In this part of the Mono Winforms programming tutorial, we will create a Snake game clone.

Snake game

Snake is an older classic video game. It was first created in late 70s. Later it was brought to PCs. In this game the player controls a snake. The objective is to eat as many apples as possible. Each time the snake eats an apple, its body grows. The snake must avoid the walls and its own body. This game is sometimes called Nibbles.

Development

The size of each of the joints of a snake is 10px. The snake is controlled with the cursor keys. Initially the snake has three joints. The game is started by pressing one of the cursor keys. If the game is finished, we display "Game Over" message in the middle of the Board.

Board.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;



public class Board : UserControl {

    private const int WIDTH = 300;
    private const int HEIGHT = 300;
    private const int DOT_SIZE = 10;
    private const int ALL_DOTS = 900;
    private const int RAND_POS = 27;

    private int[] x = new int[ALL_DOTS];
    private int[] y = new int[ALL_DOTS];

    private int dots;
    private int apple_x;
    private int apple_y;

    private bool left = false;
    private bool right = true;
    private bool up = false;
    private bool down = false;
    private bool inGame = true;

    private Timer timer;

    private Bitmap dot;
    private Bitmap apple;
    private Bitmap head;

    private IContainer components;
    
    public int BORDER_WIDTH;
    public int TITLEBAR_HEIGHT;

    public Board() {

        components = new Container();
        BackColor = Color.Black;
        DoubleBuffered = true;
        this.ClientSize = new Size(WIDTH, HEIGHT);

        try {
            dot = new Bitmap("dot.png");
            apple = new Bitmap("apple.png");
            head = new Bitmap("head.png");

        } catch (Exception e) {
            Console.WriteLine(e.Message);
            Environment.Exit(1);
        } 

        initGame();
    }

    private void OnTick(object sender, EventArgs e) {

        if (inGame) {
            checkApple();
            checkCollision();
            move();
        }
        this.Refresh();
    }


    private void initGame() {

        dots = 3;

        for (int z = 0; z < dots; z++) {
            x[z] = 50 - z * 10;
            y[z] = 50;
        }

        locateApple();
        KeyUp += new KeyEventHandler(OnKeyUp);


        timer = new Timer(this.components);
        timer.Enabled = true;
        timer.Interval = 100;
        timer.Tick += new System.EventHandler(this.OnTick);

        Paint += new PaintEventHandler(this.OnPaint);


    }


    private void OnPaint(object sender, PaintEventArgs e) {

        Graphics g = e.Graphics;

        if (inGame) {

            g.DrawImage(apple, apple_x, apple_y);

            for (int z = 0; z < dots; z++) {
                if (z == 0) {
                    g.DrawImage(head, x[z], y[z]);
                } else {
                    g.DrawImage(dot, x[z], y[z]);    
                }
            }

        } else {
            gameOver(g);
        }
    }


    private void gameOver(Graphics g) {

        String msg = "Game Over";
        StringFormat format = new StringFormat();
        format.Alignment = StringAlignment.Center;
        format.LineAlignment = StringAlignment.Center;
        
        g.DrawString(msg, Font, Brushes.White, ClientRectangle, format);
        timer.Stop();
    }


    private void checkApple() {

        if ((x[0] == apple_x) && (y[0] == apple_y)) {
            dots++;
            locateApple();
        }
    }

    private void move() {

        for (int z = dots; z > 0; z--) {
            x[z] = x[(z - 1)];
            y[z] = y[(z - 1)];
        }

        if (left) {
            x[0] -= DOT_SIZE;
        }

        if (right) {
            x[0] += DOT_SIZE;
        }

        if (up) {
            y[0] -= DOT_SIZE;
        }

        if (down) {
            y[0] += DOT_SIZE;
        }
    }

    private void checkCollision() {

        for (int z = dots; z > 0; z--) {
            if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) {
                inGame = false;
            }
        }

        if (y[0] > HEIGHT - DOT_SIZE - TITLEBAR_HEIGHT - BORDER_WIDTH) {
            inGame = false;
        }

        if (y[0] < 0) {
            inGame = false;
        }

        if (x[0] > WIDTH - DOT_SIZE - 2 * BORDER_WIDTH) {
            inGame = false;
        }

        if (x[0] < 0) {
            inGame = false;
        }
    }

    private void locateApple() {
        Random rand = new Random();
        int r = (int)(rand.Next(RAND_POS));
        apple_x = ((r * DOT_SIZE));
        r = (int)(rand.Next(RAND_POS));
        apple_y = ((r * DOT_SIZE));
    }


    private void OnKeyUp(object sender, KeyEventArgs e) {

        int key = (int) e.KeyCode;

        if ((key == (int) Keys.Left) && (!right)) {
            left = true;
            up = false;
            down = false;
        }

        if ((key == (int) Keys.Right) && (!left)) {
            right = true;
            up = false;
            down = false;
        }

        if ((key == (int) Keys.Up) && (!down)) {
            up = true;
            right = false;
            left = false;
        }

        if ((key == (int) Keys.Down) && (!up)) {
            down = true;
            right = false;
            left = false;
        }
    }
}

First we will define the constants used in our game.

The WIDTH and HEIGHT constants determine the size of the Board. The DOT_SIZE is the size of the apple and the dot of the snake. The ALL_DOTS constant defines the maximum number of possible dots on the Board. (900 = 300*300/10*10) The RAND_POS constant is used to calculate a random position of an apple. The DELAY constant determines the speed of the game.

private int[] x = new int[ALL_DOTS];
private int[] y = new int[ALL_DOTS];

These two arrays store x, y coordinates of all joints of a snake.

In the move() method we have the key algorithm of the game. To understand it, look at how the snake is moving. You control the head of the snake. You can change its direction with the cursor keys. The rest of the joints move one position up the chain. The second joint moves where the first was, the third joint where the second was etc.

for (int z = dots; z > 0; z--) {
    x[z] = x[(z - 1)];
    y[z] = y[(z - 1)];
}

This code moves the joints up the chain.

if (left) {
    x[0] -= DOT_SIZE;
}

Move the head to the left.

In the checkCollision() method, we determine if the snake has hit itself or one of the walls.

for (int z = dots; z > 0; z--) {

    if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) {
        inGame = false;
    }
}

We finish the game if the snake hits one of its joints with the head.

if (y[0] > HEIGHT - DOT_SIZE - TITLEBAR_HEIGHT - BORDER_WIDTH) {
    inGame = false;
}

We finish the game if the snake hits the bottom of the Board.

Snake.cs
using System;
using System.Drawing;
using System.Windows.Forms;

class Snake : Form {

    public Snake() {

        Text = "Snake";
        DoubleBuffered = true;
        FormBorderStyle = FormBorderStyle.FixedSingle;
        
        int borderWidth = (this.Width - this.ClientSize.Width) / 2;
        int titleBarHeight = this.Height - this.ClientSize.Height - borderWidth;

        Board board = new Board();
        board.BORDER_WIDTH = borderWidth;
        board.TITLEBAR_HEIGHT = titleBarHeight;

        Controls.Add(board);
        CenterToScreen();

    }
}

class MApplication {
    public static void Main() {
        Application.Run(new Snake());
    }
}

This is the main class.

Snake
Figure: Snake

This was the Snake game programmed using the Mono Winforms library.