C语言贪食蛇
2016-04-21 21:23
393 查看
C语言 贪吃蛇实现
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <conio.h> #define SNAKE_MAX_LENGTH 20 #define SNAKE_HEAD 'H' #define SNAKE_BODY 'X' #define SNAKE_FOOD '$' #define BLANK_CELL ' ' #define WALL_CELL '*' void snakeMove(int a, int b); void put_money(); void input(); void output(); void gameover(); void collision(); void creatfood(); char map[12][13]= {"************", "*XXXXH *", "* *", "* *", "* *", "* *", "* *", "* *", "* *", "* *", "* *", "************"}; int snakeX[SNAKE_MAX_LENGTH] = {1, 1, 1, 1, 1}; int snakeY[SNAKE_MAX_LENGTH] = {1, 2, 3, 4, 5}; int snakeLength = 4; int snakeLife = 1; int food = 0; int fx, fy; int snaketailX, snaketailY; //Use in Snake's growth char ch; int main() { while (snakeLife) { //WHILE snake live DO _sleep(300); //each 0.3second refresh the map system("cls"); if (food == 0) creatfood(); //IT no food THEN creat food input(); output(); collision(); } //END WHILE getch(); return 0; } void input() { if (kbhit() != 0) { //IF no keyboard input THEN return the latest input while(kbhit() != 0) //INPUT the last keyboard input ch = getch(); } switch (ch) { //CASE ch DO case 'a': //'a': turn left snakeMove(0, -1); break; case 'd': //'d': turn right snakeMove(0, 1); break; case 'w': //'w': up move snakeMove(-1, 0); break; case 's': //'s': down move snakeMove(1, 0); break; default: break; } //END CASE } void output() { int i, j; printf("C little game\n"); printf("created by MR.GU\n"); for (i = 1; i < 11; i++) { for(j = 1; j < 11; j++) { map[i][j] = ' '; } } map[fx][fy] = '$'; map[snakeX[snakeLength]][snakeY[snakeLength]] = 'H'; for (i = 0; i < snakeLength; i++) { map[snakeX[i]][snakeY[i]] = 'X'; } for (i = 0; i < 12 ;i++) { for (j = 0; j < 12; j++) { printf("%c", map[i][j]); } printf("\n"); } printf("Snake's Length: %d\n", snakeLength + 1); } void snakeMove(int a, int b) { int i; snaketailX = snakeX[0]; snaketailY = snakeY[0]; for (i = 0; i < snakeLength; i++) { snakeX[i] = snakeX[i + 1]; snakeY[i] = snakeY[i + 1]; } snakeX[snakeLength] += a; snakeY[snakeLength] += b; } void creatfood() { fx = rand() % 10 + 1; fy = rand() % 10 + 1; if(map[fx][fy] != 'H' && map[fx][fy]!= 'X') { food++; } else { creatfood(); } } void collision() { int i; if (snakeX[snakeLength] == 11 || snakeX[snakeLength] == 0) { //Snake | Head on WALL snakeLife = 0; //Snake die printf("Gameover!\n"); //OUTPUT "Gameover" } if (snakeY[snakeLength] == 11 || snakeY[snakeLength] == 0) { //Snake | Head on WALL snakeLife = 0; //Snake die printf("Gameover!\n"); //OUTPUT "Gameover" } if (snakeX[snakeLength] == fx && snakeY[snakeLength] == fy) { //Snake | Head on WALL food--; snakeLength++; //Food disappear and Snake grow for (i = snakeLength; i > 0; i--) { snakeX[i] = snakeX[i - 1]; snakeY[i] = snakeY[i - 1]; } snakeX[0] = snaketailX; //grow from the tail snakeY[0] = snaketailY; } for (i = 0; i < snakeLength; i++) { //Snake | Head on BODY if (snakeX[i] == snakeX[snakeLength] && snakeY[i] == snakeY[snakeLength]) { snakeLife = 0; printf("hit your body!\n"); //Snake die printf("Gameover!\n"); //OUTPUT "hit your body!","Gameover" } } }
相关文章推荐
- 2016年4月21日 21:18:25 我的第一篇博客~
- effective C++ 读书笔记 条款08
- c和c++栈
- c++map基本操作
- 如何使用VC++6.0发布程序(即release版本程序)
- 程序设计篇(1):学生经验值管理系统(单链表实现)
- 山东省第五届ACM大学生程序设计竞赛-Weighted Median(模拟)
- 循环队列的综合操作(出入队、获取队长度)C语言实现
- char* p与char p[]
- c++实现广义表
- 推荐!国外程序员整理的 C++ 资源大全
- 学习资源(均为转载内容)
- C++面向对象模型
- C++:探究纯虚析构函数以及实现数组的快速排序与链表的归并排序
- C、C++中的关键字总结
- C++和C语言函数相互调用
- C++ primer 5th 习题之10.13
- C++基础知识
- 关于C语言的问卷调查
- [转载]备忘:VC++6.0 获取其他窗口中文本框内容