Closed Thread
Results 1 to 5 of 5

Thread: breaking ou tof game while loop

  1. #1
    01hmyoung is offline Newbie
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

    breaking ou tof game while loop

    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
                {
                    
                }
    with the else i need it to break out my games while loop and to display a message saying game over. any help ?

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    gaylo565's Avatar
    gaylo565 is offline Programming Professional
    Join Date
    May 2007
    Location
    flagstaff, az
    Posts
    268
    Rep Power
    21

    Re: breaking ou tof game while loop

    I dont see a while loop? If you show your whole code maybe we can help more.

  4. #3
    01hmyoung is offline Newbie
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

    Re: breaking ou tof game while loop

    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
                {
                    
                }
    
                
    
    
    
    
    
    
            }
        }
    }

  5. #4
    gaylo565's Avatar
    gaylo565 is offline Programming Professional
    Join Date
    May 2007
    Location
    flagstaff, az
    Posts
    268
    Rep Power
    21

    Re: breaking ou tof game while loop

    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: ex
    Code:
    while (key != ConsoleKey.Q && blnIsGameOver)
    Then 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.

  6. #5
    cdg10620's Avatar
    cdg10620 is offline Programming Expert
    Join Date
    Jun 2009
    Location
    Texas
    Posts
    387
    Blog Entries
    3
    Rep Power
    12

    Re: breaking ou tof game while loop

    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

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. breaking a compond...error
    By jclarke in forum C and C++
    Replies: 1
    Last Post: 04-16-2011, 05:13 AM
  2. Breaking Up Strings
    By chili5 in forum Java Help
    Replies: 11
    Last Post: 02-14-2009, 04:53 AM
  3. Game Loop - Need some help looping..
    By Donovan in forum C and C++
    Replies: 33
    Last Post: 12-15-2008, 09:54 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts