As Snake Clear Intensity 5 0 Without Using Any Continues

I can't realize the possibility of continuing the game after a collision. The snake should stop and start moving after clicking on one of the arrow buttons.

In a collision, a window appears about the loss, I need to continue the game.

this

I press the button and the following happens:

this

I don't understand how I can save the coordinates of the snake just before the collision. In the moveTimer_Tick method, all elements move, i.e. new coordinates have already appeared at the head and body, then there is a check for collisions with the wall and body. If they are found, a window appears about the loss. New snake coordinates are not displayed. But after clicking the "Continue" button, an update occurs and the snake climbs to the border.

The question is: how can I save the coordinates of the snake, before the collision, and after continuing to start with them.

          namespace Snake{     public partial class MainWindow : Window     {           //The field on which the snake lives           Entity field;           // snake head           Head head;           // whole snake           List<PositionedEntity> snake;           // apple           Apple apple;           //number of points           int score;           // Is movement paused           bool paused;           //time           DispatcherTimer moveTimer;         //constructor form        public MainWindow()        {            InitializeComponent();             snake = new List<PositionedEntity>();            //create field 600x600pixels            field = new Entity(600, 600, "pack://application:,,,/Resources/snake.png");             //create a timer that runs every 300 ms            moveTimer = new DispatcherTimer();            moveTimer.Interval = new TimeSpan(0, 0, 0, 0, 300);            moveTimer.Tick += new EventHandler(moveTimer_Tick);         }         //redraw screen method        private void UpdateField()        {            //update the position of the elements of the snake            foreach (var p in snake)            {                Canvas.SetTop(p.image, p.y);                Canvas.SetLeft(p.image, p.x);            }             //update the position of apple            Canvas.SetTop(apple.image, apple.y);            Canvas.SetLeft(apple.image, apple.x);             //points update            lblScore.Content = String.Format("{0}000", score);        }         //timer tick handler. All movement takes place here.        void moveTimer_Tick(object sender, EventArgs e)        {            // Do not update if movement is paused            if(paused) {                return;            }             //in the reverse order we move all the elements of the snake            foreach (var p in Enumerable.Reverse(snake))            {                p.move();            }             //we check that the head of the snake did not crash into the body            foreach (var p in snake.Where(x => x != head))            {                if (p.x == head.x && p.y == head.y)                {                    //we lose                    moveTimer.Stop();                    GameOver.Visibility = Visibility.Visible;                    btnRestart.Visibility = Visibility.Visible;                    tbScore.Text = String.Format("SCORE: {0}000", score);                    return;                }            }             //check that the head of the snake did not go out of the field            if (head.x < 40 || head.x >= 540 || head.y < 40 || head.y >= 540)            {                //we lose                moveTimer.Stop();                GameOver.Visibility = Visibility.Visible;                btnRestart.Visibility = Visibility.Visible;                tbScore.Text = String.Format("SCORE: {0}000", score);                return;            }             //check that the head of the snake crashed into an apple            if (head.x == apple.x && head.y == apple.y)            {                //increase the score                score++;                //move the apple to a new place                apple.move();                var part = new BodyPart(snake.Last());                canvas1.Children.Add(part.image);                snake.Add(part);            }            UpdateField();        }         private void Window_KeyDown(object sender, KeyEventArgs e)        {            // Unpause movement when any key is pressed            if(paused) {                 paused = false;            }            switch (e.Key)            {                case Key.Up:                    head.direction = Head.Direction.UP;                    break;                case Key.Down:                    head.direction = Head.Direction.DOWN;                    break;                case Key.Left:                    head.direction = Head.Direction.LEFT;                    break;                case Key.Right:                    head.direction = Head.Direction.RIGHT;                    break;            }        }         // "Start"        private void button1_Click(object sender, RoutedEventArgs e)        {            btnStart.Visibility = Visibility.Hidden;            btnRestart.Visibility = Visibility.Hidden;            tBNotEnoughPoints.Visibility = Visibility.Hidden;            score = 0;            snake.Clear();            canvas1.Children.Clear();            // "Game Over"            GameOver.Visibility = Visibility.Hidden;             canvas1.Children.Add(field.image);            apple = new Apple(snake);            canvas1.Children.Add(apple.image);            head = new Head();            snake.Add(head);            canvas1.Children.Add(head.image);             moveTimer.Start();            UpdateField();         }        private void btnContinue_Click(object sender, RoutedEventArgs e)        {            if (score >= 2)            {                GameOver.Visibility = Visibility.Hidden;                btnRestart.Visibility = Visibility.Hidden;                score -= 2;                // Pause movement                paused = true;                moveTimer.Start();                UpdateField();            }            else            {                tBNotEnoughPoints.Visibility = Visibility.Visible;            }        }         public class Entity        {            protected int m_width;            protected int m_height;             Image m_image;            public Entity(int w, int h, string image)            {                m_width = w;                m_height = h;                m_image = new Image();                m_image.Source = (new ImageSourceConverter()).ConvertFromString(image) as ImageSource;                m_image.Width = w;                m_image.Height = h;             }             public Image image            {                get                {                    return m_image;                }            }        }         public class PositionedEntity : Entity        {            protected int m_x;            protected int m_y;            public PositionedEntity(int x, int y, int w, int h, string image)                : base(w, h, image)            {                m_x = x;                m_y = y;            }             public virtual void move() { }             public int x            {                get                {                    return m_x;                }                set                {                    m_x = value;                }            }             public int y            {                get                {                    return m_y;                }                set                {                    m_y = value;                }            }        }         public class Apple : PositionedEntity        {            List<PositionedEntity> m_snake;            public Apple(List<PositionedEntity> s)                : base(0, 0, 40, 40, "pack://application:,,,/Resources/fruit.png")            {                m_snake = s;                move();            }             public override void move()            {                Random rand = new Random();                do                {                    x = rand.Next(13) * 40 + 40 ;                    y = rand.Next(13) * 40 + 40 ;                    bool overlap = false;                    foreach (var p in m_snake)                    {                        if (p.x == x && p.y == y)                        {                            overlap = true;                            break;                        }                    }                    if (!overlap)                        break;                } while (true);             }        }         public class Head : PositionedEntity        {            public enum Direction            {                RIGHT, DOWN, LEFT, UP, NONE            };             Direction m_direction;             public Direction direction {                set                {                    m_direction = value;                    RotateTransform rotateTransform = new RotateTransform(90 * (int)value);                    image.RenderTransform = rotateTransform;                }            }             public Head()                : base(280, 280, 40, 40, "pack://application:,,,/Resources/head.png")            {                image.RenderTransformOrigin = new Point(0.5, 0.5);                m_direction = Direction.NONE;            }             public override void move()            {                switch (m_direction)                {                    case Direction.DOWN:                        y += 40;                        break;                    case Direction.UP:                        y -= 40;                        break;                    case Direction.LEFT:                        x -= 40;                        break;                    case Direction.RIGHT:                        x += 40;                        break;                }            }        }         public class BodyPart : PositionedEntity        {            PositionedEntity m_next;            public BodyPart(PositionedEntity next)                : base(next.x, next.y, 40, 40, "pack://application:,,,/Resources/body.png")            {                m_next = next;            }             public override void move()            {                x = m_next.x;                y = m_next.y;            }        }    } }                  

leonardmemmar.blogspot.com

Source: https://stackoverflow.com/questions/59457080/snake-roll-back-the-coordinates-of-the-snake-to-impact

0 Response to "As Snake Clear Intensity 5 0 Without Using Any Continues"

Enviar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel