user controls - How to make movable character in maze with C++ -
so have maze printed out once user presses 1 in order launch map , other variables. have declared player smiley face or set equal 1. however, make character movable throughout maze having trouble doing so. have created player move space in order move without overwriting other code. thinking of using switch statement , detecting user input , changing x , y positions based on dont know how put in mapcreation function. wondering if guys me generate variables such treasures @ random locations throughout map , if wall or '|' dont print. treasures spawn @ same location. input on of questions help! thank you!
** ignore cases being used dont have use them.
**update if has link somewhere would helpful! thank you!
// header files #include <cstdlib> #include <iostream> #include <windows.h> using namespace std; handle console = getstdhandle(std_output_handle); // use of setconsoletextattribute() #define level_count (2) const char* maze[level_count][12] = { { "||||||||||||\n", "| | |\n", "| | |\n", "| |||| |", "| |", "| |", "|||||| |", "| | |", "| | | |", "| | |", "| | |", "||||||||||||", }, { "||||||||||||", "| | |", "| ||||| |", "| | |", "| |", "| ||||", "| |", "| | |", "| | |", "||||||| |", "| |", "||||||||||||", }, }; // function prototypes void titlescreen(); // prints title , instructions void mapcreation( char arr[][12], int level); void drawmap(char arr[][12]); bool update(char arr[][12], int &level, int &lives, int &score); void loadmap( char arr[][12], int level); // player struct struct player{ int x; int y; char move; }; // main program int main () { // initialize variables int option; char basemap[12][12]; int level = 1; int lives = 3; int score = 0; bool gameover = false; bool levelcompleted = false; setconsoletextattribute(console, 240); // change background white system("cls");// clears screen in order remove black background titlescreen(); // display title // do-while loop starts { cin >> option; // take in input if(option == 1) // temporary option check next screen { //display maze system("cls");// clears screen in order remove black background mapcreation( basemap, 1 ); // create map, 1 map 1 drawmap(basemap); // iterate throup map , print out // update(basemap, level, lives, score); } if(option == 2) { //display maze system("cls");// clears screen in order remove black background mapcreation( basemap, 2 ); // create map, 1 map 1 drawmap(basemap); // iterate throup map , print out // update(basemap, level, lives, score); } } while( option !=1); // condition of do-while loop system("pause"); // pause user, temporary return 0; } void titlescreen(){ cout << " welcome treasure hunter!\n\n"; cout << "in order beat game must find treasure\n"; cout << " located in maze. can move using \n"; cout << " arrow keys or wasd.\n\n"; cout << " warning! there traps take life away as\n"; cout << " add life! however, hidden careful!\n "; cout << " goodluck , have fun!\n\n\n\n"; cout << " press ctrl+c quit\n"; } void mapcreation( char arr[12][12], int level ) { int traps = 0; int lives = 0; int treasure = 0; int x ; int y ; for(int = 0; < 12; i++) { for(int j = 0; j < 12; j++) { arr[i][j] = 0; } } // load map: loadmap(arr,level); switch (level) { case 1: break; case 2: // level 2 map break; } } void drawmap(char arr[12][12]) { // declare variables prtined out player player; player.x = 1; player.y = 1; player.move = ' '; int traps = 0; int lives = 0; int treasure = 0; // lives,traps, , treasure x , y positions int trap_y,trap_x,lives_x,lives_y,traps_x,traps_y; // randomly place variables trap_x = (rand() % 10); trap_y = (rand() % 10); lives_x = (rand() % 12); lives_y = (rand() % 12); traps_x = (rand() % 12); traps_y = (rand() % 12); for(int = 0; < 12; i++) { cout << endl; // print out new line for(int j = 0; j < 12; j++ ) { if(player.x == && player.y == j) arr[player.x][player.y] = 1; // if variables not equal wall character print out if(arr[trap_x][trap_y] != '|' ) arr[trap_x][trap_y] = 4; // treasure 4 treasure++; if(arr[lives_x][lives_y] != '|')// traps 2 arr[lives_x][lives_y]= 2; traps++; if(arr[traps_x][traps_y] != '|') // lives 3 arr[traps_x][traps_y]= 3; lives++; cout << arr[i][j]; } } cout << endl; } /*bool update(char arr[][12], int &level, int &lives, int &score) { bool levelcompleted = false; bool gameover = false; } */ void loadmap( char arr[][12], int level) { if((level < 0) || (level >= level_count)) return; for(int = 0; < 12; i++) { const char* row = maze[level][i]; for(int j = 0; j < 12; j++) { if(row[j] == 0) break; // end of string if(row[j] == ' ') arr[i][j] = 0; // set spaces 0 else arr[i][j] = row[j]; } } }
on windows, can use readconsoleinput()
navigate keyboard in windows console. solution checks collision using statement if (maze[player.y][player.x-1]==0)
. 0
means no wall, 1
means wall has been hit.
here's example of usage of readconsoleinput()
.
#include <stdio.h> #include <windows.h> #include <iostream> using namespace std; #define vk_w 0x57 #define vk_s 0x53 #define vk_a 0x41 #define vk_d 0x44 struct t_player{ int x,y;}; t_player player; char playersymbol=219; char mazewall=0; int mazex=31,mazey=8; int maze[16][24] = {{ 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,0,0,0,0,0,1}, { 1,0,1,1,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, { 1,0,0,0,0,1,0,0,0,1,0,1,1,1,1,0,0,1,0,0,0,0,0,1}, { 1,0,0,0,0,1,0,0,0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,1}, { 1,0,0,0,0,1,0,0,0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,1}, { 1,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1}, { 1,0,1,1,1,1,1,1,0,0,0,0,0,0,1,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,0,0,0,0,1}, { 1,0,0,0,1,0,0,0,0,1,0,0,0,0,1,1,1,1,1,0,0,0,0,1}, { 1,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, { 1,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1}, { 1,0,0,0,1,0,0,0,0,1,1,1,1,0,0,0,1,0,0,0,0,0,0,1}, { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,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,0,0,0,0,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} }; void clrscr(); void gotoxy(int x, int y); void setcolor(word color); void textcolor(unsigned char fcolor,unsigned char bcolor); void moveleft(); void moveright(); void moveup(); void movedown(); void drawmaze(int px,int py); void putmenu(); void putplayer(); int main() { dword mode; /* preserved console mode */ input_record event; /* input event */ bool exitgame = false; /* program termination flag */ unsigned int counter = 0; /* number of times 'esc' pressed */ /* console input handle */ handle hstdin = getstdhandle( std_input_handle ); /* preserve original console mode */ getconsolemode( hstdin, &mode ); /* set no line-buffering, no echo, no special-key-processing */ setconsolemode( hstdin, 0 ); player.x=20; player.y=13; clrscr(); setcolor(15); while (!exitgame) { if (waitforsingleobject( hstdin, 0 ) == wait_object_0) /* if kbhit */ { dword count; /* ignored */ /* input event */ readconsoleinput( hstdin, &event, 1, &count ); /* respond key release events */ if ((event.eventtype == key_event) && !event.event.keyevent.bkeydown) clrscr(); putmenu(); sleep(100); switch (event.event.keyevent.wvirtualkeycode) { case vk_escape: exitgame = true; break; case vk_left: // left key move player left moveleft(); break; case vk_right: // right key move player right moveright(); break; case vk_up: // key move player moveup(); break; case vk_down: // down key move player down movedown(); break; case vk_a: // left key move player left moveleft(); break; case vk_d: // right key move player right moveright(); break; case vk_w: // key move player moveup(); break; case vk_s: // down key move player down movedown(); break; }//switch putplayer(); } } gotoxy(1,23);cout<<" "; setconsolemode( hstdin, mode ); return 0; } void putplayer() { setcolor(9); gotoxy( player.x +mazex, player.y +mazey); cout<<playersymbol; setcolor(7); } void putmenu() { gotoxy(1,1);cout<<"keyboard navigator "; setcolor(14); gotoxy(31,1);cout<<"use keys w,s, a,d, left,right,up,down "; setcolor(7); gotoxy(31,2);cout<<"w or key = move player "; gotoxy(31,3);cout<<"s or down key = move player down "; gotoxy(31,4);cout<<"a or left key = move player left "; gotoxy(31,5);cout<<"d or right key = move player right "; setcolor(11); drawmaze(mazex,mazey); setcolor(7); } void drawmaze(int px,int py) { for(int y =0; y<16;y++) { for(int x=0; x<24; x++) { if (maze[y][x]==1) mazewall=219; else mazewall=32; gotoxy(x+px,y+py); cout<< mazewall; } } } void moveleft() { gotoxy(31,7); cout<<"left key move player left \n\n"; if (maze[player.y][player.x-1]==0) player.x = player.x -1; } void moveright() { gotoxy(31,7); cout<<"right key move player right \n\n"; if (maze[player.y][player.x+1]==0) player.x = player.x +1; } void moveup() { gotoxy(31,7); cout<<"up key move player \n\n"; if (maze[player.y-1][player.x]==0) player.y = player.y -1; } void movedown() { gotoxy(31,7); cout<<"down key move player down \n\n"; if (maze[player.y+1][player.x]==0) player.y = player.y +1; } void gotoxy(int x, int y) { coord coord; coord.x = x; coord.y = y; setconsolecursorposition(getstdhandle(std_output_handle), coord); return; } void setcolor(word color) { setconsoletextattribute(getstdhandle(std_output_handle),color); return; } void setforegroundandbackgroundcolor(int foregroundcolor,int backgroundcolor) { int color=16*backgroundcolor+foregroundcolor; setcolor(color); } void clrscr() { coord coordscreen = { 0, 0 }; dword ccharswritten; console_screen_buffer_info csbi; dword dwconsize; handle hconsole = getstdhandle(std_output_handle); getconsolescreenbufferinfo(hconsole, &csbi); dwconsize = csbi.dwsize.x * csbi.dwsize.y; fillconsoleoutputcharacter(hconsole, text(' '), dwconsize, coordscreen, &ccharswritten); getconsolescreenbufferinfo(hconsole, &csbi); fillconsoleoutputattribute(hconsole, csbi.wattributes, dwconsize, coordscreen, &ccharswritten); setconsolecursorposition(hconsole, coordscreen); return; }
Comments
Post a Comment