1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include <conio.h>
//map dimensions
#define MAP_WIDTH 20
#define MAP_HEIGHT 20
//tile types
#define TILE_BACKGROUND 0
#define TILE_WALL 1
using namespace std;
void clear_screen(void); //clears the screen
void display_character(int x, int y); //displays a char at x and y axis.
int nMapArray[MAP_HEIGHT][MAP_WIDTH] = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
};
void DrawMap(void);
void clear_screen ( void ) //Clears the screen
{
DWORD n; /* Number of characters written */
DWORD size; /* number of visible characters */
COORD coord = {0}; /* Top left screen position */
CONSOLE_SCREEN_BUFFER_INFO csbi;
/* Get a handle to the console */
HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
GetConsoleScreenBufferInfo ( h, &csbi );
/* Find the number of characters to overwrite */
size = csbi.dwSize.X * csbi.dwSize.Y;
/* Overwrite the screen buffer with whitespace */
FillConsoleOutputCharacter ( h, TEXT ( ' ' ), size, coord, &n );
GetConsoleScreenBufferInfo ( h, &csbi );
FillConsoleOutputAttribute ( h, csbi.wAttributes, size, coord, &n );
/* Reset the cursor to the top left position */
SetConsoleCursorPosition ( h, coord );
}
void display_character(int x, int y) //Displays a character at x and y axis.
{
DWORD size;
CONSOLE_SCREEN_BUFFER_INFO csbi;
HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
DWORD n;
GetConsoleScreenBufferInfo ( h, &csbi );
size = 1; //How many characters "@" compared to "@@"
COORD coord = {x, y}; //coordinates x and y
FillConsoleOutputCharacter(h, TEXT('@'), size, coord, &n); //the TEXT(' ') is your character.
}
void display_tile_background(int x, int y) //Displays a character at x and y axis.
{
DWORD size;
CONSOLE_SCREEN_BUFFER_INFO csbi;
HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
DWORD n;
GetConsoleScreenBufferInfo ( h, &csbi );
size = 1; //How many characters "@" compared to "@@"
COORD coord = {x, y}; //coordinates x and y
FillConsoleOutputCharacter(h, TEXT('.'), size, coord, &n); //the TEXT(' ') is your character.
}
void display_tile_wall(int x, int y) //Displays a character at x and y axis.
{
DWORD size;
CONSOLE_SCREEN_BUFFER_INFO csbi;
HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
DWORD n;
GetConsoleScreenBufferInfo ( h, &csbi );
size = 1; //How many characters "@" compared to "@@"
COORD coord = {x, y}; //coordinates x and y
FillConsoleOutputCharacter(h, TEXT('#'), size, coord, &n); //the TEXT(' ') is your character.
}
void DrawMap(void)
{
for(int y = 0; y < MAP_HEIGHT; y++)
{
for(int x = 0; x < MAP_WIDTH; x++)
{
switch(nMapArray[y][x])
{
case TILE_BACKGROUND:
display_tile_background(x, y);
break;
case TILE_WALL:
display_tile_wall(x, y);
break;
}
}
}
}
int main()
{
//clear screen
clear_screen();
//draw the map
DrawMap();
int xAxis = 5; //initializes coordinated for character to originally be on screen
int yAxis = 5;
display_character(xAxis, yAxis); //display the character
char nKey = _getch();
while(true) //constant while loop to continue the game
{
//clear screen
clear_screen();
//draw map
DrawMap();
//draw the player to the screen
display_character(xAxis, yAxis);
//wait for the user to do something
char keypress;
cin.get(keypress);
//processing phase - implement the player's command
switch(keypress)
{
//move up
case 'w':
if(yAxis >= 1) //can't go up off the screen
{
yAxis--;
break;
}
else
{
break;
}
//move down
case 's':
if(yAxis <= 23) //can't go down off the screen
{
yAxis++;
break;
}
else
{
break;
}
//move left
case 'a':
if(xAxis >= 1)
{
xAxis--;
break;
}
else
{
break;
}
//move right
case 'd':
if(xAxis <= 78)
{
xAxis++;
break;
}
else
{
break;
}
//quit
default:
break;
}
}
return 0;
}
|