public void ChangeDirection(string key) { if (!(key == "Up" || key == "Down" || key == "Left" || key == "Right")) { return; } Snake.Direction direction = Snake.Direction.Right; switch (key) { case "Up": direction = Snake.Direction.Up; break; case "Down": direction = Snake.Direction.Down; break; case "Left": direction = Snake.Direction.Left; break; case "Right": direction = Snake.Direction.Right; break; } snake.ChangeDirection(direction); }
/* The main loop for the game. The loop will: * - read input from the user * - move the snake one step in a direction */ public void gameLoop() { ConsoleKeyInfo input; Snake.Direction direction = snake.snakeDirection; while (true) { if (exitGame == true) { break; } /* Read all user input that has been buffered up to this point. * Only the last valid input will be used. */ while (Console.KeyAvailable == true) { input = Console.ReadKey(true); if (input.Key == ConsoleKey.W) { direction = Snake.Direction.Up; } else if (input.Key == ConsoleKey.S) { direction = Snake.Direction.Down; } else if (input.Key == ConsoleKey.A) { direction = Snake.Direction.Left; } else if (input.Key == ConsoleKey.D) { direction = Snake.Direction.Right; } } if (DateTime.Now > snakeNextMove) { /* Time for the snake to move one step */ if (snake.move(direction) == false) { /* The snake hit something */ gameOver(); /* Reset the user's input. */ direction = snake.snakeDirection; } snakeNextMove = DateTime.Now.AddMilliseconds(snakeDelay); } displayScore(); Thread.Sleep(readInputDelay); } }
internal void Remove(Snake snake, Snake.Direction direction) //Only happens if the snake is crashing into the wall... { switch (direction) { case Snake.Direction.Down: snake.snakeBody.First().Y -= 1; break; case Snake.Direction.Up: snake.snakeBody.First().Y += 1; break; case Snake.Direction.Left: snake.snakeBody.First().X += 1; break; case Snake.Direction.Right: snake.snakeBody.First().X -= 1; break; } Player p = (Player)collidables[snake.snakeBody.First().X, snake.snakeBody.First().Y]; p?.Remove(this); }
public void PlayTurn(bool display = false) { ++turn; --ttl; Snake.Direction dir = bot.PlayTurn(display); if ((int)dir == -(int)snake.Dir) { dir = snake.Dir; } snake.Dir = dir; try { snake.Move(grid); } catch (ArgumentException) { lost = true; } if (ttl <= 0) { lost = true; } UpdateGrid(); if (appleX == snake.Head.X && appleY == snake.Head.Y) { ++score; snake.Grow(); if (x * y - 5 - score > 0) { NewApple(); } ttl = 200; } if (display) { PrintGrid(); } }
public void MoveInDirection(Snake.Direction direction) { switch (direction) { case Snake.Direction.left: position = new Point(position.X - 1, position.Y); break; case Snake.Direction.right: position = new Point(position.X + 1, position.Y); break; case Snake.Direction.up: position = new Point(position.X, position.Y - 1); break; case Snake.Direction.down: position = new Point(position.X, position.Y + 1); break; } }
public static Vector2 Move(this Vector2 pos, Snake.Direction direction, int size) { Vector2 newPos = pos; if (direction == Snake.Direction.North) { newPos -= new Vector2(0, size); } else if (direction == Snake.Direction.South) { newPos += new Vector2(0, size); } else if (direction == Snake.Direction.West) { newPos -= new Vector2(size, 0); } else if (direction == Snake.Direction.East) { newPos += new Vector2(size, 0); } return(newPos); }
//public Task Start(IProgress<Field> progress) //{ // while (true) // { // Tick(); // progress.Report(this.Field); // } //} public GameEvent Tick(Snake.Direction direction) { return(Field.Update(direction)); }
public Game.GameEvent Update(Snake.Direction direction) { FoodBlock food = this.Blocks.Cast <Block>().Single(b => b is FoodBlock) as FoodBlock; bool foodEaten = false; Snake.CurrentHeadDirection = direction; if (Snake.Blocks.Any(block => block.Coords.Equals(Snake.Head.Coords) && block.Index != 0)) { return(Game.GameEvent.Lose); } else if (Snake.Head.Coords.Row == 0 && this.Snake.CurrentHeadDirection == Snake.Direction.Up) { Snake.MoveTo(this.Blocks[this.Height - 1, Snake.Head.Coords.Column]); } else if (Snake.Head.Coords.Row == this.Height - 1 && this.Snake.CurrentHeadDirection == Snake.Direction.Down) { Snake.MoveTo(this.Blocks[0, Snake.Head.Coords.Column]); } else if (Snake.Head.Coords.Column == 0 && this.Snake.CurrentHeadDirection == Snake.Direction.Left) { Snake.MoveTo(this.Blocks[Snake.Head.Coords.Row, this.Width - 1]); } else if (Snake.Head.Coords.Column == this.Width - 1 && this.Snake.CurrentHeadDirection == Snake.Direction.Right) { Snake.MoveTo(this.Blocks[Snake.Head.Coords.Row, 0]); } else { Snake.Move(); } // if snake ate food, it grows if (Snake.Head.Coords.Equals(food.Coords)) { Snake.Grow(); foodEaten = true; for (int i = 0; i < Snake.Blocks.Count; ++i) { if (Snake.Blocks[i].Coords.Row < 0) { Snake.Blocks[i] = new SnakeBlock(new Coordinates(this.Height - 1, Snake.Blocks[i].Coords.Column), i); } else if (Snake.Blocks[i].Coords.Row == Height) { Snake.Blocks[i] = new SnakeBlock(new Coordinates(0, Snake.Blocks[i].Coords.Column), i); } else if (Snake.Blocks[i].Coords.Column < 0) { Snake.Blocks[i] = new SnakeBlock(new Coordinates(Snake.Blocks[i].Coords.Row, this.Width - 1), i); } else if (Snake.Blocks[i].Coords.Column == Width) { Snake.Blocks[i] = new SnakeBlock(new Coordinates(Snake.Blocks[i].Coords.Row, 0), i); } } } // temporary: recreating all blocks CreateBlocks(); // updating snake UpdateSnake(); // recreating food if eaten if (foodEaten) { CreateFood(); return(Game.GameEvent.Upscore); } else // just updating { Blocks[food.Coords.Row, food.Coords.Column] = food; return(Game.GameEvent.None); } }
public void SetDirection(Snake.Direction dir) { snake.Dir = dir; }
public void Turnhead(Snake.Direction d) { _snake.TurnHead(d); }
/// <summary> /// The update. /// </summary> public static void Update() { InputHandler.CheckAllInputs(); if (!InputHandler.NewInputAvailable) { return; } switch (ChosenAction) { case AvailableInput.Up: DirectionToMove = Snake.Direction.Up; break; case AvailableInput.Right: DirectionToMove = Snake.Direction.Right; break; case AvailableInput.Down: DirectionToMove = Snake.Direction.Down; break; case AvailableInput.Left: DirectionToMove = Snake.Direction.Left; break; case AvailableInput.LowerLeft: switch (Snake.Instance.CurrentDirection) { case Snake.Direction.Right: case Snake.Direction.Left: DirectionToMove = Snake.Direction.Down; break; case Snake.Direction.Up: case Snake.Direction.Down: DirectionToMove = Snake.Direction.Left; break; } break; case AvailableInput.LowerRight: switch (Snake.Instance.CurrentDirection) { case Snake.Direction.Right: case Snake.Direction.Left: DirectionToMove = Snake.Direction.Down; break; case Snake.Direction.Up: case Snake.Direction.Down: DirectionToMove = Snake.Direction.Right; break; } break; case AvailableInput.UpperLeft: switch (Snake.Instance.CurrentDirection) { case Snake.Direction.Right: case Snake.Direction.Left: DirectionToMove = Snake.Direction.Up; break; case Snake.Direction.Up: case Snake.Direction.Down: DirectionToMove = Snake.Direction.Left; break; } break; case AvailableInput.UpperRight: switch (Snake.Instance.CurrentDirection) { case Snake.Direction.Right: case Snake.Direction.Left: DirectionToMove = Snake.Direction.Up; break; case Snake.Direction.Up: case Snake.Direction.Down: DirectionToMove = Snake.Direction.Right; break; } break; case AvailableInput.Quit: Environment.Exit(0); break; case AvailableInput.Pause: SnakeGame.Paused = !SnakeGame.Paused; DirectionToMove = Snake.Instance.CurrentDirection; break; default: DirectionToMove = Snake.Instance.CurrentDirection; break; } InputHandler.NewInputAvailable = false; }