示例#1
0
 private void TickExpand()
 {
     if (PendingExpand)
     {
         PendingExpand = false;
         if (Snake.Head.NextComponent == null)
         {
             var tp    = Snake.Head.Direction.GetTransformPoint();
             var point = Snake.Head.Point.Add(new Point(-tp.X, -tp.Y));
             var body  = new SnakeBody();
             Snake.Head.NextComponent = body;
             Grid.Set(point, body);
         }
     }
     else
     {
         var components = Snake.ToArray();
         var i          = components.Length - 2;
         if (i >= 0)
         {
             components[i].NextComponent = null;
             Grid.Set(components[i + 1].Point, null);
         }
     }
 }
示例#2
0
        private bool TickMove()
        {
            var nextPoint = Snake.GetNextPoint();
            var obj       = Grid.Get(nextPoint);

            if (Grid.IsOutOfGrid(nextPoint) || obj is SnakeComponent)
            {
                return(false);
            }
            if (obj is Food)
            {
                PendingExpand = true;
            }

            var currentPoint = Snake.Head.Point;
            var previousObj  = Snake.Head.NextComponent;

            Grid.Set(nextPoint, Snake.Head);
            if (Snake.Head.NextComponent == null)
            {
                Grid.Set(currentPoint, null);
                return(true);
            }

            var newBody = new SnakeBody {
                NextComponent = previousObj
            };

            Grid.Set(currentPoint, newBody);

            Snake.Head.NextComponent = newBody;
            return(true);
        }
示例#3
0
 public Form1()
 {
     InitializeComponent();
     DoubleBuffered = true;
     graphics       = pictureBox1.CreateGraphics();
     snake          = new SnakeBody(30, 30);
     food           = new Food();
 }
示例#4
0
 public void RemoveSnakeBody()
 {
     for (int i = 0; i < SnakeBody.Count(); i++)
     {
         Console.SetCursorPosition(SnakeBodyY[i], SnakeBodyX[i]);
         Console.Write(" ");
     }
 }
示例#5
0
 public void TrainingBorder()
 {
     if (snake.GetX() < 0 || snake.GetY() < 0 || snake.GetX() > pictureBox1.Width || snake.GetY() > pictureBox1.Height)
     {
         snake = new SnakeBody(30, 30);
         food  = new Food();
     }
 }
示例#6
0
 private static void Initialize()
 {
     Console.Clear();
     _area.Print();
     _area.Score = 0;
     _area.PrintScore();
     _snake = new SnakeBody(15, 12, 10);
     _input.Clear();
     _food.SpawnNew(_area, _snake);
     _stopwatch.Restart();
 }
示例#7
0
        public bool DetectBodyCollision()
        {
            for (int i = 0; i < SnakeBody.Count(); i++)
            {
                if (XPosition == SnakeBodyX[i] && YPosition == SnakeBodyY[i])
                {
                    return(false);
                }
            }

            return(true);
        }
示例#8
0
        /// <summary>
        /// 根据指定的参数清除一个"点"
        /// </summary>
        /// <param name="bdy">蛇身点</param>
        private void _SnakeClearPoint(SnakeBody bdy)
        {
            int x = 0, y = 0;

            _ToRealPoint(bdy.x, bdy.y, out x, out y);
            Rectangle  rect  = new Rectangle(x, y, _BodySize, _BodySize);
            SolidBrush brush = new SolidBrush(_Windows.BackColor);//用背景颜色覆盖当前点

            if (bdy.exist)
            {
                //不需要考虑形状,全部用正方形填充
                _Graphics.FillRectangle(brush, rect);
            }
        }
示例#9
0
        /// <summary>
        /// 贪吃蛇游戏初始化
        /// </summary>
        public void SnakeGameRenew()
        {
            SnakeBody _body;

            _SnakeList.Clear();//清空所有数据

            //初始化游戏运行数据
            _Direction = SnakeDirection.Right;
            _Life      = SnakeLife.Live;
            _Score     = 0;

            //初始化蛇头
            _body = new SnakeBody(0, 0, _SColor, true, SnakeProperty.Head);
            _SnakeList.Add(_body);

            //创建初始蛇身
            int initLength = 3;

            if (initLength > 1)
            {
                int dx = 0, dy = 0;
                int x = _SnakeList[0].x, y = _SnakeList[0].y;
                _GetVariation(_Direction, out dy, out dy);
                for (int i = 1; i < initLength; i++)
                {
                    x    -= (dx * _BodySize);
                    y    -= (dy * _BodySize);
                    _body = new SnakeBody(x, y, _SColor, true, SnakeProperty.Body);
                    _SnakeList.Add(_body);
                }
            }

            //初始化食物
            int fx = 0, fy = 0;

            _Food.color = _SColor;
            _Food.exist = true;
            _Food.proty = SnakeProperty.Food;
            _SnakeGetRandomXY(out fx, out fy);
            _Food.x = fx;
            _Food.y = fy;

            //重绘游戏画面
            _SnakeClearWindow();
            _SnakeReDrawGame();
        }
示例#10
0
        public void DrawSnake()
        {
            Console.SetCursorPosition(GetPreviousYPosition(), GetPreviousXPosition());
            Console.Write(" ");

            Console.SetCursorPosition(GetYPosition(), GetXPosition());
            Console.Write(GetSnakeHead());

            for (int i = 0; i < PreviousBodyXPositions.Count(); i++)
            {
                Console.SetCursorPosition(PreviousBodyYPositions[i], PreviousBodyXPositions[i]);
                Console.Write(" ");
            }

            for (int i = 0; i < SnakeBody.Count(); i++)
            {
                Console.SetCursorPosition(SnakeBodyY[i], SnakeBodyX[i]);
                Console.Write(SnakeBody[i]);
            }
        }
示例#11
0
        private void aRestartButton_Click(object sender, EventArgs e)
        {
            SoundPlayer replay = new SoundPlayer(Properties.Resources.Replay);

            replay.Play();
            CheckForRecord();
            for (int i = snake.tail.Count - 1; i > 0; i--)
            {
                Controls.Remove(snake.tail[i]);
            }

            InitializeSnake();

            snake = new SnakeBody(aScoreLabel, aRecordLabel, this, aSnakeHead, aSegment1, aSegment2, aSegment3, aSegment4);
            food  = new Food(this, aSuperFood, aFood, snake);
            food.picture.Visible = false;
            aTimer.Enabled       = false;
            timeLeft             = 10;
            timeTick             = 0;
        }
示例#12
0
        public aForm()
        {
            _timeLeft = 30;
            InitializeComponent();
            snake           = new SnakeBody(aScoreLabel, aRecordLabel, this, aSnakeHead, aSegment1, aSegment2, aSegment3, aSegment4);
            food            = new Food(this, aSuperFood, aFood, snake);
            bordersEnabled  = true;
            timeModeEnabled = false;
            timeLeft        = 10;

            if (!File.Exists("score.ini"))
            {
                using (StreamWriter writer = new StreamWriter("score.ini"))
                {
                    writer.Write("0\n0\n0\n0");
                    writer.Close();
                }
            }

            CheckForRecord();
        }
示例#13
0
        public void Draw(Point currentPosition)
        {
            Ellipse e = new Ellipse();

            e.Fill   = Color;
            e.Width  = Size;
            e.Height = Size;
            //e.Stroke = Brushes.Black;

            Canvas.SetTop(e, currentPosition.Y);
            Canvas.SetLeft(e, currentPosition.X);

            int count = gameCanvas.Children.Count;

            this.gameCanvas.Children.Add(e);
            this.SnakeBody.Add(currentPosition);

            if (count > Length)
            {
                gameCanvas.Children.RemoveAt(count - Length + foodAmount - 1);
                SnakeBody.RemoveAt(count - Length);
            }
        }
示例#14
0
 /// <summary>
 /// IComparable的接口实现
 /// </summary>
 /// <param name="obj"></param>
 /// <returns></returns>
 public int CompareTo(object obj)
 {
     if (obj is SnakeBody)
     {
         SnakeBody snakeBody = obj as SnakeBody;
         if (SnakeBodyScore < snakeBody.SnakeBodyScore)
         {
             return(1);
         }
         else if (SnakeBodyScore > snakeBody.SnakeBodyScore)
         {
             return(-1);
         }
         else
         {
             return(0);
         }
     }
     else
     {
         throw new ArgumentException("Object to compare to is not a SnakeBody object");
     }
 }
示例#15
0
        private void MakeSuperFood(PictureBox super, SnakeBody snake)
        {
            bool remake;

            do
            {
                remake         = false;
                x              = rand.Next(26);
                y              = rand.Next(26);
                super.Location = new Point(x * 20 + 10, y * 20 + 10);
                form.Controls.Add(super);
                super.BringToFront();
                super.Visible    = true;
                superFoodLocator = new Rectangle(super.Location.X + super.Width / 2, super.Location.Y + super.Height / 2, 2, 2);
                for (int i = 0; i < snake.tail.Count - 1; i++)
                {
                    if (snake.tail[i].Bounds.IntersectsWith(superFoodLocator))
                    {
                        remake = true;
                        break;
                    }
                }
            } while (remake);
        }
示例#16
0
文件: World.cs 项目: K0bin/Snake
 public void RemoveBody(SnakeBody body)
 {
     if (body != null && bodies != null)
         bodies.Remove(body);
 }
示例#17
0
 public SnakeObj()
 {
     Body = new SnakeBody();
 }
示例#18
0
        /// <summary>
        /// 蛇身前进一步
        /// </summary>
        /// <param name="dir">前进方向</param>
        /// <returns>蛇生命值</returns>
        private SnakeLife SnakeMoveStep(SnakeDirection dir)
        {
            SnakeBody tail = new SnakeBody(0, 0, _SColor, true, SnakeProperty.Body);

            tail.x = _SnakeList[_SnakeList.Count - 1].x;//保存蛇尾坐标
            tail.y = _SnakeList[_SnakeList.Count - 1].y;


            SnakeBody body_t = new SnakeBody(0, 0, _SColor, true, SnakeProperty.Body);

            for (int i = (_SnakeList.Count - 1); i > 0; i--)
            {//更新蛇身坐标
                body_t.x      = _SnakeList[i - 1].x;
                body_t.y      = _SnakeList[i - 1].y;
                _SnakeList[i] = body_t;
            }

            int dx = 0, dy = 0;

            _GetVariation(dir, out dx, out dy);                                        //根据移动方向获取移动xy轴上的增量
            SnakeBody body_h = new SnakeBody(0, 0, _SColor, true, SnakeProperty.Head); //更新蛇头坐标

            body_h.x = _SnakeList[0].x + dx;
            body_h.y = _SnakeList[0].y + dy;

            //穿墙处理
            int _maxX = _Windows.Size.Width / _BodySize;
            int _maxY = _Windows.Size.Height / _BodySize;

            if (_CrossWall == false)
            {
                if ((body_h.x >= _maxX) || (body_h.x < 0) ||
                    (body_h.y >= _maxY) || (body_h.y < 0))
                {
                    _Life = SnakeLife.Die;
                }
            }

            if (body_h.x >= _maxX)
            {
                body_h.x = 0;
            }
            if (body_h.x < 0)
            {
                body_h.x = _maxX - 1;
            }
            if (body_h.y >= _maxY)
            {
                body_h.y = 0;
            }
            if (body_h.y < 0)
            {
                body_h.y = _maxY - 1;
            }

            _SnakeList[0] = body_h;//更新蛇头坐标

            //判断是否吃到食物或咬到自己(在未更新坐标之前预判)
            int           foodx = 0, foody = 0;
            SnakeProperty p = _SnakeGetPointProperty(_SnakeList[0].x, _SnakeList[0].y);

            switch (p)
            {
            case SnakeProperty.Food:
                Console.WriteLine("SnakeProperty: " + p.ToString() + " SnakeLife: " + _Life.ToString());
                //吃到食物后产生一个食物,蛇身长度增加
                if (_SnakeGetRandomXY(out foodx, out foody))    //创建食物
                {
                    _Food.x = foodx;
                    _Food.y = foody;
                    _SnakeList.Add(tail); //增加蛇身长度
                    _Score++;             //得分增加
                    _Life = SnakeLife.Live;
                }
                else
                {
                    _Life = SnakeLife.Win;
                    //return _Life;
                }
                break;

            case SnakeProperty.Body:
            case SnakeProperty.Wall:
                Console.WriteLine("SnakeProperty: " + p.ToString() + " SnakeLife: " + _Life.ToString());
                _Life = SnakeLife.Die;    //死亡
                //return _Life;
                break;

            case SnakeProperty.Head:
            case SnakeProperty.Null:
            default:
                _Life = SnakeLife.Live;
                break;
            }

            _SnakeClearPoint(tail); //清除蛇尾
            _SnakeReDrawGame();     //更新显示蛇身

            return(_Life);
        }
示例#19
0
        public override void Update(GameTime gameTime)
        {
            if (Keyboard.GetState().IsKeyDown(Keys.Left) && snakeHeadActive.entityType != 7 && snakeHeadActive.textureImage != Game.Content.Load<Texture2D>(@"images\snakehead-right"))
            {
                snakeHeadActive = snakeHeadLeft;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Right) && snakeHeadActive.entityType != 6 && snakeHeadActive.textureImage != Game.Content.Load<Texture2D>(@"images\snakehead-left"))
            {
                snakeHeadActive = snakeHeadRight;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Up) && snakeHeadActive.entityType != 5 && snakeHeadActive.textureImage != Game.Content.Load<Texture2D>(@"images\snakehead-down"))
            {
                snakeHeadActive = snakeHeadUp;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Down) && snakeHeadActive.entityType != 4 && snakeHeadActive.textureImage != Game.Content.Load<Texture2D>(@"images\snakehead-up"))
            {
                snakeHeadActive = snakeHeadDown;
            }
            snakeHeadActive.Update(gameTime, Game.Window.ClientBounds);

            // Update Snake Body
            for (int i = 0; i < snakeLength - 1; i++)
            {
                snakeBody[i].Update(gameTime, Game.Window.ClientBounds);
                snakeBody[i].position = new Vector2(Game1.spriteManager.snakeArray[i + 1].X * 16.0f, Game1.spriteManager.snakeArray[i + 1].Y * 16.0f);
            }

            // Copy the present location of the snake into a temporary location for reading
            for (int i = 0; i < snakeLength; i++)
            {
                snakeCopyCurrentArray[i] = snakeArray[i];
            }

            // Overwrite the current snake location with the new snake location
            for (int i = 0; i < snakeLength-1; i++)
            {
                snakeArray[i + 1] = snakeCopyCurrentArray[i];
            }

            //------
            //The commented code below is a step-by-step example of how this process
            //updates the snake, using only the initial snake pieces.
            //snakeCopyCurrentArray[0] = snakeArray[0];
            //snakeCopyCurrentArray[1] = snakeArray[1];
            //snakeCopyCurrentArray[2] = snakeArray[2];

            //snakeArray[1] = snakeCopyCurrentArray[0];
            //snakeArray[2] = snakeCopyCurrentArray[1];
            //------
            // Update Fruit
            currentFruit.Update(gameTime, Game.Window.ClientBounds);

            // Check for fruit collision
            // Update Score, Update Fruit Location
            if (new Vector2((snakeHeadActive.position.X) / 16,(snakeHeadActive.position.Y) / 16) == (new Vector2((int)(currentFruit.position.X) / 16, (int)(currentFruit.position.Y) / 16)))
            {
                Game1.audioEating.Play();
                System.Console.WriteLine("Fruit collected!");
                //Update Score
                switch (currentFruit.entityType)
                {
                    case(0):
                        score += 100;
                        break;
                    case(1):
                        score += 250;
                        break;
                    case(2):
                        score += 500;
                        break;
                }

                // Add Snake Bit
                snakeLength++;
                snakeArray[snakeLength - 1] = snakeArray[snakeLength - 2];
                snakeBody[snakeLength - 2] = new SnakeBody(Game.Content.Load<Texture2D>(@"images\snakebody"),
                       new Vector2(snakeArray[snakeLength - 1].X * 16, snakeArray[snakeLength - 1].Y * 16), new Point(16, 16), new Vector2(4f, 4f), new Vector2(1, 1), Color.CornflowerBlue, new Vector2(0, 0), 8);

                // Replace fruit location with snake's head
                if(snakeHeadActive.textureImage == Game.Content.Load<Texture2D>(@"images\snakehead-right")){
                    snakeHeadActive = new SnakeHead(Game.Content.Load<Texture2D>(@"images\snakehead-right"),
                        new Vector2(snakeHeadPosition.X, snakeHeadPosition.Y),
                        new Point(16, 16), new Vector2(4f, 4f), new Vector2(1, 1), Color.CornflowerBlue, new Vector2(0, 0), 7);
                }
                if(snakeHeadActive.textureImage == Game.Content.Load<Texture2D>(@"images\snakehead-left")){
                    snakeHeadActive = new SnakeHead(Game.Content.Load<Texture2D>(@"images\snakehead-left"),
                        new Vector2(snakeHeadPosition.X, snakeHeadPosition.Y),
                        new Point(16, 16), new Vector2(4f, 4f), new Vector2(1, 1), Color.CornflowerBlue, new Vector2(0, 0), 6);
                }
                if(snakeHeadActive.textureImage == Game.Content.Load<Texture2D>(@"images\snakehead-up")){
                    snakeHeadActive = new SnakeHead(Game.Content.Load<Texture2D>(@"images\snakehead-up"),
                        new Vector2(snakeHeadPosition.X, snakeHeadPosition.Y),
                        new Point(16, 16), new Vector2(4f, 4f), new Vector2(1, 1), Color.CornflowerBlue, new Vector2(0, 0), 4);
                }
                if(snakeHeadActive.textureImage == Game.Content.Load<Texture2D>(@"images\snakehead-down")){
                    snakeHeadActive = new SnakeHead(Game.Content.Load<Texture2D>(@"images\snakehead-down"),
                        new Vector2(snakeHeadPosition.X, snakeHeadPosition.Y),
                        new Point(16, 16), new Vector2(4f, 4f), new Vector2(1, 1), Color.CornflowerBlue, new Vector2(0, 0), 5);
                }
                // Replace fruit location with snake's current head
                snakeArray[0] = (new Vector2((int)(currentFruit.position.X) / 16, (int)(currentFruit.position.Y) / 16));
                // Regenerate Fruit
                chooseFruit();
            }
            base.Update(gameTime);
        }
示例#20
0
        // Load snake head assets
        public void snakeAssets()
        {
            snakeHeadUp = new SnakeHead(Game.Content.Load<Texture2D>(@"images\snakehead-up"),
                snakeHeadPosition, new Point(16, 16), new Vector2(4f, 4f), new Vector2(1, 1), Color.CornflowerBlue, new Vector2(0, 0), 4);

            snakeHeadDown = new SnakeHead(Game.Content.Load<Texture2D>(@"images\snakehead-down"),
                snakeHeadPosition, new Point(16, 16), new Vector2(4f, 4f), new Vector2(1, 1), Color.CornflowerBlue, new Vector2(0, 0), 5);

            snakeHeadLeft = new SnakeHead(Game.Content.Load<Texture2D>(@"images\snakehead-left"),
                snakeHeadPosition, new Point(16, 16), new Vector2(4f, 4f), new Vector2(1, 1), Color.CornflowerBlue, new Vector2(0, 0), 6);

            snakeHeadRight = new SnakeHead(Game.Content.Load<Texture2D>(@"images\snakehead-right"),
                snakeHeadPosition, new Point(16, 16), new Vector2(4f, 4f), new Vector2(1, 1), Color.CornflowerBlue, new Vector2(0, 0), 7);

            initialSnakeBody = new SnakeBody(Game.Content.Load<Texture2D>(@"images\snakebody"),
                new Vector2(0,16), new Point(16, 16), new Vector2(4f, 4f), new Vector2(1, 1), Color.CornflowerBlue, new Vector2(0, 0), 8);

            initialSnakeTail = new SnakeBody(Game.Content.Load<Texture2D>(@"images\snakebody"),
                new Vector2(0,0), new Point(16, 16), new Vector2(4f, 4f), new Vector2(1, 1), Color.CornflowerBlue, new Vector2(0, 0), 8);
        }
示例#21
0
文件: World.cs 项目: K0bin/Snake
 public void AddBody(SnakeBody body)
 {
     if (body != null && bodies != null)
         bodies.Add(body);
 }
示例#22
0
 public void AddSnakeBody()
 {
     SnakeBody.Add("*");
 }