Snake Game


snake.jpg


The attached project is a snake game. The output looks like the above screen.

The goal of the game is to clear and get all apples. The game has three different levels. 

NOTE : Before you run the application, you must have .NET 3.5 or higher version installed on your machine. 

ENJOY IT ! 

Here is some sample code from the project:


        private void initGame() // START THE GAME ...
        {
            dots = 3;
            for (int z = 0; z < dots; z++)
            {
                x[z] = 50-z*10 ;
                y[z] = 50;
            }
            locateApple();
            KeyUp += new KeyEventHandler(OnKeyUp);
           timer1.Start();

        }
        private void pictureBox1_Paint(object sender, PaintEventArgs e)   //DRAW SNAKE & APPLE...
        {
            if (left == true)
            {
                head = Resource1.headl;
            }
            if (up == true)
            {
                head = Resource1.headt;
            }
            if (down == true)
            {
                head = Resource1.headd;
            }
            if (right == true)
            {
                head = Resource1.headr;
            }
            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]);
                        g.DrawImage(dote, x[dots], y[dots]);
                    }
                }
            }
            else
            {
               
            }
        }
 
        private void checkCollision()    //CHECK IF SNAKE OUTOF CLIENT ...
        {
            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()        //DRAW APPLE TO RANDOM LOCATIONS...
        {
            Thread.Sleep(200);
            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;
            }
        }
        private void move()   // MOVE THE SNAKE ...
        {
            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;
            }
        }


You may also download the game from the following URLs:

Ebook Download
View all
Learn
View all