コード例 #1
0
ファイル: Snake.cs プロジェクト: Xansher/Snake
 public Snake(Canvas gameBoard)
 {
     GameBoard = gameBoard;
     SnakeHead = new SnakeHead(GameBoard, 300, 300);
     SnakeBody = new List <SnakeBodyPart>();
     AddSnakeBody();
     AddSnakeBody();
 }
コード例 #2
0
ファイル: Snake.cs プロジェクト: Xansher/Snake
        public void Update(Food food)
        {
            // Snake head move
            SnakeHead.Move();
            SnakeHead.Update();
            //Snake bodypart move
            Direction previousDirection;
            Direction nextDirection = SnakeHead.Direction;

            foreach (var part in SnakeBody)
            {
                part.Move();
                part.Update();
                previousDirection = part.Direction;
                part.Direction    = nextDirection;
                nextDirection     = previousDirection;
            }
            //check if snake head hit wall

            if (SnakeHead.HitWall())
            {
                OnHitWall?.Invoke();
            }

            //check if snake eat food

            if (SnakeHead.EatenFood(food))
            {
                OnFoodEaten?.Invoke();
                AddSnakeBody();
            }

            //check if snakehead hit body

            foreach (var part in SnakeBody)
            {
                if (SnakeHead.X == part.X && SnakeHead.Y == part.Y)
                {
                    OnHitBody?.Invoke();
                }
            }
            IsUpdating = false;
        }
コード例 #3
0
ファイル: Snake.cs プロジェクト: Xansher/Snake
 public void ChangeDirection(Direction dir)
 {
     if (!IsUpdating)
     {
         if (SnakeHead.Direction == dir)
         {
             return;
         }
         if ((SnakeHead.Direction == Direction.East && dir == Direction.West) || (SnakeHead.Direction == Direction.West && dir == Direction.East))
         {
             return;
         }
         if ((SnakeHead.Direction == Direction.South && dir == Direction.North) || (SnakeHead.Direction == Direction.North && dir == Direction.South))
         {
             return;
         }
         IsUpdating = true;
         SnakeHead.ChangeDirection(dir);
     }
 }