创建框架,输出一个10*10带边界的字符界面
#include <cstdio>
#include <iostream>
using namespace std;
char mp[101][101] = {
"************"
"*o *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"************"
}; //*代表边界
int main(){
//输出整个界面
for(int i = 0; i <= 11; i++){
for(int j = 0; j <= 11; j++)
cout << mp[i][j];
cout << endl;
}
return 0;
}
让界面动起来,asdw代表4个方向,q退出
当按下按键的时候:1清屏,2修改(o移动),3重画
#include <cstdio>
#include <iostream>
#include <conio.h>
using namespace std;
char mp[101][101] = {
"************"
"*o *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"************"
}; //*代表边界
int x = 1, y = 1; //o的默认坐标1,1
int main(){
char key;
//程序主体是一个循环
do{
system("cls"); //清屏
mp[x][y] = ' '; //原o位置清空
if(key == 'a') y--; //左
if(key == 's') x++; //下
if(key == 'd') y++; //右
if(key == 'w') x--; //上
mp[x][y] = 'o'; //新o位置画o
//重画
for(int i = 0; i <= 11; i++){
for(int j = 0; j <= 11; j++)
cout << mp[i][j];
cout << endl;
}
key = getch(); //a s d w代表四个方向,q退出
}while(key != 'q'); //q代表退出
return 0;
}
虽然动起来,单遇到#边界时会把边界“吃掉”
判断是否边界,或者下个位置是否能走,引入下个位置的坐标nx,ny
#include <cstdio>
#include <iostream>
#include <conio.h>
using namespace std;
char mp[101][101] = {
"************"
"*o *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"************"
}; //*代表边界
int x = 1, y = 1; //o的默认坐标1,1
int nx, ny; //o的下一个位置预判
int main(){
char key;
//程序主体是一个循环
do{
system("cls"); //清屏
//求下一个位置
nx = x; ny = y;
if(key &#