贪吃蛇小游戏(C语言实现)

前言

代码分享 经典贪吃蛇小游戏,使用C语言实现。

实现代码

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <Windows.h>
#include <conio.h>
#include <math.h>

#define MAP_SIZE 40

/*
	定义上下左右四个方向的命令 
	1上 2下 3左 4右 
*/
#define UP       1
#define LEFT     2
#define DOWN     3
#define RIGHT    4

/*
	定义地图一张 g_map 10*10 
	蛇的长度 g_snake_legnth
	定义命令 g_cmd:移动的命令 
	定义食物的位置  g_food_x 和  g_food_y 
	定义贪吃蛇头部位置 
*/
int g_map[MAP_SIZE][MAP_SIZE] = {0};
int g_snake_legnth; 
int g_cmd; 
int g_food_x,g_food_y;
int g_head_x,g_head_y;
int g_end_x,g_end_y;


/*
	生成随机数 食物的位置 食物被吃掉之后重新生成 
	设置随机数种子
	生成0-MAP_SIZE-1的随机数 
*/
void Rand_food_addr()
{
	do{
		srand((unsigned)time(NULL));
		g_food_x = rand()%(MAP_SIZE-2)+1;
		g_food_y = rand()%(MAP_SIZE-2)+1;	
	}while(g_map[g_food_x][g_food_y]!=0);
	g_map[g_food_x][g_food_y]=-1;
}

/*
	参数初始化 
	初始长度为:3
	初始方向:上 
*/

void Snake_init()
{
	g_snake_legnth = 3;
	g_cmd          = UP;
	g_head_x       = 5;
	g_head_y       = 4; 
	g_map[5][4]    = 1;
	g_map[6][4]    = 2;
	g_map[7][4]    = 3;
	Rand_food_addr();
} 

void gotoxy(int x, int y)
{
	COORD pos;
	HANDLE hOutput;
	pos.X = x;
	pos.Y = y;
	hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(hOutput, pos);
}

/*
	从控制台获取命令 
*/
void Sanke_get_cmd()
{
	int data = 0;
	if(_kbhit())
	{
		switch(_getch())
		{
			case 'w':
				data = UP;
				break;
			case 's':
				data = DOWN;
				break;
			case 'a':
				data = LEFT;
				break;
			case 'd':
				data = RIGHT;
				break;
		}
		if(data!=0&&abs(g_cmd-data)!=2)
		{
			g_cmd = data;
		}
	}
} 

int Snake_move()
{
	int i,j,flag=0;//flag标记是否吃到食物 
	switch(g_cmd)
	{
		case UP:
			--g_head_x; 
			break;
		case DOWN:
			++g_head_x;
			break;
		case LEFT:
			--g_head_y;
			break;
		case RIGHT:
			++g_head_y;
			break;
	}
	//吃到食物 
	if(g_map[g_head_x][g_head_y]==-1) 
	{
		++g_snake_legnth;
		g_map[g_head_x][g_head_y]=1;
		flag=1;
		Rand_food_addr();//生成食物 
		gotoxy(g_food_y,g_food_x);
		printf("@");
	} 
	else if(g_map[g_head_x][g_head_y]>1)//吃到自己 
	{
		return 0; 
	} 
	else if(g_head_x==0||g_head_x==MAP_SIZE-1||g_head_y==0||g_head_y==MAP_SIZE-1)//撞到边框
	{
		return 0;
	} 
	else
	{
		g_map[g_head_x][g_head_y]=1; //蛇头新位置 
	}
	//动画显示蛇头部分 
	gotoxy(g_head_y,g_head_x);
	printf("O");
	
	for(i=0; i<MAP_SIZE; i++)
	{
		for(j=0; j<MAP_SIZE; j++)
		{
			if(g_map[i][j]==0||g_map[i][j]==-1)
				continue;
			else if(g_map[i][j]==g_snake_legnth-1&&flag==1)//如果吃到食物 长度加一  
			{
				g_map[i][j]=g_snake_legnth;
				gotoxy(j,i);
				printf("O");
			}
			else if(g_map[i][j]==g_snake_legnth)//如果没有吃到食物 正常移动 尾部输出空格 
			{
				g_map[i][j]=0;
				gotoxy(j,i);
				printf(" ");
			}
			else if(g_map[i][j]>0)
			{
				if(i==g_head_x&&j==g_head_y)
					continue;
				else
					g_map[i][j]++;	
			}	
		}	
	} 
	return 1;
}

void Sanke_draw()
{
	int i,j;
	system("cls");
	for(i=0; i<MAP_SIZE; i++)
	{
		for(j=0; j<MAP_SIZE; j++)
		{
			if(i==0||j==0||i==MAP_SIZE-1||j==MAP_SIZE-1)
			{
				printf("*");
			}
			else if(g_map[i][j]>0)
				printf("O");
			else if(g_map[i][j]==-1)
				printf("@");
			else
				printf(" ");
		}
		printf("\n");
	}
}

void Snake_Start(int speed)
{
	Snake_init();
	Sanke_draw();
	while(1)
	{
		Sanke_get_cmd();
		if(Snake_move()!=1)
		{
			printf("Game Over\n");
			break;
		}
		Sleep(speed);
	}
} 

void Sanke_End()
{
	system("cls");
	printf("Game Over\n");
}

int main()
{
	while(1)
	{	
		Snake_Start(200);
	}
	return 0;
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值