public VectorAndLastDirection Move(Vector2 position, float speed, GameTime gameTime, bool isSnake = false, string lastDirection = "") { var kstate = Keyboard.GetState(); if (kstate.IsKeyDown(Keys.Up) || kstate.IsKeyDown(Keys.W)) { position.Y -= speed * ((float)gameTime.ElapsedGameTime.TotalSeconds); lastDirection = "up"; } else if (kstate.IsKeyDown(Keys.Down) || kstate.IsKeyDown(Keys.S)) { position.Y += speed * ((float)gameTime.ElapsedGameTime.TotalSeconds); lastDirection = "down"; } else if (kstate.IsKeyDown(Keys.Left) || kstate.IsKeyDown(Keys.A)) { position.X -= speed * ((float)gameTime.ElapsedGameTime.TotalSeconds); lastDirection = "left"; } else if (kstate.IsKeyDown(Keys.Right) || kstate.IsKeyDown(Keys.D)) { position.X += speed * ((float)gameTime.ElapsedGameTime.TotalSeconds); lastDirection = "right"; } if (kstate.GetPressedKeyCount() == 0 && isSnake) { if (lastDirection != string.Empty) { if (lastDirection == "up") { position.Y -= speed * ((float)gameTime.ElapsedGameTime.TotalSeconds); } if (lastDirection == "down") { position.Y += speed * ((float)gameTime.ElapsedGameTime.TotalSeconds); } if (lastDirection == "left") { position.X -= speed * ((float)gameTime.ElapsedGameTime.TotalSeconds); } if (lastDirection == "right") { position.X += speed * ((float)gameTime.ElapsedGameTime.TotalSeconds); } } } VectorAndLastDirection vld = new VectorAndLastDirection(); vld.position = position; vld.lastDirection = lastDirection; return(vld); }
protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } if (!gameOver) { // TODO: Add your update logic here VectorAndLastDirection vld = movement.Move(snakes.ElementAt(0).position, snakeSpeed, gameTime, true, snakes.ElementAt(0).lastDirection); snakes.ElementAt(0).prevPosition = snakes.ElementAt(0).position; snakes.ElementAt(0).position = vld.position; snakePositions[0] = vld.position; snakes.ElementAt(0).lastDirection = snakes.ElementAt(0).currDirection; snakes.ElementAt(0).currDirection = vld.lastDirection; for (int i = 1; i < snakePositions.Count; i++) { snakes.ElementAt(i).prevPosition = snakes.ElementAt(i).position; switch (snakes.ElementAt(i - 1).lastDirection) { case "up": snakes.ElementAt(i).position = new Vector2(snakes.ElementAt(i - 1).prevPosition.X, snakes.ElementAt(i - 1).prevPosition.Y); // + snakeTexture.Height); break; case "down": snakes.ElementAt(i).position = new Vector2(snakes.ElementAt(i - 1).prevPosition.X, snakes.ElementAt(i - 1).prevPosition.Y); // - snakeTexture.Height); break; case "left": snakes.ElementAt(i).position = new Vector2(snakes.ElementAt(i - 1).prevPosition.X, snakes.ElementAt(i - 1).prevPosition.Y); break; case "right": snakes.ElementAt(i).position = new Vector2(snakes.ElementAt(i - 1).prevPosition.X, snakes.ElementAt(i - 1).prevPosition.Y); break; } snakePositions[i] = snakes.ElementAt(i).position; snakes.ElementAt(i).lastDirection = snakes.ElementAt(i - 1).lastDirection; } for (int i = 0; i < apples.Count(); i++) { //Make sure it doesn't leave the bounds Vector2 keepAppleInBounds = utilities.KeepInBounds(apples[i].position, _graphics, apples[i].texture); apples[i].position.X = keepAppleInBounds.X; apples[i].position.Y = keepAppleInBounds.Y; if (utilities.Collides(snakeTexture, apples[i].texture, snakes.ElementAt(0).position, apples[i].position)) { Snake addSnake = new Snake(snakeTexture); switch (snakes.ElementAt(0).currDirection) { case "up": addSnake.position = new Vector2(snakes.ElementAt(snakes.Count() - 1).position.X, snakes.ElementAt(snakes.Count() - 1).position.Y); // + snakeTexture.Height); break; case "down": addSnake.position = new Vector2(snakes.ElementAt(snakes.Count() - 1).position.X, snakes.ElementAt(snakes.Count() - 1).position.Y); // - snakeTexture.Height); break; case "left": addSnake.position = new Vector2(snakes.ElementAt(snakes.Count() - 1).position.X, snakes.ElementAt(snakes.Count() - 1).position.Y); break; case "right": addSnake.position = new Vector2(snakes.ElementAt(snakes.Count() - 1).position.X, snakes.ElementAt(snakes.Count() - 1).position.Y); break; } snakePositions.Add(addSnake.position); snakes.Enqueue(addSnake); snakeSpeed += 8f; score += 100; apples.RemoveAt(i); } } if (apples.Count() == 0) { Apple apple = new Apple(appleTexture, new Vector2(random.Next(0, _graphics.PreferredBackBufferHeight), random.Next(0, _graphics.PreferredBackBufferWidth))); apples.Add(apple); } Vector2 keepSnakeInBounds = utilities.KeepInBounds(snakes.ElementAt(0).position, _graphics, snakeTexture); snakes.ElementAt(0).position = keepSnakeInBounds; if (utilities.CollideWithBounds(snakes.ElementAt(0).position, _graphics, snakeTexture)) { gameOver = true; } } base.Update(gameTime); }