This is my first attempt at using classes for a program, it is sort of like a "learn how to use class's in c++ test" program for me. Of all the tutorials I have read about them, I couldn't find anything useful to use them with except maybe games (which I don't know how program).
Could anyone share with me some tips about what to change about implementation of them and how I can use them better in the future? (note: this program was only a test of how to use classes not meant to be that great or anything). Thanks in advance for your help.
Code:#include <iostream> #include <string> using namespace std; char map[50][30] = { 0 }; char message[30]; bool done = false; class Charactor { public: Charactor(); ~Charactor(); void moveup(); void movedown(); void moveleft(); void moveright(); int xpos; int ypos; int height; int width; int health; int speed; } player; Charactor::Charactor() { xpos = 5; ypos = 15; height = 1; width = 2; health = 100; speed = 1; } Charactor::~Charactor() { } void Charactor::moveup() { ypos -= speed; } void Charactor::movedown() { ypos += speed; } void Charactor::moveleft() { xpos -= speed; } void Charactor::moveright() { xpos += speed; } class Barrier { public: Barrier(); ~Barrier(); int x1,x2,y1,y2; } room; Barrier::Barrier() { x1 = 0; x2 = 50; y1 = 0; y2 = 20; } Barrier::~Barrier() { } void checkbarrier() { if (player.ypos + player.height == room.y2) { strcpy(message, "You cannot go through walls!"); player.ypos -= 1; } else if (player.ypos - player.height == room.y1) { strcpy(message, "You cannot go through walls!"); player.ypos += 1; } else if (player.xpos + player.width == room.x2) { strcpy(message, "You cannot go through walls!"); player.xpos -= 1; } else if (player.xpos - player.width == room.x1) { strcpy(message, "You cannot go through walls!"); player.xpos += 1; } else strcpy(message, "You are doing fine! "); } void drawmap() { for (int x = 1; x < 49; x++) for (int y = 1; y < 19; y++) { map[x][y] = ' '; } for (int x = 0; x < 50; x++) { map[x][0] = '-'; map[x][49] = '-'; } for (int y = 0; y < 20; y++) { map[0][y] = '|'; map[49][y] = '|'; } map[player.xpos][player.ypos] = '|'; map[player.xpos][player.ypos - 1] = 'O'; map[player.xpos + 1][player.ypos] = '-'; map[player.xpos - 1][player.ypos] = '-'; map[player.xpos + 1][player.ypos + 1] = '\\'; map[player.xpos - 1][player.ypos + 1] = '/'; } void displaymap() { for (int y = 0; y < 20; y++) { for (int x = 0; x < 50; x++) { cout << map[x][y]; } cout <<endl; } } void drawscreen() { char command; cout <<"--------------------------------------------------"<<endl; cout <<"|Commands: W - Up, S - Down, A - Left, D - Right |"<<endl; cout <<"| Q - Quit Press Enter To confirm. |"<<endl; cout <<"|Health:"<<player.health<<" Message: "<< message <<"|"<<endl; cout <<"---------------------------------------------------"<<endl; displaymap(); cout <<"Command: "; cin >> command; if (command == 'w') player.moveup(); else if (command == 's') player.movedown(); else if (command == 'a') player.moveleft(); else if (command == 'd') player.moveright(); else if (command == 'q') done = true; system("cls"); } int main() { while (!done) { drawmap(); drawscreen(); checkbarrier(); } return 0; }



LinkBack URL
About LinkBacks



i am really disorganised......