C语言贪吃蛇源码

#include <stdio.h>
#include <windows.h>
#include<conio.h>
#include <stdlib.h>
#include<time.h>

#define X 23//地图的x轴
#define Y 75//地图的y轴

#define UP 0
#define DOWN 1
#define LEFT 2
#define RIGHT 3

#define WAIT_TIME 200//等待蛇刷新的时间,可以说是速度  修改可变速

int map_0[X][Y];//地图
int Snake[X*Y][2]; // 蛇
int Slength; //蛇的长度
int direction; 
int score=0;

bool pdEatFood=false;

void csh();
void huaMap();
void huaSnake();
void gotoxy(int x,int y);
void move();
void intokey();
int check(int x,int y);
void putfood();
bool gameover();
void dy_fs();

int main()
{
	csh();
	huaMap();
	putfood();
	while(1)
    {
        huaSnake();               
        Sleep(WAIT_TIME);  
        intokey();
        move();
        dy_fs();
		if(gameover())
        {
            system("cls");          //清除屏幕内容
            printf("Game Over\n");  
            system("pause");
            getchar();
            break;
        }
		if(map_0[Snake[0][0]][Snake[0][1]]==-1)
		{
		map_0[Snake[0][0]][Snake[0][1]]=0;
		pdEatFood=true;
		putfood();
		score+=10;
		}
    }
	return 0;
}
void csh()//初始化 
{
	srand((unsigned)time(NULL)); //设置种子为现在的时间
	Slength=4;
	gotoxy(0,0);
	CONSOLE_CURSOR_INFO cursor_info = {1, 0}; //清除光标 
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);		
	int x,y;
	Snake[0][0]=X/2;
	Snake[0][1]=Y/2;
	for(x=0;x<X;x++){
		map_0[x][0]=1;
		map_0[x][Y-1]=1;
	}
	for(y=1;y<Y-1;y++){
		map_0[0][y]=1;
		map_0[X-1][y]=1;
	} 
	for(x=1;x<4;x++){	//初始化蛇的坐标 
		Snake[x][0]=Snake[0][0]+x;
		Snake[x][1]=Snake[0][1];
	}
	direction=UP;
	
}
void huaMap()//画地图 
{
	int x,y;
	for(x=0;x<X;x++){
		for(y=0;y<Y;y++){
			if(map_0[x][y]==1){
				printf("#");
			}
			if(map_0[x][y]==0){
				printf(" ");
			}
		}
		printf("\n");
	}
}
void huaSnake()//画蛇 
{
	int x;
	for(x=0;x<Slength;x++)
	{
		gotoxy(Snake[x][0],Snake[x][1]);
		printf("@");	
	}
}
void gotoxy(int i,int j)//移动光标 
{
    COORD position={j,i};
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),position);
}
void move()
{
    int i;
    gotoxy(Snake[Slength-1][0],Snake[Slength-1][1]);//擦除尾巴
    printf(" ");                            
    for(i=Slength-1;i>0;i--)    //从尾巴开始,每一个点的位置等于它前面一个点的位置
    {
        Snake[i][0]=Snake[i-1][0];
        Snake[i][1]=Snake[i-1][1];
    }
    switch(direction)
    {
    case UP:
        Snake[0][0]--;
        break;
    case DOWN:
        Snake[0][0]++;
        break;
    case LEFT:
        Snake[0][1]--;
        break;
    case RIGHT:
        Snake[0][1]++;
        break;
    }
	if(pdEatFood){
	Slength++;
	pdEatFood=false;
	}

}
void intokey()
{
    if(kbhit()!=0)          //kbhit()函数 检查当前是否有键盘输入,若有则返回一个非0值,否则返回0
    {
        char in;
        while(!kbhit()==0)  //如果玩家输入了多个按键,以最后一个按键为准
            in=getch();
        switch(in)
        {
        case 'w':
        case 'W':
            if(direction!=DOWN)         //防止缩头
                direction=UP;
            break;
        case 's':
        case 'S':
            if(direction!=UP)
                direction=DOWN;
            break;
        case 'a':
        case 'A':
            if(direction!=RIGHT)
                direction=LEFT;
            break;
        case 'd':
        case 'D':
            if(direction!=LEFT)
                direction=RIGHT;
            break;
		case 'p':
        case 'P':
            gotoxy(X,0);        
            system("pause");
            gotoxy(X,0);
            printf("                   ");  //	消去下面的按任意键继续
            break;
        }
    }
}
int check(int ii,int jj){// 检查是否能投放食物
	if(map_0[ii][jj]==1) 
		return 0;
	if(ii==0 || jj==0 || ii==X-1 || jj==Y-1)
		return 0;
	int i;
	for(i=0;i<Slength;i++){
	if(ii==Snake[i][0] && jj==Snake[i][1])
	return 0;
	}
	return 1;
}
void putfood()
{
	int i,j;
	do{
	i=rand()%X;
	j=rand()%Y;
	}while(check(i,j)==0);
	map_0[i][j]=-1;
	gotoxy(i,j);
	printf("$");
}
bool gameover()
{
	bool isgameover=false;
	int sX,sY;
	sX=Snake[0][0],sY=Snake[0][1];
	if(sX==0 || sX==X-1 || sY==0 || sY==Y-1)
		isgameover=true;
	int i;
	for(i=1;i<Slength;i++){
	if(sX==Snake[i][0] && sY==Snake[i][1])
		isgameover=true;
	}
	return isgameover;
}
void dy_fs()
{
	gotoxy(X,0);
    printf("(c)Geek------2018.1.22");
	gotoxy(X+1,0);
    printf("最终得分: %d",score);
}

  • 30
    点赞
  • 96
    收藏
    觉得还不错? 一键收藏
  • 24
    评论
用windows api 做的贪吃蛇 #include #include"resource.h" #include"Node.h" #include #include TCHAR szAppname[] = TEXT("Snack_eat"); #define SIDE (x_Client/80) #define x_Client 800 #define y_Client 800 #define X_MAX 800-20-SIDE //点x的范围 #define Y_MAX 800-60-SIDE //点y的范围 #define TIME_ID 1 #define SECOND 100 #define NUM_POINT 10 //点的总个数 #define ADD_SCORE 10 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { HWND hwnd; //窗口句柄 MSG msg; //消息 WNDCLASS wndclass; //窗口类 HACCEL hAccel;//加速键句柄 wndclass.style = CS_HREDRAW | CS_VREDRAW; //窗口的水平和垂直尺寸被改变时,窗口被重绘 wndclass.lpfnWndProc = WndProc; //窗口过程为WndProc函数 wndclass.cbClsExtra = 0; //预留额外空间 wndclass.cbWndExtra = 0; //预留额外空间 wndclass.hInstance = hInstance; //应用程序的实例句柄,WinMain的第一个参数 wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); //设置图标 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); //载入预定义的鼠标指针 wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); //设置画刷 wndclass.lpszMenuName = szAppname; //设置菜单 wndclass.lpszClassName = szAppname; //设置窗口类的名字 if (!RegisterClass(&wndclass))//注册窗口类 { MessageBox(NULL, TEXT("这个程序需要windows NT!"), szAppname, MB_ICONERROR); return 0; } hwnd = CreateWindow(szAppname, TEXT("Snack_eat"),//CreateWindow函数调用时,WndProc将受到WM_CREATE WS_OVERLAPPEDWINDOW&~WS_THICKFRAME& ~WS_MAXIMIZEBOX,//普通的层叠窗口&禁止改变大小&禁止最大化 CW_USEDEFAULT, //初始x坐标(默认) CW_USEDEFAULT, //初始y坐标 x_Client, //初始x方向尺寸 770 y_Client, //初始y方向尺寸 750 NULL, //父窗口句柄 NULL, //窗口菜单句柄 hInstance, //程序实例句柄 WinMain函数中第二个参数 NULL); //创建参数 ShowWindow(hwnd, iCmdShow);//显示窗口,iCmdShow是WinMain的第四个参数,决定窗口在屏幕中的初始化显示形式,例:SW_SHOWNORMAL表示正常显示 UpdateWindow(hwnd);//使窗口客户区重绘,通过向WndProc发送一条WM_PAINT消息而完成的 hAccel = LoadAccelerators(hInstance, szAppname);//加载加速键 while (GetMessage(&msg, NULL, 0, 0)) { if (!TranslateAccelerator(hwnd, hAccel, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } }/* while (GetMessage(&msg, NULL, 0, 0))//GetMessage函数从消息队列中得到消息,填充msg。如果msg.message等于WM_QUIT,返回0,否则返回非0 { TranslateMessage(&msg);//将msg返回给windows已进行某些键盘消息的转换 DispatchMessage(&msg);//将msg再次返回给windows }*/ return msg.wParam;//msg.wParam是PostQuitMessage函数的参数值,通常是0 } ...

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 24
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值