Click here to Skip to main content
15,895,800 members
Articles / Programming Languages / C#

The Snake

Rate me:
Please Sign up or sign in to vote.
3.48/5 (6 votes)
15 Sep 2013CPOL5 min read 20.1K   899   6  
Moving the snake by a timer and some labels
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.IO;

namespace The_Snake
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }
        int c = 0;
        string a = "r";
        int x = 50;
        int y = 100;
        int h = 0;
        int best = 0;

        int aaa = 0;

        int z = 0;
        Random m1 = new Random(DateTime.Now.Second);
        Random n1 = new Random(DateTime.Now.Millisecond);

        private void Form4_Load(object sender, EventArgs e)
        {
            string easyRecord = File.ReadAllText(Directory.GetCurrentDirectory() + "\\best_record_snakeM");
            recordLBL.Text = easyRecord;

            if (Convert.ToInt32(TargetCatchedLBL.Text) > Convert.ToInt32(recordLBL.Text))
            {
                easyRecord = TargetCatchedLBL.Text;
                File.WriteAllText(Directory.GetCurrentDirectory() + "\\best_record_snakeM", easyRecord);
            }
            best = Convert.ToInt32(recordLBL.Text);
            TargetCatchedLBL.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
            label12.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))));

            int m = m1.Next(10, 380);
            int n = n1.Next(10, 370);
            targetLBL.Location = new Point(m, n);
            timeLBL.Text = "6";

            timer1.Start();
            timer2.Start();
            timer3.Start();
        }

        private void label26_Click(object sender, EventArgs e)
        {


            if (c == 1)
            {

                timer1.Start();
                timer2.Start();
                timer3.Start();
            }
            else
            {
                label32.Visible = false;
                label33.Visible = false;
                label34.Visible = false;
                label35.Visible = false;
                label36.Visible = false;
                label37.Visible = false;

                label3.Location = new Point(50, 100);
                m1 = new Random(DateTime.Now.Second);
                n1 = new Random(DateTime.Now.Minute);

                z = 0;
                string easyRecord = File.ReadAllText(Directory.GetCurrentDirectory() + "\\best_record_snakeM");


                if (Convert.ToInt32(TargetCatchedLBL.Text) > Convert.ToInt32(recordLBL.Text))
                {
                    easyRecord = TargetCatchedLBL.Text;
                    File.WriteAllText(Directory.GetCurrentDirectory() + "\\best_record_snakeM", easyRecord);
                }

                timer1.Start();
                timer2.Start();
                timer3.Start();
                int m = m1.Next(10, 390);
                int n = n1.Next(10, 370);
                targetLBL.Location = new Point(m, n);
                timeLBL.Text = "6";
                a = "r";
            }
        }

        private void label27_Click(object sender, EventArgs e)
        {
            c = 1;

            timer1.Stop();
            timer2.Stop();
            timer3.Stop();
        }

        private void label28_Click(object sender, EventArgs e)
        {
            timer1.Stop();
            timer2.Stop();
            timer3.Stop();

            Form3 f3 = new Form3();
            f3.ShowDialog();
        }

        private void label29_Click(object sender, EventArgs e)
        {
            if (playLoobLBL.BackColor == System.Drawing.Color.White)
                playLoobLBL.BackColor = System.Drawing.Color.Black;
            else
            {
                playLoobLBL.BackColor = System.Drawing.Color.White;
            }
        }

        private void label30_Click(object sender, EventArgs e)
        {
            if (playLoobLBL.BackColor == System.Drawing.Color.White)
                playLoobLBL.BackColor = System.Drawing.Color.Black;
            else
            {
                playLoobLBL.BackColor = System.Drawing.Color.White;
            }
        }

        private void label23_Click(object sender, EventArgs e)
        {
            timer2.Stop();
            timer3.Stop();
            timer1.Stop();

            this.WindowState = FormWindowState.Minimized;

        }

        private void label14_Click(object sender, EventArgs e)
        {

            timer1.Stop();
            timer2.Stop();
            timer3.Stop();
            string easyRecord = File.ReadAllText(Directory.GetCurrentDirectory() + "\\best_record_snakeM");


            if (Convert.ToInt32(TargetCatchedLBL.Text) > Convert.ToInt32(recordLBL.Text))
            {
                easyRecord = TargetCatchedLBL.Text;
                File.WriteAllText(Directory.GetCurrentDirectory() + "\\best_record_snakeM", easyRecord);
            }
            this.Close();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            int x1 = targetLBL.Location.X;//the target location

            int y1 = targetLBL.Location.Y;

            int x2 = x1 - 10;

            x1 = x1 + 10;

            int y2 = y1 - 10;

            y1 = y1 + 10;

            int l = m1.Next(15, 380);
            int k = n1.Next(15, 360);

            if (x2 <= label3.Location.X &&//cheking that the snake got the target?
                x1 >= label3.Location.X &&
                y2 <= label3.Location.Y &&
                y1 >= label3.Location.Y)
            {
                timeLBL.Text = Convert.ToString(Convert.ToInt32(timeLBL.Text) + 4);
                targetLBL.Location = new Point(l, k);
                h = h + 1;
                TargetCatchedLBL.Text = h.ToString();
            }

            if (z > 1)
            {
                label33.Visible = true;
                label33.Location = new Point(x, y);
                if (a == "r")
                {
                    x = x + 1;
                }
                if (a == "l")
                {
                    x = x - 1;
                }
                if (a == "u")
                {
                    y = y - 1;
                }
                if (a == "d")
                {
                    y = y + 1;
                }
            }

            if (z > 0)
            {
                label32.Visible = true;
                label32.Location = new Point(x, y);
                if (a == "r")
                {
                    x = x + 1;
                }
                if (a == "l")
                {
                    x = x - 1;
                }
                if (a == "u")
                {
                    y = y - 1;
                }
                if (a == "d")
                {
                    y = y + 1;
                }
            }

            label9.Location = new Point(x, y);
            if (a == "r")
            {
                x = x + 1;
            }
            if (a == "l")
            {
                x = x - 1;
            }
            if (a == "u")
            {
                y = y - 1;
            }
            if (a == "d")
            {
                y = y + 1;
            }

            label8.Location = new Point(x, y);
            if (a == "r")
            {
                x = x + 1;
            }
            if (a == "l")
            {
                x = x - 1;
            }
            if (a == "u")
            {
                y = y - 1;
            }
            if (a == "d")
            {
                y = y + 1;
            }


            label7.Location = new Point(x, y);
            if (a == "r")
            {
                x = x + 1;
            }
            if (a == "l")
            {
                x = x - 1;
            }
            if (a == "u")
            {
                y = y - 1;
            }
            if (a == "d")
            {
                y = y + 1;
            }

            label6.Location = new Point(x, y);
            if (a == "r")
            {
                x = x + 1;
            }
            if (a == "l")
            {
                x = x - 1;
            }
            if (a == "u")
            {
                y = y - 1;
            }
            if (a == "d")
            {
                y = y + 1;
            }


            label5.Location = new Point(x, y);
            if (a == "r")
            {
                x = x + 1;
            }
            if (a == "l")
            {
                x = x - 1;
            }
            if (a == "u")
            {
                y = y - 1;
            }
            if (a == "d")
            {
                y = y + 1;
            }

            label4.Location = new Point(x, y);
            if (a == "r")
            {
                x = x + 1;
            }
            if (a == "l")
            {
                x = x - 1;
            }
            if (a == "u")
            {
                y = y - 1;
            }
            if (a == "d")
            {
                y = y + 1;
            }

            label3.Location = new Point(x, y);


            int c = label3.Location.X;

            int v = label3.Location.Y;


            if (c <= 10 || c >= 391 || v <= 10 || v >= 369)
            {
                label32.Visible = false;


                string easyRecord = File.ReadAllText(Directory.GetCurrentDirectory() + "\\best_record_snakeM");


                if (Convert.ToInt32(TargetCatchedLBL.Text) > Convert.ToInt32(recordLBL.Text))
                {
                    easyRecord = TargetCatchedLBL.Text;
                    File.WriteAllText(Directory.GetCurrentDirectory() + "\\best_record_snakeM", easyRecord);
                }

                timer1.Stop();
                timer2.Stop();
                timer3.Stop();

                if (playLoobLBL.BackColor == System.Drawing.Color.Black)
                {

                    if (h > best)
                    {
                        best = h;
                        recordLBL.Text = h.ToString();
                    }
                    h = 0;
                    z = 0;
                    MessageBox.Show("You Lose", "Game over");

                    label32.Visible = false;
                    label33.Visible = false;
                    label34.Visible = false;
                    label35.Visible = false;
                    label36.Visible = false;
                    label37.Visible = false;
                    difficultyLBL.Text = "1";
                    TargetCatchedLBL.Text = "0";
                    x = 50;
                    y = 100;
                    a = "r";

                    timeLBL.Text = "10";
                    timer1.Start();
                    timer2.Start();
                    timer3.Start();
                }
                else
                {

                    if (h > best)
                    {
                        best = h;
                        recordLBL.Text = h.ToString();
                    }
                    h = 0;
                    z = 0;
                    MessageBox.Show("You Lose", "Game over");


                    label32.Visible = false;
                    label33.Visible = false;
                    label34.Visible = false;
                    label35.Visible = false;
                    label36.Visible = false;
                    label37.Visible = false;

                    difficultyLBL.Text = "1";
                    TargetCatchedLBL.Text = "0";
                    x = 50;
                    y = 100;
                    a = "r";
                    timeLBL.Text = "10";
                }
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            int a1;

            a1 = Convert.ToInt32(timeLBL.Text) - 1;

            timeLBL.Text = Convert.ToString(a1);

            if (a1 < 3)
                timeLBL.ForeColor = System.Drawing.Color.DarkRed;
            if (a1 > 3)
                timeLBL.ForeColor = System.Drawing.Color.Black;

            if (a1 < 1)
            {
                timer1.Stop();
                timer2.Stop();
                timer3.Stop();
                z = 0;
                MessageBox.Show("You Lose -> Time's Up ;D", "Game Over");


                label32.Visible = false;
                label33.Visible = false;
                label34.Visible = false;
                label35.Visible = false;
                label36.Visible = false;
                label37.Visible = false;
                label6.Visible = false;

                TargetCatchedLBL.Text = "0";
                x = 50;
                y = 100;
                a = "r";
                timeLBL.Text = "10";
                timeLBL.BackColor = System.Drawing.Color.Black;
                difficultyLBL.Text = "1";
            }
        }

        private void timer3_Tick(object sender, EventArgs e)
        {
            z++;
            if (z < 3)
            {
                difficultyLBL.Text = z.ToString();
            }
        }

        string key = "";
        protected override void OnKeyDown(KeyEventArgs keyEvent)
        {
            //These codes are used to get the key down properties and are useful
            //***************************************************************************

            string KeyCode = "";
            string KeyData = "";
            string KeyValue = "";
            string Alt = "";
            string Ctrl = "";
            string Shift = "";

            // Gets the key code 
            KeyCode = "KeyCode: " + keyEvent.KeyCode.ToString();

            // Gets the key data; recognizes combination of keys 
            KeyData = "KeyData: " + keyEvent.KeyData.ToString();

            // Integer representation of KeyData 
            KeyValue = "KeyValue: " + keyEvent.KeyValue.ToString();

            // Returns true if Alt is pressed 
            Alt = "Alt: " + keyEvent.Alt.ToString();

            // Returns true if Ctrl is pressed 
            Ctrl = "Ctrl: " + keyEvent.Control.ToString();

            // Returns true if Shift is pressed 
            Shift = "Shift: " + keyEvent.Shift.ToString();

            //***************************************************************************

            string keyCode = "";

            if (Alt == "Alt: True" && keyCode != "18")//check the alt is pressed or no
            {
                keyCode = keyCode + "18";//this is used to not to get the old alt pressed againg
            }
            if (KeyValue == "KeyValue: 115")//this is the key value of f4 button
            {
                keyCode = keyCode + "115";
            }
            if (keyCode == "18115")//chech if alt and f4 are pressed together
            {
                timer1.Stop();
                timer2.Stop();
                timer3.Stop();
            }

            keyCode = "";

            key = keyEvent.KeyCode.ToString();

            if (key == "Return")
            {
                if (playLoobLBL.BackColor == System.Drawing.Color.Black)
                    playLoobLBL.BackColor = System.Drawing.Color.White;
                else
                    playLoobLBL.BackColor = System.Drawing.Color.Black;

            }

            if (key == "Space")
            {
                if (aaa == 0)
                {
                    c = 1;

                    timer1.Stop();
                    timer2.Stop();
                    timer3.Stop();
                    aaa = -1;
                }
                else
                {
                    timer1.Start();
                    timer2.Start();
                    timer3.Start();
                    aaa = 0;
                }
            }
            if (key == "Right")
            {

                if (a != "l")
                    a = "r";
            }
            if (key == "Left")
            {

                if (a != "r")
                    a = "l";
            }
            if (key == "Up")
            {

                if (a != "d")
                    a = "u";
            }
            if (key == "Down")
            {

                if (a != "u")
                    a = "d";
            }

        }

        private void Form4_KeyDown(object sender, KeyEventArgs e)
        {

            //Doesnt Work Anymore (Becuase of protected over vide void OnKeyDown)

        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student
Iran (Islamic Republic of) Iran (Islamic Republic of)
I'm programming since 2009. i started it from school and c# language and then i learnt working sql and now i'm working on asp.net projects
Big Grin | :-D

Comments and Discussions