c++游戏1(未完成)
2026-01-10 10:53:23
发布于:广东
#include <bits/stdc++.h>
using namespace std;
struct Node{
int x,y;
Node* next;
};
const int w=20;//W
const int h=20;//H
Node* head=NULL;
Node* tail=NULL;
int fx,fy;//FoodX,Foodyvoid
bool Lap(int x,int y){
Node* c=head;
while(c!=NULL){
if(c->x==x&&c->y==y) return true;
c=c->next;
}
return false;
}
food(){
do{
fx=rand()%(w-2)+1;
fy=rand()%(h-2)+1;
}
while(Lap(fx,fy));
}
void ih(int x,int y){
Node* nN=new Node;
nN->x=x;
nN->y=y;
nN->next=head;
head=nN;
if(tail==NULL) tail=head;
}
void game1(){
srand(time(0));
int sx=w/2;
int sy=h/2;
for(int i=0;i<3;i++) ih(sx-i,sy);
food();
}
//void draw{
//}
int main(){
game1();
cout<<"=================================="<<endl;
cout<<" 欢迎光临,人机餐厅 "<<endl;
cout<<"=================================="<<endl;
cout<<endl;
cout<<"按下任意键开始吃…电池…"<<endl;
cin.get();
return 0;
}
全部评论 1
#include <bits/stdc++.h> using namespace std; struct Node{ int x,y; Node* next; }; const int w=20;//W const int h=20;//H Node* head=NULL; Node* tail=NULL; int d=2,s=0; int fx,fy;//FoodX,Foodyvoid bool gameo=false; bool Lap2(int x,int y){ Node* c=head->next; while(c!=NULL){ if(c->x==x&&c->y==y) return true; c=c->next; } return false; } bool Lap1(int x,int y){ Node* c=head; while(c!=NULL){ if(c->x==x&&c->y==y) return true; c=c->next; } return false; } food(){ do{ fx=rand()%(w-2)+1; fy=rand()%(h-2)+1; }while(Lap1(fx,fy)); } void ih(int x,int y){ Node* nN=new Node; nN->x=x; nN->y=y; nN->next=head; head=nN; if(tail==NULL) tail=head; } void game1(){ srand(time(0)); int sx=w/2; int sy=h/2; for(int i=0;i<3;i++) ih(sx-i,sy); food(); } void draw(){ cout<<"\n\n\n"; cout<<" ====吃电池…=== 分数:"<<s<<endl; for(int y=0;y<h;y++){ cout<<" "; for(int x=0;x<w;x++){ if(y==0||y==h-1||x==0||x==w-1)cout<<"墙"; else if(x==fx&&y==fy)cout<<"饭"; else if(head!=NULL&&x==head->x&&y==head->y)cout<<"头"; else if(Lap2(x,y))cout<<"身"; else cout<<" "; } cout<<endl; } cout<<"<=={+=========================> "; cout<<endl; cout<<"W↑A←D→S↓"<<endl; cout<<"(*Φ口Φ*)===============>"<<endl ; } /// void input(){ char key; cin>>key; switch(key){ case'w':case'W':if(d!=1)d=0;break; case's':case'S':if(d!=0)d=1;break; case'd':case'D':if(d!=3)d=2;break; case'a':case'A':if(d!=2)d=3;break; case'q':case'Q':gameo=true;break; } } /// void u(){ int nx=head->x; int ny=head->y; switch(d){ case'0':ny--;break; case'1':ny++;break; case'2':nx--;break; case'3':nx++;break; } if(nx<=0||nx>=w-1||ny<=0||ny>=h-1){ gameo=true; return; } if(Lap2(nx,ny)){ gameo=true; return; } ih(nx,ny); if(nx==fx&&ny==fy){ s+=10; food(); } //else r(); } int main(){ game1(); cout<<"=================================="<<endl; cout<<" 欢迎光临,蛇蛇餐厅 "<<endl; cout<<"=================================="<<endl; cout<<end2026-01-17 来自 广东
0




















有帮助,赞一个