flappy bird

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#include<time.h>
 
//全局变量
int high,width;           //游戏画面大小
int bird_x,bird_y;        //小鸟位置
int barl_x[5],barl_y[5];      //障碍物
int score;                //得分 
int speed = 0;                //下落速度 

void gotoxy(int x,int y)            //将光标移动到(x,y)位置
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(handle,pos);
 } 
 
 void HideCursor()
{
	CONSOLE_CURSOR_INFO cursor_info={1,0};
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
}
 void startup()          //初始化
 {
 	high =20;
 	width = 50;
 	bird_x = high/2;
 	bird_y = 3;
    for(int i=0;i<5;i++)
    {
    	barl_x[i] = rand()%14+3;
    	barl_y[i] = width+10*i;
	}
  } 
  
  void show()              //显示画面
  {
  	gotoxy(0,0);           //重画清屏
	int i,j,m;
	for(i=0;i<high;i++)
	{
		for(j=0;j<width;j++)
		{   
		    if((j==barl_y[0])&&((i<barl_x[0]-2)||(i>barl_x[0]+2)))
		    printf("*");
		    else if((j==barl_y[1])&&((i<barl_x[1]-2)||(i>barl_x[1]+2)))
		    printf("*");
		    else if((j==barl_y[2])&&((i<barl_x[2]-2)||(i>barl_x[2]+2)))
		    printf("*");
		    else if((j==barl_y[3])&&((i<barl_x[3]-2)||(i>barl_x[3]+2)))
		    printf("*");
		    else if((j==barl_y[4])&&((i<barl_x[4]-2)||(i>barl_x[4]+2)))
		    printf("*");
			else if((i==bird_x)&&(j==bird_y))
			printf("@");
			else
			printf(" ");
		}
		printf("\n");
	 } 
	 printf("得分:%d\n",score);
   } 
   
   void updateWithoutInput()       //与用户输入无关的更新
   {
   	int i;
   	for(i=0;i<5;i++)
   	{
   	if(bird_y==barl_y[i])
   	{
   		if((bird_x>=barl_x[i]-2)&&(bird_x<=barl_x[i]+2))
   		score++;
   		else
   		{
   			printf("游戏失败!\n");
   			Sleep(2000);
   			system("pause");
   			exit(0);
		   }
	   }
	   if(barl_y[i]<=0)           //重新生成障碍物
	   {
	   	barl_y[i] = width;
        barl_x[i] = rand()%14+3;
		} 
	}
	if(bird_x<=0||bird_x>=high)
	{
	printf("游戏失败!\n");
	Sleep(2000);
	system("pause");
	exit(0);
}
	
		Sleep(150);
		bird_x+=speed;
   	    speed++;
   	    for(i=0;i<5;i++)
   	    barl_y[i]--;
	} 
	
	void updateWithInput()           //与用户输入有关的更新
	{
		char input;
		if(kbhit())
		{
			input = getch();
			if(input==' ')
			{
				bird_x-=2;
				speed = 0;
			}
		}
    }
		
		int main()
		{
			srand(time(0));
			HideCursor();
			startup();
			while(1)
			{
				show();
				updateWithoutInput();     
				updateWithInput();
			}
			return 0;
		}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值