with the else i need it to break out my games while loop and to display a message saying game over. any help ?Code:static public void UpdateLevel(TileType[,] level, Snake player) { SnakeSegment head = player.GetHead(); if (level[head.y + player.vec.y, head.x + player.vec.x] != TileType.WALL) { // ADD CODE HERE if (level[head.y + player.vec.y, head.x + player.vec.x] == TileType.FOOD) { SnakeSegment newHead1 = new SnakeSegment( head.x + player.vec.x, head.y + player.vec.y); level[newHead1.y, newHead1.x] = TileType.PLAYER; DrawTile(level, newHead1.x, newHead1.y); player.body.Add(newHead1); } SnakeSegment newHead = new SnakeSegment( head.x + player.vec.x, head.y + player.vec.y); level[newHead.y, newHead.x] = TileType.PLAYER; DrawTile(level, newHead.x, newHead.y); SnakeSegment tail = player.GetTail(); level[tail.y, tail.x] = TileType.NONE; DrawTile(level, tail.x, tail.y); player.body.Add(newHead); player.RemoveTail(); // if (level[head.x, head.y] == TileType.WALL) //{ // break; //} } else { }
I dont see a while loop? If you show your whole code maybe we can help more.
Code:using System; using System.Collections.Generic; using System.Text; using System.Threading; namespace ConsoleSnakeGameAssignment { class Program { public enum TileType { NONE = 0, PLAYER, FOOD, WALL, DEADPLAYER }; public class Vector2 { public int x = 0; public int y = 0; } public class Snake { public int xpos; public int ypos; public TileType type; public Vector2 vec = new Vector2(); public List<SnakeSegment> body = new List<SnakeSegment>(); public SnakeSegment GetHead() { return body[body.Count - 1]; } public SnakeSegment GetTail() { return body[0]; } public void RemoveTail() { body.RemoveAt(0); } } public class SnakeSegment { public SnakeSegment(int x, int y) { this.x = x; this.y = y; } public int x; public int y; } static void Main(string[] args) { TileType[,] levelOne = new TileType[25, 80]; Snake player = new Snake(); // The player is stopped when we start the game // so the vector is <0,0> player.vec.x = 0; player.vec.y = 0; Array.Clear(levelOne, 0, levelOne.Length); // Set's all tiles to zero ConsoleKey key = ConsoleKey.Z; // Unused bogus key // Clear the screen Console.Clear(); Console.CursorVisible = false; // You could create an array of characters e.g bad guys /* GameCharacter[] levelCharacters = new GameCharacter[1]; for (int index = 0; index < levelCharacters.Length; index++) { levelCharacters[index] = new GameCharacter(); levelCharacters[index].movement = new Vector2(); } CreateLevel(levelOne, levelCharacters); */ // Create levelOne by setting the tiles to the // correct values including the walls, player // and randomly placed food. CreateLevel(levelOne, player); // Draw the level DrawLevel(levelOne); // The snake starts a single segment // which we add to the head of the // list to form the snake's head. SnakeSegment seg = new SnakeSegment(player.xpos, player.ypos); player.body.Add(seg); // GAME LOOP // Keep reading the keys until the // player quits the game. while (key != ConsoleKey.Q) { if (Console.KeyAvailable == true) { key = Console.ReadKey(true).Key; // Update the players vector to make // the snake move in a new direction player.vec = UpdatePlayer(key); } // Update the position of player(i.e. snake) and draw // the it. // If you want bad guys, then move them around in here // as well. Perhaps move them K-steps in one direction // then reverse them, or randomly select a new // direction after a random number of steps, etc. // This method also checks for walls, etc, and contains // the game logic. // Note: Because this is quite a simple game we also // redraw the tiles in here. In a 'proper' game, we // would have a seperate draw method and just use the // this to update the positions. // UpdateLevel(levelOne, player); // Set the game speed Thread.Sleep(100); } Console.ReadLine(); // Pause } // Fill the tiles with the correct values. // Use FOR loops to add the walls along the sides of the // window. // Position the player in the level. // Randomly place the food. // You may add your own characters, mines, pits, etc. static void CreateLevel(TileType[,] levelOne, Snake player) { int tilePositionX = 0; int tilePositionY = 0; int index = 0; for (tilePositionY = 0; tilePositionX < 70; tilePositionX++) { levelOne[tilePositionY, tilePositionX] = TileType.WALL; } for (tilePositionX = 69; tilePositionY < 25; tilePositionY++) { levelOne[tilePositionY, tilePositionX] = TileType.WALL; } for (tilePositionY = 24; tilePositionX > 0; tilePositionX--) { levelOne[tilePositionY, tilePositionX] = TileType.WALL; } for (tilePositionX = 0; tilePositionY > 0; tilePositionY--) { levelOne[tilePositionY, tilePositionX] = TileType.WALL; } #region GameCode_DO_NOT_CHANGE // Randomly place the food in the level. // The WHILE loop below is used to make sure that we // don't add food where the wall or the player is. Random randObj = new Random(); for (index = 1; index < 50; index++) { tilePositionX = 0; tilePositionY = 0; if (levelOne[tilePositionY, tilePositionX] != TileType.PLAYER) { while (levelOne[tilePositionY, tilePositionX] != TileType.NONE) { tilePositionX = (int)(randObj.NextDouble() * (levelOne.GetUpperBound(1))); if (tilePositionX > 69) { tilePositionX = tilePositionX - 11; } tilePositionY = (int)(randObj.NextDouble() * (levelOne.GetUpperBound(0))); } levelOne[tilePositionY, tilePositionX] = TileType.FOOD; DrawTile(levelOne, tilePositionX, tilePositionY); } #endregion } player.xpos = 10; player.ypos = 10; levelOne[player.xpos, player.ypos] = TileType.PLAYER; } // Using a nested FOR loop, this method draws each row and // draw each character along each row by calling DrawTile. static void DrawLevel(TileType[,] level) { int tilePositionX = 1; int tilePositionY = 1; for (tilePositionY = 0; tilePositionY < 25; tilePositionY++) { for (tilePositionX = 0; tilePositionX < 80; tilePositionX++) { DrawTile(level, tilePositionX, tilePositionY); } } #region GameCode_DO_NOT_CHANGE // Added at end of method to avoid leaving cursor in // the wrong place and causing the text to shift up // an unwanted extra line. Console.CursorLeft = 0; Console.CursorTop = 0; #endregion } #region GameCode_DO_NOT_CHANGE // Draw the required at the provided tile position. // You may change the graphics here if you wish. static void DrawTile(TileType[,] level, int tilePositionX, int tilePositionY) { Console.CursorLeft = tilePositionX; Console.CursorTop = tilePositionY; if (level[tilePositionY, tilePositionX] == TileType.WALL) { Console.BackgroundColor = ConsoleColor.Black; Console.ForegroundColor = ConsoleColor.Green; Console.Write("#"); } else if (level[tilePositionY, tilePositionX] == TileType.PLAYER) { Console.BackgroundColor = ConsoleColor.Black; Console.ForegroundColor = ConsoleColor.Yellow; Console.Write("*"); } else if (level[tilePositionY, tilePositionX] == TileType.FOOD) { Console.BackgroundColor = ConsoleColor.DarkMagenta; Console.Write("F"); } else if (level[tilePositionY, tilePositionX] == TileType.DEADPLAYER) { Console.Write("D"); } else { Console.BackgroundColor = ConsoleColor.Black; Console.Write(" "); } } #endregion // Generate a new 2 Dimensional normalized vector indicating // the direction the player should be moving. static Vector2 UpdatePlayer(ConsoleKey key) { Vector2 vec = new Vector2(); switch (key) { case ConsoleKey.W: vec.x = 0; vec.y = -1; break; case ConsoleKey.D: vec.x = 1; vec.y = 0; break; case ConsoleKey.S: vec.x = 0; vec.y = 1; break; case ConsoleKey.A: vec.x = -1; vec.y = 0; break; } return vec; } // Update the players position and check that the snake // hasn't hit a wall, itself, etc. // // Remove ths segement of the list tail as the snake // moves around, but grow the snake by one segment if it eats food. // static public void UpdateLevel(TileType[,] level, Snake player) { SnakeSegment head = player.GetHead(); if (level[head.y + player.vec.y, head.x + player.vec.x] != TileType.WALL) { if (level[head.y + player.vec.y, head.x + player.vec.x] == TileType.FOOD) { SnakeSegment newHead1 = new SnakeSegment( head.x + player.vec.x, head.y + player.vec.y); level[newHead1.y, newHead1.x] = TileType.PLAYER; DrawTile(level, newHead1.x, newHead1.y); player.body.Add(newHead1); } SnakeSegment newHead = new SnakeSegment( head.x + player.vec.x, head.y + player.vec.y); level[newHead.y, newHead.x] = TileType.PLAYER; DrawTile(level, newHead.x, newHead.y); SnakeSegment tail = player.GetTail(); level[tail.y, tail.x] = TileType.NONE; DrawTile(level, tail.x, tail.y); player.body.Add(newHead); player.RemoveTail(); // if (level[head.x, head.y] == TileType.WALL) //{ // break; //} } else { } } } }
I would suggest making a bool variable (somewhere in the class but not in a method) and set it one way or the other initially. Add the new bool variable to your while statements condition: exThen you can use your else if block to control the value of the bool variable. This way, if you change the value in your else statement, it will break the while statement next time it evaluates. Hope this helps you out.Code:while (key != ConsoleKey.Q && blnIsGameOver)
There is not while loop at the bottom of that code. The code that you posted has an if else statement. Where your break statement is currently will just take you out of the if statement.
-CDG10620
Software Developer
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks