C语言——贪吃蛇小游戏

2023-09-17 18:08:29

目录

一、ncurse

1.1 为什么需要用ncurse:

1.2 ncurse的输入输出:

1.2.1 如何使用ncurse:

1.2.2 编译ncurse的程序:

1.2.3 测试输入一个按键ncurse的响应速度:

1.3 ncurse上下左右键获取:

1.3.1 如何查看宏定义的.h文件:

1.3.2 ncurse上下左右键获取:

二、地图规划

2.1 地图规划算法显示第一行:

2.2 实现贪吃蛇完整地图:

2.3 优化贪吃蛇地图:

三、显示贪吃蛇身子

3.1 显示贪吃蛇身子的一个节点:

3.2 显示贪吃蛇完整身子:

3.3 显示贪吃蛇完整身子改进:

四、贪吃蛇移动

4.1 按下▶贪吃蛇向右移动:

4.2 贪吃蛇撞墙重新开始: 

4.3 贪吃蛇脱缰自由向右行走

五、Linux线程引入

5.1 贪吃蛇方向移动和刷新界面一起实现面临的问题:

5.2 线程的基本用法:

5.3 线程demo案例:

5.4 使用线程解决贪吃蛇方向移动和刷新界面一起实现面临的问题:

六、贪吃蛇跑起来

6.1 实现贪吃蛇四方向的风骚走位:

6.2 用绝对值方式来解决不合理的走位:

6.3 贪吃蛇吃饭了(食物的位置是随机的):

七、项目代码


  • 项目运行环境:Linux,基于Ncurse图形库的C语言小游戏

  • 项目的目的和意义:起到承上启下的作用,对于前面学的C语言的基础和数据结构链表做一个比较好的巩固,对于后面的Linux系统编程的开发做铺垫

  • 项目基础要求:C语言基础、Linux基本操作

    /*项目步骤*/
    (1)选择ncurses库的原因
    在进行贪吃蛇游戏时,贪吃蛇的行进方向需要你按下上下左右键进行操控,如果使用C语言自带的函数,例如:scanf或者getchar之类的,需要你按下回车键,程序才能进行响应,而这显然是十分不方便的,但是ncurses库就很好的解决了这个问题。ncurses库自带的函数getch就能实现迅速便捷的贪吃蛇方向响应。
    
    (2)ncurses库的基本入门
    对于该项目而言,ncurses库我们不需要进行过于深入的学习,只需要知道一些基本的函数使用即可。下列程序中的函数,就是一个基于ncurses库的基本代码框架。
    #include <curses.h>
     
    int main()
    {
            initscr();//ncurse界面的初始化函数
            printw("this is a curses window\n");//在ncurse模式下的printf
            getch();//等待用户的输入,如果没有这句话,程序就退出了,看不到运行的结果,也就是无法看到上面那句话
            endwin();//程序退出,恢复shell终端的显示,如果没有这句话,shell终端字乱码,坏掉
     
            return 0;
    }
    
    (3)贪吃蛇地图的整体规划
    整个贪吃蛇地图的大小将它设置成一个20*20的近似正方形,使用"|"来表示左右边框,使用"--"来表示上下边框。
    
    (4)实现贪吃蛇第一个节点的显示
        
    (5)显示贪吃蛇的完整身子
    注意,在这里,我们设置两个全局变量,struct Snake *head和struct Snake *tail,一个指向贪吃蛇的头,一个指向贪吃蛇的尾。在将第一个节点打印完后,将尾指向头,即:head = tail。每一次节点的添加,我们调用一个单独的函数去执行,并其使用尾插法实现。
    
    (6)实现贪吃蛇的右移
    贪吃蛇的移动,整体来说就是链表节点的删除与添加。我们首先实现贪吃蛇的右移,每当按键按下时,贪吃蛇右移一格,即左侧的头结点删除head = head->next,右侧再次添加一个新的节点。新节点的坐标应该是行不变,列加一。注意:不要忘记清楚垃圾节点。
    
    (7)实现贪吃蛇的撞墙死亡
    将贪吃蛇的尾节点坐标进行判断,判断其是否达到边界坐标。满足条件时,将贪吃蛇重新初始化。注意:不要忘记清楚垃圾节点。
    
    (8)实现贪吃蛇的自由行走
    在这里,我们发现了一个问题,地图需要实时刷新进行贪吃蛇位置的变更,这是一个while(1)的死循环,而获取键值也是一个实时读取的操作,因此也是一个while(1)死循环,代码执行逻辑上出现了问题,所以我们引入了线程的概念。
    
    (9)了解什么是线程
        
    (10)用线程解决上述问题,实现贪吃蛇的分骚走位
    开辟两个线程,一个用来执行地图刷新操作,一个用来获取键值。
     pthread_create(&t1,NULL,refreshScreen,NULL);
     pthread_create(&t2,NULL,changeDir,NULL);
    
    (11)解决贪吃蛇的不合理走位
    在这里,我们使用绝对值法来解决问题,abs函数的作用便是取绝对值,我们将上下左右键,两两对应,宏定义为1,-1,2,-2之类的数就能成功解决问题。
    
    (12)实现贪吃蛇食物的打印
        
    (13)实现食物的随机出现
    取随机数,C语言有一个自带的函数可以解决这个问题,rand()函数可以实现随机取数,我们只要再对它进行取余操作,便可以防止食物出现在地图以外的位置。
    
    (14)实现贪吃蛇咬到自己结束游戏,重新开始的操作
    当贪吃蛇的尾节点与自身除尾巴节点以外的其他节点进行比较后,若行列数相同,则初始化整个贪吃蛇,注意:不要忘记垃圾节点的清除(我们可以在每次贪吃蛇初始化之前进行这个操作)。

    一、ncurse

    1.1 为什么需要用ncurse:

  • 因为的按键响应牛逼哄哄

  • 1.2 ncurse的输入输出:

  • ncurse用的最多的地方是在Linux内核编译之前的内核配置

  • 1.2.1 如何使用ncurse:

1.2.2 编译ncurse的程序:

1.2.3 测试输入一个按键ncurse的响应速度:

1 #include <curses.h>
  2 
  3 int main()
  4 {
  5         char c;
  6 
  7         initscr();
  8         c = getch();
  9         printw("you Input :%c\n",c);
 10         getch();
 11         endwin();
 12         return 0;
 13 }           
  • 使用ncurse的好处是:按下一个按键不需要按下回车,直接就可以输出c的值,和我们C语言的其他输入函数好用

1.3 ncurse上下左右键获取:

1.3.1 如何查看宏定义的.h文件:

vi /usr/include/curses.h    //查看宏定义.h文件的指令
:q                            //退出查看

1.3.2 ncurse上下左右键获取:

 1 #include <curses.h>
  2 
  3 int main()
  4 {
  5         int key;
  6 
  7         initscr();
  8         keypad(stdscr,1);	//这个函数允许使用功能键,例如:F1、F2、方向键等功能键。几乎所有的交互式程序都需要使用功能			键,因为绝大多数用户界面主要用方向键进行操作。使用keypad(stdscr,TURE)就为“标准屏幕”(stdscr)激活了功能键。
  9 
 10         while(1){
 11                 key = getch();
 12                 switch(key){
 13                         case KEY_DOWN:
 14                                 printw("DOWN\n");
 15                                 break;
 16                         case KEY_UP:
 17                                 printw("up\n");
 18                                 break;
 19                         case KEY_LEFT:
 20                                 printw("LEFT\n");
 21                                 break;
 22                         case KEY_RIGHT:
 23                                 printw("RIGHT\n");
 24                                 break;
 25                 }
 26 
 27 
 28         }
 29         endwin();
 30         return 0;
 31 }

  • 我们按下上下左右▲▼◀▶之后,可以获取到上下左右的打印信息

二、地图规划

2.1 地图规划算法显示第一行:

#include <curses.h>

void initNcurse()
{
	initscr();
	keypad(stdscr,1);
}

void gamPic()
{
	int hang;
	int lie;
	
	for(hang=0; hang<20; hang++){
		if(hang == 0){
			for(lie=0; lie<20; lie++){
				printw("--");
			}
			printw("\n");
			for(lie=0; lie<=20; lie++){
				if(lie == 0 || lie == 20){
					printw("|");
				}else{
					printw("  ");
				}
			}
		}
	}
}

int main()
{
	initNcurse();	//初始化Ncurse
	gamPic();		//地图规划显示第一行
	
	getch();
	endwin();
	return 0;
}

2.2 实现贪吃蛇完整地图:

#include <curses.h>

void initNcurse()
{
	initscr();
	keypad(stdscr,1);
}

void gamPic()
{
	int hang;
	int lie;
	
	for(hang=0; hang<20; hang++){
		if(hang == 0){
			for(lie=0; lie<20; lie++){
				printw("--");
			}
			printw("\n");
			for(lie=0; lie<=20; lie++){
				if(lie == 0 || lie == 20){
					printw("|");
				}else{
					printw("  ");
				}
			}
			printw("\n");
		}
		
		if(hang>0 && hang<=19) {
			for(lie=0; lie<=20; lie++){
				if(lie == 0 || lie == 20){
					printw("|");
				}else{
					printw("  ");
				}
			}
			printw("\n");
		}

		if(hang == 19){
			for(lie=0; lie<20; lie++){
				printw("--");
			}
			printw("\n");
			printw("By ShiYaHao!\n");
		}
	}
}

int main()
{
	initNcurse();	//初始化Ncurse
	gamPic();		//实现贪吃蛇地图
	
	getch();
	endwin();
	return 0;
}

2.3 优化贪吃蛇地图:

#include <curses.h>

void initNcurse()
{
	initscr();
	keypad(stdscr,1);
}

void gamPic()
{
	int hang;
	int lie;
	
	for(hang=0; hang<20; hang++){
		if(hang == 0){						//第0行打“--”
			for(lie=0; lie<20; lie++){
				printw("--");
			}
			printw("\n");
		}
		
		if(hang>=0 && hang<=19) {			//第0行-19行的第0列和第20列打“|”
			for(lie=0; lie<=20; lie++){
				if(lie == 0 || lie == 20){
					printw("|");
				}else{
					printw("  ");
				}
			}
			printw("\n");
		}

		if(hang == 19){						//第19行打“--”
			for(lie=0; lie<20; lie++){
				printw("--");
			}
			printw("\n");
			printw("By ShiYaHao!\n");		//作者
		}
	}
}

int main()
{
	initNcurse();
	gamPic();
	
	getch();
	endwin();
	return 0;
}
//实现的贪吃蛇地图和上面一样,只不过是优化了一下代码

三、显示贪吃蛇身子

3.1 显示贪吃蛇身子的一个节点:

#include <curses.h>

struct Snake
{
	int hang;
	int lie;
	struct Snake *next;
};

struct Snake node1 = {2,2,NULL};

void initNcurse()
{
	initscr();
	keypad(stdscr,1);
}

void gamPic()
{
	int hang;
	int lie;
	
	for(hang=0; hang<20; hang++){
		if(hang == 0){
			for(lie=0; lie<20; lie++){
				printw("--");
			}
			printw("\n");
		}
		
		if(hang>=0 && hang<=19) {
			for(lie=0; lie<=20; lie++){
				if(lie == 0 || lie == 20){
					printw("|");
				}else if(node1.hang == hang && node1.lie == lie){
					printw("[]");
				}else{
					printw("  ");
				}
			}
			printw("\n");
		}

		if(hang == 19){
			for(lie=0; lie<20; lie++){
				printw("--");
			}
			printw("\n");
			printw("By ShiYaHao!\n");
		}
	}
}

int main()
{
	initNcurse();
	gamPic();
	
	getch();
	endwin();
	return 0;
}

3.2 显示贪吃蛇完整身子:

#include <curses.h>

struct Snake
{
	int hang;
	int lie;
	struct Snake *next;
};

struct Snake node1 = {2,2,NULL};
struct Snake node2 = {2,3,NULL};
struct Snake node3 = {2,4,NULL};
struct Snake node4 = {2,5,NULL};

void initNcurse()
{
	initscr();
	keypad(stdscr,1);
}

int hasSnakeNode(int i, int j)
{
	struct Snake *p = &node1;

	while(p != NULL){
		if(p->hang == i && p->lie == j){
			return 1;
		}
		p = p->next;
	}
	return 0;
}

void gamPic()
{
	int hang;
	int lie;
	
	for(hang=0; hang<20; hang++){
		if(hang == 0){
			for(lie=0; lie<20; lie++){
				printw("--");
			}
			printw("\n");
		}
		
		if(hang>=0 && hang<=19) {
			for(lie=0; lie<=20; lie++){
				if(lie == 0 || lie == 20){
					printw("|");
				}else if(hasSnakeNode(hang,lie)){
					printw("[]");
				}else{
					printw("  ");
				}
			}
			printw("\n");
		}

		if(hang == 19){
			for(lie=0; lie<20; lie++){
				printw("--");
			}
			printw("\n");
			printw("By ShiYaHao!\n");
		}
	}
}

int main()
{
	initNcurse();

	node1.next = &node2;
	node2.next = &node3;
	node3.next = &node4;

	gamPic();
	
	getch();
	endwin();
	return 0;
}

3.3 显示贪吃蛇完整身子改进:

#include <curses.h>
#include <stdlib.h>

struct Snake
{
	int hang;
	int lie;
	struct Snake *next;
};

struct Snake *head = NULL;	//指向链表头
struct Snake *tail = NULL;	//指向链表尾

void initNcurse()
{
	initscr();
	keypad(stdscr,1);
}

int hasSnakeNode(int i, int j)
{
	struct Snake *p = head;

	while(p != NULL){
		if(p->hang == i && p->lie == j){
			return 1;
		}
		p = p->next;
	}
	return 0;
}

void gamPic()				//地图规划
{
	int hang;
	int lie;
	
	for(hang=0; hang<20; hang++){
		if(hang == 0){
			for(lie=0; lie<20; lie++){
				printw("--");
			}
			printw("\n");
		}
		
		if(hang>=0 && hang<=19) {
			for(lie=0; lie<=20; lie++){
				if(lie == 0 || lie == 20){
					printw("|");
				}else if(hasSnakeNode(hang,lie)){
					printw("[]");
				}else{
					printw("  ");
				}
			}
			printw("\n");
		}

		if(hang == 19){
			for(lie=0; lie<20; lie++){
				printw("--");
			}
			printw("\n");
			printw("By ShiYaHao!\n");
		}
	}
}

void addNode()
{
	struct Snake *new =(struct Snake *) malloc(sizeof(struct Snake));	//创建新节点
	if(new == NULL){
		printw("malloc error\n");
	}
	new->hang = tail->hang;		//新节点的行等于链表尾的行
	new->lie  = tail->lie+1;	//新节点的行等于链表尾的列+1
	new->next = NULL;

	tail->next = new;			//从链表尾部插入新节点
	tail = new;					//新节点当作尾部
}

void initSnake()
{
	head = (struct Snake *)malloc(sizeof(struct Snake));	//创建链表头
	if(head == NULL){
		printw("malloc error\n");
	}
	head->hang = 2;			
	head->lie = 2;
	head->next = NULL;

	tail = head;		//第一个节点链表头和链表尾是一样的

	addNode();			//调用一次代表增加一个节点
	addNode();
	addNode();
}

int main()
{
	initNcurse();
	
	initSnake();

	gamPic();
	
	getch();
	endwin();
	return 0;
}

四、贪吃蛇移动

4.1 按下▶贪吃蛇向右移动:

#include <curses.h>
#include <stdlib.h>

struct Snake
{
	int hang;
	int lie;
	struct Snake *next;
};

struct Snake *head = NULL;
struct Snake *tail = NULL;

void initNcurse()
{
	initscr();
	keypad(stdscr,1);
}

int hasSnakeNode(int i, int j)
{
	struct Snake *p = head;

	while(p != NULL){
		if(p->hang == i && p->lie == j){
			return 1;
		}
		p = p->next;
	}
	return 0;
}

void gamPic()
{
	int hang;
	int lie;
	
	move(0,0);
		
	for(hang=0; hang<20; hang++){
		if(hang == 0){
			for(lie=0; lie<20; lie++){
				printw("--");
			}
			printw("\n");
		}
		
		if(hang>=0 && hang<=19) {
			for(lie=0; lie<=20; lie++){
				if(lie == 0 || lie == 20){
					printw("|");
				}else if(hasSnakeNode(hang,lie)){
					printw("[]");
				}else{
					printw("  ");
				}
			}
			printw("\n");
		}

		if(hang == 19){
			for(lie=0; lie<20; lie++){
				printw("--");
			}
			printw("\n");
			printw("By ShiYaHao!\n");
		}
	}
}

void addNode()
{
	struct Snake *new =(struct Snake *) malloc(sizeof(struct Snake));
	
	new->hang = tail->hang;
	new->lie  = tail->lie+1;
	new->next = NULL;

	tail->next = new;
	tail = new;
}

void initSnake()
{
	head = (struct Snake *)malloc(sizeof(struct Snake));
	if(head == NULL){
		printw("malloc error\n");
	}
	head->hang = 2;
	head->lie = 2;
	head->next = NULL;

	tail = head;

	addNode();
	addNode();
	addNode();
	addNode();
}

void deletNode()
{
	struct Snake *p;
	p = head;
	
	head = head->next;
	free(p);
}

void moveSnake()
{
	addNode();		//增加一个节点
	deletNode();	//删除头节点
}

int main()
{
	int con;

	initNcurse();
	initSnake();

	gamPic();
	
	while(1){
		con = getch();			//con获取键值
		if(con == KEY_RIGHT){	//如果是右键
			moveSnake();		//向右移动
			gamPic();			//必须刷新一下界面,否则看不到🐍移动
		}		
	}
	getch();
	endwin();
	return 0;
 }

4.2 贪吃蛇撞墙重新开始: 

#include <curses.h>
#include <stdlib.h>

struct Snake
{
	int hang;
	int lie;
	struct Snake *next;
};

struct Snake *head = NULL;
struct Snake *tail = NULL;

void initNcurse()
{
	initscr();
	keypad(stdscr,1);
}

int hasSnakeNode(int i, int j)
{
	struct Snake *p = head;

	while(p != NULL){
		if(p->hang == i && p->lie == j){
			return 1;
		}
		p = p->next;
	}
	return 0;
}

void gamPic()
{
	int hang;
	int lie;
	
	move(0,0);
		
	for(hang=0; hang<20; hang++){
		if(hang == 0){
			for(lie=0; lie<20; lie++){
				printw("--");
			}
			printw("\n");
		}
		
		if(hang>=0 && hang<=19) {
			for(lie=0; lie<=20; lie++){
				if(lie == 0 || lie == 20){
					printw("|");
				}else if(hasSnakeNode(hang,lie)){
					printw("[]");
				}else{
					printw("  ");
				}
			}
			printw("\n");
		}

		if(hang == 19){
			for(lie=0; lie<20; lie++){
				printw("--");
			}
			printw("\n");
			printw("By ShiYaHao!\n");
		}
	}
}

void addNode()
{
	struct Snake *new =(struct Snake *) malloc(sizeof(struct Snake));
	if(new == NULL){
		printw("malloc error\n");
	}
	new->hang = tail->hang;
	new->lie  = tail->lie+1;
	new->next = NULL;

	tail->next = new;
	tail = new;
}

void initSnake()
{
	struct Snake *p;
	while(head != NULL){	判断蛇是否为空,清理内存 
		p = head;
		head = head->next;
		free(p);
	}

	head = (struct Snake *)malloc(sizeof(struct Snake));
	if(head == NULL){
		printw("malloc error\n");
	}
	head->hang = 1;
	head->lie = 1;
	head->next = NULL;

	tail = head;

	addNode();
	addNode();
	addNode();
	addNode();
}

void deletNode()
{
	struct Snake *p;
	p = head;
	
	head = head->next;
	free(p);
}

void moveSnake()
{
	addNode();
	deletNode();
	
    //判断蛇的尾巴碰到上下左右的四个边框后就重新开始
	if(tail->hang == 0 || tail->lie == 0 || tail->hang == 20 || tail->lie == 20){
		initSnake();
	}
}

int main()
{
	int con;

	initNcurse();
	initSnake();

	gamPic();
	
	while(1){
		con = getch();
		if(con == KEY_RIGHT){
			moveSnake();
			gamPic();
		}		
	}
	getch();
	endwin();
	return 0;
 }

4.3 贪吃蛇脱缰自由向右行走

#include <curses.h>
#include <stdlib.h>

struct Snake
{
	int hang;
	int lie;
	struct Snake *next;
};

struct Snake *head = NULL;
struct Snake *tail = NULL;

void initNcurse()
{
	initscr();
	keypad(stdscr,1);
}

int hasSnakeNode(int i, int j)
{
	struct Snake *p = head;

	while(p != NULL){
		if(p->hang == i && p->lie == j){
			return 1;
		}
		p = p->next;
	}
	return 0;
}

void gamPic()
{
	int hang;
	int lie;
	
	move(0,0);
		
	for(hang=0; hang<20; hang++){
		if(hang == 0){
			for(lie=0; lie<20; lie++){
				printw("--");
			}
			printw("\n");
		}
		
		if(hang>=0 && hang<=19) {
			for(lie=0; lie<=20; lie++){
				if(lie == 0 || lie == 20){
					printw("|");
				}else if(hasSnakeNode(hang,lie)){
					printw("[]");
				}else{
					printw("  ");
				}
			}
			printw("\n");
		}

		if(hang == 19){
			for(lie=0; lie<20; lie++){
				printw("--");
			}
			printw("\n");
			printw("By ShiYaHao!\n");
		}
	}
}

void addNode()
{
	struct Snake *new =(struct Snake *) malloc(sizeof(struct Snake));
	if(new == NULL){
		printw("malloc error\n");
	}
	new->hang = tail->hang;
	new->lie  = tail->lie+1;
	new->next = NULL;

	tail->next = new;
	tail = new;
}

void initSnake()
{
	struct Snake *p;
	while(head != NULL){
		p = head;
		head = head->next;
		free(p);
	}

	head = (struct Snake *)malloc(sizeof(struct Snake));
	if(head == NULL){
		printw("malloc error\n");
	}
	head->hang = 1;
	head->lie = 1;
	head->next = NULL;

	tail = head;

	addNode();
	addNode();
	addNode();
	addNode();
}

void deletNode()
{
	struct Snake *p;
	p = head;
	
	head = head->next;
	free(p);
}

void moveSnake()
{
	addNode();
	deletNode();

	if(tail->hang == 0 || tail->lie == 0 || tail->hang == 20 || tail->lie == 20){
		initSnake();
	}
}

int main()
{
	int con;

	initNcurse();
	initSnake();

	gamPic();
	
	while(1){	//之前受方向键控制,现在自由行走
			moveSnake();	
			gamPic();	
			refresh();			//刷新界面
			usleep(100000);		//延时100ms
	}
	getch();
	endwin();
	return 0;
 }

五、Linux线程引入

5.1 贪吃蛇方向移动和刷新界面一起实现面临的问题:

#include <curses.h>
#include <stdlib.h>

struct Snake
{
	int hang;
	int lie;
	struct Snake *next;
};

struct Snake *head = NULL;
struct Snake *tail = NULL;

void initNcurse()
{
	initscr();
	keypad(stdscr,1);
}

int hasSnakeNode(int i, int j)
{
	struct Snake *p = head;

	while(p != NULL){
		if(p->hang == i && p->lie == j){
			return 1;
		}
		p = p->next;
	}
	return 0;
}

void gamPic()
{
	int hang;
	int lie;
	
	move(0,0);
		
	for(hang=0; hang<20; hang++){
		if(hang == 0){
			for(lie=0; lie<20; lie++){
				printw("--");
			}
			printw("\n");
		}
		
		if(hang>=0 && hang<=19) {
			for(lie=0; lie<=20; lie++){
				if(lie == 0 || lie == 20){
					printw("|");
				}else if(hasSnakeNode(hang,lie)){
					printw("[]");
				}else{
					printw("  ");
				}
			}
			printw("\n");
		}

		if(hang == 19){
			for(lie=0; lie<20; lie++){
				printw("--");
			}
			printw("\n");
			printw("By ShiYaHao!\n");
		}
	}
}

void addNode()
{
	struct Snake *new =(struct Snake *) malloc(sizeof(struct Snake));
	if(new == NULL){
		printw("malloc error\n");
	}
	new->hang = tail->hang;
	new->lie  = tail->lie+1;
	new->next = NULL;

	tail->next = new;
	tail = new;
}

void initSnake()
{
	struct Snake *p;
	while(head != NULL){
		p = head;
		head = head->next;
		free(p);
	}

	head = (struct Snake *)malloc(sizeof(struct Snake));
	if(head == NULL){
		printw("malloc error\n");
	}
	head->hang = 1;
	head->lie = 1;
	head->next = NULL;

	tail = head;

	addNode();
	addNode();
	addNode();
	addNode();
}

void deletNode()
{
	struct Snake *p;
	p = head;
	
	head = head->next;
	free(p);
}

void moveSnake()
{
	addNode();
	deletNode();

	if(tail->hang == 0 || tail->lie == 0 || tail->hang == 20 || tail->lie == 20){
		initSnake();
	}
}

int main()
{
	int key;

	initNcurse();
	initSnake();

	gamPic();
	
	while(1){
			moveSnake();
			gamPic();
			refresh();
			usleep(100000);
	}
	while(1){
		
		key = getch();
		switch(key){
			case KEY_DOWN:
				printw("DOWN\n");
				break;	
			case KEY_UP:
				printw("UP\n");
				break;
			case KEY_LEFT:
				printw("LEFT\n");
				break;
			case KEY_RIGHT:
				printw("RIGHT\n");
				break;
		}
	}	
	getch();
	endwin();
	return 0;
 }
  • 在上面的程序中main函数中有两个while(1)循环,这样就会出现问题,程序运行的现象是:获取按键值的这个while循环根本不会执行,那该如何解决?于是引入“Linux线程”!

  • 在贪吃蛇运动过程中,我们需要改变蛇的移动方向,这是就需要不停扫描键盘输入的值来判断方向,同时还需要不停的刷新界面,为了多个while循环并存这里需要引入linux线程。

5.2 线程的基本用法:

#include <pthread.h>  // 头文件

pthread_t:当前Linux中可理解为:typedef  unsigned long int  pthread_t;
如:pthread_t t1;  //多线程定义

pthread_create(&t1,NULL,refreshInterface,NULL); 
参数1:传出参数,保存系统为我们分配好的线程ID
参数2:通常传NULL,表示使用线程默认属性。若想使用具体属性也可以修改该参数。
参数3:函数指针,指向线程主函数(线程体),该函数运行结束,则线程结束。
参数4:线程主函数执行期间所使用的参数,如要传多个参数, 可以用结构封装。

使用多线程的函数必须返回指针型,如void *refreshInterface()

注:gcc xxx.c -lcurses -lpthead  //编译需要连接pthead库

5.3 线程demo案例:

/*
	在这个程序当中只有func1一个函数会被执行,func2函数根本不会执行
	想要解决这个问题就需要引入Linux的线程
*/
#include <stdio.h>

void pfunc1()
{
    while(1){
         printf("this is a pfunc1\n");
       	 sleep(1);
    } 
}

void pfunc2()
{
    while(1){
         printf("this is a pfunc2\n");
       	 sleep(1);
    } 
}

int main()
{
    pfunc1();
    pfunc2();
    return 0;
}
/*
	引入Linux线程修改代码,func1和func2两个函数都可以执行
*/
#include <stdio.h>
#include <pthread.h>	//线程头文件

void* func1()
{
	while(1){

		printf("this is a func1\n");
		sleep(1);
	}
	
}

void* func2()
{
	while(1){

		printf("this is a func2\n");
		sleep(1);
	}
}

int main()
{
	pthread_t th1;	//定义一个th1线程
	pthread_t th2;	//定义一个th2线程

	pthread_create(&th1, NULL, func1, NULL);
	pthread_create(&th2, NULL, func2, NULL);
	while(1);
	return 0;
}

5.4 使用线程解决贪吃蛇方向移动和刷新界面一起实现面临的问题:

#include <curses.h>
#include <stdlib.h>

struct Snake
{
	int hang;
	int lie;
	struct Snake *next;
};

struct Snake *head = NULL;
struct Snake *tail = NULL;

int key;

void initNcurse()
{
	initscr();
	keypad(stdscr,1);
}

int hasSnakeNode(int i, int j)
{
	struct Snake *p = head;

	while(p != NULL){
		if(p->hang == i && p->lie == j){
			return 1;
		}
		p = p->next;
	}
	return 0;
}

void gamPic()
{
	int hang;
	int lie;
	
	move(0,0);
		
	for(hang=0; hang<20; hang++){
		if(hang == 0){
			for(lie=0; lie<20; lie++){
				printw("--");
			}
			printw("\n");
		}
		
		if(hang>=0 && hang<=19) {
			for(lie=0; lie<=20; lie++){
				if(lie == 0 || lie == 20){
					printw("|");
				}else if(hasSnakeNode(hang,lie)){
					printw("[]");
				}else{
					printw("  ");
				}
			}
			printw("\n");
		}

		if(hang == 19){
			for(lie=0; lie<20; lie++){
				printw("--");
			}
			printw("\n");
			printw("By ShiYaHao!key = %d\n",key);
		}
	}
}

void addNode()
{
	struct Snake *new =(struct Snake *) malloc(sizeof(struct Snake));

	new->hang = tail->hang;
	new->lie  = tail->lie+1;
	new->next = NULL;

	tail->next = new;
	tail = new;
}

void initSnake()
{
	struct Snake *p;
	while(head != NULL){
		p = head;
		head = head->next;
		free(p);
	}

	head = (struct Snake *)malloc(sizeof(struct Snake));

	head->hang = 1;
	head->lie = 1;
	head->next = NULL;

	tail = head;

	addNode();
	addNode();
	addNode();
	addNode();
}

void deletNode()
{
	struct Snake *p;
	p = head;
	
	head = head->next;
	free(p);
}

void moveSnake()
{
	addNode();
	deletNode();

	if(tail->hang == 0 || tail->lie == 0 || tail->hang == 20 || tail->lie == 20){
		initSnake();
	}
}

void* refreshJieMian()
{
	
	while(1){
			moveSnake();
			gamPic();
			refresh();
			usleep(100000);
	}
}

void* changeDir()
{
	
	while(1){
		
		key = getch();
		switch(key){
			case KEY_DOWN:
				printw("DOWN\n");
				break;	
			case KEY_UP:
				printw("UP\n");
				break;
			case KEY_LEFT:
				printw("LEFT\n");
				break;
			case KEY_RIGHT:
				printw("RIGHT\n");
				break;
		}
	}	
}

int main()
{
	initNcurse();
	initSnake();
	
    //注意:线程创建要放在初始化后面,不然就会导致程序段错误(答疑老师解决)
	pthread_t t1;
	pthread_t t2;
	pthread_create(&t1, NULL, refreshJieMian, NULL);
	pthread_create(&t2, NULL, changeDir, NULL);

	gamPic();
	
	while(1);
	getch();
	endwin();
	return 0;
 }

  • 蛇在向右移动的同时也可以按方向键,这就是引入线程之后的牛逼之处!

六、贪吃蛇跑起来

6.1 实现贪吃蛇四方向的风骚走位:

#include <curses.h>
#include <stdlib.h>
#include <pthread.h>

#define UP    1
#define DOWN  2
#define LEFT  3
#define RIGHT 4

struct Snake
{
	int hang;
	int lie;
	struct Snake *next;
};

struct Snake *head = NULL;
struct Snake *tail = NULL;

int key;
int dir;

void initNcurse()
{
	initscr();
	keypad(stdscr,1);
}

int hasSnakeNode(int i, int j)
{
	struct Snake *p = head;

	while(p != NULL){
		if(p->hang == i && p->lie == j){
			return 1;
		}
		p = p->next;
	}
	return 0;
}

void gamPic()
{
	int hang;
	int lie;
	
	move(0,0);
		
	for(hang=0; hang<20; hang++){
		if(hang == 0){
			for(lie=0; lie<20; lie++){
				printw("--");
			}
			printw("\n");
		}
		
		if(hang>=0 && hang<=19) {
			for(lie=0; lie<=20; lie++){
				if(lie == 0 || lie == 20){
					printw("|");
				}else if(hasSnakeNode(hang,lie)){
					printw("[]");:
				}else{
					printw("  ");
				}
			}
			printw("\n");
		}

		if(hang == 19){
			for(lie=0; lie<20; lie++){
				printw("--");
			}
			printw("\n");
			printw("By ShiYaHao!key = %d\n",key);
		}
	}
}

void addNode()
{
	struct Snake *new =(struct Snake *) malloc(sizeof(struct Snake));

	new->hang = tail->hang;
	new->lie  = tail->lie+1;
	new->next = NULL;

	switch(dir){
		case UP:	
      		new->hang = tail->hang-1;
			new->lie  = tail->lie;
			break;
		case DOWN:	
      		new->hang = tail->hang+1;
			new->lie  = tail->lie;
			break;
		case LEFT:	
      		new->hang = tail->hang;
			new->lie  = tail->lie-1;
			break;
		case RIGHT:	
      		new->hang = tail->hang;
			new->lie  = tail->lie+1;
			break;
	}

	tail->next = new;
	tail = new;
}

void initSnake()
{
	struct Snake *p;

	dir = RIGHT;
	
	while(head != NULL){
		p = head;
		head = head->next;
		free(p);
	}

	head = (struct Snake *)malloc(sizeof(struct Snake));

	head->hang = 1;
	head->lie = 1;
	head->next = NULL;

	tail = head;

	addNode();
	addNode();
	addNode();
	addNode();
}

void deletNode()
{
	struct Snake *p;
	p = head;
	
	head = head->next;
	free(p);
}

void moveSnake()
{
	addNode();
	deletNode();

	if(tail->hang == 0 || tail->lie == 0 || tail->hang == 20 || tail->lie == 20){
		initSnake();
	}
}

void* refreshJieMian()
{
	
	while(1){
			moveSnake();
			gamPic();
			refresh();
			usleep(100000);
	}
}

void* changeDir()
{
	
	while(1){
		
		key = getch();
		switch(key){
			case KEY_DOWN:
				dir = DOWN;
				break;	
			case KEY_UP:
				dir = UP;
				break;
			case KEY_LEFT:
				dir = LEFT;
				break;
			case KEY_RIGHT:
				dir = RIGHT;
				break;
		}
	}	
}

int main()
{
	pthread_t t1;
	pthread_t t2;

	initNcurse();
	initSnake();

	gamPic();

	pthread_create(&t1, NULL, refreshJieMian, NULL);
	pthread_create(&t2, NULL, changeDir, NULL);

	
	while(1);
	getch();
	endwin();
	return 0;
 }

6.2 用绝对值方式来解决不合理的走位:

#include <curses.h>
#include <stdlib.h>

#define UP     1
#define DOWN  -1
#define LEFT   2
#define RIGHT -2

struct Snake
{
	int hang;
	int lie;
	struct Snake *next;
};

struct Snake *head = NULL;
struct Snake *tail = NULL;

int key;
int dir;

void initNcurse()
{
	initscr();
	keypad(stdscr,1);
	noecho();
}

int hasSnakeNode(int i, int j)
{
	struct Snake *p = head;

	while(p != NULL){
		if(p->hang == i && p->lie == j){
			return 1;
		}
		p = p->next;
	}
	return 0;
}

void gamPic()
{
	int hang;
	int lie;
	
	move(0,0);
		
	for(hang=0; hang<20; hang++){
		if(hang == 0){
			for(lie=0; lie<20; lie++){
				printw("--");
			}
			printw("\n");
		}
		
		if(hang>=0 && hang<=19) {
			for(lie=0; lie<=20; lie++){
				if(lie == 0 || lie == 20){
					printw("|");
				}else if(hasSnakeNode(hang,lie)){
					printw("[]");
				}else{
					printw("  ");
				}
			}
			printw("\n");
		}

		if(hang == 19){
			for(lie=0; lie<20; lie++){
				printw("--");
			}
			printw("\n");
			printw("By ShiYaHao!key = %d\n",key);
		}
	}
}

void addNode()
{
	struct Snake *new =(struct Snake *) malloc(sizeof(struct Snake));

	new->hang = tail->hang;
	new->lie  = tail->lie+1;
	new->next = NULL;

	switch(dir){
		case UP:	
      			new->hang = tail->hang-1;
			new->lie  = tail->lie;
			break;
		case DOWN:	
      			new->hang = tail->hang+1;
			new->lie  = tail->lie;
			break;
		case LEFT:	
      			new->hang = tail->hang;
			new->lie  = tail->lie-1;
			break;
		case RIGHT:	
      			new->hang = tail->hang;
			new->lie  = tail->lie+1;
			break;
	}

	tail->next = new;
	tail = new;
}

void initSnake()
{
	struct Snake *p;

	dir = RIGHT;
	
	while(head != NULL){
		p = head;
		head = head->next;
		free(p);
	}

	head = (struct Snake *)malloc(sizeof(struct Snake));

	head->hang = 1;
	head->lie = 1;
	head->next = NULL;

	tail = head;

	addNode();
	addNode();
	addNode();
	addNode();
}

void deletNode()
{
	struct Snake *p;
	p = head;
	
	head = head->next;
	free(p);
}

void moveSnake()
{
	addNode();
	deletNode();

	if(tail->hang == 0 || tail->lie == 0 || tail->hang == 20 || tail->lie == 20){
		initSnake();
	}
}

void refreshJieMian()
{
	
	while(1){
			moveSnake();
			gamPic();
			refresh();
			usleep(100000);
	}
}

void turn(int direction)			通过绝对值判断相反方向不触发
{
	if(abs(dir) != abs(direction)){
		dir = direction;
	}
}

void changeDir()
{
	
	while(1){
		
		key = getch();
		switch(key){
			case KEY_DOWN:
				turn(DOWN);
				break;	
			case KEY_UP:
				turn(UP);
				break;
			case KEY_LEFT:
				turn(LEFT);
				break;
			case KEY_RIGHT:
				turn(RIGHT);
				break;
		}
	}	
}

int main()
{
	pthread_t t1;
	pthread_t t2;

	initNcurse();
	initSnake();

	gamPic();

	pthread_create(&t1, NULL, refreshJieMian, NULL);
	pthread_create(&t2, NULL, changeDir, NULL);

	
	while(1);
	getch();
	endwin();
	return 0;
 }

6.3 贪吃蛇吃饭了(食物的位置是随机的):

#include <curses.h>
#include <stdlib.h>
#include <pthread.h>

#define UP     1
#define DOWN  -1
#define LEFT   2
#define RIGHT -2

struct Snake
{
	int hang;
	int lie;
	struct Snake *next;
};

struct Snake *head = NULL;
struct Snake *tail = NULL;

int key;
int dir;

struct Snake food;

void initFood()
{
	int x = rand()%20;
	int y = rand()%20;

	food.hang = x;
	food.lie  = y;
}

void initNcurse()
{
	initscr();
	keypad(stdscr,1);
	noecho();
}

int hasSnakeNode(int i, int j)
{
	struct Snake *p = head;

	while(p != NULL){
		if(p->hang == i && p->lie == j){
			return 1;
		}
		p = p->next;
	}
	return 0;
}

int hasFood(int i, int j)
{
	if(food.hang == i && food.lie == j){
		return 1;
	}
	return 0;
}

void gamPic()
{
	int hang;
	int lie;
	
	move(0,0);
		
	for(hang=0; hang<20; hang++){
		if(hang == 0){
			for(lie=0; lie<20; lie++){
				printw("--");
			}
			printw("\n");
		}
		
		if(hang>=0 && hang<=19) {
			for(lie=0; lie<=20; lie++){
				if(lie == 0 || lie == 20){
					printw("|");
				}else if(hasSnakeNode(hang,lie)){
					printw("[]");
				}else if(hasFood(hang,lie)){
					printw("##");
				}else{
					printw("  ");
				}
			}
			printw("\n");
		}

		if(hang == 19){
			for(lie=0; lie<20; lie++){
				printw("--");
			}
			printw("\n");
			printw("By ShiYaHao! food.hang = %d,food.lie = %d\n",food.hang,food.lie);
		}
	}
}

void addNode()
{
	struct Snake *new =(struct Snake *) malloc(sizeof(struct Snake));

	new->hang = tail->hang;
	new->lie  = tail->lie+1;
	new->next = NULL;

	switch(dir){
		case UP:	
      			new->hang = tail->hang-1;
			new->lie  = tail->lie;
			break;
		case DOWN:	
      			new->hang = tail->hang+1;
			new->lie  = tail->lie;
			break;
		case LEFT:	
      			new->hang = tail->hang;
			new->lie  = tail->lie-1;
			break;
		case RIGHT:	
      			new->hang = tail->hang;
			new->lie  = tail->lie+1;
			break;
	}

	tail->next = new;
	tail = new;
}

void initSnake()
{
	struct Snake *p;

	dir = RIGHT;
	
	while(head != NULL){
		p = head;
		head = head->next;
		free(p);
	}
	initFood();
	head = (struct Snake *)malloc(sizeof(struct Snake));

	head->hang = 1;
	head->lie = 1;
	head->next = NULL;

	tail = head;

	addNode();
	addNode();
	addNode();
	addNode();
}

void deletNode()
{
	struct Snake *p;
	p = head;
	
	head = head->next;
	free(p);
}

void moveSnake()
{
	addNode();
	if(hasFood(tail->hang,tail->lie)){
		initFood();
	}else{	
		deletNode();
	}

	if(tail->hang < 0 || tail->lie == 0 || tail->hang == 20 || tail->lie == 20){
		initSnake();
	}
}

void* refreshJieMian()
{
	
	while(1){
			moveSnake();
			gamPic();
			refresh();
			usleep(100000);
	}
}

void turn(int direction)
{
	if(abs(dir) != abs(direction)){
		dir = direction;
	}
}

void* changeDir()
{
	
	while(1){
		
		key = getch();
		switch(key){
			case KEY_DOWN:
				turn(DOWN);
				break;	
			case KEY_UP:
				turn(UP);
				break;
			case KEY_LEFT:
				turn(LEFT);
				break;
			case KEY_RIGHT:
				turn(RIGHT);
				break;
		}
	}	
}

int main()
{
	pthread_t t1;
	pthread_t t2;

	initNcurse();
	initSnake();

	gamPic();

	pthread_create(&t1, NULL, refreshJieMian, NULL);
	pthread_create(&t2, NULL, changeDir, NULL);

	
	while(1);
	getch();
	endwin();
	return 0;
 }

七、项目代码

#include <curses.h>
#include <stdlib.h>
#include <pthread.h>

#define UP     1
#define DOWN  -1
#define LEFT   2
#define RIGHT -2
 
struct Snake{
	int hang;
	int lie;
	struct Snake *next;
};

struct Snake *head = NULL;
struct Snake *tail = NULL;
struct Snake food;
int key;
int dir;
 
void addNode();                   /*从尾部插入新节点*/

//void initNcurses();               /*ncurses库的初始化函数*/
//void gameMap();                   /*贪吃蛇地图的初始化*/
//int printSnakeNode(int i,int j);  /*在地图上打印贪吃蛇的节点*/
//void initSnake();                 /*初始化贪吃蛇*/

//void deletNode();                 /*删除头结点*/
//void moveSnake();                 /*实现贪吃蛇的移动*/
//void *refreshScreen();            /*线程实现图像刷新*/
//void *changeDir();                /*线程实现贪吃蛇方向的改变*/
//void turn(int direction);         /*防止出现不合理走位*/
//void creatFood();                 /*随机出现食物*/
//int hasFood(int i,int j);         /*打印食物*/
//int ifSnakeDie();                 /*判断贪吃蛇是否死亡*/

 
/*随机出现食物*/
void creatFood()
{
	int x = rand()%20;
	int y = rand()%19+1;
 
	food.hang = x;
	food.lie = y;
}
 
int hasFood(int i,int j)
{
	if(food.hang == i && food.lie == j){
		return 1;
	}
	return 0;
}
 
/*ncurses库的初始化函数*/
void initNcurses()
{
	initscr();//ncurse界面的初始化函数
	keypad(stdscr,1);//使用keypad函数,才可以使用键盘功能键
	noecho();//防止打印无关键值
}
 
/*贪吃蛇地图的初始化*/
void gameMap()
{
	int hang;
	int lie;
 
	move(0,0);//把光标的位置移到头,实现地图刷新时的覆盖
	for(hang=0;hang<20;hang++){
		if(hang == 0){
			for(lie=0;lie<20;lie++){
				printw("--");
			}
			printw("\n");
		}
		if(hang>=0 && hang<=19){
			for(lie=0;lie<=20;lie++){
				if(lie == 0 || lie == 20){
					printw("|");
				}else if(printSnakeNode(hang,lie)){
					printw("[]");
				}else if(hasFood(hang,lie)){
					printw("##");
				}else{
					printw("  ");
				}
			}
			printw("\n");
		}
		if(hang == 19){
			for(lie=0;lie<20;lie++){
				printw("--");
			}
			printw("\n");
		}
	}
		printw("By ShiYaHao!,food.hang = %d,food.lie = %d\n",food.hang,food.lie);
}
 
/*在地图上打印贪吃蛇的节点*/
int printSnakeNode(int i,int j)
{
	struct Snake *p = head;
	while(p != NULL){
		if(p->hang == i && p->lie == j){
			return 1;
		}
		p = p->next;
	}
	return 0;
}
 
/*初始化贪吃蛇*/
void initSnake()
{	
	struct Snake *p = NULL;
	if(head != NULL){   //当贪吃蛇死亡后,把多余节点释放
		p = head;
		head = head->next;	
		free(p);
	}
	
	creatFood();
	dir = RIGHT;
 
	head = (struct Snake *)malloc(sizeof(struct Snake));
	head->hang = 1;
	head->lie = 1;
	head->next = NULL;
 
	tail = head;
	
	addNode();
	addNode();
	addNode();
}
 
/*从尾部插入新节点*/
void addNode()
{
	struct Snake *new = (struct Snake *)malloc(sizeof(struct Snake));
 
	switch(dir){
		case UP:
			new->hang = tail->hang-1;
			new->lie = tail->lie;
			break;
		case DOWN:
			new->hang = tail->hang+1;
			new->lie = tail->lie;
			break;
		case LEFT:
			new->hang = tail->hang;
			new->lie = tail->lie-1;
			break;
		case RIGHT:
			new->hang = tail->hang;
			new->lie = tail->lie+1;
			break;
	}
	new->next = NULL;
	tail->next = new;
	tail = new;
 
}
 
/*删除头结点*/
void deletNode()
{
	struct Snake *p = head;
	head = head->next;
	free(p);	
}
 
/*判断贪吃蛇是否死亡*/
int ifSnakeDie()
{
	struct Snake *p;
	p = head;
	if(tail->hang < 0 || tail->hang == 20 || tail->lie == 0 || tail->lie == 20){
		return 1;
	}
 
	while(p->next != NULL){
		if(p->hang == tail->hang && p->lie == tail->lie){
			return 1;
		}
		p = p->next;
	}
	return 0;
 
}
 
/*实现贪吃蛇的移动*/
void moveSnake()
{
	addNode();
	if(hasFood(tail->hang,tail->lie)){
		creatFood();
	}else{
		deletNode();
	}
 
	if(ifSnakeDie()){
		initSnake();
	}
 
}
 
/*线程实现图像刷新*/
void *refreshScreen()
{
	usleep(100000);
	while(1){
		moveSnake();
		gameMap();//刷新地图	
		refresh();//界面刷新函数
		usleep(100000);
	}
}
/*防止不合理走位*/
void turn(int direction)
{
	if(abs(dir) != abs(direction)){
		dir = direction;
	}
}
 
/*线程实现贪吃蛇方向的改变*/
void *changeDir()
{
	while(1){
		key = getch();
		switch(key){
			case KEY_UP:
				turn(UP);
				break;
			case KEY_DOWN:
				turn(DOWN);
				break;
			case KEY_LEFT:
				turn(LEFT);
				break;
			case KEY_RIGHT:
				turn(RIGHT);
				break;
		}
	}
}

int main()
{
	pthread_t t1;
	pthread_t t2;
 
	initNcurses();
	initSnake();
	gameMap();
 
	pthread_create(&t1,NULL,refreshScreen,NULL);
	pthread_create(&t2,NULL,changeDir,NULL);
 
	while(1);
	getch();//等待用户的输入,如果没有这句话,程序就退出了,看不到运行的结果,也就是无法看到上面那句话
	endwin();//程序退出,恢复shell终端的显示,如果没有这句话,shell终端字乱码,坏掉
 
	return 0;
}

更多推荐

深度解析Java虚拟机(JVM)的垃圾回收机制

AI绘画关于SD,MJ,GPT,SDXL百科全书面试题分享点我直达2023Python面试题2023最新面试合集链接2023大厂面试题PDF面试题PDF版本java、python面试题项目实战:AI文本OCR识别最佳实践AIGamma一键生成PPT工具直达链接玩转cloudStudio在线编码神器玩转GPUAI绘画、A

浅谈大数据背景下用户侧用电数据在电力系统的应用与发展分析

安科瑞华楠摘要:随着能源互联网、互联网+、新型传感技术的持续推进,电力用户侧用电数据呈现指数级剧增、异构性增强的情况,逐渐构成了用户侧用电行为大数据。然而目前对电力领域的数据价值挖掘于电网内部和电源端,用户侧庞大且蕴含丰富价值的电力数据而未能得到很好利用。文章主要是在用户侧用电行为大数据的基础上,对用户侧电力数据价值在

机器学习(16)---聚类(KMeans)

聚类一、聚类概述1.1无监督学习与聚类算法1.2sklearn中的聚类算法二、KMeans2.1基本原理2.2簇内误差平方和三、sklearn中的KMeans3.1所用模块3.2聚类算法的模型评估指标3.3轮廓系数3.4CHI(卡林斯基-哈拉巴斯指数)四、KMeans做矢量量化4.1概述4.2案例一、聚类概述1.1无监

负载均衡算法介绍及应用连接池负载不均问题分析

在分布式系统架构下,为了满足高并发和高扩展性的要求,负载均衡设备得以广泛的使用。结合应用连接池的配置,在实际使用过程中可能会出现负载不均的问题。本文简单介绍了负载均衡算法、Druid连接池配置以及连接池负载不均的问题分析及优化方法。1、负载均衡基本概念1.1负载均衡介绍在分布式架构下随着逻辑业务的快速发展,系统架构也随

元宇宙安全与著作权相关市场与技术动态:韩国视角

元宇宙市场动态元宇宙安全与著作权维护技术现状元宇宙有可能为商业创造巨大价值,尤其是在零售和时尚领域。时尚产品的象征性价值不仅在物理空间中得以保持,在虚拟空间中也是如此。通过元宇宙平台,企业可以开发虚拟产品,降低供应链和库存管理的风险。随着虚拟化和触觉技术的发展,消费者可以通过体验产品的物理特性做出更好的购买决策。通过将

go 1.18新特性(泛性 模糊测试 WorkSpace)

文章目录泛型interface的功能扩展泛型使用模糊测试模糊测试的使用模糊测试的缺点GoWorkSpace泛型interface的功能扩展在1.18之前,官方对interface的定义是方法的集合,可以存储实现该方法的任意类型。1.18对interface的定义改为类型集合。接口类型的变量可以存储这个接口类型集合的任意

go-channel

设计原理Go提及的设计模式就是:不要通过共享内存的方式进行通信,而是应该通过通信的方式共享内存。共享内存方式:多个协程共享同一块内存,但是多个协程中读写变量是操作同一块内存,会产生多线程问题的并发问题,所以需要使用互斥锁来实现临界区的互斥访问,会大大影响效率通信方式(go语言使用):channel通道当做通信的中间件队

Go底层总结

Go专家编程常见数据结构实现原理channelchannel主要用于进程内各goroutine间通信,如果需要跨进程通信,建议使用分布式系统的方法来解决向channel写数据如果等待接收队列recvq不为空,说明缓冲区中没有数据或者没有缓冲区,此时直接从recvq取出G,并把数据写入,最后把该G唤醒,结束发送过程;如果

《数字图像处理-OpenCV/Python》连载(7)视频文件的读取与保存

《数字图像处理-OpenCV/Python》连载(7)视频文件的读取与保存本书京东优惠购书链接:https://item.jd.com/14098452.html本书CSDN独家连载专栏:https://blog.csdn.net/youcans/category_12418787.html第1章图像的基本操作为了方便

OpenCV项目实战(2)— 如何用OpenCV实现弹球动画

前言:Hello大家好,我是小哥谈。OpenCV能够在画布上绘制静态的图形,例如,线段、矩形、正方形、圆形、多边形、文字等。那么,能不能让这些静态的图形移动起来?如果能,又该如何编写代码呢?本实例将使用OpenCV提供的绘制圆形的方法,先绘制一个实心圆,再让这个实心圆在画布上移动起来,呈现“弹球动画”的视觉效果。🌈目

Routing路径系列数学建模(TSP+CVRP)

1.TravelingSalespersonProblem(TSP)参考:维基百科TSP给定一些城市和城市之间的距离,找到最短路径,经过每个城市最后返回起点,组合优化问题中属于NP-hard难度。对于TSP问题有两类混合整数规划模型:Miller–Tucker–Zemlin(MTZ)形式和Dantzig–Fulkers

热文推荐