public static Point GetNewBlockPoint(SnakePlayer player)
        {
            Point result = new Point();
            SnakeDirection dir = player.Body.Blocks.Count > 0 ? player.Body.Blocks.Last().Direction : player.Direction;
            Point lastPos = player.Body.Blocks.Count > 0 ? player.Body.Blocks.Last().Position : player.Head.Position;
            switch (dir)
            {
                case SnakeDirection.Up:
                    result = new Point(lastPos.X, lastPos.Y + 1);
                    break;
                case SnakeDirection.Right:
                    result = new Point(lastPos.X - 1, lastPos.Y);
                    break;
                case SnakeDirection.Down:
                    result = new Point(lastPos.X, lastPos.Y - 1);
                    break;
                case SnakeDirection.Left:
                    result = new Point(lastPos.X + 1, lastPos.Y);
                    break;
                default:
                    break;
            }

            return result;
        }
        public SnakePlayer BuildSnake()
        {
            SnakePlayer player = new SnakePlayer(ScreenManager);
            player.Speed = 50f;
            player.Direction = SnakeDirection.Down;
            player.PreviousDirection = SnakeDirection.Down;
            player.Name = "Default";
            player.Head = new SnakeHead(new Point(5, 5));
            player.Head.Texture = Content.Load<Texture2D>("snakehead");
            player.Head.Source = player.Head.Texture.Bounds;
            player.Body = new SnakeBody();
            player.Body.Texture = Content.Load<Texture2D>("snakeblock");
            player.Body.Source = player.Body.Texture.Bounds;

            for (int i = 0; i < 5; i++)
            {
                player.AddSnakeBlock();
            }

            return player;
        }
Exemple #3
0
        private void UpdatePlayer(GameTime gameTime, SnakePlayer player)
        {
            player.Update(gameTime);
            if (player.Head.Position.Y > map.Height - 1 ||
                player.Head.Position.X > map.Width - 1 ||
                player.Head.Position.Y < 0 ||
                player.Head.Position.X < 0)
            {

                switch (player.Direction)
                {
                    case SnakeDirection.Up:
                        player.Head.Position = new Point(player.Head.Position.X, map.Height - 1);
                        break;
                    case SnakeDirection.Right:
                        player.Head.Position = new Point(0, player.Head.Position.Y);
                        break;
                    case SnakeDirection.Down:
                        player.Head.Position = new Point(player.Head.Position.X, 0);
                        break;
                    case SnakeDirection.Left:
                        player.Head.Position = new Point(map.Width - 1, player.Head.Position.Y);
                        break;
                    default:
                        break;
                }
                //worldReset = true;
            }

                MapCell currentCell = map.Cells[player.Head.Position.Y * map.Width + player.Head.Position.X];
                Tile currentTile = null;
                int currentX = player.Head.Position.X;
                int currentY = player.Head.Position.Y;
                if (currentX > 0 && currentX < tiledMap.Width - 1 && currentY > 0 && currentY < tiledMap.Height)
                {
                    if (tiledMap.TileLayers["Environment"].Tiles[currentX][currentY] != null)
                        currentTile = tiledMap.SourceTiles[tiledMap.TileLayers["Environment"].Tiles[currentX][currentY].SourceID];
                }
                else
                {
                    currentTile = null;
                }

                if (currentTile != null && currentTile.Properties.Count > 0 && currentTile.Properties.Keys.Contains("Col"))
                {
                    PlayerEnvironmentCollision(player);
                }

                if (currentTile != null && currentTile.Properties.Count > 0 && currentTile.Properties.Keys.Contains("Wall"))
                {
                    if(!worldReset)
                        worldReset = true;
                }
                if (currentCell != null && currentCell.ContainsPickup)
                {
                    PickupFood(player, currentCell);
                }

                if (HasPlayerHitSelf(player))
                {
                    if (!worldReset)
                        worldReset = true;
                }
        }
Exemple #4
0
 private void PlayerEnvironmentCollision(SnakePlayer player)
 {
     if (player.Body.Blocks.Count > 4)
     {
         if (!refreshHitCells)
         {
             int limit = player.Body.Blocks.Count - 5;
             for (int i = player.Body.Blocks.Count - 1; i > limit; i--)
             {
                 player.Body.Blocks.RemoveAt(i);
                 player.Score -= 40;
             }
             refreshHitCells = true;
         }
     }
     else
     {
         if (!worldReset)
             worldReset = true;
     }
 }
Exemple #5
0
 private void PickupFood(SnakePlayer player, MapCell currentCell)
 {
     particleSys.CreatePlayerExplosion(SnakeHelper.MapToScreen(
         player.Head.Position,
         ScreenManager.Game.Content.Load<Texture2D>("smoke"),
         ScreenManager.Game.GraphicsDevice));
     player.Score += currentCell.Pickup.PointsValue;
     player.AddSnakeBlock();
     player.Speed += 5;
     currentCell.Pickup = null;
     currentCell.ContainsPickup = false;
     map.FoodCount--;
     player.TotalFoodCollected++;
 }
Exemple #6
0
 private bool HasPlayerHitSelf(SnakePlayer player)
 {
     foreach (var block in player.Body.Blocks)
     {
         if (block.Position == player.Head.Position)
         {
             return true;
         }
     }
     return false;
 }