我试图在c ++上创建一个地图,它应该是一个四向交叉点,如下所示:
http://i.stack.imgur.com/GYTHA.png
我使用数组和ascii作为纹理,我不能只是简单地导入图像。
每次我尝试时,我都会看到这样的乱码输出:
http://i.stack.imgur.com/BPRp7.png
继承我的代码,有谁知道我做错了什么?
main.cpp中:
#include "functions.h"
#include <iostream>
using namespace std;
int main(){
const int ROWS = 47;
const int COLS = 70;
//create an array to represent the map row x cols
int map [ROWS][COLS];
/*green = 2, dark gray=8, light gray=7
*green occupies {1x1:15x23}, {1x48:15x70}, {33x1:47x23}, {33x48:47x70}
*dark gray occuppies{16x1:16x25},{1x24:15x25},{1x46:15x47},{16x46:16x70}, {32x1:32x25},{33x24:47x25},{32x46:32x70},{33x46:47x47}
*light gray occuppies{1x26:47x45},{17x1:31x25},{17x46:31x70}
*/
int green[4][4]={{0,0,14,22},{0,47,14,69}, {32,0,46,22}, {32,47,46,69}};
int dGray[8][4]={{15,0,15,24},{0,23,14,24},{0,45,14,46},{15,45,15,69}, {31,0,31,24},{32,23,46,24},{31,45,31,69},{32,45,46,46}};
int lGray[3][4]={{0,25,46,44},{16,0,30,24},{16,45,30,69}};
//save green to map array
for(int iG=0; iG < 4; iG++){ //loops through the green array with 4 rows
int sRow=green[iG][0]; //start row
int fRow=green[iG][2]; //finish row
int sCol=green[iG][1]; //start col
int fCol=green[iG][3]; //finish col
for(int jG=sRow; jG<fRow+1; jG++){
for(int kG=sCol; kG<fCol+1; kG++){
map[jG][kG]=2;
}
}
}
//save dGray to map array
for(int idG=0; idG < 8; idG++){ //loops through the green array with 8 rows
int sRow= dGray[idG][0]; //start row
int fRow=dGray[idG][2]; //finish row
int sCol=dGray[idG][1]; //start col
int fCol=dGray[idG][3]; //finish col
for(int jdG=sRow; jdG<fRow+1; jdG++){
for(int kdG=sCol; kdG<fCol+1; kdG++){
map[jdG][kdG]=8;
}
}
}
//save lGray to map array
for(int ilG=0; ilG < 3; ilG++){ //loops through the green array with 3 rows
int sRow= lGray[ilG][0]; //start row
int fRow=lGray[ilG][2]; //finish row
int sCol=lGray[ilG][1]; //start col
int fCol=lGray[ilG][3]; //finish col
for(int jlG=sRow; jlG<fRow+1; jlG++){
for(int klG=sCol; klG<fCol+1; klG++){
map[jlG][klG]=7;
}
}
}
//Loop through the map array and call the coutchar functions with corresponding textures
for(int i=0; i < ROWS; i++){
for(int j=0; j< COLS; j++){
//gotoxy(i,j);
if(map[i][j]==2){ //green grass texture space 219
coutchar(2,219);
}else if(map[i][j]==8){ // dark gray curb texture 176
coutchar(8,176);
}else if(map[i][j]==7){ // light gray road texture space 219
coutchar(7,219);
}
}
}
functions.cpp:
#include <iostream>
#include <iomanip>
#include <cmath>
#include <time.h>
#include <conio.h>
using namespace std;
#include <windows.h>
#include "functions.h"
//prints charcters on the screen screen with a given color (Eg: 2 is green)
void coutc(int color, char* output)
{
HANDLE handle= GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute( handle, color);
cout<< output;
SetConsoleTextAttribute( handle, color);
}
//prints number on the screen with a given color
void coutc(int color, int output)
{
HANDLE handle= GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute( handle, color);
cout<< output;
SetConsoleTextAttribute( handle, color);
}
//prints character based on ASCII code
void coutchar(int color, int output)
{
HANDLE handle= GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute( handle, color);
cout<< char(output);
SetConsoleTextAttribute( handle, color);
}
//move cursor to location(x, y)
void gotoxy ( short x, short y )
{
COORD coord = {x, y};
SetConsoleCursorPosition ( GetStdHandle ( STD_OUTPUT_HANDLE ), coord );
}
functions.h:
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
//prints charcters on the screen screen with a given color (Eg: 2 is green)
void coutc(int color, char* output);
//prints number on the screen with a given color
void coutc(int color, int output);
//prints character based on ASCII code
void coutchar(int color, int output);
//move cursor to location(x, y)
void gotoxy ( short x, short y );
#endif