2个回答
展开全部
贪吃蛇游戏代码
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<conio.h>
#include<time.h>
#include<windows.h>
#define ML 100
using namespace std;
struct snake
{
int head,tail,body[200],length;
};
snake T;
int map[100][100];
char maze[100][100];
void init()
{
T.head=T.tail=0;
// T.length=0;
memset(T.body,0,sizeof(T.body));
}
int card[400],T_T;
char order;
int getnum()
{
static int n=0;
n++;
n%=400+1;
return card[n];
}
void www()
{
int sum,i;
sum=T.body[(T.head-1+ML)%ML]-100;
if(map[sum/100][sum%100]==1)
{
T.length++;
T.body[T.head++]=sum;
T.head%=ML;
map[sum/100][sum%100]=0;
for(i=T.tail;i!=T.head;)
{
sum=T.body[i];
map[sum/100][sum%100]=-1;
i++;
i%=ML;
}
while(1)
{
sum=getnum();
if(map[sum/100][sum%100]==0)
{
map[sum/100][sum%100]=1;
break;
}
}
for(i=T.tail;i!=T.head;)
{
sum=T.body[i];
map[sum/100][sum%100]=0;
i++;
i%=ML;
}
}
else
{
T.body[T.head++]=sum;
T.head%=ML;
T.tail=(++T.tail)%ML;
}
}
void sss()
{
int sum,i;
sum=T.body[(T.head-1+ML)%ML]+100;
if(map[sum/100][sum%100]==1)
{
T.length++;
T.body[T.head++]=sum;
T.head%=ML;
map[sum/100][sum%100]=0;
for(i=T.tail;i!=T.head;)
{
sum=T.body[i];
map[sum/100][sum%100]=-1;
i++;
i%=ML;
}
while(1)
{
sum=getnum();
if(map[sum/100][sum%100]==0)
{
map[sum/100][sum%100]=1;
break;
}
}
for(i=T.tail;i!=T.head;)
{
sum=T.body[i];
map[sum/100][sum%100]=0;
i++;
i%=ML;
}
}
else
{
T.body[T.head++]=sum;
T.head%=ML;
T.tail=(++T.tail)%ML;
}
}
void ddd()
{
int sum,i;
sum=T.body[(T.head-1+ML)%ML]+1;
if(map[sum/100][sum%100]==1)
{
T.length++;
T.body[T.head++]=sum;
T.head%=ML;
map[sum/100][sum%100]=0;
for(i=T.tail;i!=T.head;)
{
sum=T.body[i];
map[sum/100][sum%100]=-1;
i++;
i%=ML;
}
while(1)
{
sum=getnum();
if(map[sum/100][sum%100]==0)
{
map[sum/100][sum%100]=1;
break;
}
}
for(i=T.tail;i!=T.head;)
{
sum=T.body[i];
map[sum/100][sum%100]=0;
i++;
i%=ML;
}
}
else
{
T.body[T.head++]=sum;
T.head%=ML;
T.tail=(++T.tail)%ML;
}
}
void aaa()
{
int sum,i;
sum=T.body[(T.head-1+ML)%ML]-1;
if(map[sum/100][sum%100]==1)
{
T.length++;
T.body[T.head++]=sum;
T.head%=ML;
map[sum/100][sum%100]=0;
for(i=T.tail;i!=T.head;)
{
sum=T.body[i];
map[sum/100][sum%100]=-1;
i++;
i%=ML;
}
while(1)
{
sum=getnum();
if(map[sum/100][sum%100]==0)
{
map[sum/100][sum%100]=1;
break;
}
}
for(i=T.tail;i!=T.head;)
{
sum=T.body[i];
map[sum/100][sum%100]=0;
i++;
i%=ML;
}
}
else
{
T.body[T.head++]=sum;
T.head%=ML;
T.tail=(++T.tail)%ML;
}
}
int main()
{
memset(map,0,sizeof(map));
memset(maze,' ',sizeof(maze));
int i,j,sum=0,k=0,temp;
for(i=1;i<20;i++)
for(j=1;j<20;j++)
{
card[k++]=i*100+j;
}
srand(time(0));
temp=rand()%10+1;
while(temp--)
random_shuffle(card,card+19*19);
for(i=0;i<=20;i++)
map[i][0]=map[20][i]=map[i][20]=map[0][i]=-1;
init();
T.length=1;
/*while(sum/100>19||sum%100>19||map[sum/100][sum%100]==-1)
{
srand(time(0));
sum=rand()%10000+101;
}*/
T.body[T.head++]=101;
//printf("%d\n",sum);
T.head%=ML;
sum=getnum();
map[sum/100][sum%100]=1;
T_T=0;
char jud='s';
int TM=300,start;
while(1)
{
TM=300-T.length*15;
start=clock();
while(clock()-start<=TM&&!kbhit())
{
;
}
if(kbhit()&&(order=getch(),order=='w'||order=='s'||order=='a'||order=='d'))
{
sum=T.body[(T.head-1+ML)%ML];
system("CLS");
for(i=T.tail;i!=T.head;)
{
temp=T.body[i];
map[temp/100][temp%100]=-1;
i++;
i%=ML;
}
if(order=='w')
{
jud=order;
//printf("-->w\n");
if(map[sum/100-1][sum%100]!=-1)
{
for(i=T.tail;i!=T.head;)
{
temp=T.body[i];
map[temp/100][temp%100]=0;
i++;
i%=ML;
}
www();
}
else
{
system("CLS");
printf("哈哈,你输了!最后得分:%d\n",T.length*10-10);
break;
}
}
else if(order=='a')
{
jud=order;
//printf("-->a\n");
if(map[sum/100][sum%100-1]!=-1)
{
for(i=T.tail;i!=T.head;)
{
temp=T.body[i];
map[temp/100][temp%100]=0;
i++;
i%=ML;
}
aaa();
}
else
{
system("CLS");
printf("哈哈,你输了!最后得分:%d\n",T.length*10-10);
break;
}
}
else if (order=='s')
{
jud=order;
//printf("-->s\n");
if(map[sum/100+1][sum%100]!=-1)
{
for(i=T.tail;i!=T.head;)
{
temp=T.body[i];
map[temp/100][temp%100]=0;
i++;
i%=ML;
}
sss();
}
else
{
system("CLS");
printf("哈哈,你输了!最后得分:%d\n",T.length*10-10);
break;
}
}
else if(order=='d')
{
jud=order;
//printf("-->d\n");
if(map[sum/100][sum%100+1]!=-1)
{
for(i=T.tail;i!=T.head;)
{
temp=T.body[i];
map[temp/100][temp%100]=0;
i++;
i%=ML;
}
ddd();
}
else
{
system("CLS");
printf("哈哈,你输了!最后得分:%d\n",T.length*10-10);
break;
}
}
memset(maze,' ',sizeof(maze));
for(i=0;i<=20;i++)
for(j=0;j<=20;j++)
{
if(map[i][j]==-1)
maze[i][j]='*';
else if(map[i][j]==1)
maze[i][j]='.';
}
for(i=T.tail;i!=T.head;)
{
sum=T.body[i];
maze[sum/100][sum%100]='#';
++i;
i%=ML;
}
sum=T.body[(i-1+ML)%ML];
maze[sum/100][sum%100]='@';
printf("score: %d\n",T.length*10-10);
for(i=0;i<=20;i++)
{
for(j=0;j<=20;j++)
{
printf("%c",maze[i][j]);
}
printf("\n");
}
}
else
{
sum=T.body[(T.head-1+ML)%ML];
system("CLS");
for(i=T.tail;i!=T.head;)
{
temp=T.body[i];
map[temp/100][temp%100]=-1;
i++;
i%=ML;
}
if(jud=='w')
{
//printf("-->w\n");
if(map[sum/100-1][sum%100]!=-1)
{
for(i=T.tail;i!=T.head;)
{
temp=T.body[i];
map[temp/100][temp%100]=0;
i++;
i%=ML;
}
www();
}
else
{
system("CLS");
printf("哈哈,你输了!最后得分:%d\n",T.length*10-10);
break;
}
}
else if(jud=='a')
{
//printf("-->a\n");
if(map[sum/100][sum%100-1]!=-1)
{
for(i=T.tail;i!=T.head;)
{
temp=T.body[i];
map[temp/100][temp%100]=0;
i++;
i%=ML;
}
aaa();
}
else
{
system("CLS");
printf("哈哈,你输了!最后得分:%d\n",T.length*10-10);
break;
}
}
else if (jud=='s')
{
//printf("-->s\n");
if(map[sum/100+1][sum%100]!=-1)
{
for(i=T.tail;i!=T.head;)
{
temp=T.body[i];
map[temp/100][temp%100]=0;
i++;
i%=ML;
}
sss();
}
else
{
system("CLS");
printf("哈哈,你输了!最后得分:%d\n",T.length*10-10);
break;
}
}
else if(jud=='d')
{
//printf("-->d\n");
if(map[sum/100][sum%100+1]!=-1)
{
for(i=T.tail;i!=T.head;)
{
temp=T.body[i];
map[temp/100][temp%100]=0;
i++;
i%=ML;
}
ddd();
}
else
{
system("CLS");
printf("哈哈,你输了!最后得分:%d\n",T.length*10-10);
break;
}
}
memset(maze,' ',sizeof(maze));
for(i=0;i<=20;i++)
for(j=0;j<=20;j++)
{
if(map[i][j]==-1)
maze[i][j]='*';
else if(map[i][j]==1)
maze[i][j]='.';
}
for(i=T.tail;i!=T.head;)
{
sum=T.body[i];
maze[sum/100][sum%100]='#';
++i;
i%=ML;
}
sum=T.body[(i-1+ML)%ML];
maze[sum/100][sum%100]='@';
printf("score: %d\n",T.length*10-10);
for(i=0;i<=20;i++)
{
for(j=0;j<=20;j++)
{
printf("%c",maze[i][j]);
}
printf("\n");
}
}
}
system("pause");
return 0;
}
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<conio.h>
#include<time.h>
#include<windows.h>
#define ML 100
using namespace std;
struct snake
{
int head,tail,body[200],length;
};
snake T;
int map[100][100];
char maze[100][100];
void init()
{
T.head=T.tail=0;
// T.length=0;
memset(T.body,0,sizeof(T.body));
}
int card[400],T_T;
char order;
int getnum()
{
static int n=0;
n++;
n%=400+1;
return card[n];
}
void www()
{
int sum,i;
sum=T.body[(T.head-1+ML)%ML]-100;
if(map[sum/100][sum%100]==1)
{
T.length++;
T.body[T.head++]=sum;
T.head%=ML;
map[sum/100][sum%100]=0;
for(i=T.tail;i!=T.head;)
{
sum=T.body[i];
map[sum/100][sum%100]=-1;
i++;
i%=ML;
}
while(1)
{
sum=getnum();
if(map[sum/100][sum%100]==0)
{
map[sum/100][sum%100]=1;
break;
}
}
for(i=T.tail;i!=T.head;)
{
sum=T.body[i];
map[sum/100][sum%100]=0;
i++;
i%=ML;
}
}
else
{
T.body[T.head++]=sum;
T.head%=ML;
T.tail=(++T.tail)%ML;
}
}
void sss()
{
int sum,i;
sum=T.body[(T.head-1+ML)%ML]+100;
if(map[sum/100][sum%100]==1)
{
T.length++;
T.body[T.head++]=sum;
T.head%=ML;
map[sum/100][sum%100]=0;
for(i=T.tail;i!=T.head;)
{
sum=T.body[i];
map[sum/100][sum%100]=-1;
i++;
i%=ML;
}
while(1)
{
sum=getnum();
if(map[sum/100][sum%100]==0)
{
map[sum/100][sum%100]=1;
break;
}
}
for(i=T.tail;i!=T.head;)
{
sum=T.body[i];
map[sum/100][sum%100]=0;
i++;
i%=ML;
}
}
else
{
T.body[T.head++]=sum;
T.head%=ML;
T.tail=(++T.tail)%ML;
}
}
void ddd()
{
int sum,i;
sum=T.body[(T.head-1+ML)%ML]+1;
if(map[sum/100][sum%100]==1)
{
T.length++;
T.body[T.head++]=sum;
T.head%=ML;
map[sum/100][sum%100]=0;
for(i=T.tail;i!=T.head;)
{
sum=T.body[i];
map[sum/100][sum%100]=-1;
i++;
i%=ML;
}
while(1)
{
sum=getnum();
if(map[sum/100][sum%100]==0)
{
map[sum/100][sum%100]=1;
break;
}
}
for(i=T.tail;i!=T.head;)
{
sum=T.body[i];
map[sum/100][sum%100]=0;
i++;
i%=ML;
}
}
else
{
T.body[T.head++]=sum;
T.head%=ML;
T.tail=(++T.tail)%ML;
}
}
void aaa()
{
int sum,i;
sum=T.body[(T.head-1+ML)%ML]-1;
if(map[sum/100][sum%100]==1)
{
T.length++;
T.body[T.head++]=sum;
T.head%=ML;
map[sum/100][sum%100]=0;
for(i=T.tail;i!=T.head;)
{
sum=T.body[i];
map[sum/100][sum%100]=-1;
i++;
i%=ML;
}
while(1)
{
sum=getnum();
if(map[sum/100][sum%100]==0)
{
map[sum/100][sum%100]=1;
break;
}
}
for(i=T.tail;i!=T.head;)
{
sum=T.body[i];
map[sum/100][sum%100]=0;
i++;
i%=ML;
}
}
else
{
T.body[T.head++]=sum;
T.head%=ML;
T.tail=(++T.tail)%ML;
}
}
int main()
{
memset(map,0,sizeof(map));
memset(maze,' ',sizeof(maze));
int i,j,sum=0,k=0,temp;
for(i=1;i<20;i++)
for(j=1;j<20;j++)
{
card[k++]=i*100+j;
}
srand(time(0));
temp=rand()%10+1;
while(temp--)
random_shuffle(card,card+19*19);
for(i=0;i<=20;i++)
map[i][0]=map[20][i]=map[i][20]=map[0][i]=-1;
init();
T.length=1;
/*while(sum/100>19||sum%100>19||map[sum/100][sum%100]==-1)
{
srand(time(0));
sum=rand()%10000+101;
}*/
T.body[T.head++]=101;
//printf("%d\n",sum);
T.head%=ML;
sum=getnum();
map[sum/100][sum%100]=1;
T_T=0;
char jud='s';
int TM=300,start;
while(1)
{
TM=300-T.length*15;
start=clock();
while(clock()-start<=TM&&!kbhit())
{
;
}
if(kbhit()&&(order=getch(),order=='w'||order=='s'||order=='a'||order=='d'))
{
sum=T.body[(T.head-1+ML)%ML];
system("CLS");
for(i=T.tail;i!=T.head;)
{
temp=T.body[i];
map[temp/100][temp%100]=-1;
i++;
i%=ML;
}
if(order=='w')
{
jud=order;
//printf("-->w\n");
if(map[sum/100-1][sum%100]!=-1)
{
for(i=T.tail;i!=T.head;)
{
temp=T.body[i];
map[temp/100][temp%100]=0;
i++;
i%=ML;
}
www();
}
else
{
system("CLS");
printf("哈哈,你输了!最后得分:%d\n",T.length*10-10);
break;
}
}
else if(order=='a')
{
jud=order;
//printf("-->a\n");
if(map[sum/100][sum%100-1]!=-1)
{
for(i=T.tail;i!=T.head;)
{
temp=T.body[i];
map[temp/100][temp%100]=0;
i++;
i%=ML;
}
aaa();
}
else
{
system("CLS");
printf("哈哈,你输了!最后得分:%d\n",T.length*10-10);
break;
}
}
else if (order=='s')
{
jud=order;
//printf("-->s\n");
if(map[sum/100+1][sum%100]!=-1)
{
for(i=T.tail;i!=T.head;)
{
temp=T.body[i];
map[temp/100][temp%100]=0;
i++;
i%=ML;
}
sss();
}
else
{
system("CLS");
printf("哈哈,你输了!最后得分:%d\n",T.length*10-10);
break;
}
}
else if(order=='d')
{
jud=order;
//printf("-->d\n");
if(map[sum/100][sum%100+1]!=-1)
{
for(i=T.tail;i!=T.head;)
{
temp=T.body[i];
map[temp/100][temp%100]=0;
i++;
i%=ML;
}
ddd();
}
else
{
system("CLS");
printf("哈哈,你输了!最后得分:%d\n",T.length*10-10);
break;
}
}
memset(maze,' ',sizeof(maze));
for(i=0;i<=20;i++)
for(j=0;j<=20;j++)
{
if(map[i][j]==-1)
maze[i][j]='*';
else if(map[i][j]==1)
maze[i][j]='.';
}
for(i=T.tail;i!=T.head;)
{
sum=T.body[i];
maze[sum/100][sum%100]='#';
++i;
i%=ML;
}
sum=T.body[(i-1+ML)%ML];
maze[sum/100][sum%100]='@';
printf("score: %d\n",T.length*10-10);
for(i=0;i<=20;i++)
{
for(j=0;j<=20;j++)
{
printf("%c",maze[i][j]);
}
printf("\n");
}
}
else
{
sum=T.body[(T.head-1+ML)%ML];
system("CLS");
for(i=T.tail;i!=T.head;)
{
temp=T.body[i];
map[temp/100][temp%100]=-1;
i++;
i%=ML;
}
if(jud=='w')
{
//printf("-->w\n");
if(map[sum/100-1][sum%100]!=-1)
{
for(i=T.tail;i!=T.head;)
{
temp=T.body[i];
map[temp/100][temp%100]=0;
i++;
i%=ML;
}
www();
}
else
{
system("CLS");
printf("哈哈,你输了!最后得分:%d\n",T.length*10-10);
break;
}
}
else if(jud=='a')
{
//printf("-->a\n");
if(map[sum/100][sum%100-1]!=-1)
{
for(i=T.tail;i!=T.head;)
{
temp=T.body[i];
map[temp/100][temp%100]=0;
i++;
i%=ML;
}
aaa();
}
else
{
system("CLS");
printf("哈哈,你输了!最后得分:%d\n",T.length*10-10);
break;
}
}
else if (jud=='s')
{
//printf("-->s\n");
if(map[sum/100+1][sum%100]!=-1)
{
for(i=T.tail;i!=T.head;)
{
temp=T.body[i];
map[temp/100][temp%100]=0;
i++;
i%=ML;
}
sss();
}
else
{
system("CLS");
printf("哈哈,你输了!最后得分:%d\n",T.length*10-10);
break;
}
}
else if(jud=='d')
{
//printf("-->d\n");
if(map[sum/100][sum%100+1]!=-1)
{
for(i=T.tail;i!=T.head;)
{
temp=T.body[i];
map[temp/100][temp%100]=0;
i++;
i%=ML;
}
ddd();
}
else
{
system("CLS");
printf("哈哈,你输了!最后得分:%d\n",T.length*10-10);
break;
}
}
memset(maze,' ',sizeof(maze));
for(i=0;i<=20;i++)
for(j=0;j<=20;j++)
{
if(map[i][j]==-1)
maze[i][j]='*';
else if(map[i][j]==1)
maze[i][j]='.';
}
for(i=T.tail;i!=T.head;)
{
sum=T.body[i];
maze[sum/100][sum%100]='#';
++i;
i%=ML;
}
sum=T.body[(i-1+ML)%ML];
maze[sum/100][sum%100]='@';
printf("score: %d\n",T.length*10-10);
for(i=0;i<=20;i++)
{
for(j=0;j<=20;j++)
{
printf("%c",maze[i][j]);
}
printf("\n");
}
}
}
system("pause");
return 0;
}
追问
哦!然来可以这样。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
给邮箱啊
更多追问追答
追问
忘了。1638773545@qq.com
追答
已发
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询