谁能帮我把贪吃蛇动起来
贪吃蛇.rar
(98.79 KB)
#include <iostream> #include <time.h> #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <process.h> /* _beginthread, _endthread */ #include <windows.h> /* Sleep() */ using namespace std; int a[100][100]={0}; int yes; int p,q,sum; class Snake { public: Snake() { x[0]=0;y[0]=4;x[1]=0;y[1]=3;x[2]=0;y[2]=2; line=3; sorce=0; } int new1() { if(a[x[line-1]-1][y[line-1]]==0) { x[line]=x[line-1]-1; y[line]=y[line-1]; } if(a[x[line-1]+1][y[line-1]]==0) { x[line]=x[line-1]+1; y[line]=y[line-1]; } if(a[x[line-1]][y[line-1]-1]==0) { x[line]=x[line-1]; y[line]=y[line-1]-1; } if(a[x[line-1]][y[line-1]+1]==0) { x[line]=x[line-1]; y[line]=y[line-1]+1; } if(sum!=0) { line+=1; sorce+=10; } return 0; } int run(); int round(); int x[200]; int y[200]; int line; int sorce; }; class food { public: int born() { if(1) { for(;;) { x=rand()%10; y=rand()%10; if(a[x][y]!=42) break; } a[x][y]=3; } return 0; } int x; int y; }; Snake sanke; food fod; char way = 'd';// 刚开始的方向是向前 int again() { for(int i=0;i<100;i++) for(int j=0;j<100;j++) if(i!=fod.x||j!=fod.y) a[i][j]=0; return 0; } int Snake::round() { //char way; Snake(); for(int i=sanke.line-1;i>0;i--) { sanke.x[i]=sanke.x[i-1]; sanke.y[i]=sanke.y[i-1]; } // way = getch(); //不在这里输入 system("cls"); if(way=='w'||way=='a'||way=='s'||way=='d') sum+=1; switch(way) { case 'D': case 'd': { if((sanke.y[0])==9||a[sanke.x[0]][sanke.y[0]+1]==42) { cout<<"-_-!你撞死了"<<endl; cin.get(); exit(0); } sanke.y[0]=sanke.y[0]+1; again(); if(yes==1) fod.born(); for(int t=0;t<=line-1;t++) a[sanke.x[t]][sanke.y[t]]=42; };break; case 'S': case 's': { if(sanke.x[0]==9||a[sanke.x[0]+1][sanke.y[0]]==42) { cout<<"-_-!你撞死了"<<endl; cin.get(); exit(0); } sanke.x[0]+=1; again(); if(yes==1) fod.born(); for(int t=0;t<=line-1;t++) a[sanke.x[t]][sanke.y[t]]=42; };break; case 'A': case 'a': { if(sanke.y[0]==0||a[sanke.x[0]][sanke.y[0]-1]==42) { cout<<"-_-!你撞死了"<<endl; cin.get(); exit(0); } sanke.y[0]-=1; again(); if(yes==1) fod.born(); for(int t=0;t<=line-1;t++) a[sanke.x[t]][sanke.y[t]]=42; };break; case 'W': case 'w': { if(sanke.x[0]==0||a[sanke.x[0]-1][sanke.y[0]]==42) { cout<<"-_-!你撞死了"<<endl; cin.get(); exit(0); } sanke.x[0]-=1; again(); if(yes==1) fod.born(); for(int t=0;t<=line-1;t++) a[sanke.x[t]][sanke.y[t]]=42; };break; } return 0; } int Snake::run() { yes=1; for(p=0;p<10;p++) for(q=0;q<10;q++) { if(a[p][q]==3) { yes=0; break; } } if(yes==1) sanke.new1(); sanke.round(); for(int b=0;;) { if(1) { for(int t=0;t<10;t++) cout<<"---"; for(int i=0;i<10;i++) { cout<<"|"; cout<<endl; for(int j=0;j<10;j++) printf("%3c",a[i][j]); } cout<<endl; for( t=0;t<10;t++) cout<<"---"; cout<<" 你的得分是:"<<sanke.sorce<<endl; break; } } for(int t=0;t<12;t++) cout<<endl; // sanke.run(); //这里的递归不要了 return 0; } int main() { int b; cout<<"用w s a d和回车来控制上下左右,按住任意方向键即可快速移动"<<endl; cout<<"\t空格键开始游戏"<<endl; a[0][2]=42;a[0][3]=42;a[0][4]=42; for(int t=0;t<10;t++) cout<<"---"; for(int i=0;i<10;i++) { cout<<"|"; cout<<endl; for(int j=0;j<10;j++) printf("%3c",a[i][j]); } cout<<endl; for(t=0;t<10;t++) cout<<"---"; cout<<" 你的得分是:"<<sanke.sorce<<way<<endl; sum=0; while((b = getch()) != ' '); // 防止自动开始 while(1)// {// while(!kbhit()) // 没有按键按下 { sanke.run();// if(kbhit())// ;// else// Sleep(500);// } // way = getch(); // } // cout<<endl; return 0; }
#include <iostream> #include <time.h> #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <process.h> /* _beginthread, _endthread */ #include <windows.h> /* Sleep() */ using namespace std; int a[100][100]={0}; int yes; int p,q,sum; class Snake { public: Snake() { x[0]=0;y[0]=4;x[1]=0;y[1]=3;x[2]=0;y[2]=2; line=3; sorce=0; } int new1() { if(a[x[line-1]-1][y[line-1]]==0) { x[line]=x[line-1]-1; y[line]=y[line-1]; } if(a[x[line-1]+1][y[line-1]]==0) { x[line]=x[line-1]+1; y[line]=y[line-1]; } if(a[x[line-1]][y[line-1]-1]==0) { x[line]=x[line-1]; y[line]=y[line-1]-1; } if(a[x[line-1]][y[line-1]+1]==0) { x[line]=x[line-1]; y[line]=y[line-1]+1; } if(sum!=0) { line+=1; sorce+=10; } return 0; } int run(); int round(); int x[200]; int y[200]; int line; int sorce; }; class food { public: int born() { if(1) { for(;;) { x=rand()%10; y=rand()%10; if(a[x][y]!=42) break; } a[x][y]=3; } return 0; } int x; int y; }; Snake sanke; food fod; char way = 'd';// 刚开始的方向是向前 int again() { for(int i=0;i<100;i++) for(int j=0;j<100;j++) if(i!=fod.x||j!=fod.y) a[i][j]=0; return 0; } int Snake::round() { //char way; Snake(); for(int i=sanke.line-1;i>0;i--) { sanke.x[i]=sanke.x[i-1]; sanke.y[i]=sanke.y[i-1]; } // way = getch(); //不在这里输入 system("cls"); if(way=='w'||way=='a'||way=='s'||way=='d') sum+=1; switch(way) { case 'D': case 'd': { if((sanke.y[0])==9||a[sanke.x[0]][sanke.y[0]+1]==42) { cout<<"-_-!你撞死了"<<endl; cin.get(); exit(0); } sanke.y[0]=sanke.y[0]+1; again(); if(yes==1) fod.born(); for(int t=0;t<=line-1;t++) a[sanke.x[t]][sanke.y[t]]=42; };break; case 'S': case 's': { if(sanke.x[0]==9||a[sanke.x[0]+1][sanke.y[0]]==42) { cout<<"-_-!你撞死了"<<endl; cin.get(); exit(0); } sanke.x[0]+=1; again(); if(yes==1) fod.born(); for(int t=0;t<=line-1;t++) a[sanke.x[t]][sanke.y[t]]=42; };break; case 'A': case 'a': { if(sanke.y[0]==0||a[sanke.x[0]][sanke.y[0]-1]==42) { cout<<"-_-!你撞死了"<<endl; cin.get(); exit(0); } sanke.y[0]-=1; again(); if(yes==1) fod.born(); for(int t=0;t<=line-1;t++) a[sanke.x[t]][sanke.y[t]]=42; };break; case 'W': case 'w': { if(sanke.x[0]==0||a[sanke.x[0]-1][sanke.y[0]]==42) { cout<<"-_-!你撞死了"<<endl; cin.get(); exit(0); } sanke.x[0]-=1; again(); if(yes==1) fod.born(); for(int t=0;t<=line-1;t++) a[sanke.x[t]][sanke.y[t]]=42; };break; } return 0; } int Snake::run() { yes=1; for(p=0;p<10;p++) for(q=0;q<10;q++) { if(a[p][q]==3) { yes=0; break; } } if(yes==1) sanke.new1(); sanke.round(); for(int b=0;;) { if(1) { for(int t=0;t<10;t++) cout<<"---"; for(int i=0;i<10;i++) { cout<<"|"; cout<<endl; for(int j=0;j<10;j++) printf("%3c",a[i][j]); } cout<<endl; for( t=0;t<10;t++) cout<<"---"; cout<<" 你的得分是:"<<sanke.sorce<<endl; break; } } for(int t=0;t<12;t++) cout<<endl; // sanke.run(); //这里的递归不要了 return 0; } void ThreadFunc1(PVOID param) { while(1) { Sleep(500); sanke.run(); } } void ThreadFunc2(PVOID param) { char i; while(1) { while(!kbhit()); i = getch(); if(i=='d' || i=='D' ||i=='w' ||i=='W' ||i=='s' ||i=='S' || i=='A'|| i=='a') way = i; } } int main() { int b; cout<<"用w s a d和回车来控制上下左右,按住任意方向键即可快速移动"<<endl; cout<<"\t空格键开始游戏"<<endl; a[0][2]=42;a[0][3]=42;a[0][4]=42; for(int t=0;t<10;t++) cout<<"---"; for(int i=0;i<10;i++) { cout<<"|"; cout<<endl; for(int j=0;j<10;j++) printf("%3c",a[i][j]); } cout<<endl; for(t=0;t<10;t++) cout<<"---"; cout<<" 你的得分是:"<<sanke.sorce<<way<<endl; sum=0; while((b = getch()) != ' '); // 防止自动开始 _beginthread(ThreadFunc1,0,NULL); _beginthread(ThreadFunc2,0,NULL); while (1); return 0; }