#include <stdio.h> #include <windows.h> #include <stdlib.h> #include <conio.h> #include <time.h> #define ShowTime 300 #define CheckTime 10 #define ShowRangeX 30//边界范围大小(横向) #define ShowRangeY 20//边界范围大小(纵向) #define ShowPlaceX 21//显示位置(横向) #define ShowPlaceY 6//显示位置(纵向) #define RIGHT 0x4d #define LEFT 0x4b #define UP 0x48 #define DOWN 0x50 #define Left 1 #define Down 2 #define Up 3 #define Right 4 void Pause(); void gotoxy(int x, int y); void DrawBoard();//画边界 void DrawInitalSnake(int x);//画蛇初始位置 void DrawFood();//画食物位置 int move(int direct);//移动 void HideCursor();//隐藏光标 void InitMap(); void Init(); int isfood (int x,int y); int NextY,NextX; int score=0; int NowX=ShowPlaceX+ShowRangeX; int NowY=ShowPlaceY+ShowRangeY/2; int FoodX,FoodY; int Head,Tail; int DirectX[]={0,-1,0,0,1}; int DirectY[]={0,0,1,-1,0}; int MaxLen=ShowRangeX * ShowRangeY; int SnakeBody[ShowRangeX * ShowRangeY][2]; bool Map[2*ShowRangeX][ShowRangeY]; int main(void) { system("color 0C"); int key,time,next,difficult; system("mode con cols=100 lines=30");//设置窗口大小 printf("\n\t***********************************贪吃蛇小游戏************************************"); HideCursor();//隐藏光标 DrawBoard();//画边界 InitMap();//初始化地图 printf("\n\t\t\t\t\t请输入难度级别(小于14): "); scanf("%d",&difficult); DrawInitalSnake(difficult-1);//画初始位置 DrawFood();//画食物位置 next=Right;//初始运动方向 while(1) { time=ShowTime/CheckTime; while(time--)//多次检测键盘按键状态 { if(_kbhit()) { key=getch(); switch(key) { case LEFT :if(next!=Right) next=Left;break; case DOWN :if(next!=Up)next=Down;break; case UP :if(next!=Down)next=Up;break; case RIGHT :if(next!=Left) next=Right;break; } } if(GetAsyncKeyState(VK_SPACE)) Pause(); Sleep(CheckTime); } //system("cls"); //DrawBoard();//重画边界 if (move(next)==0) //移动 { printf("\n\n\n\t\t\t\t\tGame Over !!!\n"); //Sleep(CheckTime); return 0; } Init(); } return 0; } void gotoxy(int x, int y) { COORD coord = {x, y}; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); } void Init() { gotoxy(3,8); printf(" 方向键:"); gotoxy(3,10); printf("上( up ): ↑"); printf("\n\n 下( down): ↓"); gotoxy(3,14); printf("左(left):←"); printf("\n\n 右(right):→"); gotoxy(3,18); printf("暂 停:"); gotoxy(3,20); printf("-SPACE-"); gotoxy(2,22); printf("cccc"); printf("\n\n 软件"); gotoxy(83,8); printf(" 简易贪吃蛇 "); gotoxy(83,10); printf(" 制作人: MR ZHOU "); gotoxy(83,12); printf(" 版 本: Version1 "); gotoxy(83,14); printf(" 开发环境: VC6.0 "); gotoxy(83,17); printf(" 食物坐标:%d %d",FoodX,FoodY); gotoxy(83,19); printf(" 蛇头坐标:%d %d",NextX,NextY); gotoxy(83,21); printf(" 分 数: %d 分",score); } void DrawBoard()//画边界 { int i; gotoxy(ShowPlaceX-2,ShowPlaceY-1); printf("■"); for(i=1;i<=ShowRangeX;i++) { printf("■"); } printf("■"); for (i=0;i<ShowRangeY;i++) { gotoxy(ShowPlaceX-2,ShowPlaceY+i); printf("■"); gotoxy(ShowPlaceX+2*ShowRangeX,ShowPlaceY+i); printf("■"); } gotoxy(ShowPlaceX-2,ShowPlaceY+ShowRangeY); printf("■"); for(i=1;i<=ShowRangeX;i++) { printf("■"); } printf("■"); } void DrawInitalSnake(int x)//画蛇的初始位置 { Head = 0, Tail = 0; int i=1; SnakeBody[Head][0]=ShowPlaceX+ShowRangeX; SnakeBody[Head][1]=ShowPlaceY+ShowRangeY/2; gotoxy(SnakeBody[Head][0],SnakeBody[Head][1]); printf("■"); Map[SnakeBody[Head][0]-ShowPlaceX][SnakeBody[Head][1]-ShowPlaceY]=1;//在地图上做标记 for(;i<=x;i++)//这样以改变,可以设置任意的初始蛇的长度如果长度为9,则输入8 { SnakeBody[Head+i][0]=SnakeBody[Head+i-1][0]+2; SnakeBody[Head+i][1]=SnakeBody[Head+i-1][1]; gotoxy(SnakeBody[Head+i][0],SnakeBody[Head+i][1]); printf("■"); Map[SnakeBody[Head+i-1][0]-ShowPlaceX][SnakeBody[Head+i-1][1]-ShowPlaceY]=1; } Head = i-1; } void DrawFood()//画食物位置 { srand((time_t )time(NULL)); do { FoodX=(rand()%ShowRangeX)*2+ShowPlaceX; FoodY=rand()%(ShowRangeY)+ShowPlaceY; }while(Map[FoodX][FoodY]); gotoxy(FoodX,FoodY); printf("@"); } int move(int next)//移动 { NextX=SnakeBody[Head][0]+2*DirectX[next]; NextY=SnakeBody[Head][1]+DirectY[next]; if(NextX==ShowPlaceX-2||NextX==ShowPlaceX+2*ShowRangeX||NextY==ShowPlaceY-1||NextY==ShowPlaceY+ShowRangeY||Map[NextX-ShowPlaceX][NextY-ShowPlaceY]==1) { return 0; } else { if(!isfood(NextX,NextY)) { //清除尾巴 gotoxy(SnakeBody[Tail][0],SnakeBody[Tail][1]); printf(" "); Map[SnakeBody[Tail][0]-ShowPlaceX][SnakeBody[Tail][1]-ShowPlaceY]=0; Tail=(Tail+1)%MaxLen; } //向前移动 Head=(Head+1)%MaxLen; gotoxy(NextX,NextY); if(Head==Tail) { printf("恭喜你,你过关了 !\n"); return 0; } printf("■"); SnakeBody[Head][0]=NextX; SnakeBody[Head][1]=NextY; Map[NextX-ShowPlaceX][NextY-ShowPlaceY]=1; if(isfood(NextX,NextY)) { score++; DrawFood(); } } return 1; } int isfood (int x,int y) { if(x==FoodX && y==FoodY) return 1; return 0; } void InitMap() { int i,j; for (i=0;i<2*ShowRangeX;i++) for (j=0;j<ShowRangeY;j++) Map[i][j]; } void HideCursor()//隐藏光标 { CONSOLE_CURSOR_INFO cursor_info = {1,0}; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info); } void Pause() { while(1) { Sleep(600); if(GetAsyncKeyState(VK_SPACE)) break; } return; }