零基础自学快两个月了,昨天刚刚写小贪食蛇,分享下,以前写的小游戏就不发了! GCC 编译器 312 行
程序代码:
//左上角坐标 1,1 右上角坐标 30,1 左下角坐标 1,22 右下角坐标 30,22 //贪食蛇 gcc版 欢迎交流 QQ 87835836 -- Rol #include <stdio.h> #include <stdlib.h> #include <time.h> #include <conio.h> #include <Windows.h> #define W 40 //控制台宽(40*2) #define H 24 //控制台高 #define LEFT 75 #define UP 72 #define RIGHT 77 #define DOWN 80 #define ESC 27 #define SPACE 32 #define ENTER 13 typedef struct { int x; int y; } COORSNAKE; //蛇坐标 typedef struct SHE { COORSNAKE coorsnake; struct SHE *next; } NODESNAKE; //链表 NODESNAKE *head=NULL, *last=NULL; //蛇头蛇尾指针 COORSNAKE food, new_coor, tmp_coor; //坐标 static COORD food_position; //坐标 int score=0; //分数 int level=1; //难度 void gotoxy(int x, int y, int color); void map(); void huashe(int x,int y,int color); char getkey(); void mksnake(NODESNAKE **plast); void printfood(int x,int y,int color); void mkfood(int *x,int *y); void HideCursor(); int main(void) { HideCursor(); int key,isfood,isself,dx,dy; //key为取键值,isfood标记有没有吃到食物,isself标记有没有碰到自己,dx表示横向的方向,dy表示纵向方向; NODESNAKE *pcheck; //检查指针 system ("title SNAKE -- BY ROL"); system ("cls"); head=NULL; map(); gotoxy(11,12,0x0b); printf("Enter键开始,ESC退出!"); while((key=getkey())!=ESC||key!=ENTER) //开始游戏 { if(key==ESC) return 0; else break; } gotoxy(11,12,0x0b); printf(" "); //清屏 mksnake(&last); suiji: srand((unsigned int)time(NULL)); last->coorsnake.x = rand()%((W-12)/2)+2; //现在的蛇头和蛇尾指针都指向蛇头 保证蛇头在左边 last->coorsnake.y = rand()%(H-2)+1; srand((unsigned int)time(NULL)); food.x=rand()%(W-11)+2; food.y=rand()%(H-2)+1; if(food.x==head->coorsnake.x&&food.y==head->coorsnake.y) goto suiji; printfood(food.x,food.y,0x0f); huashe(last->coorsnake.x,last->coorsnake.y,0x0c); dx=1,dy=0; //默认方向向右 while(1) { isself=isfood=0; gotoxy(35,16,0x0d); printf("%-5d",score); gotoxy(35,12,0x0d); printf("%-3d",level); if (_kbhit()) //当吃到一个食物后才会执行 { fflush(stdin); switch (key=getkey()) { case LEFT: dx=-1; dy= 0;break; case RIGHT: dx= 1; dy= 0;break; case UP: dx= 0; dy=-1;break; case DOWN: dx= 0; dy= 1;break; case SPACE: getch();break; } } new_coor.x=head->coorsnake.x+dx; //新蛇头 new_coor.y=head->coorsnake.y+dy; if (head->next != NULL && head->next->coorsnake.x == new_coor.x && head->next->coorsnake.y == new_coor.y) //如果按按键与原来方向一致,则还原新蛇头 { new_coor.x = head->coorsnake.x - dx; new_coor.y = head->coorsnake.y - dy; } for(pcheck=head;pcheck->next!=NULL;pcheck=pcheck->next) //判断是否撞到自身 if(pcheck->coorsnake.x==new_coor.x&&pcheck->coorsnake.y==new_coor.y) { isself=1;break; } if(isself==1||new_coor.x<1||new_coor.x>30||new_coor.y<1||new_coor.y>22) //撞到自身或者越界,游戏结束! { gotoxy(11,12,0x0b); printf("Game Over! 按任意键退出"); break; } if(new_coor.x==food.x&&new_coor.y==food.y) //吃到食物 { isfood=1; score+=10; mksnake(&last); //增加一节蛇 mkfood(&food.x,&food.y); //创建新食物 printfood(food.x,food.y,0x0f); //打印食物 } if (isfood == 0) //去掉尾巴 { huashe(last->coorsnake.x, last->coorsnake.y, 0x00); } for (pcheck=head; pcheck != NULL; pcheck = pcheck->next) //蠕动的动作 tmp_coor为中间变量 { tmp_coor = pcheck->coorsnake; pcheck->coorsnake = new_coor; new_coor = tmp_coor; } for (pcheck=head; pcheck != NULL; pcheck = pcheck->next) //打印蛇身各节 { if (pcheck == head) { huashe(pcheck->coorsnake.x, pcheck->coorsnake.y, 0x0c); } else { huashe(pcheck->coorsnake.x, pcheck->coorsnake.y, 0x0f); } } switch(score/50) //分数用来控制等级和蛇移动速度 { case 0:level = 1; Sleep(500); break; case 1:level = 2; Sleep(400); break; case 2:level = 3; Sleep(300); break; case 3:level = 4; Sleep(200); break; case 4:level = 5; Sleep(150); break; case 5:level = 6; Sleep(100); break; default:level = 7; Sleep(50); } } getch(); return 0; } void gotoxy(int x,int y,int color) //移动光标 { static HANDLE hOut; static COORD position; //坐标 position.X = x*2; position.Y = y; hOut==NULL?(hOut = GetStdHandle(STD_OUTPUT_HANDLE)):hOut; SetConsoleCursorPosition(hOut, position); SetConsoleTextAttribute(hOut, color); } void map() { int i,j; gotoxy(0,0,0x0b); for(i=0;i<W;i++) { for(j=0;j<H;j++) if(i==0||j==0||i==W-1||j==H-1||i==W-9) huashe(i,j,0x0b); if (i == W - 1 && j == H)break; //不打印最后一个换行 printf("\n"); } gotoxy(34,2,0x0b); printf ("贪吃蛇"); gotoxy(35,3,0x0b); printf ("--by rol"); gotoxy(33,5,0x0b); printf (" 操作方法:"); gotoxy(33,7,0x0b); printf ("← ↑ ↓ →"); gotoxy(33,8,0x0b); printf ("左 上 下 右"); gotoxy(34,10,0x0b); printf ("等 级"); gotoxy(34,14,0x0b); printf ("得 分"); gotoxy(34,17,0x0b); printf ("帮 助"); gotoxy(33,19,0x0b); printf ("空格键 暂停"); gotoxy(33,21,0x0b); printf ("任意键 继续"); } void huashe(int x,int y,int color) //画蛇头或蛇身以及边界 { static HANDLE hOut; static COORD position; //坐标 position.X = x*2; position.Y = y; hOut==NULL?(hOut = GetStdHandle(STD_OUTPUT_HANDLE)):hOut; SetConsoleCursorPosition(hOut, position); SetConsoleTextAttribute(hOut, color); printf("■"); } char getkey() //取键盘按键值 { char ch1,ch2; ch1=getch(); if(ch1==ESC||ch1==SPACE||ch1==ENTER) return ch1; else if(ch1==-32) { ch2=getch(); return ch2; } else return 0; } void mksnake(NODESNAKE **plast) //创建蛇头或蛇身链表 { NODESNAKE *pnew; if ((pnew = (NODESNAKE *)malloc(sizeof(NODESNAKE))) == NULL) { gotoxy(11,12,0x0b); printf("分配内存失败!按任意键退出!"); getch(); exit(0); } if(head==NULL)head=(*plast)=pnew; //第一次创建蛇头 else { (*plast)->next=pnew,(*plast)=pnew; } pnew->next=NULL; } void printfood(int x,int y,int color) //打印食物 { static HANDLE hOut; static COORD position; //坐标 position.X = x*2; position.Y = y; hOut==NULL?(hOut = GetStdHandle(STD_OUTPUT_HANDLE)):hOut; SetConsoleCursorPosition(hOut, position); SetConsoleTextAttribute(hOut, color); printf("⊙"); } void mkfood(int *x,int *y) //创建随机食物坐标 { int issnake, i; //issnake 标记是否在蛇身上 NODESNAKE *pd; srand((unsigned int)time(NULL)); for (i=0; i<W*H; i++) //生成食物供执行40*25次(控制台:80/2 * 25) { issnake = 0; do { *x = 1+rand()%(W-11); *y = 1+rand()%(H-3); food_position.X = *x*2; food_position.Y = *y; } while (food_position.X<2||food_position.Y<2); for (pd=head; pd != NULL; pd = pd->next) if (pd->coorsnake.x == *x && pd->coorsnake.y == *y) //判断是不是属于蛇身 { issnake = 1; break; } if (issnake == 0) return; //如果不是蛇身就返回 } gotoxy(11,12,0x0b); printf("你是大神,已经没有位置可以放食物了!\n 按任意键结束游戏!"); getch(); exit(1); } void HideCursor() //隐藏光标 { CONSOLE_CURSOR_INFO cursor_info = { 1, 0 }; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); }