Advertisment


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

0 Members and 1 Guest are viewing this topic.

Offline Celestialkey

  • Administrator
  • Hero Member
  • *
  • Posts: 3953
  • Reputation 4874
  • Gender: Male
  • Its Alive!!
    • View Profile
    • www.CelestialCoding.com
[24] A simple Maze
« on: December 21, 2008, 12:11:46 pm »
Code: cpp [Select]

/************************************************************
// $Program: Intro to Buffers!
//
// $Description: We will teach you how to use buffers now
// to paste images all at the same time instead of
// one character at a time. (faster)
*************************************************************/
#include <windows.h>

#define WIDTH 30 // Maze width
#define HEIGHT 20 // Maze Height
#define sLocX 0 // Player Start X
#define sLocY 10 // Player Start Y
#define SPRITE 1 // Player Sprite
#define BLOCKER 5 // Bricks

void Render_Maze(CHAR_INFO tempBuffer[], COORD Location);
bool Move_Sprite(CHAR_INFO tempBuffer[], COORD &Location);

int main()
{
CHAR_INFO Maze_Buffer[WIDTH*HEIGHT] = {0};
COORD Maze_Size = {WIDTH, HEIGHT};
COORD Current_Loc = {sLocX, sLocY};
srand( GetTickCount() ); // Set a random seed for our random generation

for(int y=0; y<Maze_Size.Y; y++)
{
for(int x=2; x<Maze_Size.X; x++)
{
if(!(rand()%3)) // Set it up so if the random number is a 2 then it will draw a blocker
{
Maze_Buffer[x + (y * WIDTH)].Char.UnicodeChar = BLOCKER; // Create our wall
Maze_Buffer[x + (y * WIDTH)].Attributes = FOREGROUND_GREEN | FOREGROUND_BLUE;
}
}
}

Render_Maze(Maze_Buffer, Current_Loc); // Draw the maze to the console window

while(true) // Our game loop
{
if(Move_Sprite(Maze_Buffer, Current_Loc))
Render_Maze(Maze_Buffer, Current_Loc);
}

return 0;
}

void Render_Maze(CHAR_INFO tempBuffer[], COORD tLocation)
{
SMALL_RECT Render_Area = {0, 0, WIDTH-1, HEIGHT-1}; // (Start x, start y, width x, height y)
COORD Maze_Size = {WIDTH, HEIGHT};
COORD PointZero = {0, 0}; // Our location to draw from in the rectangle
HANDLE Monitor = GetStdHandle(STD_OUTPUT_HANDLE);

tempBuffer[tLocation.X + (tLocation.Y * WIDTH)].Char.AsciiChar = SPRITE;
tempBuffer[tLocation.X + (tLocation.Y * WIDTH)].Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;

WriteConsoleOutput(Monitor, tempBuffer, Maze_Size, PointZero, &Render_Area);
}

bool Move_Sprite(CHAR_INFO tempBuffer[], COORD &Location)
{
INPUT_RECORD record;
COORD LastPos = Location;
DWORD eNum = 0; // Amount of events taken place
HANDLE Keyboard = GetStdHandle(STD_INPUT_HANDLE);
bool Player_Has_Moved = false;
int KeyDownEvent = 0;
 
ReadConsoleInput(Keyboard, &record, 1, &eNum);

KeyDownEvent = record.Event.KeyEvent.bKeyDown;

if(record.EventType == KEY_EVENT && KeyDownEvent)
{ // If the user hits the keyboard:
switch(record.Event.KeyEvent.wVirtualKeyCode)
{
case VK_RIGHT:
if(Location.X < (WIDTH - 1) ) // Check to make sure our new position doesn't go outside of our buffer
{
Location.X++; // Increase the player's X value because we are going RIGHT
Player_Has_Moved = true; // Set the flag to TRUE that the player moved
}
break;
// Check to see if we hit the LEFT arrow key
case VK_LEFT:
if(Location.X > 0) // Check to make sure our new position doesn't go outside of our buffer
{
Location.X--; // Decrease the player's X value because we are going LEFT
Player_Has_Moved = true; // Set the flag to TRUE that the player moved
}
break;
// Lets check if we hit the UP arrow:
case VK_UP:
if(Location.Y > 0) // Check to make sure our new position doesn't go outside of our buffer
{
Location.Y--; // Decrease the player's Y value because we are going UP
Player_Has_Moved = true; // Set the flag to TRUE that the player moved
}
break;
// Let's check if we hit the DOWN
case VK_DOWN:
if(Location.Y < (HEIGHT - 1) ) // Check to make sure our new position doesn't go outside of our buffer
{
Location.Y++; // Increase the player's Y value because we are going DOWN
Player_Has_Moved = true; // Set the flag to TRUE that the player moved
}
break;
// If we hit ESCAPE
case VK_ESCAPE:
exit(0); // Quit the program
break;
}
}

if(Player_Has_Moved)
{
if(tempBuffer[Location.X + (Location.Y * WIDTH)].Char.AsciiChar != BLOCKER)
{
tempBuffer[LastPos.X + (LastPos.Y * WIDTH)].Char.AsciiChar = ' ';
tempBuffer[LastPos.X + (LastPos.Y * WIDTH)].Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
}
else
{
Location = LastPos;
}

return true;
}
return false;

}
« 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.