// sets up initial conditions public void restart() { timer1.Enabled = false; MessageBox.Show("No!!!! Snake is DEAD!\r\nYour score: "+ score); ingame = false; snakeScoreLabel.Text = "0"; score = 0; spaceBarLabel.Text = "Press Space Bar to Start"; snake = new Snake(); }
private void pad(bool nono) { GamePadState currentState = GamePad.GetState(PlayerIndex.One); if (currentState.ThumbSticks.Left.Y <= -0.8 && up == false) { left = false; right = false; down = true; up = false; } if (currentState.ThumbSticks.Left.Y >= 0.8 && down == false) { left = false; right = false; up = true; down = false; } if ( currentState.ThumbSticks.Left.X >= 0.8 && left == false) { up = false; down = false; right = true; left = false; } if ( currentState.ThumbSticks.Left.X <= -0.8 && right == false) { left = true; up = false; down = false; right = false; } if (currentState.Buttons.Start == Microsoft.Xna.Framework.Input.ButtonState.Pressed && timer1.Enabled == false) { Snake snake1 = new Snake(); snake = snake1; left = false; right = true; up = false; down = false; SolidBrush brush_kulka = new SolidBrush(System.Drawing.Color.Green); timer1.Enabled = true; snake.score = 0; label1.Text = ""; } if (currentState.Buttons.Back == Microsoft.Xna.Framework.Input.ButtonState.Pressed) { if (timer1.Enabled == false) { timer1.Enabled = true; } else { timer1.Enabled = false; } } nono = false; }
static void Main(string[] args) { StartOfTheGame.StartText(); int snakeSpeed = 100; bool correctLevel = false; string userLevel = Console.ReadLine(); while (!correctLevel) { if (int.TryParse(userLevel, out int choosedLevel)) { if (choosedLevel == 1 || choosedLevel == 2 || choosedLevel == 3 || choosedLevel == 4) { snakeSpeed = Levels.ReturnSpeed(choosedLevel); correctLevel = true; } else { Console.WriteLine("Incorrect level. Try again"); userLevel = Console.ReadLine(); } } else { Console.WriteLine("Error! It wasn`t a number. Please, try again"); userLevel = Console.ReadLine(); } } Console.Clear(); int mapWeidth = 80; int mapHeight = 25; Random rnd = new Random(); Walls walls = new Walls(mapWeidth, mapHeight); walls.Draw(); Point startTailPoint = new Point(rnd.Next(5, mapWeidth - 5), rnd.Next(5, mapHeight - 5), '*'); Snake snake = new Snake(startTailPoint, 5, Direction.Right); snake.Draw(); FoodCreator foodCreator = new FoodCreator(mapWeidth - 2, mapHeight - 2); Point foodForSnake = foodCreator.CreateFood(snake); foodCreator.Draw(); while (true) { if (walls.IsHit(snake) || snake.IsHitWithTail()) { break; } if (snake.Eat(foodForSnake)) { foodForSnake = foodCreator.CreateFood(snake); foodCreator.Draw(); } else { snake.Move(); } Thread.Sleep(snakeSpeed); if (Console.KeyAvailable) { ConsoleKeyInfo enteredKey = Console.ReadKey(); snake.EnteredKey(enteredKey); } } EndOfGame theEnd = new EndOfGame(); theEnd.EndGameResult(snake, mapWeidth, mapHeight); theEnd.DrawEndGameResult(); Console.ReadLine(); }
private void Form1_KeyDown(object sender, KeyEventArgs e) { if ((e.KeyData == System.Windows.Forms.Keys.Down || currentState.ThumbSticks.Left.Y <= -0.8) && up == false) { left = false; right = false; down = true; up = false; } if ((e.KeyData == System.Windows.Forms.Keys.Up || currentState.ThumbSticks.Left.Y >= 0.8) && down == false) { left = false; right = false; up = true; down = false; } if ((e.KeyData == System.Windows.Forms.Keys.Right|| currentState.ThumbSticks.Left.X <= -0.8) && left == false) { up = false; down = false; right = true; left = false; } if ((e.KeyData == System.Windows.Forms.Keys.Left|| currentState.ThumbSticks.Left.X >= 0.8) && right == false) { left = true; up = false; down = false; right = false; } if (e.KeyData == System.Windows.Forms.Keys.Space && timer1.Enabled == false) { Snake snake1 = new Snake(); snake = snake1; left = false; right = true; up = false; down = false; SolidBrush brush_kulka = new SolidBrush(System.Drawing.Color.Green); timer1.Enabled = true; snake.score = 0; label1.Text = ""; } if (e.KeyData == System.Windows.Forms.Keys.P) { if (timer1.Enabled == false) { timer1.Enabled = true; } else { timer1.Enabled = false; } } }
// Plays the game itself public static int Game(int refreshRate, int numCones, int snakeLength) { // Set the game parameters based on the arguments Program.REFRESH_RATE = refreshRate; Program.NUM_CONES = numCones; Program.START_LEN = snakeLength; int pixelsToMove = 0; // Number of pixels for the snake to move from a given frame to the next (initialized to zero to circumvent "unassigned variable" errors) int score = 0; // Final score = # of pieces of food eaten // Since the length increases by exactly one for each food: final score = final length - initial length of snake (+1 because the tail gets deleted during the final move) Random rand = new Random(); // Used to randomly generate the position of the cones int START_X_VAL = 5 + Program.START_LEN; int START_Y_VAL = SCREEN_HEIGHT / 2; // Initialize all relevant objects Screen GameScreen = new Screen(1, 2, SCREEN_WIDTH - 2, SCREEN_HEIGHT - 1, 'X'); Snake snake = new Snake(START_X_VAL, START_Y_VAL, GameScreen); int yVal; // Temporarily contains the ranomly generated y-coordinate for each cone's positions (need to check if this value is equal to starting y-coordinate) // Randomly place the required number of cones for (int i = 0; i < Program.NUM_CONES; i++) { do { yVal = rand.Next(3, SCREEN_HEIGHT - 2); } while (yVal == START_Y_VAL); // Don't print cones on the starting line new Cone(rand.Next(2, SCREEN_WIDTH - 2), yVal, GameScreen); } Food food = new Food(); food.Spawn(GameScreen, snake); GameScreen.PrintFullScreen(snake, food); // Initialize scoreboard Console.SetCursorPosition(0, 0); Console.BackgroundColor = ConsoleColor.DarkGray; // Print dark gray background for (int y = 0; y < 2; y++) { for (int x = 0; x <= 22; x++) { Console.SetCursorPosition(x, y); Console.Write(' '); } } Console.ForegroundColor = ConsoleColor.White; Console.SetCursorPosition(0, 0); Console.WriteLine("SCORE: 0"); Console.WriteLine("OLD HIGH SCORE: {0}", Program.highScore); // Print initial messages (but don't save them to the list) // First make space on screen by printng dark gray rectangle Console.BackgroundColor = ConsoleColor.DarkGray; for (int y = 11; y <= 15; y++) { for (int x = SCREEN_WIDTH / 2 - 34; x <= SCREEN_WIDTH / 2 + 34; x++) { Console.SetCursorPosition(x, y); Console.Write(' '); } } // Then print messages Console.SetCursorPosition(SCREEN_WIDTH / 2 - 12, 12); Console.Write("Press any key to start..."); Console.SetCursorPosition(SCREEN_WIDTH / 2 - 32, 14); Console.Write("Once the game starts, press CTRL + X to return to the main menu"); Console.ResetColor(); // Reset background colour to default Console.ReadKey(true); // Once the game starts, clear messages from the screen GameScreen.PrintFullScreen(snake, food, SCREEN_WIDTH / 2 - 34, 11, SCREEN_WIDTH / 2 + 34, 15); // Print screen between (SCREEN_WIDTH/2 - 34, 11) and (SCREEN_WIDTH/2 + 34, 15) ConsoleKeyInfo cki = new ConsoleKeyInfo(); // Create ConsoleKeyInfo object to receive user keystrokes while (!snake.dead) { // If the user presses a key, update the snake's velocity if (Console.KeyAvailable) { cki = Console.ReadKey(true); switch (cki.Key) { case ConsoleKey.UpArrow: // If the user presses the up arrow or 'W', the snake moves up case ConsoleKey.W: snake.SetVelocity(0, yVelocity); break; case ConsoleKey.DownArrow: // If the user presses the down arrow or 'S', the snake moves down case ConsoleKey.S: snake.SetVelocity(0, -yVelocity); break; case ConsoleKey.RightArrow: // If the user presses the right arrow or 'D', the snake moves to the right case ConsoleKey.D: snake.SetVelocity(xVelocity, 0); break; case ConsoleKey.LeftArrow: // If the use presses the left arrow or 'A', the snake moves to the left case ConsoleKey.A: snake.SetVelocity(-xVelocity, 0); break; case ConsoleKey.X: // If the user presses CTRL + X, end the game if (cki.Modifiers == ConsoleModifiers.Control) { snake.dead = true; } snake.segmentList.RemoveAt(snake.segmentList.Count - 1); // Delete the tail so that the score is displayed properly break; default: break; } } if (snake.v_x == 0) // Moving vertically { pixelsToMove = Math.Abs(snake.v_y); } else if (snake.v_y == 0) // Moving horizontally { pixelsToMove = Math.Abs(snake.v_x); } for (int i = 0; i < pixelsToMove; i++) { if (!snake.dead) { // Avoid "index out of range" errors by stopping snake before it leaves screen snake.Move(GameScreen, food); food.Spawn(GameScreen, snake); } } GameScreen.PrintScreen(snake, food); // Print the updated screen System.Threading.Thread.Sleep(1000 / REFRESH_RATE); // Wait until it's time to print the next frame (assume the execution time for a single frame is negligible) } // Create a dark gray rectangle in the center of the screen for the GAME OVER message Console.BackgroundColor = ConsoleColor.DarkGray; for (int y = 11; y <= 17; y++) { for (int x = SCREEN_WIDTH / 2 - 23; x <= SCREEN_WIDTH / 2 + 23; x++) { Console.SetCursorPosition(x, y); Console.Write(' '); } } // Print GAME OVER message, display score, and prompt them to return to the main menu Console.SetCursorPosition(SCREEN_WIDTH / 2 - 5, 12); Console.ForegroundColor = ConsoleColor.Red; Console.Write("GAME OVER"); Console.ForegroundColor = ConsoleColor.White; System.Threading.Thread.Sleep(750); score = snake.segmentList.Count - Program.START_LEN + 1; if (score <= highScore) { Console.SetCursorPosition(SCREEN_WIDTH / 2 - 7, 14); Console.Write("Final Score: {0}", score); } else { Console.SetCursorPosition(SCREEN_WIDTH / 2 - 16, 14); Console.Write("Final Score: {0} (", score); Console.ForegroundColor = ConsoleColor.Green; Console.Write("NEW HIGH SCORE!"); Console.ForegroundColor = ConsoleColor.White; Console.Write(")"); } System.Threading.Thread.Sleep(750); Console.SetCursorPosition(SCREEN_WIDTH / 2 - 19, 16); Console.Write("Press ENTER to return to the main menu"); Console.ResetColor(); while (true) { if (Console.ReadKey(true).Key == ConsoleKey.Enter) { break; } } // Return the current score if it's a new high score if (score > Program.highScore) { return(score); } else { return(Program.highScore); } }