1、1 程序总体结构图2贪吃蛇程序系统的构图2界面设计如图,通过W、S、A、D、space键进行操作3输入方式设计A 用W、S、A、D、键控制蛇的上下左右移动,space键停止,esc键结束。B 游戏目标:操纵屏幕上那条可爱的小蛇,在方框中不停吃豆豆,而每吃一个豆豆,蛇的身体都变的更长一些,即增加一颗星星。游戏的难度也会随之加大。C 游戏终止:终止条件1蛇头撞到墙上;终止条件2蛇身形成一个圈,也就是蛇头碰到自身的任何一处4重要数据的数据结构设计5函数设计6函数调用图7. 实体属性图四、 关键技术、难点及其解决方法五、 测试方法六、 总结附录1程序源代码#include stdlib.htime.h
2、stdio.hstring.hconio.h#define N 21int apple3;char score3;char tail3;void gotoxy(int x, int y) /输出坐标 COORD pos; pos.X = x; pos.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);void color(int b) /颜色函数 HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE) ; SetConsoleTextAttribute(hCon
3、sole,b) ; int Block(char head2) /判断出界 if (head0 N) | (head1 N) return 1; return 0;int Eat(char snake2) /吃了苹果 if (snake0 = apple0) & (snake1 = apple1) apple0 = apple1 = apple2 = 0; gotoxy(N+44,10); color(13); printf(%d,score0*10); color(11); void Draw(char *snake, int len) /蛇移动 if (apple2) gotoxy(app
4、le1 * 2, apple0); color(12);); gotoxy(tail1 * 2, tail0); if (tail2) color(14); else gotoxy(snake01 * 2, snake00); color(14); putchar(nchar* Move(char *snake, char dirx, int *len) /控制方向 int i, full = Eat(snake0); memcpy(tail, snake(*len)-1, 2); for (i = (*len) - 1; i 0; -i) memcpy(snakei, snakei-1, 2
5、); switch (dirx) case w:W -snake00; break;sS +snake00;aA -snake01;dD +snake01; default: ; if (full) snake = (char *)realloc(snake, sizeof(char *) * (*len) + 1); snake(*len) = (char *)malloc(sizeof(char) * 2); memcpy(snake(*len), tail, 2); +(*len); +score0; if(score3 16) +score3; tail2 = 1; tail2 = 0
6、; return snake;void init(char plateN+2N+2, char *snake_x, int *len) /初始化 int i, j; char *snake = NULL; *len = 3; score0 = score3 =3; snake = (char *)realloc(snake, sizeof(char *) * (*len); for (i = 0; i *len; +i) snakei = (char *)malloc(sizeof(char) * 2); 3; +i) snakei0 = N/2 + 1; snakei1 = N/2 + 1
7、+ i; for (i = 1;= N; for (j = 1; j score0) gotoxy(10,10); puts(闯关失败 加油耶 gotoxy(0,N+2);w+ if(fputc(-score0,fp)=EOF)输出失败n gotoxy(10,10);恭喜您打破记录void Free(char *snake, int len) /释放空间 int i; len; free(snakei); free(snake);int main(void) int len; char ch = g; char aN+2N+2 = 0; char *snake; srand(unsigned)
8、time(NULL); File_in(); init(a, &snake, &len); Manual(); while (ch != 0x1B) / 按 ESC 结束 Draw(snake, len); if (!apple2) Sleep(200-score3*10); setbuf(stdin, NULL); if (kbhit() gotoxy(0, N+2); ch = getche(); snake = Move(snake, ch, & if (Block(snake0)=1) gotoxy(N+2, N+2);你输了 File_out(); Free(snake, len); getche();
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1