Untitled
unknown
c_cpp
2 years ago
1.5 kB
10
Indexable
#include <iostream>
#include <conio.h>
#include <stdio.h>
using namespace std;
void drawMap(int x, int y, int playerX, int playerY) {
for (int j = 0; j < y; j++) {
cout << '#';
}
cout << endl;
for (int i = 0; i < x - 2; i++) {
cout << "#";
for (int j = 0; j < y - 2; j++) {
if (playerX == i + 1 && playerY == j) {
cout << 'A';
} else {
cout << ' ';
}
}
cout << "#" << endl;
}
for (int j = 0; j < y; j++) {
cout << '#';
}
}
bool newPositionIsValid(int x, int y, int mapX, int mapY) {
if (x < mapX - 1 && x > 0 && y < mapY - 2 && y >= 0) {
return true;
}
return false;
}
void movePlayer(int &x, int &y, char btn, int mapX, int mapY) {
//cout << "previous: " << x << ' ' << y << ' ' << endl;
int newX = x;
int newY = y;
if (btn == 'w') {
newX--;
}
if (btn == 'a') {
newY--;
}
if (btn == 's') {
newX++;
}
if (btn == 'd') {
newY++;
}
if (newPositionIsValid(newX, newY, mapX, mapY)) {
x = newX;
y = newY;
} else {
return;
}
//cout << "now: " << x << ' ' << y << ' ' << endl;
/*} else {
cout << " border\n";
return;
}*/
}
int main() {
char btn;
int playerX = 1;
int playerY = 0;
int mapX = 20;
int mapY = 35;
cout << "AAA";
while (true) {
drawMap(mapX, mapY, playerX, playerY);
btn = _getch();
if (btn == 27) {
return 0;
}
system("cls");
cout << ' ' << btn << endl;
movePlayer(playerX, playerY, btn, mapX, mapY);
}
}
Editor is loading...