c++小游戏:笨鸟先飞(Flappy bird hhh)

这又是一个安静的午后,于是,故事开始了

可能我们小时候都玩过这样一种游戏:
在这里插入图片描述
开居一只鸟,装备。。。额扯远了。。

就是有这样一只疯狂的小鸟,在大街小巷中飞行,不扑打翅膀的话,就会以一定的速度向下掉,当碰上前方来得障碍物时候,就会die,
所以我们就叫他笨鸟吧,所以游戏名字也就是笨鸟先飞(滑稽)

列一个大概的框架在这里插入图片描述

#include<iostream>
#include<cstdlib>
#include<conio.h>
#include<windows.h>    //  gotoxy 和HideCursor的头文件

show函数进行初始化

void show()//开始的初始化(达到循环显示的目的)
{
         gotoxy(0,0) ;
         	HideCursor()   ;
         	for(int i=1;i<y;++i)
         	{
         		for(int j=1;j<x;++j)
         		{
         			if(j==birdx&&i==birdy)
         			{
         				cout<<"\b"<<"鸟"; //\b的作用是因为输出的汉字多占了一个大小的空间,\b删除之前一个位置的输出
					 }
				    else if(j==xx&&(i>=1&&i<=yy)||j==xx&&(i>=yy+5&&i<=y))
					 cout<<"*" ;

					 else
					 cout<<" ";
				 }
				 cout<<endl;
			 }

			 cout<<"得分:"<<count/6<<endl;

}

\b的作用是因为输出的汉字多占了一个大小的空间,\b删除之前一个位置的输出就是删除多输出的哪个空格

和输入有关的,就一个用空格控制的笨鸟扑打翅膀(hhh)

void in()
{
if(kbhit())
{
	   string ss;
	  ss=getch();

		  if(ss==" ")
		  {
			if(birdy>3)birdy-=3;
		  }


}
}

而鸟的下落以及障碍物盗来的速度放在了和输入无关的函数中

void notin()  //改变小鸟的位置和障碍物的位置
{
		if (birdx==xx&& (birdy>=yy&&birdy<=yy+5))
        count+=1;

      if(speed!=3)
      {
      	speed++;
	  }
	   if(birdy!=y&&speed==3)
   {
		   birdy+=1;
		   speed=0;
	}


	  if(speed2!=5)
	  {
	  	speed2++;
	  }
	  else if(speed2==5)
	  {
	  	if(xx>0)
	  {
	  	xx--;

	  }


	  if(xx==0)
	  {
	  	 xx=x/2;//障碍物的x坐标
         yy= rand()%(y/2);
         while(yy==0)
         {
			yy= rand()%(y/2);
		 }

	  }
   speed2=0;
	  
}

speed和speed2分别控制小鸟下落的速度和障碍物的速度,是用一种循环到某个值才进行一个操作,然后再清零speed和speed2来控制速度的,就比较巧妙(滑稽)
而`

void gotoxy(int x,int 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);
}

gotoxy分别用来控制命令行的光标(清屏)和隐藏光标(也就是光标不闪烁)的作用

那么那么,完整的程序是:

#include<iostream>
#include<cstdlib>
#include<conio.h>
#include<windows.h>
using namespace std;
int x=50;  //边界的x和y
int y=20;
int birdx=x/5;
int birdy=y/4;
int speed=0;//控制速度
int speed2=0;//控制障碍物的来临速度
int xx=x/2;//障碍物的x坐标
int yy= rand()%(y/2);

int count=0;//记录笨鸟的得分


void notin();
void in();
void gotoxy(int x,int 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 show()//开始的初始化(达到循环显示的目的)
{
         gotoxy(0,0) ;
         	HideCursor()   ;
         	for(int i=1;i<y;++i)
         	{
         		for(int j=1;j<x;++j)
         		{
         			if(j==birdx&&i==birdy)
         			{
         				cout<<"\b"<<"鸟"; //\b的作用是因为输出的汉字多占了一个大小的空间,\b删除之前一个位置的输出
					 }
					 else if(j==xx&&(i>=1&&i<=yy)||j==xx&&(i>=yy+5&&i<=y))
					 cout<<"*" ;

					 else
					 cout<<" ";
				 }
				 cout<<endl;
			 }

			 cout<<"得分:"<<count/6<<endl;

}
void in()
{
if(kbhit())
{
	   string ss;
	  ss=getch();

		  if(ss==" ")
		  {
			if(birdy>3)birdy-=3;
		  }


}
}
void notin()  //改变小鸟的位置和障碍物的位置
{
		if (birdx==xx&& (birdy>=yy&&birdy<=yy+5))
        count+=1;

      if(speed!=3)
      {
      	speed++;
	  }
	   if(birdy!=y&&speed==3)
   {
		   birdy+=1;
		   speed=0;
	}


	  if(speed2!=5)
	  {
	  	speed2++;
	  }
	  else if(speed2==5)
	  {
	  	if(xx>0)
	  {
	  	xx--;

	  }


	  if(xx==0)
	  {
	  	 xx=x/2;//障碍物的x坐标
         yy= rand()%(y/2);
         while(yy==0)
         {
			yy= rand()%(y/2);
		 }

	  }
   speed2=0;
	  
}



}
int main()
{
	   while(yy==0)
{
	  yy= rand()%(y/2);
}
      while(1)
{


		    show();
		    notin();
		    in() ;
		    if(birdx==xx&&(((birdy>1&&birdy<yy))||(birdy >yy+5&&birdy<y)))
{
		 cout<<"YOU ARE FILLED!!"<<endl;
		 break;
}



}
		return 0;
}

程序的运行结果:
在这里插入图片描述

当然还有许多可以改进的地方,比如可以做很多个障碍物,我们只要保证每个障碍物距离间隔相同的情况下,多生成几个障碍物,或者也可以加入wsad来控制丝丢皮得(放洋屁)的小鸟的位置,来增加一些可玩性,赶紧赶紧试试吧hhh(建议全屏食用yes)

  • 47
    点赞
  • 271
    收藏
    觉得还不错? 一键收藏
  • 24
    评论
当然,我可以帮你写一个简单的游戏,名为“笨鸟先飞”。以下是使用Mu Pygame Zero模式和Python代码编写的游戏示例: ```python import pgzrun import random WIDTH = 400 HEIGHT = 600 bird = Actor("bird1") bird.pos = (75, 200) pipe_top = Actor("top") pipe_bottom = Actor("bottom") game_over = False score = 0 GRAVITY = 0.3 FLAP_STRENGTH = 6 PIPE_GAP = 150 PIPE_SPEED = 2 def reset_pipes(): pipe_gap_y = random.randint(200, HEIGHT - 200) pipe_top.pos = (WIDTH, pipe_gap_y - PIPE_GAP // 2) pipe_bottom.pos = (WIDTH, pipe_gap_y + PIPE_GAP // 2) def update(): global game_over, score if game_over: return bird.y += GRAVITY if bird.colliderect(pipe_top) or bird.colliderect(pipe_bottom): game_over = True elif bird.y < 0 or bird.y > HEIGHT: game_over = True pipe_top.x -= PIPE_SPEED pipe_bottom.x -= PIPE_SPEED if pipe_top.right < 0: score += 1 reset_pipes() def on_key_down(): if not game_over: bird.y -= FLAP_STRENGTH def draw(): screen.clear() bird.draw() pipe_top.draw() pipe_bottom.draw() screen.draw.text("Score: " + str(score), (10, 10), color="white") if game_over: screen.draw.text("Game Over", center=(WIDTH/2, HEIGHT/2), color="white") reset_pipes() pgzrun.go() ``` 将上述代码复制到Mu编辑器中,并运行代码,即可开始玩“笨鸟先飞小游戏。按下任意键使鸟上升,避免与上下两根管道碰撞,通过通过管道的间隙得分。游戏结束时,屏幕上会显示得分和"Game Over"字样。 请注意,这只是一个简单的示例,您可以根据自己的需求进行修改和扩展。祝您玩得愉快!
评论 24
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值