Nibbles

In this part of the Qyoto C# programming tutorial, we will create a Nibbles game clone.

Nibbles 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.

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 starts immediately. When the game is finished, we display "Game Over" message in the center of the window.

board.cs
using System;
using QtCore;
using QtGui;

public class Board : QFrame 
{
    const int WIDTH = 300;
    const int HEIGHT = 300;
    const int DOT_SIZE = 10;
    const int ALL_DOTS = 900;
    const int RAND_POS = 30;
    const int DELAY = 140;

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

    int dots;
    int apple_x;
    int apple_y;

    bool left = false;
    bool right = true;

    bool up = false;
    bool down = false;
    bool inGame = true;

    QBasicTimer timer;
    QImage ball;
    QImage apple;
    QImage head;


    public Board() 
    {        
        StyleSheet = "QWidget { background-color: black }";

        FocusPolicy = Qt.FocusPolicy.StrongFocus;

        ball = new QImage("dot.png");
        apple = new QImage("apple.png");
        head = new QImage("head.png");
        
        InitGame();
    }


    void InitGame() 
    {
        dots = 3;

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

        LocateApple();

        timer = new QBasicTimer();
        timer.Start(DELAY, this);
    }


    protected override void OnPaintEvent(QPaintEvent e)
    {
        QPainter painter = new QPainter();
        painter.Begin(this);

        if (inGame) 
        {
            DrawObjects(painter);
        } else {
            GameOver(painter);
        }

        painter.End();
    }

    void DrawObjects(QPainter painter) 
    {
        painter.DrawImage(apple_x, apple_y, apple);

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

    void GameOver(QPainter painter) 
    {
        String msg = "Game Over";
        QFont small = new QFont("Helvetica", 12,
            (int) QFont.Weight.Bold);
        QFontMetrics metr = new QFontMetrics(small);
        
        int textWidth = metr.Width(msg);
        int h = Height;
        int w = Width;

        painter.SetPen(GlobalColor.white);
        painter.Font = small;
        painter.Translate(new QPoint(w/2, h/2));
        painter.DrawText(-textWidth/2, 0, msg);
    }


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

    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;
        }
    }


    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) 
        {
            inGame = false;
        }

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

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

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

    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));
    }

    protected override void OnTimerEvent(QTimerEvent arg1) 
    {
        if (inGame) 
        {
            CheckApple();
            CheckCollision();
            Move();
        } else 
        {
            timer.Stop();
        }

        Repaint();
    }


    protected override void OnKeyPressEvent(QKeyEvent e)
    {        
        int key = e.Key();
        
        if (key == (int) Qt.Key.Key_Left && !right) 
        {
            left = true;
            up = false;
            down = false;
        }
        
        if ((key == (int) Qt.Key.Key_Right) && !left) 
        {
            right = true;
            up = false;
            down = false;            
        }
        
        if ((key == (int) Qt.Key.Key_Up) && !down) 
        {
            up = true;
            right = false;
            left = false;            
        }
        
        if ((key == (int) Qt.Key.Key_Down) && !up) 
        {
            down = true;
            right = false;
            left = false;       
        }
    }
}

First we will define some globals 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. The RAND_POS constant is used to calculate a random position of an apple. The DELAY constant determines the speed of the game.

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

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

The InitGame() method initializes variables, loads images and starts a timeout function.

if (inGame) 
{
    DrawObjects(painter);
} else {
    GameOver(painter);
}

Inside the PaintEvent() method, we check the inGame variable. If it is true, we draw our objects. The apple and the snake joints. Otherwise we display "Game over" text.

void DrawObjects(QPainter painter) 
{
    painter.DrawImage(apple_x, apple_y, apple);

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

The DrawObjects() method draws the apple and the joints of the snake. The first joint of a snake is its head, which is represented by a red circle.

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

The CheckApple() method checks if the snake has hit the apple object. If so, we add another snake joint and call the LocateApple() method, which randomly places a new apple object.

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) 
{
    inGame = false;
}

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

The LocateApple() method locates an apple randomly on the board.

Random rand = new Random();

int r = (int) (rand.Next(RAND_POS));

We get a random number from 0 to RAND_POS - 1.

apple_x = ((r * DOT_SIZE));
...
apple_y = ((r * DOT_SIZE));

These line set the x, y coordinates of the apple object.

if (inGame) 
{
    CheckApple();
    CheckCollision();
    Move();
} else 
{
    timer.Stop();
}

Every 140 ms, the TimerEvent() method is called. If we are in the game, we call three methods that build the logic of the game. Otherwise we stop the timer.

In the KeyPressEvent() method of the Board class, we determine the keys that were pressed.

if (key == (int) Qt.Key.Key_Left && !right) 
{
    left = true;
    up = false;
    down = false;
}

If we hit the left cursor key, we set left variable to true. This variable is used in the Move() method to change coordinates of the snake object. Notice also that when the snake is heading to the right, we cannot turn immediately to the left.

nibbles.cs
using System;
using QtCore;
using QtGui;

/**
 * ZetCode Qyoto C# tutorial
 *
 * In this program, we create
 * a Nibbles game clone.
 *
 * @author Jan Bodnar
 * website zetcode.com
 * last modified October 2012
 */


public class QyotoApp : QMainWindow 
{    
    public QyotoApp() 
    {
        WindowTitle = "Nibbles";

        CentralWidget= new Board();

        Resize(310, 310);
        Move(300, 300);
        Show();
    }

    [STAThread]
    public static int Main(String[] args) 
    {
        new QApplication(args);
        new QyotoApp();

        return QApplication.Exec();
    }
}

In this class, we set up the Nibbles game.

Nibbles
Figure: Nibbles

This was the Nibbles computer game programmed with the Qyoto library and the C# programming language.