C语言编写贪吃蛇

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <signal.h>
#include <termios.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>

#define KEYCODE_U 0x41   // 向上按键
#define KEYCODE_D 0x42   // 向下按键
#define KEYCODE_L 0x44   // 向左按键
#define KEYCODE_R 0x43   // 向右按键

int kfd = 0;
struct termios cooked, raw;
char dir = KEYCODE_U;    // 当前蛇的移动方向

// 获取键盘响应:上、下、左、右键
void* get_dir(void *a)
{
	while(1)
	{
		char c;                                                              
		tcgetattr(kfd, &cooked); // 得到 termios 结构体保存,然后重新配置终端
		memcpy(&raw, &cooked, sizeof(struct termios));
		raw.c_lflag &=~ (ICANON | ECHO);                       
		raw.c_cc[VEOL] = 1;
		raw.c_cc[VEOF] = 2;
		tcsetattr(kfd, TCSANOW, &raw);

		if(read(kfd, &c, 1) < 0)
		{
			perror("read():");
			exit(-1);
		}
		tcsetattr(kfd, TCSANOW, &cooked);//在程序结束时在恢复原来的配置
		dir = c;
	}
}

unsigned char map[17][17] = {0};   // 游戏地图
int snake[50] = {133};   // 初始化蛇坐标,游戏开始的时候蛇在(8,5)这个位置
int food = 67;       	   // 食物的坐标,游戏开始的时候在(4,3)这个位置
int len = 1;                   	   // 保存蛇的当前长度

// 将 数字 转化为坐标系
void num_to_xy(int num, int *x, int *y)
{
	//char *p1=x;
	//char *p2=y;
	*x = num / 17 ;
	*y = num % 17 ;
}

// 更新地图数据
void update_map()
{
	int i=0,j;
	int xfood,yfood;
	num_to_xy(food, &xfood ,&yfood);
	int xsnake,ysnake;
	int k;
		
	for(i = 0; i < 17; i++ )
	{
		for(j = 1; j < 17; j++ )
		{
			if(i == 0 || i == 16 || j == 0 || j == 16)
				map[i][j] = '#'	;
			else if(i == xfood && j == yfood)
			{
				map[i][j] = '!'
			}
			else
				map[i][j] = ' ';
		}
	}
	for(k = 0; k < len ; k++)
		num_to_xy(snake[k], &xsnake,&ysnake);
	for(i = 0; i < 17; i++ )
	{
		for(j = 1; j < 17; j++ )
		{
			if(i == x && j == y)
				map[i][j] = '*';
		}
	)
		
}

// 打印地图
void print_map()
{
	//usleep(len / 3 + 1);//睡眠时间
	//system("clear");
	
	int i=0,j;
	for(i = 0; i < 17; i++ )
	{
		for(j = 1; j < 17; j++ )
		{
			if(i == 0 || i == 16 || j == 0 || j == 16)
				map[i][j] = '#'	;
		}
	}
	                      //地图边框
	
}

// 生成食物
void generate_food(char* food)
{
	int x,y;
	int k;
	srand(time(NULL));
	food = rand()%289;
	num_to_xy(food ,&x , &y);
	
	while(i == 0 || i == 16 || j == 0 || j == 16)
	{
		food = rand()%289;
		num_to_xy(food ,&x , &y);
	}
	for(k = 0; k < len ; k++)
	{
		num_to_xy(snake[0], &xsnake,&ysnake);
		if(snake[0] == food)
		{
			food = rand()%289;
			k = 0;
			num_to_xy(food,&x,&y)
		}
			
	}
	

	
	
}

// 移动蛇
void move_snake()
{
	int x,y;  // 坐标
	num_to_xy(snake[0], &x, &y);   		// 获取蛇头的坐标
	int i, end;
	
	// 判断移动方向
	switch (dir)
	{
		case KEYCODE_U:   // 向上移动
			x--;
			break;
		case KEYCODE_D:   // 向下移动
			x++;
			break;
		case KEYCODE_L:   // 向左移动
			y--;
			break;
		case KEYCODE_R:   // 向右移动
			y++;
			break;
	}
	
	end = snake[len - 1];
	for(i = len - 1; i > 0; i --)
	{
		snake[i] = snake[i-1];
	}
	snake[0] = x*17+y;
	if(snake[0] == food)
	{
		len ++;
		snake[len - 1]=len;
	}

}

// 判断蛇是否应该存活,如果返回值是1代表应该存活,0代表不应该存活
int isalive()
{
	int temp = 1;
	int x,y;
	int i;
	
	num_to_xy(snake[0], &x, &y);
	
	if(i == 0 || i == 16 || j == 0 || j == 16)
		temp = 0;
	//查重
	for(i = 1; i < len ; i++)
	{
		if(snake[0]==snake[i])
			temp = 0 ;
	}
	
	
}

int main()
{
	// 开启一个线程用于获取键盘的上下左右键响应
	pthread_t tid1;
	pthread_create(&tid1, NULL, get_dir, NULL);
	while(1)
	{
		// 更新地图数据
		update_map();
		
		// 打印地图
		system("clear");
		print_map();
		usleep(500000(len / 3 + 1));//睡眠时间
		
		
		// 移动蛇
		move_snake();
		
		// if (isalive() == 1)
		// {
			// break;
		// }	
	// }
	tcsetattr(kfd, TCSANOW, &cooked);//在程序结束时在恢复原来的配置
	
	printf ("Game Over!\n");
	return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
用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 } ...
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值