最近我初学了C++,就想做游戏,但我不会啊…
于是,我到网上疯狂搜教学,直到找到了这个:
C++ 字符跑酷#4 游戏制作实录_一块铌金属的博客-CSDN博客
这……字符跑酷?(当时我脑子里想的是以为只靠print做出来的,然后就入坑了)
太适合我了,二话不说,开搞!
首先拿出我们亲爱的基础引擎~
#include <iostream>
#include <cstdio>
#include <conio.h>
#include <windows.h>
using namespace std;
int main(){
}
接着,那肯定是创建一个窗口啊用上我们的system("");函数
system("title 迷宫游戏");
然后,就是迷宫了(我的画画技术不好)。
char a[100][100]={
"#########################",
"# I # # #",
"# # #",
"### ### ###### #### #",
"# ### ## # # #",
"## #### # # # #",
"# # ## # ## #",
"#########################",
接着,就是移动了,而且,这里不需要任何高端函数,简单的要命!
我写一段代码你们大概就明白了
ch=_getch();
if(ch=='a'){
if(a[x][y-1]!='#'){
a[x][y]=' ';
y--;
a[x][y]='*';
}
}
看吧,如此简单!
接着,我把代码完善(此处省略)
然后,全部代码如下喽:
#include <iostream>
#include <cstdio>
#include <conio.h>
#include <windows.h>
using namespace std;
int main(){
system("title 迷宫游戏");
char a[100][100]={
"#########################",
"# I # # #",
"# # #",
"### ### ###### #### #",
"# ### ## # # #",
"## #### # # # #",
"# # ## # ## #",
"#########################",
};
for(int i=0;i<=10;i++)
puts(a[i]);
char ch;
int x=1,y=1;
while(1){
ch=_getch();
if(ch=='a'){
if(a[x][y-1]!='#'){
a[x][y]=' ';
y--;
a[x][y]='*';
}
}
if(ch=='s'){
if(a[x+1][y]!='#'){
a[x][y]=' ';
x++;
a[x][y]='*';
}
}
if(ch=='d'){
if(a[x][y+1]!='#'){
a[x][y]=' ';
y++;
a[x][y]='*';
}
}
if(ch=='w'){
if(a[x-1][y]!='#'){
a[x][y]=' ';
x--;
a[x][y]='*';
}
}
system("cls");
for(int i=0;i<=10;i++)
puts(a[i]);
}
return 0;
}
记得,点赞,收藏,关注,转发,一键三连!!!