static void Main() { Console.CursorVisible = false; Console.BufferWidth = Console.WindowWidth = 50; Console.BufferHeight = Console.WindowHeight = 30; Coords direction = new Coords(0, -1); Apple apple = new Apple(); Rocks rock = new Rocks(); Snake snake = new Snake(); WelcomeMessage(); DrawBoarder(); // Game Loop while (!isGameOver) { ResetWindowSize(); Console.ForegroundColor = ConsoleColor.Black; // just so you cant write in the console Input(direction); rock.Update(snake, apple); apple.Update(snake, rock); snake.Update(direction, apple, rock); apple.Draw(); rock.Draw(); snake.Draw(direction); speed = ChangeSpeed(score, speed); Thread.Sleep(speed); snake.Delete(); apple.Delete(); } Console.Clear(); Console.SetCursorPosition(20, 6); Console.WriteLine("GAME OVER!"); Console.SetCursorPosition(18, 8); Console.WriteLine("Your Score: " + score); // for replay Console.SetCursorPosition(17, 10); Console.WriteLine("Replay ? (Y / N)"); ConsoleKeyInfo key = Console.ReadKey(); if (key.Key == ConsoleKey.Y) { direction = new Coords(0, -1); apple = new Apple(); snake = new Snake(); isGameOver = false; score = 0; speed = 100; GC.Collect(); Console.Clear(); Main(); } else { return; } }
protected void Draw() { d2dRenderTarget.BeginDraw(); d2dRenderTarget.Clear(Color.Black); apple.Draw(d2dRenderTarget, d2dFactory); snake.Draw(d2dRenderTarget, d2dFactory); d2dRenderTarget.EndDraw(); swapChain.Present(0, PresentFlags.None); }
protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.Black); var position = new Vector2(100, 100); spriteBatch.Begin(); apple.Draw(spriteBatch, rectTexture); snake.Draw(spriteBatch, rectTexture); spriteBatch.End(); base.Draw(gameTime); }
protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.Black); spriteBatch.Begin(); if (snake.IsAlive) { spriteBatch.DrawString(defaultFont, snake.Score.ToString(), Vector2.Zero, Color.White); spriteBatch.DrawString(defaultFont, gameManager.Speed.ToString(), new Vector2(GameManager.SCREEN_WIDTH - (10 * (gameManager.Speed.ToString().Length)), 0), Color.White); foreach (var badApple in badApples) { badApple.Draw(spriteBatch, whitePixel); } apple.Draw(spriteBatch, whitePixel); snake.Draw(spriteBatch, whitePixel); } else if (startOfGame) { spriteBatch.DrawString(defaultFont, "Snake and the Bad Apples", new Vector2((GameManager.SCREEN_WIDTH / 2) - 80, (GameManager.SCREEN_HEIGHT / 3)), Color.White); spriteBatch.Draw(whitePixel, new Rectangle(140 + GameManager.BLOCK_WIDTH / 4, (GameManager.SCREEN_HEIGHT / 3) + 40 + GameManager.BLOCK_WIDTH / 4, GameManager.BLOCK_WIDTH / 2, GameManager.BLOCK_WIDTH / 2), Color.Red); spriteBatch.Draw(whitePixel, new Rectangle(160 + GameManager.BLOCK_WIDTH / 4, (GameManager.SCREEN_HEIGHT / 3) + 40 + GameManager.BLOCK_WIDTH / 4, GameManager.BLOCK_WIDTH / 2, GameManager.BLOCK_WIDTH / 2), Color.Red); spriteBatch.Draw(whitePixel, new Rectangle(180 + GameManager.BLOCK_WIDTH / 4, (GameManager.SCREEN_HEIGHT / 3) + 40 + GameManager.BLOCK_WIDTH / 4, GameManager.BLOCK_WIDTH / 2, GameManager.BLOCK_WIDTH / 2), Color.Red); spriteBatch.Draw(whitePixel, new Rectangle(200 + GameManager.BLOCK_WIDTH / 4, (GameManager.SCREEN_HEIGHT / 3) + 40 + GameManager.BLOCK_WIDTH / 4, GameManager.BLOCK_WIDTH / 2, GameManager.BLOCK_WIDTH / 2), Color.Green); spriteBatch.Draw(whitePixel, new Rectangle(220 + GameManager.BLOCK_WIDTH / 4, (GameManager.SCREEN_HEIGHT / 3) + 40 + GameManager.BLOCK_WIDTH / 4, GameManager.BLOCK_WIDTH / 2, GameManager.BLOCK_WIDTH / 2), Color.Red); spriteBatch.Draw(whitePixel, new Rectangle(240 + GameManager.BLOCK_WIDTH / 4, (GameManager.SCREEN_HEIGHT / 3) + 40 + GameManager.BLOCK_WIDTH / 4, GameManager.BLOCK_WIDTH / 2, GameManager.BLOCK_WIDTH / 2), Color.Red); spriteBatch.Draw(whitePixel, new Rectangle(260 + GameManager.BLOCK_WIDTH / 4, (GameManager.SCREEN_HEIGHT / 3) + 40 + GameManager.BLOCK_WIDTH / 4, GameManager.BLOCK_WIDTH / 2, GameManager.BLOCK_WIDTH / 2), Color.Red); spriteBatch.DrawString(defaultFont, "Press enter to start", new Vector2((GameManager.SCREEN_WIDTH / 4) + 45, (GameManager.SCREEN_HEIGHT / 2) + 10), Color.White); } else { spriteBatch.DrawString(defaultFont, "Game Over", new Vector2((GameManager.SCREEN_WIDTH / 2) - 30, (GameManager.SCREEN_HEIGHT / 3) - 10), Color.White); spriteBatch.DrawString(defaultFont, snake.Score.ToString(), new Vector2((GameManager.SCREEN_WIDTH / 2) + 5, (GameManager.SCREEN_HEIGHT / 3) + 10), Color.White); spriteBatch.DrawString(defaultFont, "Press enter to play again", new Vector2((GameManager.SCREEN_WIDTH / 4) + 20, (GameManager.SCREEN_HEIGHT / 2) + 10), Color.White); } spriteBatch.End(); base.Draw(gameTime); }
public void Draw() { if (czyMenu == true) { menu.Rysowanie(spriteBatch); } else { foreach (GUIElement element in GetGame()) { element.Rysowanie(spriteBatch); } foreach (GUIElement element in GetScore()) { element.Rysowanie(spriteBatch); element.Sprawdzanie(); } waz.Draw(spriteBatch); jablko.Draw(spriteBatch); } if (isPause == false) { waz.Draw(spriteBatch); foreach (GUIElement element in GetPauseMenu()) { element.Rysowanie(spriteBatch); element.Sprawdzanie(); } } if (GUIHighScore.CzyEnter2 == true) { czyMenu = true; menu = new Menu(this, GetOver()); GUIHighScore.CzyEnter2 = false; GUIHighScore.CzyEnter = false; } }
public bool Run() { while (GameRunning) { if (GameMode == GameMode.SinglePlayer) { if (Snake.CheckBoardCollision(Board)) { GameRunning = false; this.Winner = "Someone Else"; break; } if (Snake.CheckTailCollision()) { GameRunning = false; this.Winner = "Someone Else"; break; } } //-------------------------------------------- // Check for collision with tail and board //-------------------------------------------- if (GameMode == GameMode.SnakeVsApple) { if (Snake.CheckBoardCollision(Board)) { GameRunning = false; this.Winner = "Apple"; break; } if (Snake.CheckTailCollision()) { GameRunning = false; this.Winner = "Apple"; break; } } else if (GameMode == GameMode.SnakeVsSnake) { //-------------------------------------------- // Check for collision on Snake Player Two //-------------------------------------------- if (SnakePlayerTwo.CheckBoardCollision(Board)) { GameRunning = false; this.Winner = "Player 1"; break; } if (SnakePlayerTwo.CheckTailCollision()) { GameRunning = false; this.Winner = "Player 1"; break; } if (SnakePlayerTwo.CheckSnakeCollision(Snake)) { GameRunning = false; this.Winner = "Player 1"; break; } //-------------------------------------------- // Check for collision with other Snake //-------------------------------------------- //if (Snake.CheckSnakeCollision(SnakePlayerTwo)) //{ // GameRunning = false; // this.Winner = "Player 2"; // break; //} } //-------------------------------------------- // Getting movement direction //-------------------------------------------- if (Console.KeyAvailable) { Direction = Joystick.SetKeyDirection(SnakeDirection); if (Direction == KeyDirection.Up || Direction == KeyDirection.Down || Direction == KeyDirection.Left || Direction == KeyDirection.Right) { SnakeDirection = Direction; } else { AppleDirection = Direction; } } //-------------------------------------------- // Eating of the apple //-------------------------------------------- if (GameMode == GameMode.SnakeVsApple) { SnakeEnergy = Snake.GetEnergy(); IsFoodEaten = Snake.Eat(Apple); } else if (GameMode == GameMode.SinglePlayer) { IsFoodEaten = Snake.Eat(Food); } else if (GameMode == GameMode.SnakeVsSnake) { OnePlayerFoodIsEaten = Snake.Eat(Food); TwoPlayerFoodIsEaten = SnakePlayerTwo.Eat(Food); if (OnePlayerFoodIsEaten) { Score++; IsFoodEaten = true; } if (TwoPlayerFoodIsEaten) { TwoPlayerScore++; IsFoodEaten = true; } } Apple.EraseOldApple(); if (GameMode == GameMode.SnakeVsApple) { Snake.Move(SnakeDirection, IsFoodEaten); AppleDirection = Apple.Move(AppleDirection); } else if (GameMode == GameMode.SnakeVsSnake) { SnakeDirectionPlayerTwo = SnakePlayerTwo.TranslateAppleDirectionToSnake(AppleDirection, SnakeDirectionPlayerTwo); Snake.Move(SnakeDirection, OnePlayerFoodIsEaten); SnakePlayerTwo.Move(SnakeDirectionPlayerTwo, TwoPlayerFoodIsEaten); } else if (GameMode == GameMode.SinglePlayer) { Snake.Move(SnakeDirection, IsFoodEaten); } if (IsFoodEaten) { if (GameMode == GameMode.SnakeVsApple) { Apple.LoseLife(); Apple.MakeFood(BoardWidth, BoardHeight); Energy = Apple.GetEnergy(); Snake.GetEnergyFromApple(Energy); } else if (GameMode == GameMode.SnakeVsSnake) { Food.MakeFood(BoardWidth, BoardHeight); IsFoodEaten = false; } else { Score++; Food.MakeFood(BoardWidth, BoardHeight); } } //-------------------------------------------- // Drawing the graphics in both modes //-------------------------------------------- Snake.Draw("Green"); //-------------------------------------------- // Drawing different for Two vs One Player mode //-------------------------------------------- if (GameMode == GameMode.SnakeVsApple) { Apple.RottTheApple(); Energybar.Draw(BoardHeight, SnakeEnergy); Snake.LoseEnergy(); Apple.Draw(); Apple.DrawLifes(BoardHeight); } else if (GameMode == GameMode.SnakeVsSnake) { SnakePlayerTwo.Draw("Magenta"); Food.Draw(); } else { Food.Draw(); ShowScore(Score, BoardHeight); } if (SnakeEnergy < 1 && GameMode == GameMode.SnakeVsApple) { this.GameRunning = false; this.Winner = "Apple"; } if (Apple.GetLifes() < 1 && GameMode == GameMode.SnakeVsApple) { this.GameRunning = false; this.Winner = "Snake"; } //-------------------------------------------- // Timer for loop //-------------------------------------------- Thread.Sleep(100 - Speed); } Console.Clear(); Console.ForegroundColor = ConsoleColor.White; GameOver(this.Winner); bool playAgain = AskIfPlayAgain(); return(playAgain); }
static void Main(string[] args) { var game = new Game(); var apple = new Apple(); _window = new Window(3) { Title = "Snake" }; _window.OnKeyDown = key => { switch (key) { case ConsoleKey.RightArrow: if (game.Paused) { break; } if (snake.Orientation != "left") { snake.Orientation = "right"; } break; case ConsoleKey.LeftArrow: if (game.Paused) { break; } if (snake.Orientation != "right") { snake.Orientation = "left"; } break; case ConsoleKey.UpArrow: if (game.Paused) { break; } if (snake.Orientation != "down") { snake.Orientation = "up"; } break; case ConsoleKey.DownArrow: if (game.Paused) { break; } if (snake.Orientation != "up") { snake.Orientation = "down"; } break; case ConsoleKey.Escape: Console.WriteLine("Exit"); Environment.Exit(0); break; case ConsoleKey.P: if (game.Ended) { break; } if (game.Paused) { game.Resume(); break; } _window.Invoke(() => { _window.Screen.Text(205, 8, "Paused"); }); game.Pause(); break; case ConsoleKey.S: if (!game.Started || !game.Ended) { game.Start(); } break; case ConsoleKey.B: if (!game.Started) { snake.SetColors("#0000ff", "#ffff00"); } break; case ConsoleKey.O: if (!game.Started) { snake.SetColors("#ff9900", "#ffcc00"); } break; default: return; } }; _window.OnReady = window => { window.Invoke(() => { _window.Screen.SetColor("#33cc33"); _window.Screen.Rectangle(0, 0, 200, 200); _window.Screen.SetColor("#669999"); _window.Screen.Rectangle(200, 0, 120, 200); _window.Screen.SetColor("#000"); _window.Screen.Rectangle(200, 0, 2, 200); _window.Screen.SetColor("#000"); _window.Screen.Text(200 / 2 - 40, 200 / 2 - 20, "Press \"S\" to start the game"); _window.Screen.Text(205, 10, "Press \"B\" for Blue Snake"); _window.Screen.Text(205, 15, "Press \"O\" for Orange Snake"); }); snake = new Snake(); game.OnTick = () => { if (snake.CheckForCol()) { game.Stop(); window.Invoke(() => { _window.Screen.SetColor("#ff9900"); _window.Screen.Rectangle(75, 45, 60, 60); _window.Screen.SetColor("#000"); _window.Screen.Rectangle(80, 50, 50, 50); _window.Screen.SetColor("#FFF"); _window.Screen.Text(90, 67, "GAME OVER"); _window.Screen.Text(84, 74, "Press \"ESC\" to"); _window.Screen.Text(93, 79, "End Game"); }); } window.Invoke(() => { if (game.Ended) { return; } _window.Screen.SetColor("#33cc33"); _window.Screen.Rectangle(0, 0, 200, 200); apple.Draw(Random, _window.Screen, snake.Parts); if (apple.CheckIfEat(_window.Screen, snake.Parts)) { snake.AddPart(); snake.AddPart(); Score += 1; } snake.Draw(_window.Screen); _window.Screen.SetColor("#669999"); _window.Screen.Rectangle(200, 0, 120, 200); _window.Screen.SetColor("#000"); _window.Screen.Rectangle(200, 0, 2, 200); _window.Screen.SetColor("#000"); _window.Screen.Text(205, 2, $"Score: {Score}"); }); }; }; _window.Load(); }