C语言实现贪吃蛇

C语言实现贪吃蛇

用到EasyX图形库
大家需要的话可以下载
代码还有待完善

#include<stdio.h>
#include<graphics.h>	//easyX图形库
#include<conio.h>
#include<time.h>

//窗口大小
enum wincoord
{
	vx = 800,
	vy = 600
};

//坐标
typedef struct coordinate
{
	int x, y;
}Coord;

//蛇
struct Snake
{
	Coord sxy[100];	//坐标
	int position;	//方向
	int num;		//长度
}snake;

//食物
struct Food
{
	Coord fxy;	//坐标
	int flag;	//标记
	int grade;	//分数
}food;

//方向
enum position
{
	up,		//上
	down,	//下
	left,	//左
	right	//右
};

//初始化蛇
void initsnakea()
{
	snake.sxy[0].x = 20;	//蛇头
	snake.sxy[0].y = 0;

	snake.sxy[1].x = 10;
	snake.sxy[1].y = 0;

	snake.sxy[2].x = 0;
	snake.sxy[2].y = 0;

	snake.position = right;		//方向
	snake.num = 3;		//长度
}

//打印蛇
void drawsnake()
{
	for (int i = 0; i < snake.num; i++)
	{
		setlinecolor(BLACK);
		setfillcolor(RGB(rand() % 255, rand() % 255, rand() % 255));	//填充随机颜色
		fillrectangle(snake.sxy[i].x, snake.sxy[i].y, snake.sxy[i].x + 10, snake.sxy[i].y + 10);	//画有边框矩形

	}
}

//移动蛇
void movesnake()
{
	for (int i = snake.num - 1; i > 0; i--)		//改变蛇身坐标
	{
		snake.sxy[i].x = snake.sxy[i - 1].x;
		snake.sxy[i].y = snake.sxy[i - 1].y;
	}
		switch (snake.position)		//改变蛇头坐标
		{
		case up:
			snake.sxy[0].y -= 10;
			break;
		case down:
			snake.sxy[0].y += 10;
			break;
		case left:
			snake.sxy[0].x -= 10;
			break;
		case right:
			snake.sxy[0].x += 10;
		}
}

//按键操作
void keydown()
{
	char userkey = _getch();
	switch (userkey)
	{
	case 'w' :
	case 'W' :
	case 72:
		if (snake.position != down)
		{
			snake.position = up;
		}
		break;
	case 's' :
	case 'S' :
	case 80:
		if (snake.position != up)
		{
			snake.position = down;
		}
		break;
	case 'a' :
	case 'A' :
	case 75:
		if (snake.position != right)
		{
			snake.position = left;
		}
		break;
	case 'd' :
	case 'D' :
	case 77:
		if (snake.position != left)
		{
			snake.position = right;
		}
		break;
	}

}

//初始化食物
void initfood()
{
	food.fxy.x = rand() % (vx / 10) * 10;	//食物出现的位置
	food.fxy.y = rand() % (vy / 10) * 10;

	food.flag = 1;	//食物是否存在
	food.grade = 0;

	for (int i = 0; i < snake.num; i++)		//判断食物是否与蛇重叠
	{
		if (food.fxy.x == snake.sxy[i].x && food.fxy.y == snake.sxy[i].y)
		{
			initfood();		//递归
			break;
			
		}
	}
}

//打印食物
void drawfood()
{
	setlinecolor(BLACK);
	setfillcolor(RGB(rand() % 255, rand() % 255, rand() % 255));
	fillrectangle(food.fxy.x,food.fxy.y,food.fxy.x+10,food.fxy.y+10);
}

//吃食物
void eatfood()
{
	if (snake.sxy[0].x == food.fxy.x && snake.sxy[0].y == food.fxy.y)
	{
		snake.num += 1;		//长度
		food.flag = 0;
		food.grade += 10;		//得分
	}
}

bool snakedie()		//蛇的状态
{
	if (snake.sxy[0].x == -10 || snake.sxy[0].x == vx)		//是否撞墙
	{
		return true;
	}
	if (snake.sxy[0].y == -10 || snake.sxy[0].y == vy)
	{
		return true;
	}

	for (int i = 1; i < snake.num; i++)		//是否撞自身
	{
		if (snake.sxy[0].x == snake.sxy[i].x && snake.sxy[0].y == snake.sxy[i].y)
		{
			return true;
		}
	}

	return false;
}



int main()
{
	srand((unsigned int)time(NULL));	//设置随机数
	initgraph(vx, vy);
	setbkcolor(RGB(140,161,159));
	cleardevice();	//刷新

	initsnakea();	//初始化蛇
	drawsnake();	//打印
	
	while (1)
	{
		cleardevice();	//刷新
		movesnake();	//移动
		drawsnake();	//打印	

		if (food.flag == 0)
		{
			initfood();
		}

		if (_kbhit())
		{
			keydown();
		}

		drawfood();

		eatfood();

		if (snakedie())
		{
			break;
		}

		Sleep(60);
	}

	_getch();	//防止闪屏
	closegraph();
	return 0;
}
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
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 } ...
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CYJ_716

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值