i need help in creating code to deleted the old head when the snake is moving. atm it just makes one long snake. any help plz?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 class SnakeSegment { 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(); seg.x = player.xpos; seg.y = 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) { if (level[player.ypos + player.vec.y, player.xpos + player.vec.x] != TileType.WALL) { // ADD CODE HERE int newX = player.xpos + player.vec.x; int newY = player.ypos + player.vec.y; level[newY, newX] = TileType.PLAYER; DrawTile(level, newX, newY); player.xpos = newX; player.ypos = newY; player.body.RemoveAt(player.body.Count - 1); } else { } // ADD CODE HERE } } }
Moved inner classes to other files. And changed two.
Snake class should do more:Code:using System; using System.Threading; namespace ConsoleSnakeGameAssignment { class Program { 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(); seg.x = player.XPosition; seg.y = player.YPosition; player.body.Add(seg); // GAME LOOP // Keep reading the keys until the // player quits the game. while (key != ConsoleKey.Q) { if (Console.KeyAvailable) { key = Console.ReadKey(true).Key; // Update the players vector to make // the snake move in a new direction player.ChangeMovementDirection(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.Clear(); Console.WriteLine("Bye bye..."); Thread.Sleep(1000); // 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 index = 0; const int rightWallPosition = 69; const int downWallPosition = 24; const int upWallPosition = 0; const int leftWallPosition = 0; //up wall for (var tilePositionX = 0; tilePositionX <= rightWallPosition; tilePositionX++) levelOne[upWallPosition, tilePositionX] = TileType.WALL; //right wall for (var tilePositionY = 0; tilePositionY <= downWallPosition; tilePositionY++) levelOne[tilePositionY, rightWallPosition] = TileType.WALL; //down wall for (var tilePositionX = 0; tilePositionX <= rightWallPosition; tilePositionX++) levelOne[downWallPosition, tilePositionX] = TileType.WALL; //left wall for (var tilePositionY = 0; tilePositionY <= downWallPosition; tilePositionY++) levelOne[tilePositionY, leftWallPosition] = 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++) { var tilePositionX = 0; var tilePositionY = 0; if (levelOne[tilePositionY, tilePositionX] == TileType.PLAYER) continue; while (levelOne[tilePositionY, tilePositionX] != TileType.NONE) { tilePositionX = (int)(randObj.NextDouble() * (levelOne.GetUpperBound(1))); if (tilePositionX > rightWallPosition) { tilePositionX = tilePositionX - 11; } tilePositionY = (int)(randObj.NextDouble() * (levelOne.GetUpperBound(0))); } levelOne[tilePositionY, tilePositionX] = TileType.FOOD; DrawTile(levelOne, tilePositionX, tilePositionY); } #endregion levelOne[player.XPosition, player.YPosition] = 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) { for (var tilePositionY = 0; tilePositionY < 25; tilePositionY++) for (var 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; switch (level[tilePositionY, tilePositionX]) { case TileType.WALL: Console.BackgroundColor = ConsoleColor.Black; Console.ForegroundColor = ConsoleColor.Green; Console.Write("#"); break; case TileType.PLAYER: Console.BackgroundColor = ConsoleColor.Black; Console.ForegroundColor = ConsoleColor.Yellow; Console.Write("*"); break; case TileType.FOOD: Console.BackgroundColor = ConsoleColor.DarkMagenta; Console.Write("F"); break; case TileType.DEADPLAYER: Console.Write("D"); break; default: Console.BackgroundColor = ConsoleColor.Black; Console.Write(" "); break; } } #endregion // 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. // private static void UpdateLevel(TileType[,] level, Snake player) { if (level[player.YPosition + player.vec.y, player.XPosition + player.vec.x] == TileType.WALL) return; // removes last snake position level[player.YPosition, player.XPosition] = TileType.NONE; DrawTile(level, player.XPosition, player.YPosition); // ADD CODE HERE player.Move(); level[player.YPosition, player.XPosition] = TileType.PLAYER; DrawTile(level, player.XPosition, player.YPosition); // ADD CODE HERE } } }
Try to use methods that have some logic instead of using public fields. For example use change direction method instead of modifing fields directly. Nice game. Find some svn repo and try to program with some other people.Code:using System; using System.Collections.Generic; namespace ConsoleSnakeGameAssignment { public class Snake { private int xPosition = 10; private int yPosition = 10; //todo: not used (what is that?) //public TileType type; public Vector2 vec = new Vector2(); public List<SnakeSegment> body = new List<SnakeSegment> { new SnakeSegment() }; public int XPosition { get { return xPosition; } //set { xPosition = value; } } public int YPosition { get { return yPosition; } //set { yPosition = value; } } /// <summary> /// Moves snake in direction based on his velocity. /// </summary> public void Move() { xPosition += vec.x; yPosition += vec.y; } /// <summary> /// Generate a new 2 Dimensional normalized vector indicating /// the direction the player should be moving. /// </summary> public void ChangeMovementDirection(ConsoleKey key) { 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; } } } }
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks