网上看到的一个贪吃蛇程序,修复了一些bug(如可以直接转反方向,苹果与蛇重叠,撞到自身不会死亡等),下面是源代码。
--------------------------------------------------------------------------------------------------
1 #include<iostream> 2 #include<windows.h> 3 #include<time.h> 4 #include<stdlib.h> 5 #include<conio.h> 6 #define N 21 7 using namespace std; 8 //移动光标 9 void gotoxy(int x,int y) 10 { 11 COORD pos; 12 pos.X=2*x; 13 pos.Y=y; 14 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos); 15 } 16 //调整颜色 17 void color(int a) 18 { 19 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a); 20 } 21 //初始化函数 22 void init(int apple[2]) 23 { 24 int i,j; 25 int wall[N+2][N+2]={{0}}; 26 for(i=1;i<=N;i++) 27 { 28 for(j=1;j<=N;j++) 29 wall[i][j]=1; 30 } 31 color(11); 32 for(i=0;i<N+2;i++) 33 { 34 for(j=0;j<N+2;j++) 35 { 36 if(wall[i][j]) 37 cout<<"■"; 38 else cout<<"□" ; 39 } 40 cout<<endl; 41 } 42 gotoxy(N+3,1); 43 color(20); 44 cout<<"按 W S A D 移动方向"<<endl; 45 gotoxy(N+3,2); 46 color(20); 47 cout<<"按任意键暂停"<<endl; 48 gotoxy(N+3,3); 49 color(20); 50 cout<<"得分:"<<endl; 51 apple[0]=rand()%(N-2)+2; 52 apple[1]=rand()%(N-2)+2; 53 gotoxy(apple[0],apple[1]); 54 color(12); 55 cout<<"●"<<endl; 56 } 57 int main() 58 { 59 int i,j; 60 int** snake=NULL; 61 int apple[2]; 62 int score=0; 63 int tail[2]; 64 int len=3; 65 char ch=‘p‘; 66 srand((unsigned)time(NULL)); 67 init(apple); 68 snake=(int**)realloc(snake,sizeof(int*)*len); 69 for(i=0;i<len;i++) 70 snake[i]=(int*)malloc(sizeof(int)*2); 71 for(i=0;i<len;i++) 72 { 73 snake[i][0]=N/2; 74 snake[i][1]=N/2+i; 75 gotoxy(snake[i][0],snake[i][1]); 76 color(14); 77 cout<<"★"<<endl; 78 } 79 //进入消息循环 80 while(1) 81 { 82 tail[0]=snake[len-1][0]; 83 tail[1]=snake[len-1][1]; 84 gotoxy(tail[0],tail[1]); 85 color(11); 86 cout<<"■"<<endl; 87 for(i=len-1;i>0;i--) 88 { 89 snake[i][0]=snake[i-1][0]; 90 snake[i][1]=snake[i-1][1]; 91 gotoxy(snake[i][0],snake[i][1]); 92 color(14); 93 cout<<"★"<<endl; 94 } 95 char origch=ch; 96 if(kbhit()) 97 { 98 gotoxy(0,N+2); 99 ch=getche(); 100 } 101 if((origch==‘w‘&&ch==‘s‘)||(origch==‘s‘&&ch==‘w‘)||(origch==‘a‘&&ch==‘d‘)||(origch==‘d‘&&ch==‘a‘)) 102 ch=origch; 103 switch(ch) 104 { 105 case ‘w‘:snake[0][1]--;break; 106 case ‘s‘:snake[0][1]++;break; 107 case ‘a‘:snake[0][0]--;break; 108 case ‘d‘:snake[0][0]++;break; 109 default: break; 110 } 111 int i; 112 //撞到自身失败 113 for(i=1;i<len;i++) 114 { 115 if(snake[0][0]==snake[i][0]&&snake[0][1]==snake[i][1]&&len>3) 116 { 117 gotoxy(N/2,N/2); 118 color(30); 119 cout<<"失败!!!"<<endl; 120 for(i=0;i<len;i++) 121 free(snake[i]); 122 Sleep(INFINITE); 123 exit(0); 124 } 125 } 126 gotoxy(snake[0][0],snake[0][1]); 127 color(14); 128 cout<<"★"<<endl; 129 Sleep(abs(200-0.5*score)); 130 //吃掉苹果后蛇分数加1,蛇长加1 131 if(snake[0][0]==apple[0]&&snake[0][1]==apple[1]) 132 { 133 score++; 134 len++; 135 snake=(int**)realloc(snake,sizeof(int*)*len); 136 snake[len-1]=(int*)malloc(sizeof(int)*2); 137 int flag=1; 138 do 139 { 140 flag=1; 141 apple[0]=rand()%(N-2)+2; 142 apple[1]=rand()%(N-2)+2; 143 for(i=0;i<len;i++) 144 { 145 if(apple[0]==snake[i][0]&&apple[1]==snake[i][1]) 146 flag=0; 147 } 148 } 149 while(!flag); 150 gotoxy(apple[0],apple[1]); 151 color(12); 152 cout<<"●"<<endl; 153 gotoxy(N+5,3); 154 color(20); 155 cout<<score<<endl; 156 } 157 //撞到围墙后失败 158 if(snake[0][1]==0||snake[0][1]==N||snake[0][0]==0||snake[0][0]==N) 159 { 160 gotoxy(N/2,N/2); 161 color(30); 162 cout<<"失败!!!"<<endl; 163 for(i=0;i<len;i++) 164 free(snake[i]); 165 Sleep(INFINITE); 166 exit(0); 167 } 168 169 } 170 return 0; 171 }
时间: 2024-11-08 03:13:03