C语言贪吃蛇的实现

贪吃蛇对于初学者来说是个检验自己代码能力的好项目,那么如何实现呢?

首先要创建地图

void createMap()//对于地图上下左右边界的创建
{
	for(int i=0+ MOVE_CENTER;i<= MAP_WIDTH+ MOVE_CENTER;i++)

	{
		gotoxy(i, 0);
		printf(MAP_MODE);

		gotoxy(i, MAP_HEIGHT);
		printf(MAP_MODE);
	}
	for (int i = 0; i <=MAP_HEIGHT; i++)
	{
			gotoxy(0+ MOVE_CENTER,i);
			printf(MAP_MODE);

			gotoxy(MAP_WIDTH+ MOVE_CENTER, i);
			printf(MAP_MODE);
	} 
	gotoxy(98, 0);//使得光标远离地图更加美观
	initSnake();//蛇的初始化后续会提到
}

然后就是对于食物的创建

void initSnake()//蛇的初始化
{
	snake.currentLen = INITLEN;//蛇的初始长度
	snake.x[0] = MAP_WIDTH / 2 + MOVE_CENTER;//蛇头的创建
	snake.y[0] = MAP_HEIGHT / 2;//蛇身的创建
	gotoxy(snake.x[0], snake.y[0]);
	printf(SNAKE_HEAD);
	for (int i = 1; i < snake.currentLen; i++)//打印蛇
	{
		snake.x[i] = snake.x[i - 1]+2;
		snake.y[i] = snake.y[i - 1];
		gotoxy(snake.x[i], snake.y[i]);
		printf(SNAKE_BODY);
	}
	gotoxy(98, 0);
}

再接下来就是蛇的移动了

int direct = 'a';
void moveSnke()
{
	if (_kbhit())//对于键盘的监控
	{
		fflush(stdin);
		direct = _getch();
	}
	gotoxy(snake.x[snake.currentLen - 1], snake.y[snake.currentLen - 1]);//蛇移动后尾巴的消失
	printf("  ");
	for (int i = snake.currentLen - 1; i > 0; i--)
	{
		snake.x[i] = snake.x[i - 1];
		snake.y[i] = snake.y[i - 1];
		gotoxy(snake.x[i], snake.y[i]);
		printf(SNAKE_BODY);
	}
	switch (direct)
	{
	case 'w':
	case 'W':
		snake.y[0]--;
		break;
	case 's':
	case 'S':
		snake.y[0]++;
		break;
	case 'a':
	case 'A':
		snake.x[0]-=2;
		break;
	case 'd':
	case 'D':
		snake.x[0]+=2;
		break;
	}
	gotoxy(snake.x[0], snake.y[0]);
	printf(SNAKE_HEAD);
	gotoxy(98, 0);

	if (snake.x[0] == food.x && snake.y[0] == food.y)//蛇吃到食物后身体加长
	{
		snake.currentLen++;
		flag = 1;
	}
}

最后判断游戏的状态

int statement()
{
	if (snake.x[0] == 0 + MOVE_CENTER|| snake.x[0]== MAP_WIDTH + MOVE_CENTER||snake.y[0]==0|| snake.y[0]== MAP_HEIGHT)//蛇头不能撞到墙上
	{
		return 1;
	}
	for (int i = 1; i < snake.currentLen; i++)//蛇不能碰到自己的身体
	{
		if (snake.x[0] == snake.x[i] && snake.y[0] == snake.y[i])
		{
			return 1;
		}
	}
	return 0;
}

最后可能帮助到大家的

struct Food {
	int x;
	int y;
}food;//食物结构体的创建
struct Snake {
	int x[MAXLEN];
	int y[MAXLEN];
	int currentLen;
}snake;//蛇的结构体的创建
void gotoxy(int x, int y)
{
	COORD pos = { x,y };
	HANDLE hOult = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(hOult, pos);
}//对于光标的应用
//对于大家可能用的到的一些头文件
#include <stdio.h>
#include <time.h>
#include <conio.h>
#include <Windows.h>
#include <stdlib.h>
//下面是一些自己对于一些宏的定义
#define MAXLEN 30
#define INITLEN 3
#define MAP_MODE "█"
#define SNAKE_HEAD "☉"
#define SNAKE_BODY "●"
#define MAP_WIDTH 80
#define MAP_HEIGHT 30
#define MOVE_CENTER 15

其实贪吃蛇挺简单的,主要考察大家对于结构体、函数、数组的运用。希望大家也能写出自己的第一个小游戏

  • 13
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用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、付费专栏及课程。

余额充值