Exemple #1
0
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (hardness == 1)
            {
                timer1.Interval = 100;
                hardmode        = false;
            }
            if (hardness == 2)
            {
                timer1.Interval = 50;
                hardmode        = false;
            }
            if (hardness == 3)
            {
                timer1.Interval = 50;
                hardmode        = true;
            }
            for (int i = snake.li.Count() - 1; i >= 0; i--)
            {
                if (i == 0)
                {
                    snake.GetSecondElement();
                    snake.GetTail();
                    snake.Move();
                    if (snake.path == 'U')
                    {
                        var temp1 = snake.li[0];
                        temp1.b     = snake.SnakeHeadU;
                        snake.li[0] = temp1;
                        if (snake.li.Count() == 2)
                        {
                            var temp9 = snake.li[1];
                            temp9.b     = snake.SnakeTailU;
                            snake.li[1] = temp9;
                        }
                    }
                    if (snake.path == 'D')
                    {
                        var temp1 = snake.li[0];
                        temp1.b     = snake.SnakeHeadD;
                        snake.li[0] = temp1;
                        if (snake.li.Count() == 2)
                        {
                            var temp9 = snake.li[1];
                            temp9.b     = snake.SnakeTailD;
                            snake.li[1] = temp9;
                        }
                    }
                    if (snake.path == 'L')
                    {
                        var temp1 = snake.li[0];
                        temp1.b     = snake.SnakeHeadL;
                        snake.li[0] = temp1;
                        if (snake.li.Count() == 2)
                        {
                            var temp9 = snake.li[1];
                            temp9.b     = snake.SnakeTailL;
                            snake.li[1] = temp9;
                        }
                    }
                    if (snake.path == 'R')
                    {
                        var temp1 = snake.li[0];
                        temp1.b     = snake.SnakeHeadR;
                        snake.li[0] = temp1;
                        if (snake.li.Count() == 2)
                        {
                            var temp9 = snake.li[1];
                            temp9.b     = snake.SnakeTailR;
                            snake.li[1] = temp9;
                        }
                    }
                }
                else
                {
                    var temp = snake.li[i];
                    temp.x = snake.li[i - 1].x;
                    temp.y = snake.li[i - 1].y;
                    if (i == 1)
                    {
                        if (snake.li.Count() != 2)
                        {
                            snake.GetSecondElement();
                        }
                    }
                    if (i == snake.li.Count() - 1 && snake.li.Count() != 2)
                    {
                        snake.GetTail();
                        temp   = snake.li[i];
                        temp.x = snake.li[i - 1].x;
                        temp.y = snake.li[i - 1].y;
                    }
                    if (i != 0 && i != 1 && i != snake.li.Count() - 1 && snake.li.Count() != 2)
                    {
                        temp.b = snake.li[i - 1].b;
                    }
                    snake.li[i] = temp;
                }
            }
            if ((snake.li[0].x == Berry.x) && (snake.li[0].y == Berry.y))
            {
                count     += 100;
                Score.Text = "Счёт:" + count.ToString();
                Berry.ChangeThere();
                var temp1 = snake.li.Last();
                snake.li.Add(new Coordinate()
                {
                    x = temp1.x, y = temp1.y, b = temp1.b
                });
            }
            snake.GetTail();

            Graphics g = Graphics.FromImage(bi);

            g.Clear(Color.LightBlue);



            for (int i = 0; i < snake.li.Count(); i++)
            {
                g.DrawImage(snake.li[i].b, snake.li[i].x * width, snake.li[i].y * height, width, height);
            }
            g.DrawImage(Berry.texture, Berry.x * width, Berry.y * height, 25, 25);

            for (int i = 1; i < snake.li.Count(); i++)
            {
                if ((snake.li[0].x == snake.li[i].x) && (snake.li[0].y == snake.li[i].y))
                {
                    gameover = true;
                    break;
                }
            }
            if (hardmode && (snake.li[0].x < 0 || snake.li[0].y < 0 || snake.li[0].x >= 20 || snake.li[0].y >= 20))
            {
                gameover = true;
            }
            if (hardmode == false)
            {
                if (snake.li[0].x < 0)
                {
                    var temp = snake.li[0];
                    temp.x      = 19;
                    snake.li[0] = temp;
                }
                if (snake.li[0].x >= 20)
                {
                    var temp = snake.li[0];
                    temp.x      = 0;
                    snake.li[0] = temp;
                }
                if (snake.li[0].y >= 20)
                {
                    var temp = snake.li[0];
                    temp.y      = 0;
                    snake.li[0] = temp;
                }
                if (snake.li[0].y < 0)
                {
                    var temp = snake.li[0];
                    temp.y      = 19;
                    snake.li[0] = temp;
                }
            }
            if (gameover)
            {
                gamestarted = false;
                timer1.Stop();
                timer2.Stop();
                youLose.Show();
                button1.Show();
                youLoseText.Show();
                Back.Show();
                ScoreWas.Text = "Счёт:" + count.ToString();
                ScoreWas.Show();
            }
            gameover      = false;
            Monitor.Image = bi;
        }