Author Topic: [24] A simple Maze  (Read 1049 times)

0 Members and 1 Guest are viewing this topic.

Offline Celestialkey

  • Administrator
  • Hero Member
  • *
  • Posts: 3961
  • Reputation 4874
  • Gender: Male
  • Its Alive!!
    • View Profile
    • www.CelestialCoding.com
[24] A simple Maze
« on: December 21, 2008, 12:11:46 PM »
Code: C++
  1. /************************************************************
  2. //      $Program: Intro to Buffers!
  3. //
  4. //      $Description: We will teach you how to use buffers now
  5. //              to paste images all at the same time instead of
  6. //              one character at a time. (faster)
  7. *************************************************************/
  8. #include <windows.h>
  9.  
  10. #define WIDTH   30      // Maze width
  11. #define HEIGHT  20      // Maze Height
  12. #define sLocX   0       // Player Start X
  13. #define sLocY   10      // Player Start Y
  14. #define SPRITE  1       // Player Sprite
  15. #define BLOCKER 5       // Bricks
  16.  
  17. void Render_Maze(CHAR_INFO tempBuffer[], COORD Location);
  18. bool Move_Sprite(CHAR_INFO tempBuffer[], COORD &Location);
  19.  
  20. int main()
  21. {
  22.         CHAR_INFO Maze_Buffer[WIDTH*HEIGHT]     =       {0};
  23.         COORD Maze_Size         =       {WIDTH, HEIGHT};
  24.         COORD Current_Loc       =       {sLocX, sLocY};
  25.         srand( GetTickCount() );        // Set a random seed for our random generation
  26.  
  27.         for(int y=0; y<Maze_Size.Y; y++)
  28.         {
  29.                 for(int x=2; x<Maze_Size.X; x++)
  30.                 {
  31.                         if(!(rand()%3)) // Set it up so if the random number is a 2 then it will draw a blocker
  32.                         {
  33.                                 Maze_Buffer[x + (y * WIDTH)].Char.UnicodeChar   =       BLOCKER; // Create our wall
  34.                                 Maze_Buffer[x + (y * WIDTH)].Attributes =       FOREGROUND_GREEN | FOREGROUND_BLUE;
  35.                         }
  36.                 }
  37.         }
  38.  
  39.         Render_Maze(Maze_Buffer, Current_Loc); // Draw the maze to the console window
  40.        
  41.         while(true)     // Our game loop
  42.         {
  43.                 if(Move_Sprite(Maze_Buffer, Current_Loc))
  44.                         Render_Maze(Maze_Buffer, Current_Loc);
  45.         }
  46.  
  47.         return 0;
  48. }
  49.  
  50. void Render_Maze(CHAR_INFO tempBuffer[], COORD tLocation)
  51. {
  52.         SMALL_RECT Render_Area = {0, 0, WIDTH-1, HEIGHT-1}; // (Start x, start y, width x, height y)
  53.         COORD Maze_Size = {WIDTH, HEIGHT};
  54.         COORD PointZero = {0, 0};       // Our location to draw from in the rectangle
  55.         HANDLE Monitor  = GetStdHandle(STD_OUTPUT_HANDLE);
  56.  
  57.         tempBuffer[tLocation.X + (tLocation.Y * WIDTH)].Char.AsciiChar = SPRITE;
  58.         tempBuffer[tLocation.X + (tLocation.Y * WIDTH)].Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
  59.  
  60.         WriteConsoleOutput(Monitor, tempBuffer, Maze_Size, PointZero, &Render_Area);
  61. }
  62.  
  63. bool Move_Sprite(CHAR_INFO tempBuffer[], COORD &Location)
  64. {
  65.         INPUT_RECORD record;
  66.         COORD LastPos = Location;
  67.         DWORD eNum = 0; // Amount of events taken place
  68.         HANDLE Keyboard = GetStdHandle(STD_INPUT_HANDLE);
  69.         bool Player_Has_Moved = false;
  70.         int KeyDownEvent = 0;
  71.  
  72.         ReadConsoleInput(Keyboard, &record, 1, &eNum);
  73.  
  74.         KeyDownEvent = record.Event.KeyEvent.bKeyDown;
  75.  
  76.         if(record.EventType == KEY_EVENT && KeyDownEvent)
  77.         {                                                                                                       // If the user hits the keyboard:
  78.                 switch(record.Event.KeyEvent.wVirtualKeyCode)
  79.                 {
  80.                 case VK_RIGHT:                                                                 
  81.                         if(Location.X < (WIDTH - 1) )           // Check to make sure our new position doesn't go outside of our buffer
  82.                         {
  83.                                 Location.X++;                                                   // Increase the player's X value because we are going RIGHT
  84.                                 Player_Has_Moved = true;                                        // Set the flag to TRUE that the player moved
  85.                         }
  86.                         break;
  87.                                                                                                                 // Check to see if we hit the LEFT arrow key
  88.                 case VK_LEFT:
  89.                         if(Location.X > 0)                                                      // Check to make sure our new position doesn't go outside of our buffer
  90.                         {
  91.                                 Location.X--;                                                   // Decrease the player's X value because we are going LEFT
  92.                                 Player_Has_Moved = true;                                        // Set the flag to TRUE that the player moved
  93.                         }
  94.                         break;                                                                                 
  95.                                                                                                                 // Lets check if we hit the UP arrow:                                                          
  96.                 case VK_UP:
  97.                         if(Location.Y > 0)                                                      // Check to make sure our new position doesn't go outside of our buffer
  98.                         {
  99.                                 Location.Y--;                                                   // Decrease the player's Y value because we are going UP
  100.                                 Player_Has_Moved = true;                                        // Set the flag to TRUE that the player moved
  101.                         }
  102.                         break;
  103.                                                                                                                 // Let's check if we hit the DOWN
  104.                 case VK_DOWN:                                                  
  105.                         if(Location.Y < (HEIGHT - 1) )          // Check to make sure our new position doesn't go outside of our buffer
  106.                         {
  107.                                 Location.Y++;                                                   // Increase the player's Y value because we are going DOWN
  108.                                 Player_Has_Moved = true;                                        // Set the flag to TRUE that the player moved
  109.                         }
  110.                         break;
  111.                         // If we hit ESCAPE
  112.                 case VK_ESCAPE:                                                                
  113.                         exit(0);                                                                        // Quit the program
  114.                         break;
  115.                 }
  116.         }
  117.  
  118.         if(Player_Has_Moved)
  119.         {
  120.                 if(tempBuffer[Location.X + (Location.Y * WIDTH)].Char.AsciiChar != BLOCKER)
  121.                 {
  122.                         tempBuffer[LastPos.X + (LastPos.Y * WIDTH)].Char.AsciiChar = ' ';
  123.                         tempBuffer[LastPos.X + (LastPos.Y * WIDTH)].Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
  124.                 }
  125.                 else
  126.                 {
  127.                         Location = LastPos;
  128.                 }
  129.  
  130.                 return true;
  131.         }
  132.         return false;
  133.  
  134. }
  135.  
« Last Edit: February 16, 2010, 09:09:08 AM by Celestialkey »
Created By: Eczuo
Quote
I have noticed that people who claim that everything is predestined, and we can do nothing to change it, look both ways before they cross the road.
Quote
I'd prefer to die standing, than to live on my knees - Che Guevara
Quote
If you change the way you look at something, does that something change in any way?
- Quantum Theory

Hacking
Quote
Never in the field of human conflict was so much owed by so many to so few. - Winston Churchill


Quote from: Revelations 12:4
And his tail drew the third part of the stars of heaven, and did cast them into the earth; and the dragon stood before the woman which was ready to be delivered, for to devour her child as soon as it was born.

Quote
It takes skill to build an empire. It takes an idiot to maintain it.