基于ncurse的贪吃蛇游戏

本文介绍了如何使用ncurses库逐步开发一个贪吃蛇游戏,包括按键处理、地图绘制、蛇身创建与移动、食物生成及碰撞检测等功能,通过这个项目加深了对链表的运用和程序优化技巧的理解。
#include<curses.h>   //使用ncurse
#include<stdlib.h>
#include<pthread.h>   //使用多线程
#include<stdio.h>
#define UP     1     
#define DOWN  -1
#define	LEFT   2
#define RIGHT -2

struct snake          //创建蛇身链表
{
   
   
	int hang;
	int lie;
	struct snake *next;
};

struct snake food;        
struct snake *head=NULL;
struct snake *tall=NULL;
int dir;
int key;

void initfood()          //食物初始化
{
   
   
	int x=rand()%20;
	int y=rand()%21;
	struct snake *p=head;
	while(p!=NULL){
   
   
		
		if(p->hang==x&&p->lie==y){
   
   
			  x=rand()%20;
		          y=rand()%21;
			  
		  	  p=head;
       			  continue;
		}
		p=p->next;
		
	}
	food.hang=x;
	while(y==0){
   
   
		y=rand()%21;
	}
	food.lie=y;
}

int hassnakenode(int i,int j)     //判断地图中该位置是否有蛇身
{
   
   
	struct snake *p;
	p=head
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值