cvitullo
07-10-2008, 01:29 AM
hey, just wondering, anyone know what's wrong with this? it started out as a
proof of concept for a friend who's trying to teach himself c++, but i was bored.
#include <iostream>
using namespace std;
class Player {
protected:
int row;
int col;
int health;
int xp;
int level;
int super;
bool key;
public:
bool Key(){
return key;
}
int getHealth() {
return health;
}
void setHealth(int h) {
health = h;
}
int getRow() {
return row;
}
void setRow(int r) {
row = r;
}
int getCol() {
return col;
}
void setCol(int c) {
col = c;
}
};
Player player;
class Enemy {
protected:
int row;
int col;
int health;
};
int main() {
char board[49][49];
int i = 0, j = 0;
int r = 0, c = 0;
int level = 1;
char input;
player.setCol(5);
player.setRow(5);
while (true) {
int getCol(c);
int getRow(r);
for (i = 0; i <= 49; i++) {
if (i == player.getCol() && j == player.getRow())
board[i][j] = 'D';
else if (i == 5 && j == 0)
board[i][j] = '^';
else if (i == 43 && j ==45 && player.Key() == false)
board[i][j] = 'K';
else if (i == 0 || i == 48 || j == 0 || j == 49)
board[i][j] = '[';
else
board[i][j] = ' ';
if (i == 49 && j == 49) {
i = 0;
j = 0;
break;
}
if (i == 49) {
i = 0;
j++;
cout << endl;
}
cout << board[i][j];
}
cin >> input;
if (input == 'w') {
player.setRow(r--);
}
if (input == 's') {
player.setRow(r++);
}
if (input == 'a') {
player.setCol(c--);
}
if (input == 'd') {
player.setCol(c++);
}
if (input == 'p') {
break;
}
if (r == 49)
player.setRow(r--);
if (c == 48)
player.setCol(c--);
if (r == 0 && c !=5)
player.setRow(r++);
if (c == 0)
player.setCol(c++);
system("cls");
}
return 0;
}
it's not done, so there are a lot of empty functions, but for some reason the
hit detection doesn't work at all and if you reverse directions if keeps going for one more redraw.
it worked fine for a while, but then i decided to change a bunch of it into
classes and functions and expand it into an actual game.
originally, all of the classes were structs and there were no functions (wasn't the cleanest piece of code).
i still have that version, but i don't want to post it now, it would make the post too long.
proof of concept for a friend who's trying to teach himself c++, but i was bored.
#include <iostream>
using namespace std;
class Player {
protected:
int row;
int col;
int health;
int xp;
int level;
int super;
bool key;
public:
bool Key(){
return key;
}
int getHealth() {
return health;
}
void setHealth(int h) {
health = h;
}
int getRow() {
return row;
}
void setRow(int r) {
row = r;
}
int getCol() {
return col;
}
void setCol(int c) {
col = c;
}
};
Player player;
class Enemy {
protected:
int row;
int col;
int health;
};
int main() {
char board[49][49];
int i = 0, j = 0;
int r = 0, c = 0;
int level = 1;
char input;
player.setCol(5);
player.setRow(5);
while (true) {
int getCol(c);
int getRow(r);
for (i = 0; i <= 49; i++) {
if (i == player.getCol() && j == player.getRow())
board[i][j] = 'D';
else if (i == 5 && j == 0)
board[i][j] = '^';
else if (i == 43 && j ==45 && player.Key() == false)
board[i][j] = 'K';
else if (i == 0 || i == 48 || j == 0 || j == 49)
board[i][j] = '[';
else
board[i][j] = ' ';
if (i == 49 && j == 49) {
i = 0;
j = 0;
break;
}
if (i == 49) {
i = 0;
j++;
cout << endl;
}
cout << board[i][j];
}
cin >> input;
if (input == 'w') {
player.setRow(r--);
}
if (input == 's') {
player.setRow(r++);
}
if (input == 'a') {
player.setCol(c--);
}
if (input == 'd') {
player.setCol(c++);
}
if (input == 'p') {
break;
}
if (r == 49)
player.setRow(r--);
if (c == 48)
player.setCol(c--);
if (r == 0 && c !=5)
player.setRow(r++);
if (c == 0)
player.setCol(c++);
system("cls");
}
return 0;
}
it's not done, so there are a lot of empty functions, but for some reason the
hit detection doesn't work at all and if you reverse directions if keeps going for one more redraw.
it worked fine for a while, but then i decided to change a bunch of it into
classes and functions and expand it into an actual game.
originally, all of the classes were structs and there were no functions (wasn't the cleanest piece of code).
i still have that version, but i don't want to post it now, it would make the post too long.