Esempio n. 1
0
 public void SetCoords(int _x, int _y)
 {
     collisionMap[X, Y]   = false; //update collisionmap
     collisionMap[_x, _y] = true;
     X = _x;
     Y = _y;
     screenPosition = GameSurface.ToScreenCoords(_x, _y);
 }
Esempio n. 2
0
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            GameSurface.gridWidth  = 31;
            GameSurface.gridHeight = 31;
            var size = GraphicsDevice.PresentationParameters;

            GameSurface.Build(size.BackBufferWidth, size.BackBufferHeight);

            float x = (float)GameSurface.gridWidth / 2.0f, y = (float)GameSurface.gridHeight / 2.0f; //init pos

            snake = new Player((int)x, (int)y, 5);

            base.Initialize();
        }
Esempio n. 3
0
        public Player(int gridX, int gridY, int count = 1)
        {
            X = gridX;
            Y = gridY;
            for (int i = 0; i < count; i++)
            {
                segs.Add(new Segment(gridX - i, gridY));
                //collisionMap[x, y] = true;
                //segments.Add(GameSurface.ToScreenCoords(x, y));
            }

            lastSegmentIndex = segs.Count - 1;

            GameSurface.PlaceFood(20, 20);
        }
Esempio n. 4
0
        public void Update() //remove last element and add a new one in front
        {
            if (partStep >= 1.0f)
            {
                int wholeSteps = (int)partStep;
                partStep = 0.0f;

                while (wholeSteps > 0) //update until all steps are processed; might not be the best solution if the game is running faster
                {
                    switch (state)
                    {
                    case State.UP:
                        Y -= 1;
                        break;

                    case State.DOWN:
                        Y += 1;
                        break;

                    case State.LEFT:
                        X -= 1;
                        break;

                    case State.RIGHT:
                        X += 1;
                        break;
                    }

                    //Collision detection
                    int w = GameSurface.gridWidth;
                    int h = GameSurface.gridHeight;

                    if ((X < 0 || Y < 0 || X >= w || Y >= h) ||
                        Segment.collisionMap[X, Y])
                    {
                        state = State.CRASHED;
                        return;
                    }


                    var i = segs[lastSegmentIndex];                         //get last segment...

                    if (GameSurface.food.X == X && GameSurface.food.Y == Y) //food collision
                    {
                        segs.Insert(lastSegmentIndex, new Segment(i.X, i.Y));
                        collected++;
                        GameSurface.PlaceFood();
                        //AddSegment = false;
                    }
                    else
                    {
                        if (lastSegmentIndex > 0)
                        {
                            lastSegmentIndex--;
                        }
                        else
                        {
                            lastSegmentIndex = segs.Count - 1;
                        }
                    }
                    i.SetCoords(X, Y); //...so we can move it to the front



                    //var i = segments[segments.Count - 1];
                    //segments.RemoveAt(segments.Count - 1);
                    //segments.Insert(0, GameSurface.ToScreenCoords(X, Y));

                    wholeSteps--;
                }
            }
        }