private void LoadGame() //Replaces current main game objects with the objects that have been saved previously { if (LoadGameBoard()) { this.snake = (TheSnake)LoadFromBinaryFile(System.IO.Path.GetFullPath("SnakeData.dat")); this.foodOnBoard = (List <Point>)LoadFromBinaryFile(System.IO.Path.GetFullPath("FoodData.dat")); this.currentScore = (int)LoadFromBinaryFile(System.IO.Path.GetFullPath("CurrentScoreData.dat")); this.currentScoreDisplay.Text = "Current Score: " + this.currentScore; this.speedMultiplier = (double)LoadFromBinaryFile(System.IO.Path.GetFullPath("SpeedData.dat")); } }
private void resetVariables() //Sets up a new Snake, Canvas and populates with head of snake and food { snake = new TheSnake(); gameBoard.Children.Clear(); animateSnake(snake.CurrentPosition); foodOnBoard = new List <Point>(); currentScore = 0; speedMultiplier = 0.5; for (int i = 0; i < 10; i++) { generateFood(i); } }
public void CheckIfScoreShouldBeInc(TheSnake snake, Direction movementDir, ref int score) { if (m_berry.Position.XPos == snake.Head.XPos && m_berry.Position.YPos == snake.Head.YPos) { score++; Pixel newBodyPartLocation; if (snake.Body.Count > 0) { newBodyPartLocation = snake.setNewBodyPartLocation(movementDir, snake.Body[snake.Body.Count - 1]); } else { newBodyPartLocation = snake.setNewBodyPartLocation(movementDir, snake.Head); } snake.Body.Add(newBodyPartLocation); setBerryOnRandLocation(); } }
static void Main() { var score = 0; SetWindowSize(32, 16); var borderManager = new BorderManager(WindowWidth, WindowHeight); var snake = new TheSnake(null, null, ConsoleColor.Red); var berryManager = new BerryManager(WindowWidth, WindowHeight); var currentMovement = Direction.Right; var gameOver = false; while (true) { Clear(); gameOver |= (snake.Head.XPos == WindowWidth - 1 || snake.Head.XPos == 0 || snake.Head.YPos == WindowHeight - 1 || snake.Head.YPos == 0); borderManager.DrawBorder(); berryManager.CheckIfScoreShouldBeInc(snake, currentMovement, ref score); snake.DrawSnake(ref gameOver); if (gameOver) { break; } berryManager.DrawBerry(); snake.Move(ref currentMovement); } SetCursorPosition(WindowWidth / 5, WindowHeight / 2); WriteLine($"Game over, Score: {score}"); SetCursorPosition(WindowWidth / 5, WindowHeight / 2 + 1); ReadKey(); }
public MainWindow() { InitializeComponent(); Boolean paused = false; Boolean currentlyPlaying = false; this.highScore = (int)LoadFromBinaryFile(System.IO.Path.GetFullPath("Highscore.dat")); speedMultiplier = 0.5; foodOnBoard = new List <Point>(); timer = new DispatcherTimer(); timer.Interval = new TimeSpan(0, 0, 0, 0, 1); timer.Tick += Timer_Tick; KeyDown += MainWindow_KeyDown; snake = new TheSnake(); animateSnake(snake.CurrentPosition); //after this moment, the total number of objects on the canvas is 1 for (int i = 0; i < 10; i++) { generateFood(i); //after this moment, the total number of objects on the canvas is 11 } }