1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
| #include<stdio.h> #include<conio.h> #include <windows.h> struct Foodxy//定义食物坐标结构体 { int x; int y; }foodxy; struct Location//蛇的结构体 { int x[999]; int y[999]; int len=1; }location; int score=0; int stop=1; char i='a';
void gotoxy(short x, short y); void go(); void rail(); void food(); void snake(); int carboon(); void gotoxy(short x, short y) { COORD coord = {x, y}; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); } void go() { snake(); Sleep(300); if(kbhit()) { fflush(stdin); i=getch(); } gotoxy(location.x[0],location.y[0]); printf(" "); switch (i) { case 'w': case 'W': location.y[0]--; break; case 's': case 'S': location.y[0]++; break; case 'a': case 'A': location.x[0]-=2; break; case 'd': case 'D': location.x[0]+=2; break; default: gotoxy(10,12); printf("输入错误,wsad分别控制上下左右"); stop=0; } if(location.x[0]<=2||location.y[0]<=2||location.x[0]>=52||location.y[0]>=25) { gotoxy(10,12); printf("你撞墙了,好好练习一下吧~"); stop=0; } else { if((location.x[0]==foodxy.x)&&(location.y[0]==foodxy.y)) { location.len++; score++; food(); } gotoxy(54,3); printf("成绩:%d ",score); } if(carboon()==1) { gotoxy(10,12); printf("你撞到自己了,好好练习一下吧~"); stop=0; } } void rail() { int i,j,count=25; printf("\t\t<简易版贪吃蛇>By:LZJ\n 说明:w,s,a,d分别代表上下左右,输入其他任意键退出。"); for ( i = 4; i <= count*2; i++) { if(i%2==0) { gotoxy(i,2); printf("#"); gotoxy(i,25); printf("#"); } } for ( j = 2; j <= count; j++) { gotoxy(3,j); printf("#"); gotoxy(52,j); printf("#"); } } void food() { int i; i=rand()%(51-4)+4; if(i%2==0) { foodxy.x = i; foodxy.y = rand()%(25-3)+3; gotoxy(foodxy.x,foodxy.y); printf("●"); } else food(); } void snake() { int i; if(location.len>1) { gotoxy(location.x[location.len],location.y[location.len]); printf(" "); for(i=location.len;i>0;i--) { location.x[i]=location.x[i-1]; location.y[i]=location.y[i-1]; gotoxy(location.x[i],location.y[i]); printf("●"); } } gotoxy(location.x[0],location.y[0]); printf("⊙"); if(score<10) { gotoxy(59,3); } else if(score>=10&&score<100) { gotoxy(60,3); } else if (score>=100&&score<1000) { gotoxy(61,3); } else { gotoxy(62,3); } } int carboon() { int i; for(i=location.len;i>1;i--) { if((location.x[0]==location.x[i])&&(location.y[0]==location.y[i])) { return 1; } } return 0; } int main() { location.x[0] = 20; location.y[0] = 13; rail(); gotoxy(location.x[0],location.y[0]); printf("⊙"); food(); while (stop) { go(); carboon(); } gotoxy(10,13); printf("game over!!! score:%d ",score); return 0; }
|