private void MainLoop(object sender, EventArgs e) { CheckEffects(); DidAlreadySnakeMoved = false; Snake.Move(); if (CanShowWalls) { foreach (var obstacle in Obstacles) { Snake.DidHitAWall(obstacle.AsRectangle); } } if (!Snake.IsAlive) { Stop(); return; } foreach (var food in Food) { if (Snake.TryToEat(food.AsRectangle)) { SoundManager.PlaySound(Soundtrack.Pop); Snake.ExtendBody(); switch (food.Type) { case FoodType.Normal: food.NewPosition(Snake); Score = ScoreMultiplier; QuestManager.TrySaveToXp(User, ScoreMultiplier); break; case FoodType.Gold: Food.Remove(food); Score = ScoreMultiplier; QuestManager.TrySaveToXp(User, ScoreMultiplier); FoodModel.ChangeFoodColor(Food, Brushes.Gold); DoubleScoreEffect(10); break; case FoodType.Special: Food.Remove(food); Score = ScoreMultiplier * 2; QuestManager.TrySaveToXp(User, ScoreMultiplier * 2); SpecialEffect(10); break; } _scoreTextBlock.Text = Score.ToString(); for (int i = 0; i < Settings.FoodSpawnRate - 1; i++) { FoodModel newFood = new FoodModel(); newFood.NewPosition(Snake); Food.Add(newFood); } FoodModel.ChanceOtherFoodToAppear(Food, Snake); break; } } Update(); }
static void Main(string[] args) { //Can only set window size in windows if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { Console.SetWindowSize(30, 22); } //Pull in the game board Frame.SetFrame(); Display.FrameChar.AddRange(Display.FrameString.ToString().Select(Chars => Chars.ToString())); //Set the values for movement calculations string[] Lines = Display.FrameString.ToString().Split((Char)10); int Width = Lines[0].Length + 1; int Position = (Display.FrameChar.Count / 2) + (Width / 2); //Start thred to read key press Task.Factory.StartNew(() => Key.Press()); //Game Loop do { //Set text color Console.ForegroundColor = Display.Color; //Check Direction for movement switch (Snake.Direction) { case "Left": Position--; break; case "Right": Position++; break; case "Up": Position -= Width; break; case "Down": Position += Width; break; } //Add current location to list Snake.Location.Add(Position); //Pull Display for updating Display.FrameChar.Clear(); Display.FrameChar.AddRange(Display.FrameString.ToString().Select(Chars => Chars.ToString())); //Reverse locations to get most recent first Snake.Location.Reverse(); int[] Locations = Snake.Location.ToArray(); Snake.Location.Reverse(); //Check for collision if (Display.FrameChar[Position] != " " && Display.FrameChar[Position] != "§" && Display.FrameChar[Position] != "Θ") { Snake.Dead = true; } //Add food to board Food.Add(); //Check if eating if (Display.FrameChar[Position] == "§") { Task.Factory.StartNew(() => Beep.Good()); Snake.Length++; Food.Feed.Change = true; if (Snake.Speed > 15) { Snake.Speed -= 15; } } //Draw snake to board Display.FrameChar[Position] = "Θ"; if (Locations.Length > Snake.Length) { for (int i = 1; i < Snake.Length; i++) { Display.FrameChar[Locations[i]] = "O"; } } //Confirm board has food if (!Display.FrameChar.Contains("§")) { Food.Feed.Change = true; Food.Add(); } //Check for collision if (Display.FrameChar[Position] != " " && Display.FrameChar[Position] != "§" && Display.FrameChar[Position] != "Θ") { Task.Factory.StartNew(() => Beep.Bad()); Snake.Dead = true; } //Update Display Display.DisplayFrame.Clear(); Display.FrameChar.ForEach(Item => Display.DisplayFrame.Append(Item)); Display.DisplayFrame.Append($"Score: {Snake.Length - 1} Speed: {505 - Snake.Speed}"); Display.DisplayFrame.Append(System.Environment.NewLine); //Write Display to console Console.CursorVisible = false; Console.SetCursorPosition(0, 0); Console.Write(Display.DisplayFrame); //Set game speed System.Threading.Thread.Sleep(Snake.Speed); } while (!Snake.Dead); //Reset game System.Threading.Thread.Sleep(1000); Console.Clear(); Reset.Now(); Main(args); }
private void SetDeffaultSettings() { if (FastMoveEffectTimer >= 0) { Settings.Speed /= 2; } if (SlowMoveEffectTimer >= 0) { Settings.Speed *= 2; } DispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, Settings.Speed); DoubleScoreEffectTimer = -1; FastMoveEffectTimer = -1; SlowMoveEffectTimer = -1; ScoreMultiplier = 1; FoodModel.ChangeFoodColor(Food, Brushes.Red); FoodModel.IsGoldenFoodShown = false; FoodModel.IsSpecialFoodShown = false; _scoreTextBlock.Text = "0"; _timeTextBlock.Text = "00:00"; _area.Background = Settings.Background; _area.Opacity = 1; HideAreaTextBlocks(); score = 0; Timer = new DateTime(); Snake = new SnakeModel(Settings.BodyColor, Settings.HeadColor); switch (Settings.Difficulty) { case Difficulties.Easy: Snake.CanTeleport = true; CanShowWalls = false; break; case Difficulties.Normal: Snake.CanTeleport = false; CanShowWalls = false; break; case Difficulties.Hard: Snake.CanTeleport = false; CanShowWalls = true; CreateWalls(); FoodModel.Walls = Obstacles; break; case Difficulties.Python: Snake.CanTeleport = false; CanShowWalls = true; CreateWalls(); FoodModel.Walls = Obstacles; Snake.Starve = 60; break; } for (int i = Settings.FoodSpawnCount; i > 0; i--) { FoodModel food = new FoodModel(); food.NewPosition(Snake); Food.Add(food); } _pauseTextBlock.Text = "\uf04c"; }