【LV7 Flappy Bird】---项目实战

通过链表和curse库,定时器函数实现Flappy Bird项目
使用@替代小鸟,+替代管道,小鸟碰到管道后游戏结束。

项目需求
1.按下空格键小鸟上升, 不按小鸟下落
2. 搭建小鸟需要穿过的管道
3. 管道自动左移和创建
4. 小鸟撞到管道游戏结束
代码实现
#include <stdio.h>
#include<curses.h>
#include <signal.h>
#include<sys/time.h>
#define BIRD '@'
#define BLANK ' '
#define PIPE '+'
#include <stdlib.h>
typedef struct pipe{
	int x;
	int y;
	struct pipe *next;
}pipe_node,*pipe_list;

pipe_list head,tail;

void creat_list();//创建链表
void show_pipe();//显示管道
void clear_pipe();//清除管道
void move_pipe();//移动管道

int bird_y,bird_x;//小鸟坐标
void show_bird();// 显示小鸟
void clear_bird();//清除小鸟
void move_bird();//移动小鸟

void init_curses();//curses库初始化
int set_timer(int ms_t);//设置定时器
void handler(int sig);//信号处理函数
int main(int argc, char *argv[])
{
	bird_y=15;//行
	bird_x=10;//列
	init_curses();
	
	signal(SIGALRM,handler);//每隔0.5s小鸟下落
	set_timer(500);//500ms
	srand(time(0));
	creat_list();
	show_pipe();
	show_bird();
	move_bird();
	

	return 0;
}

void init_curses()//curse界面初始化
{
	initscr();
	curs_set(0);
	noecho();
	keypad(stdscr,1);
	start_color();
}
int set_timer(int ms_t)//定时器函数实现
{
	struct itimerval timer;
	long t_sec,t_usec;
	int ret;
	t_sec=ms_t/1000;
	t_usec=(ms_t%1000) *1000;

	timer.it_value.tv_sec=t_sec;
	timer.it_value.tv_usec=t_usec;

	timer.it_interval.tv_sec=t_sec;
	timer.it_interval.tv_usec=t_usec;

	ret=setitimer(ITIMER_REAL,&timer,NULL);
	return ret;
}
void show_bird()//显示小鸟位置
{
	move(bird_y,bird_x);
	addch(BIRD);
	refresh();

}
void clear_bird()//清除小鸟
{
	move(bird_y,bird_x);
	addch(BLANK);
	refresh();
}
void move_bird()//按下空格键后小鸟上升
{
	char key;
	while(1)
	{
		key=getch();
		if(key==' ')
		{
		clear_bird();
		bird_y--;
		show_bird();
		//游戏结束判断//
		if((char)inch()==PIPE)
		{
		set_timer(0);
		endwin();
		exit(1);		
		}	
		}		
	}
}
void handler(int sig)
{
	pipe_list p,new;
	//小鸟下落//
	int i,j;
	clear_bird();
	bird_y++;
	show_bird();
	
	//游戏结束判断//
		if((char)inch()==PIPE)
		{
		set_timer(0);
		endwin();
		exit(1);		
		}
	p=head->next;
	//管道到达屏幕左端后清除
	if(p->x==0)
	{
		head->next=p->next;
		for(i=p->x;i<p->x+10;i++)
		{
			//上半部分管道//
			for(j=0;j<p->y;j++)
			{
				move(j,i);
				addch(BLANK);
			}
			//下半部分管道//
			for(j=p->y+5;j<25;j++)
			{
				move(j,i);
				addch(BLANK);
			
			}
			refresh();
		}
	free(p);
	//在屏幕右端建立新的管道
	new=(pipe_list)malloc(sizeof(pipe_node));
	new->x=tail->x+20;
	new->y=rand()%11+5;
	new->next=NULL;
	tail->next=new;
	tail=new;

	}
	//管道移动//
	clear_pipe();
	move_pipe();
	show_pipe();


}
void creat_list()//创建链表
{
	int i;
	pipe_list p,new;
	head=(pipe_list)malloc(sizeof(pipe_node));
	head->next=NULL;
	p=head;
	//创建五个管道//
	for(i=0;i<5;i++)
	{
		new=(pipe_list)malloc(sizeof(pipe_node));
		new->x=(i+1)*20;
		new->y=rand()%11+5;
		new->next=NULL;
		p->next=new;
		p=new;
	}
	tail=p;
}
void show_pipe()//显示管道
{
	pipe_list p;
	int i,j;
	p=head->next;
	while(p)
	{
		for(i=p->x;i<p->x+10;i++)
		{
			//上半部分管道//
			for(j=0;j<p->y;j++)
			{
				move(j,i);
				addch(PIPE);
			}
			//下半部分管道//
			for(j=p->y+5;j<25;j++)
			{
				move(j,i);
				addch(PIPE);
			
			}
		}
		refresh();
		p=p->next;
	}

}
void clear_pipe()//清除管道
{	
	pipe_list p;
	int i,j;
	p=head->next;
	while(p)
	{
		for(i=p->x;i<p->x+10;i++)
		{
			//上半部分管道//
			for(j=0;j<p->y;j++)
			{
				move(j,i);
				addch(BLANK);
			}
			//下半部分管道//
			for(j=p->y+5;j<25;j++)
			{
				move(j,i);
				addch(BLANK);
			
			}
		}
		refresh();
		p=p->next;
	}



}
	
	
void move_pipe()//移动管道
{
	pipe_list p;
	p=head->next;
	while(p)
	{	
	p->x--;
	p=p->next;
	}
}
实现效果如图所示:

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值