废话不多说,上代码:
#include <conio.h>
#include <iostream>
#include <windows.h>
using namespace std;
int jp(int a = 0) {
int ch;
if (_kbhit()) {
ch = _getch();
if (ch == 119) {//119上 97左 100右 115下 n--上 m--左
return 0;
}
if (ch == 97) {
return 1;
}
if (ch == 100) {
return 2;
}
if (ch == 115) {
return 3;
}
}
return 5;
}
int main() {
int ch, n, m, h = 0, q, e, fs = 0;
char x[11][11];
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
x[i][j] = '#';
}
}
x[5][5] = '@';
x[0][0] = '!';
n = 5;
m = 5;
q = -1;
e = -1;
Sleep(500);
while (1) {
system("cls");
h = jp(ch);
if (h == 0) {
x[n][m] = '#';
n--;
x[n][m] = '@';
}
if (h == 1) {
x[n][m] = '#';
m--;
x[n][m] = '@';
}
if (h == 2) {
x[n][m] = '#';
m++;
x[n][m] = '@';
}
if (h == 3) {
x[n][m] = '#';
n++;
x[n][m] = '@';
}//q--上 e--左
if (q > n) {
x[q][e] = '#';
q--;
x[q][e] = '!';
} else if (q < n) {
x[q][e] = '#';
q++;
x[q][e] = '!';
} else if (e > m) {
x[q][e] = '#';
e--;
x[q][e] = '!';
} else if (e < m) {
x[q][e] = '#';
e++;
x[q][e] = '!';
}
if (h == 5) {
ch = q;
q = n;
n = ch;
ch = e;
e = m;
m = ch;
x[q][e] = '!';
x[n][m] = '@';
ch = 0;
}
for (int i = 0; i < 11; i++) {
for (int a = 0, j = i; a < 11; a++) {
cout << x[j][a] << " ";
x[q][e] = '!';
x[n][m] = '@';
if (q != 0 && e != 0) { //补丁
x[0][0] = '#';
}
}
cout << "\n";
}
cout << n << " " << m << " || " << q << " " << e << "\n分数:" << fs;
if (q == n && e == m) {
system("cls");
cout << "你输了!";
Sleep(1000);
return 0;
}
x[q][e] = '!';
x[n][m] = '@';
Sleep(700);
fs++;
if (fs == 50) {
system("cls");
cout << "你赢了!";
Sleep(1000);
return 0;
}
}
}