Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

TheNerd Tk421

getch()

This topic is 5430 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
hmm, why are you having problems with that? that''s a very good question. maybe you don''t like the way the syntax goes in C++?

Why won''t winamp or foobar do my tax return? why oh WHY?! hehehe





--{You fight like a dairy farmer!}

Share this post


Link to post
Share on other sites

#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include <conio.h>
#define max_x 40
#define max_y 20
using namespace std;
void startup_menu();
int main_menu();
void move();void location(int, int);
void ClearArray();
int x,y;
int main(){
startup_menu();return 0;
}
void location(int xcoord, int ycoord){
cout << setw(73) << "Your location is " << xcoord << ", " << ycoord << endl;
}
int PlayerPos[max_y][max_x];//player position

void ClearArray()
{
for(int y=0;y<max_y;y++)
{
for(int x=0;x<max_x;x++)
{
PlayerPos[y][x] = 0;
}
}
}void move(){char dir = ''a'';while(dir != ''q'')
{ dir = getch();
system("cls");
for(int loop_y=0;loop_y < max_y;loop_y++)
{
for(int loop_x =0;loop_x < max_x;loop_x++)
{ int n = PlayerPos[loop_y][loop_x]; if(n == 1)
{
cout<<"C";
}
else cout<<" ";
}
cout<<endl;
}
switch(dir)
{
case ''w'': y--;
cout << "\nYou move North." << endl;
ClearArray();
PlayerPos[y][x] = 1;
break;
case ''s'': y++;
cout << "\nYou move South." << endl;
ClearArray();PlayerPos[y][x] = 1;break;
case ''d'': x++;
cout << "\nYou move East." << endl;ClearArray();
PlayerPos[y][x] = 1;
break; case ''a'': x--;
cout << "\nYou move West." << endl;
ClearArray(); PlayerPos[y][x] = 1;
break;
case ''l'': location(x,y);
break;
case ''q'': system("cls");
return; break;default: cout << "Try again"<<endl;
}
if(x==7 && y==11)
{
cout << "\nYou found the treasure!";
}
else if(x==12 && y==12)
{
cout << "\nYou have entered the town of Aranor." << endl;
}
}
}
void startup_menu()
{
char startgame;
startgame = ''@'';
ClearArray();
PlayerPos[max_y/2][max_x/2] = 1;
x = max_x/2;
y = max_y/2;
while(startgame != ''\r'')
{
cout << endl << endl << endl << endl;
cout << setw(45) << "NeverQuest" << endl;
cout<< setw(46) << "Press Enter!";
startgame = getch();
system("cls");
}
main_menu();
}
int main_menu()
{
cout << "*************" << endl;
cout<< "Main Menu" << endl;
cout<< "*************" << endl;
cout<< "1) New Game" << endl;
cout<< "2) Load Game " << endl;
cout<< "3) Manual" << endl;
cout<< "4) Credits" << endl;
cout<< "5) Exit" << endl;
cout<< "*************" << endl;
while(1){char choice;
choice = getch();
if(choice==''1'')
{
system("cls");
move();
}
if(choice==''4'')
{
cout<<"Made by TheNerd Tk421"<<endl;
cout<<""<<endl;
}
if(choice==''3'')
{
cout<<"
Go north: press ''w''"<<endl;
cout<<"
Go south: press ''s''"<<endl;
cout<<"
Go east: press ''d''"<<endl;
cout<<"
Go west: press ''a''"<<endl;
cout<<"
Quit to menu: press ''q''"<<endl;
}
else if(choice==''5'')
{
return 0;
}
}
}

when i compile it i get an error:
30 implicit declaration of function `int getchar(...)''
-------------------------------------------------------
line 30:
{ dir = getch();
-------------------------------------------------------

Share this post


Link to post
Share on other sites
I''m going to assume you''re using dev-c++ and the mingw compiler judging from your error msg. Try including the conio.c file into your project





--{You fight like a dairy farmer!}

Share this post


Link to post
Share on other sites
when I say add conio.c to your project, I actually meant you goto your project manager window, right-click add source file and find this conio.c file somewhere in your include directory or where it is and add it.





--{You fight like a dairy farmer!}

Share this post


Link to post
Share on other sites
quote:
Original post by TheNerd Tk421

#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include <conio.h>
#define max_x 40
#define max_y 20
using namespace std;
void startup_menu();
int main_menu();
void move();void location(int, int);
void ClearArray();
int x,y;
int main(){
startup_menu();return 0;
}
void location(int xcoord, int ycoord){
cout << setw(73) << "Your location is " << xcoord << ", " << ycoord << endl;
}
int PlayerPos[max_y][max_x];//player position

void ClearArray()
{
for(int y=0;y<max_y;y++)
{
for(int x=0;x<max_x;x++)
{
PlayerPos[y][x] = 0;
}
}
}void move(){char dir = ''a'';while(dir != ''q'')
{ dir = getch();
system("cls");
for(int loop_y=0;loop_y < max_y;loop_y++)
{
for(int loop_x =0;loop_x < max_x;loop_x++)
{ int n = PlayerPos[loop_y][loop_x]; if(n == 1)
{
cout<<"C";
}
else cout<<" ";
}
cout<<endl;
}
switch(dir)
{
case ''w'': y--;
cout << "\nYou move North." << endl;
ClearArray();
PlayerPos[y][x] = 1;
break;
case ''s'': y++;
cout << "\nYou move South." << endl;
ClearArray();PlayerPos[y][x] = 1;break;
case ''d'': x++;
cout << "\nYou move East." << endl;ClearArray();
PlayerPos[y][x] = 1;
break; case ''a'': x--;
cout << "\nYou move West." << endl;
ClearArray(); PlayerPos[y][x] = 1;
break;
case ''l'': location(x,y);
break;
case ''q'': system("cls");
return; break;default: cout << "Try again"<<endl;
}
if(x==7 && y==11)
{
cout << "\nYou found the treasure!";
}
else if(x==12 && y==12)
{
cout << "\nYou have entered the town of Aranor." << endl;
}
}
}
void startup_menu()
{
char startgame;
startgame = ''@'';
ClearArray();
PlayerPos[max_y/2][max_x/2] = 1;
x = max_x/2;
y = max_y/2;
while(startgame != ''\r'')
{
cout << endl << endl << endl << endl;
cout << setw(45) << "NeverQuest" << endl;
cout<< setw(46) << "Press Enter!";
startgame = getch();
system("cls");
}
main_menu();
}
int main_menu()
{
cout << "*************" << endl;
cout<< "Main Menu" << endl;
cout<< "*************" << endl;
cout<< "1) New Game" << endl;
cout<< "2) Load Game " << endl;
cout<< "3) Manual" << endl;
cout<< "4) Credits" << endl;
cout<< "5) Exit" << endl;
cout<< "*************" << endl;
while(1){char choice;
choice = getch();
if(choice==''1'')
{
system("cls");
move();
}
if(choice==''4'')
{
cout<<"Made by TheNerd Tk421"<<endl;
cout<<""<<endl;
}
if(choice==''3'')
{
cout<<"
Go north: press ''w''"<<endl;
cout<<"
Go south: press ''s''"<<endl;
cout<<"
Go east: press ''d''"<<endl;
cout<<"
Go west: press ''a''"<<endl;
cout<<"
Quit to menu: press ''q''"<<endl;
}
else if(choice==''5'')
{
return 0;
}
}
}



great use of whitespace, that code is so easy to read

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!