纯C语言写的贪吃蛇源码

本文探讨了游戏开发领域中AI音视频处理的应用,包括图像处理、AR特效、音视频直播流媒体等内容,深入解析了这些技术在游戏开发中的实际应用场景和实现方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

#define N 225

struct Food
{
	int x;
	int y;
	int yes;//1表示需要出现新食物,0表示已有食物。
}food;

struct Snake
{
	struct Food body[N];
	int life;//1表示蛇死啦,0表示蛇活着。
	int node;//蛇的节数。
	char direction;//蛇自动运动的方向。
}snake;

int score=0;

int main()
{
	FILE *p;
	int i,j,k,b;
	char map[16][16],c;

	p=fopen("E:\\file.txt","r");
	if(p==NULL)
	{
		printf("error");
		exit(1);
	}
	for(i=0;i<16;i++)
	{
		for(j=0;j<16;j++)
		{
			map[i][j]=fgetc(p);
		}
		fgetc(p);
	}

	snake.body[0].x=8;//蛇头。
	snake.body[0].y=8;

	snake.body[1].x=8;
	snake.body[1].y=7;

	snake.body[2].x=8;
	snake.body[2].y=6;

	snake.node=3;//蛇的节数。
	food.yes=1;

	srand(time(NULL));
	snake.direction='d';//一开始蛇自动往右跑。

	while(1)
	{
		if(kbhit())
		{
			c=getch();
			if(snake.life==1)
				break;
			if(c=='w' && snake.direction!='s')
				snake.direction='w';
			else if(c=='a' && snake.direction!='d')
				snake.direction='a';
			else if(c=='s' && snake.direction!='w')
				snake.direction='s';	
			else if(c=='d' && snake.direction!='a')
				snake.direction='d'; 
		}
		if(food.yes==1)//需要随机出现新的食物。
		{
			food.x=1+rand()%14;
			food.y=1+rand()%14;

			for(i=0;i<snake.node;i++)
			{
				if(food.x==snake.body[i].x &&  food.y==snake.body[i].y)
				{
					food.x=1+rand()%14;
					food.y=1+rand()%14;
				}
			}
			food.yes=0;
		}

		if(snake.body[0].x==food.x && snake.body[0].y==food.y)
		{	// 蛇吃到食物了。
			snake.node++;//蛇身长一节。
			food.yes=1;//地图上需要出现新的食物。
			system("cls");
			score+=5;
		}


		for(i=snake.node-1;i>0;i--)//蛇往前移动。
		{
			snake.body[i].x=snake.body[i-1].x;
			snake.body[i].y=snake.body[i-1].y;
		}

		switch(snake.direction)//蛇头方向。
		{
			case'a':
				snake.body[0].y-=1;
				break;
			case'w':
				snake.body[0].x-=1;
				break;
			case'd':
				snake.body[0].y+=1;
				break;
			case's':
				snake.body[0].x+=1;
				break;
		}


		if(food.yes==0)//显示蛇,食物和围墙。
		{
			system("cls");
			for(i=0;i<16;i++)
			{
				for(j=0;j<16;j++)
				{
					b = 1;
					for(k=0;k<snake.node;k++)
					{
						if(i == snake.body[k].x  && j == snake.body[k].y )
						{					
							if(k==0)
								printf("●");//蛇头。
							else
								printf("□");//蛇身。
							b=0;
							break;
						}
					}
					if(b==1)
					{
						if(i==food.x && j==food.y && i<15 && i>0 && j<15 && j>0)//食物。
							printf("★");
						else if(map[i][j]=='1')
							printf("■");
						else 
							printf("  ");
					}
							
				}
				putchar('\n');
			}
			Sleep(250);//休眠函数。
		}
		
		for(i=3;i<snake.node;i++)//判断蛇是否咬到自己。
		{
			if(snake.body[i].x==snake.body[0].x && snake.body[i].y==snake.body[0].y)
			{
				snake.life=1;
				break;
			}
		}
		if(snake.life==1)
		{
			printf("蛇把自己咬死了\n");
			printf("共得分:%d\n",score);
			break;
		}
		if(snake.body[0].x>=15 || snake.body[0].x<=0 || snake.body[0].y>=15 || snake.body[0].y<=0)
		{
			printf("蛇撞墙死了!\n");
			printf("共得分:%d\n",score);
			snake.life=1;
			break;
		}
	}	
	fclose(p);
	return 0;
}
下面是file.txt:
1111111111111111
1000000000000001
1000000000000001
1000000000000001
1000000000000001
1000000000000001
1000000000000001
1000000000000001
1000000000000001
1000000000000001
1000000000000001
1000000000000001
1000000000000001
1000000000000001
1000000000000001
1111111111111111

用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 } ...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值