Exemplo n.º 1
0
        public void ChangeDirection(SnakePart.PartDirection newDirection)
        {
            if (newDirection == Direction)
            {
                return;
            }

            SnakePart newpart = new SnakePart()
            {
                Direction = newDirection, x = FirstPart.x, y = FirstPart.y, color = FirstPart.color
            };

            if (Direction == SnakePart.PartDirection.RIGHT && newpart.Direction == SnakePart.PartDirection.UP)
            {
                newpart.MoveSnakePartBack();
            }
            else if (Direction == SnakePart.PartDirection.DOWN && newpart.Direction == SnakePart.PartDirection.LEFT)
            {
                newpart.MoveSnakePartBack();
            }

            parts.Insert(0, newpart);
        }
Exemplo n.º 2
0
        void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            //first check for collision
            if (snake.parts.Count > 2)
            {
                for (int i = 2; i < snake.parts.Count; i++)
                {
                    SnakePart part = snake.parts[i];

                    if (part.GetRectangle().IntersectsWith(snake.FirstPart.GetRectangle()))
                    {
                        timer.Stop();

                        MessageBox.Show("Aww Too Bad. Game Over");

                        ResetGame();

                        toolStripStatusLabel1.Text = "";

                        return;
                    }
                }
            }

            //check for direction change first
            if (snake.Direction != newDirection)
            {
                snake.ChangeDirection(newDirection);
            }


            if (new Rectangle(foodpiece.x, foodpiece.y, 5, 5).IntersectsWith(snake.FirstPart.GetRectangle()))
            {
                snake.FirstPart.color = foodpiece.color;


                Random r = new Random(DateTime.Now.Millisecond);

                foodpiece.x = 10 + (r.Next() % (this.ClientRectangle.Width - 20));
                foodpiece.y = 10 + (r.Next() % (this.ClientRectangle.Height - 50));

                foodpiece.color = Color.FromArgb(r.Next() % 200, r.Next() % 200, r.Next() % 200);

                snake.LastPart.Length += 1;


                score++;

                toolStripStatusLabel1.Text = "Score : " + score.ToString();
            }


            //move the snake
            snake.MoveSnake();

            this.Invalidate();

            //for (int i = 0; i < snake.parts.Count; i++ )
            //{
            //    SnakePart part =  snake.parts[i];
            //    Console.WriteLine("Part [" + i.ToString() + "] \r\n" + new Point(part.x, part.y).ToString() + " Length : " + part.Length.ToString() + ", Direction : " + part.Direction.ToString() + "\r\n");
            //}
        }