Exemplo n.º 1
0
        public Form1()
        {
            InitializeComponent();
            FitEntireScreen();

            head = new SnakeHead();
        }
Exemplo n.º 2
0
        private void NextStep()
        {
            ClearSnake();

            _snake.Insert(0, SnakeHead + _direction);

            if (_snakeGrowth > 0)
            {
                _snakeGrowth -= 1;
            }
            else
            {
                _snake.RemoveAt(_snake.Count - 1);
            }

            CheckSnakeHeadCoordinates();

            if (SnakeHead.Equals(_rabbit))
            {
                _snakeGrowth += 1;
                PlaceRabbit();
            }

            DrawSnake();

            if (GameOver())
            {
                Environment.Exit(1);
            }
        }
Exemplo n.º 3
0
        // Schlange erzeugen (nur Kopf)
        internal void AddSnake()
        {
            // den Schlangenkopf erzeugen und positionieren
            SnakeHead mySnakeHead = new SnakeHead(new Point(playground.ActualWidth / 2, playground.ActualHeight / 2), Colors.Red);

            // in die Liste setzen
            snake.Add(mySnakeHead);
        }
Exemplo n.º 4
0
        public SnakeGame()
        {
            Grid   = new Grid(new Size(20, 20));
            Drawer = new ConsoleDrawer(Grid);

            var head = new SnakeHead();

            Grid.Set(GetRandomPoint(), head);
            Snake = new Snake(head);
        }
Exemplo n.º 5
0
Arquivo: World.cs Projeto: K0bin/Snake
        public World(Game game, IRenderItemFactory factory, GameRules rules)
        {
            this.game = game;
            this.factory = factory;

            Head = new SnakeHead(game, factory);
            CreateWorld(game, factory, rules, rules.WorldSize);

            Random random = new Random();
            Treat = new Treat(game, factory, new Vector2I(random.Next(1, rules.WorldSize.X - 1), random.Next(1, rules.WorldSize.Y - 1)));
        }
Exemplo n.º 6
0
 /// <summary>
 /// Adds snake components to the snake so it gets longer
 /// </summary>
 /// <param name="count">Amount of snake parts to add</param>
 private void AddSnakePart(int count = 1)
 {
     if (_snake.Count == 0 && _snakeHead == null)
     {
         _snakeHead = new SnakeHead(Config.StartPosition);
         count--;
     }
     for (int i = 0; i < count; i++)
     {
         _snake.Add(new SnakeComponent(
                        (_snake.Count > 0 ? _snake.Last() : _snakeHead).PositionInt));
     }
 }
Exemplo n.º 7
0
        public int GetWallDistance(Direction direction)
        {
            switch (direction)
            {
            case Direction.Left:
                return(SnakeHead.GetPosition().X);

            case Direction.Right:
                return(_gameSize.Width - SnakeHead.GetPosition().X);

            case Direction.Up:
                return(SnakeHead.GetPosition().X);

            case Direction.Down:
                return(_gameSize.Height - SnakeHead.GetPosition().Y);

            default:
                throw new ArgumentOutOfRangeException(nameof(direction), direction, null);
            }
        }
Exemplo n.º 8
0
        public int GetFoodDistance(Direction direction)
        {
            int FixNegative(int value) => value < 0 ? Math.Abs(value) * 2 : value;

            switch (direction)
            {
            case Direction.Left:
                return(FixNegative(SnakeHead.GetPosition().X - CurrentFood.GetPosition().X));

            case Direction.Right:
                return(FixNegative(CurrentFood.GetPosition().X - SnakeHead.GetPosition().X));

            case Direction.Up:
                return(FixNegative(SnakeHead.GetPosition().Y - CurrentFood.GetPosition().Y));

            case Direction.Down:
                return(FixNegative(CurrentFood.GetPosition().Y - SnakeHead.GetPosition().Y));

            default:
                throw new ArgumentOutOfRangeException(nameof(direction), direction, null);
            }
        }
Exemplo n.º 9
0
 public Snake(SnakeHead head)
 {
     Head = head;
 }
Exemplo n.º 10
0
 //Function: LoadContent
 protected override void LoadContent()
 {
     spriteBatch = new SpriteBatch(Game.GraphicsDevice);
     //Get the actual sprites related to the snake and fruit
     //Set appropriate game logic objects to appropriate snake body parts
     //Note: The "tail" is never formally kept track of, it was just a convienent
     //name for the second body part of the initial snake.
     snakeAssets();
     snakeHeadActive = snakeHeadDown;
     snakeBody[0] = initialSnakeBody;
     snakeBody[1] = initialSnakeTail;
     chooseFruit();
     base.LoadContent();
 }
Exemplo n.º 11
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);
        }
Exemplo n.º 12
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);
        }