Exemple #1
0
 internal Snake(int unitLength)
 {
     head = new SnakeBody(0, 0, unitLength);
     tail = head;
     verticalMove = unitLength;
     horizonMove = 0;
 }
        public void increaseLength()
        {
            SnakeBody newbody = new SnakeBody();

            newbody.x = bodies[Length - 1].x0;
            newbody.y = bodies[Length - 1].y0;
            bodies.Add(newbody);
            Length++;
        }
Exemple #3
0
 internal SnakeBody(int left, int top, int unitLength)
 {
     next = null;
     rectangleLeft = left;
     rectangleTop = top;
     bodyBlock = new Rectangle();
     bodyBlock.Width = unitLength;
     bodyBlock.Height = unitLength;
     bodyBlock.Fill = Brushes.Black;
 }
Exemple #4
0
        public void Grow(Position position)
        {
            Position reversePosition = new Position(position.X * -1, position.Y * -1);
            Position oldPosition     = SnakeBody.Tail.Value;

            Node newHead = new Node(new Position(oldPosition.X, oldPosition.Y));

            newHead.Value.ChangePosition(reversePosition);
            Checker.CheckIfSnakeIsOutside(newHead.Value, reversePosition);
            SnakeBody.AddLast(newHead);
        }
Exemple #5
0
        internal int moveSnake(Game myGame, int unitLength)
        {
            bool hitFood = false;
            SnakeBody index = this.head;
            int lastX = index.rectangleLeft;
            int lastY = index.rectangleTop;
            index.rectangleLeft += horizonMove;
            index.rectangleTop += verticalMove;
            if(head.rectangleLeft >= myGame.borderRight || head.rectangleLeft < myGame.borderLeft || head.rectangleTop >= myGame.borderBottom || head.rectangleTop < myGame.borderTop)
            {
                MessageBox.Show("too bad");
                Environment.Exit(1);
                return 1;
            }

            if (head.rectangleLeft == myGame.foodLocX && head.rectangleTop == myGame.foodLocY)
            {
                hitFood = true;
            }
            index = index.next;
            while (index != null)
            {
                int tmpX = index.rectangleLeft;
                int tmpY = index.rectangleTop;
                index.rectangleLeft = lastX;
                index.rectangleTop = lastY;
                lastX = tmpX;
                lastY = tmpY;
                index = index.next;
            }
            if (hitFood)
            {
                SnakeBody newBody = new SnakeBody(lastX, lastY, unitLength);
                newBody.bodyBlock.Fill = myGame.foodRec.Fill;
                tail.next = newBody;
                tail = newBody;
                myGame.generateFoodLoc(unitLength);
            }
            else
            {
                myGame.obstacle.RemoveAt(0);
            }
            Tuple<int, int> newTuple = new Tuple<int, int>(head.rectangleLeft, head.rectangleTop);
            if(myGame.obstacle.Contains(newTuple))
            {
                MessageBox.Show("too bad");
                Environment.Exit(1);
            }
            else
            {
                myGame.obstacle.Add(new Tuple<int, int>(head.rectangleLeft, head.rectangleTop));
            }
            return 0;
        }
        public void Grow(Position position)
        {
            var reverse     = new Position(position.X * -1, position.Y * -1);
            var oldPosition = SnakeBody.Tail.Value;

            var newHead = new Node(new Position(oldPosition.X, oldPosition.Y));

            newHead.Value.ChangePosition(reverse);
            BoundariesChecker.CheckBoundaries(newHead.Value, reverse);
            SnakeBody.AddLast(newHead);
        }
Exemple #7
0
 public void Draw()
 {
     SnakeBody.ForEach(n =>
     {
         string text = "*";
         if (n == SnakeBody.Head)
         {
             text = "@";
         }
         Helper.Helper.Write(n.Value, text);
     });
 }
 public void Draw()
 {
     SnakeBody.ForEach(n =>
     {
         var text = "*";
         if (n == SnakeBody.Head)
         {
             text = "@";
         }
         ConsoleHelper.Write(n.Value, text);
     });
 }
Exemple #9
0
        /// <summary>
        /// 创建蛇
        /// </summary>
        private void CreateSnake()
        {
            this.snake = new Snake(Config.MapCellWidth / 2, Config.MapCellHeight / 2, Config.SnakeInitLength);
            SnakeBody snakeBody = this.snake.tail;

            for (int i = 0; i < snake.length; i++)
            {
                GameArea.Children.Add(snakeBody.rectangle);
                snakeBody = snakeBody.prev;
            }
            this.snake.onSnakeTimerTick += this.onSnakeTimerTick;
            this.snake.onSnakeDead      += this.onSnakeIsDead;
        }
        public Boolean colision(SnakeBody datos)
        {
            bool choque      = true;
            int  diferenciaX = Math.Abs(this.x - datos.x);
            int  diferenciaY = Math.Abs(this.y - datos.y);

            if (diferenciaX >= 0 && diferenciaX < ancho && diferenciaY >= 0 && diferenciaY < ancho)
            {
                return(choque);
            }
            else
            {
                return(!choque);
            }
        }
Exemple #11
0
        /// <summary>
        /// ヘビを起動する。毎100millisecondsに再起動する。※interval = 100
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void moveNext(object sender, EventArgs e)
        {
            int x = s_body[front].Location.X;  // X Position
            int y = s_body[front].Location.Y;  // Y Position

            if (dx == 0 && dy == 0)
            {
                return;
            }
            if (game_over(x + dx, y + dy))
            {
                timer.Stop();
                MessageBox.Show("Congratulation! Your score is " + score,
                                "Game Over", MessageBoxButtons.OK);
                this.Close();
                return;
            }
            // 食物を食べるかどうかチェック
            if (collisionFood(x + dx, y + dy))
            {
                score        += 1;
                lblScore.Text = "Score: " + score.ToString();
                if (hits((y + dy) / 20, (x + dx) / 20))
                {
                    return;
                }
                SnakeBody head = new SnakeBody(x + dx, y + dy);
                front         = (front - 1 + 1250) % 1250;
                s_body[front] = head;
                isAvailable[head.Location.Y / 20, head.Location.X / 20] = true;
                Controls.Add(head);
                randomFood();
            }
            else
            {
                // ヘビの体を打つかどうかチェック
                if (hits((y + dy) / 20, (x + dx) / 20))
                {
                    return;
                }
                isAvailable[s_body[back].Location.Y / 20, s_body[back].Location.X / 20] = false;
                front                  = (front - 1 + 1250) % 1250;
                s_body[front]          = s_body[back];
                s_body[front].Location = new Point(x + dx, y + dy);
                back = (back - 1 + 1250) % 1250;
                isAvailable[(y + dy) / 20, (x + dx) / 20] = true;
            }
        }
Exemple #12
0
        public bool IsCanibal()
        {
            HashSet <Position> set = new HashSet <Position>();
            bool isCanibal         = false;

            SnakeBody.ForEach(n =>
            {
                if (set.Contains(n.Value))
                {
                    isCanibal = true;
                }
                set.Add(n.Value);
            });

            return(isCanibal);
        }
        public Snake(int x, int y)
        {
            head.x         = x;
            head.y         = y;
            head.direction = Directions.right;

            for (int i = 0; i < Length; i++)
            {
                SnakeBody newpart = new SnakeBody();
                newpart.setCrood(x - 1 - i, y);
                newpart.direction = Directions.right;



                newpart.stat = BodyStatic.horizontal;

                bodies.Add(newpart);
            }
        }
 public void CreateSnakeBody(int count, int x, int y)
 {
     sb[count] = new SnakeBody(x, y);
 }