Example #1
0
        private void openFile()
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Filter = "Snake file (*.snk)|*.snk";
            openFileDialog.Title  = "Open snake doc file";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                FileName = openFileDialog.FileName;
                try
                {
                    using (FileStream fileStream = new FileStream(FileName, FileMode.Open))
                    {
                        IFormatter formater = new BinaryFormatter();
                        snake = (SnakeDoc)formater.Deserialize(fileStream);
                        food  = (SnakeFood)formater.Deserialize(fileStream);

                        minutesElapsed = (int)formater.Deserialize(fileStream);
                        secondsElapsed = (int)formater.Deserialize(fileStream);
                        bodyColor      = (Color)formater.Deserialize(fileStream);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Could not read file: " + FileName);
                    FileName = null;
                    return;
                }
                Invalidate(true);
            }
        }
Example #2
0
        void timer_Tick(object sender, EventArgs e)
        {
            //game over
            if (snake.IsDead() == true)
            {
                timer.Stop();
                timerSpec.Stop();
                MessageBox.Show("Your score is " + snake.score, "Elapsed time : " + string.Format("{0:00}:{1:00}", minutesElapsed, secondsElapsed));
                DialogResult result = MessageBox.Show("New game?", "You lost.", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    NewGame();
                }
                else
                {
                    this.Close();
                }
            }
            //elapsed time
            sec--;
            if (sec == 0)
            {
                sec             = 10;
                secondsElapsed += 1;
                if (secondsElapsed % 60 == 0)
                {
                    secondsElapsed  = 0;
                    minutesElapsed += 1;
                }
            }
            //snake movement and food status

            snake.Move();

            food.CheckIfEaten(snake);
            if (foodSpec != null)
            {
                foodSpec.CheckIfEaten(snake);
                if (foodSpec.isEaten == true)
                {
                    timerSpec.Stop();
                    snake.score                  += secondsLeft * 3;
                    secondsLeft                   = 10;
                    toolStripLabel3.Text          = secondsLeft.ToString();
                    foods[foodSpec.Y][foodSpec.X] = false;
                    foodSpec = null;
                }
            }
            if (food.isEaten == true)
            {
                snake.AddPart(bodyColor);
                food = new SnakeFood(SIZE);
                GenerateFood();
                snake.foodIsEaten = true;
            }
            Invalidate(true);
        }
Example #3
0
 void timerSpec_Tick(object sender, EventArgs e)
 {
     secondsLeft--;
     if (secondsLeft == 0)
     {
         timerSpec.Stop();
         foodSpec    = null;
         secondsLeft = 10;
     }
     toolStripLabel3.Text = secondsLeft.ToString();
 }
Example #4
0
        public void NewGame()
        {
            SIZE                = 20;
            sec                 = 10;
            secondsLeft         = 10;
            pause               = false;
            snake               = new SnakeDoc(6, 8, SIZE);//snake init
            menuStrip1.AutoSize = false;
            menuStrip1.Height   = SIZE;
            this.ClientSize     = new Size((WIDTH + 1) * snake.size, (HEIGHT + 1) * (snake.size) + toolStrip1.Height);

            bodyColor    = Color.Black;
            snake.Width  = WIDTH;
            snake.Height = HEIGHT;

            timer     = new Timer();                 //timer
            timerSpec = new Timer();                 //timerSpec

            food           = new SnakeFood(SIZE);    //food
            foodSpec       = null;
            foods          = new bool[HEIGHT + 1][]; //matrix
            secondsElapsed = 0;
            minutesElapsed = 0;

            timer.Interval = (100);//NE GO MENVAJ TIMEROT zaradi vremetraenje ke se izmeni
            timer.Start();
            timer.Tick += timer_Tick;

            toolStripLabel2.Text = "0";
            toolStripLabel3.Text = secondsLeft.ToString();

            for (int i = 1; i < HEIGHT + 1; ++i)
            {
                foods[i] = new bool[WIDTH + 1];
                for (int j = 0; j < WIDTH + 1; ++j)
                {
                    foods[i][j] = false;
                }
            }
            foods[8][6] = true;//head
            foods[8][7] = true;
            GenerateFood();
            timerSpec.Interval = 1000;
            timerSpec.Stop();
            timerSpec.Tick      += timerSpec_Tick;
            toolStripLabel3.Text = secondsLeft.ToString();

            this.MinimumSize = this.Size;
            this.MaximumSize = this.Size;
        }
Example #5
0
        public void GenerateFood()
        {
            foreach (SnakePart part in snake.body)
            {
                foods[part.Y][part.X] = true;
            }

            int coorX, coorY, coorSpecX, coorSpecY;

            coorX = x.Next(0, WIDTH);
            coorY = x.Next(1, HEIGHT);

            /*while (foods[coorY][coorX] != false)
             * {
             *  coorX = x.Next(0, WIDTH);
             *  coorY = y.Next(1, HEIGHT);
             * }*/

            if (foods[coorY][coorX] == true)
            {
                GenerateFood();
            }

            food.X = coorX;
            food.Y = coorY;
            foods[food.Y][food.X] = true;
            if (snake.body.Count % 5 == 0)
            {
                coorSpecX = x.Next(0, WIDTH);
                coorSpecY = x.Next(1, HEIGHT);
                while (foods[coorSpecY][coorSpecX] != false)
                {
                    coorSpecX = x.Next(0, WIDTH);
                    coorSpecY = x.Next(1, HEIGHT);
                }
                timerSpec.Start();
                foodSpec         = new SnakeFood(SIZE);
                foodSpec.Special = true;
                foodSpec.X       = coorSpecX;
                foodSpec.Y       = coorSpecY;
                foods[foodSpec.Y][foodSpec.X] = true;
            }

            foreach (SnakePart part in snake.body)
            {
                foods[part.Y][part.X] = false;
            }
        }
Example #6
0
        public void GenerateFood()
        {
            foreach (SnakePart part in snake.body)
            {
                foods[part.Y][part.X] = true;
            }

            int coorX, coorY, coorSpecX, coorSpecY;
            coorX = x.Next(0,WIDTH);
            coorY = x.Next(1,HEIGHT);

            /*while (foods[coorY][coorX] != false)
            {
                coorX = x.Next(0, WIDTH);
                coorY = y.Next(1, HEIGHT);
            }*/

            if (foods[coorY][coorX] == true)
            {
                GenerateFood();
            }

            food.X = coorX;
            food.Y = coorY;
            foods[food.Y][food.X] = true;
            if(snake.body.Count%5==0)
            {
                coorSpecX=x.Next(0,WIDTH);
                coorSpecY=x.Next(1,HEIGHT);
                while (foods[coorSpecY][coorSpecX] != false)
                {
                    coorSpecX = x.Next(0,WIDTH);
                    coorSpecY = x.Next(1,HEIGHT);
                }
                timerSpec.Start();
                foodSpec=new SnakeFood(SIZE);
                foodSpec.Special = true;
                foodSpec.X=coorSpecX;
                foodSpec.Y=coorSpecY;
                foods[foodSpec.Y][foodSpec.X]=true;
            }

            foreach (SnakePart part in snake.body)
            {
                foods[part.Y][part.X] = false;
            }
        }
Example #7
0
        void timer_Tick(object sender, EventArgs e)
        {
            //game over
            if (snake.IsDead() == true)
            {
                timer.Stop();
                timerSpec.Stop();
                MessageBox.Show("Your score is "+  snake.score,"Elapsed time : "+string.Format("{0:00}:{1:00}",minutesElapsed,secondsElapsed));
                DialogResult result=MessageBox.Show("New game?", "You lost.", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    NewGame();
                }
                else
                    this.Close();
            }
            //elapsed time
            sec--;
            if (sec == 0)
            {
                sec = 10;
                secondsElapsed+=1;
                if(secondsElapsed%60==0)
                {
                    secondsElapsed=0;
                    minutesElapsed+=1;
                }
            }
            //snake movement and food status

            snake.Move();

            food.CheckIfEaten(snake);
            if (foodSpec != null)
            {
                foodSpec.CheckIfEaten(snake);
                if (foodSpec.isEaten == true)
                {
                    timerSpec.Stop();
                    snake.score += secondsLeft * 3;
                    secondsLeft = 10;
                    toolStripLabel3.Text = secondsLeft.ToString();
                    foods[foodSpec.Y][foodSpec.X] = false;
                    foodSpec = null;
                }
            }
            if (food.isEaten == true )
            {
                snake.AddPart(bodyColor);
                food = new SnakeFood(SIZE);
                GenerateFood();
                snake.foodIsEaten = true;
            }
            Invalidate(true);
        }
Example #8
0
 void timerSpec_Tick(object sender, EventArgs e)
 {
     secondsLeft--;
     if (secondsLeft == 0)
     {
         timerSpec.Stop();
         foodSpec = null;
         secondsLeft = 10;
     }
     toolStripLabel3.Text = secondsLeft.ToString();
 }
Example #9
0
        private void openFile()
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Snake file (*.snk)|*.snk";
            openFileDialog.Title = "Open snake doc file";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                FileName = openFileDialog.FileName;
                try
                {
                    using (FileStream fileStream = new FileStream(FileName, FileMode.Open))
                    {
                        IFormatter formater = new BinaryFormatter();
                        snake = (SnakeDoc)formater.Deserialize(fileStream);
                        food = (SnakeFood)formater.Deserialize(fileStream);

                        minutesElapsed = (int)formater.Deserialize(fileStream);
                        secondsElapsed = (int)formater.Deserialize(fileStream);
                        bodyColor = (Color)formater.Deserialize(fileStream);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Could not read file: " + FileName);
                    FileName = null;
                    return;
                }
                Invalidate(true);
            }
        }
Example #10
0
        public void NewGame()
        {
            SIZE = 20;
            sec = 10;
            secondsLeft = 10;
            pause = false;
            snake = new SnakeDoc(6, 8,SIZE);//snake init
            menuStrip1.AutoSize = false;
            menuStrip1.Height = SIZE;
            this.ClientSize = new Size((WIDTH+1) * snake.size, (HEIGHT+1) * (snake.size)+toolStrip1.Height);

            bodyColor = Color.Black;
            snake.Width = WIDTH;
            snake.Height = HEIGHT;

            timer = new Timer();//timer
            timerSpec = new Timer();//timerSpec

            food = new SnakeFood(SIZE);//food
            foodSpec = null;
            foods = new bool[HEIGHT+1][];//matrix
            secondsElapsed = 0;
            minutesElapsed = 0;

            timer.Interval = (100);//NE GO MENVAJ TIMEROT zaradi vremetraenje ke se izmeni
            timer.Start();
            timer.Tick += timer_Tick;

            toolStripLabel2.Text = "0";
            toolStripLabel3.Text = secondsLeft.ToString();

            for (int i = 1; i < HEIGHT+1; ++i)
            {
                foods[i] = new bool[WIDTH+1];
                for (int j = 0; j < WIDTH+1; ++j)
                {
                    foods[i][j] = false;
                }
            }
            foods[8][6] = true;//head
            foods[8][7] = true;
            GenerateFood();
            timerSpec.Interval = 1000;
            timerSpec.Stop();
            timerSpec.Tick += timerSpec_Tick;
            toolStripLabel3.Text = secondsLeft.ToString();
        }