随手写的,一个十分有趣的贪吃蛇小游戏。
用了随机数与二维数组实现。欢迎各位大佬提出修改意见
#include<iostream>
#include <cstdlib>
using namespace std;
int x=0,y=0,bx,by,f=0;
char move;
char map[20][20];
int body[20][2];
int ifb=0;
int main(int argc, const char * argv[]){
bx=rand()%20;
by=rand()%20;
while(1){
cin>>move;
if(move=='w'){
x--;
}
else if(move=='s'){
x++;
}
else if(move=='a'){
y--;
}
else if(move=='d'){
y++;
}
x=x%20;
y=y%20;
if(x<0){
x=0;
}
if(y<0){
y=0;
}
for(int i=f;i>=1;i--){
body[i][0]=body[i-1][0];
body[i][1]=body[i-1][1];
}
body[0][0]=x;
body[0][1]=y;
system("cls");
for(int i=0;i<20;i++){
for(int j=0;j<20;j++){
ifb=0;
for(int k=0;k<=f;k++){
if(i==body[k][0]&&j==body[k][1]){
cout<<" "<<"0"<<" ";
ifb=1;
}
}
if(ifb==0){
if(i==x&&j==y){
cout<<" "<<"O"<<" ";
}
else if(i==bx&&j==by){
cout<<" "<<"H"<<" ";
}
else{
cout<<" "<<"/"<<" ";
}
}
}
cout<<endl;
}
cout<<"分数:"<<f;
if(x==bx&&y==by){
f++;
if(f>=20){
f=20;
cout<<"不能再多了";
}
bx=rand()%20;
by=rand()%20;
}
}
}
标签:body,20,int,move,c++,else,char,小游戏,贪吃蛇
From: https://blog.csdn.net/2301_79451377/article/details/145167177