当前位置:首页 > 趣味小程序 > 正文内容

C++小游戏——flappy bird简单实现

亿万年的星光4年前 (2021-04-24)趣味小程序1348

上一篇小游戏中,我们简单实现了打砖块小游戏。这一篇中,我们根据前面的框架,简单实现flappy bird小游戏。

1.游戏框架


   2.实现下落的小鸟

#include <cstdio>
#include <cstdlib>
#include <conio.h>
#include <windows.h>

// 全局变量
int high,width; // 游戏画面大小
int bird_x,bird_y; // 小鸟的坐标
int bar1_y,bar1_xDown,bar1_xTop; // 障碍物

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 startup()  // 数据初始化
{
	high = 15;
	width = 20;
	bird_x = 0;
	bird_y = width/3;
}

void show()  // 显示画面
{
	gotoxy(0,0);  // 清屏	
	int i,j;
	
	for (i=0;i<high;i++)
	{
		for (j=0;j<width;j++)
		{
			if ((i==bird_x) && (j==bird_y))
				printf("♀");  //   输出小鸟
			else
				printf(" ");  //   输出空格
		}
		printf("\n");
	}
}	

void updateWithoutInput()  // 与用户输入无关的更新
{
	bird_x ++;
	Sleep(150);
}

void updateWithInput()  // 与用户输入有关的更新
{	
	char input;
	if(kbhit())  // 判断是否有输入
	{
		input = getch();  // 根据用户的不同输入来移动,不必输入回车
		if (input == ' ')  
		{
			bird_x = bird_x - 2;
		}
		
	}
}

int main()
{
	startup();  // 数据初始化
	HideCursor(); //隐藏光标	
	while (1)  //  游戏循环执行
	{
		show();  // 显示画面
		updateWithoutInput();  // 与用户输入无关的更新
		updateWithInput();     // 与用户输入有关的更新
	}
	return 0; 
}



2.同步显示小鸟和障碍物

#include <cstdio>
#include <cstdlib>
#include <conio.h>
#include <windows.h>
// 全局变量
int high,width; // 游戏画面大小
int bird_x,bird_y; // 小鸟的坐标
int bar1_y,bar1_xDown,bar1_xTop; // 障碍物的相关坐标

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 startup()  // 数据初始化
{
	high = 15;
	width = 20;
	bird_x = 0;
	bird_y = width/3;
	bar1_y = width/2;
	bar1_xDown = high/3;
	bar1_xTop = high/2;
}

void show()  // 显示画面
{
	gotoxy(0,0);  // 清屏
	int i,j;
	
	for (i=0;i<high;i++)
	{
		for (j=0;j<width;j++)
		{
			if ((i==bird_x) && (j==bird_y))
				printf("#");  //   输出小鸟
			else if ((j==bar1_y) && ((i<bar1_xDown)||(i>bar1_xTop)) )
				printf("||");  //   输出墙壁
			else
				printf(" ");  //   输出空格
		}
		printf("\n");
	}

}	

void updateWithoutInput()  // 与用户输入无关的更新
{
	bird_x ++;
	Sleep(150);
}

void updateWithInput()  // 与用户输入有关的更新
{	
	char input;
	if(kbhit())  // 判断是否有输入
	{
		input = getch();  // 根据用户的不同输入来移动,不必输入回车
		if (input == ' ')  
		{
			bird_x = bird_x - 2;
		}

	}
}

int  main()
{
	startup();  // 数据初始化	
	HideCursor(); //隐藏光标
	while (1)  //  游戏循环执行
	{
		show();  // 显示画面
		updateWithoutInput();  // 与用户输入无关的更新
		updateWithInput();     // 与用户输入有关的更新
	}
	return 0; 
}

效果:

3.障碍物向左移动

#include <cstdio>
#include <cstdlib>
#include <conio.h>
#include <windows.h>
// 全局变量
int high,width; // 游戏画面大小
int bird_x,bird_y; // 小鸟的坐标
int bar1_y,bar1_xDown,bar1_xTop; // 障碍物的相关坐标

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 startup()  // 数据初始化
{
	high = 20;
	width = 50;
	bird_x = high/2;
	bird_y = 1;
	bar1_y = width/2;
	bar1_xDown = high/3;
	bar1_xTop = high/2;
}

void show()  // 显示画面
{
	gotoxy(0,0);  // 清屏	
	int i,j;
	
	for (i=0;i<high;i++)
	{
		for (j=0;j<width;j++)
		{
			if ((i==bird_x) && (j==bird_y))
				printf("#");  //   输出小鸟
			else if ((j==bar1_y) && ((i<bar1_xDown)||(i>bar1_xTop)) )
				printf("||");  //   输出墙壁
			else
				printf(" ");  //   输出空格
		}
		printf("\n");
	}

}	

void updateWithoutInput()  // 与用户输入无关的更新
{
	bird_x ++;
	bar1_y --;
	Sleep(150);
}

void updateWithInput()  // 与用户输入有关的更新
{	
	char input;
	if(kbhit())  // 判断是否有输入
	{
		input = getch();  // 根据用户的不同输入来移动,不必输入回车
		if (input == ' ')  
		{
			bird_x = bird_x - 2;
		}

	}
}

int main()
{
	startup();  // 数据初始化	
	HideCursor(); //隐藏光标
	while (1)  //  游戏循环执行
	{
		show();  // 显示画面
		updateWithoutInput();  // 与用户输入无关的更新
		updateWithInput();     // 与用户输入有关的更新
	}
	return 0;
}


效果:

4.判断小鸟是通过还是碰撞

#include <cstdio>
#include <cstdlib>
#include <conio.h>
#include <windows.h>
// 全局变量
int high,width; // 游戏画面大小
int bird_x,bird_y; // 小鸟的坐标
int bar1_y,bar1_xDown,bar1_xTop; // 障碍物的相关坐标
int score; // 得分,经过障碍物的个数

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 startup()  // 数据初始化
{
	high = 20;
	width = 50;
	bird_x = high/2;
	bird_y = 3;
	bar1_y = width/2;
	bar1_xDown = high/3;
	bar1_xTop = high/2;
	score = 0;
}

void show()  // 显示画面
{
	gotoxy(0,0);  // 清屏	
	int i,j;
	
	for (i=0;i<high;i++)
	{
		for (j=0;j<width;j++)
		{
			if ((i==bird_x) && (j==bird_y))
				printf("#");  //   输出小鸟
			else if ((j==bar1_y) && ((i<bar1_xDown)||(i>bar1_xTop)) )
				printf("||");  //   输出墙壁
			else
				printf(" ");  //   输出空格
		}
		printf("\n");
	}
	printf("得分:%d\n",score);
}	

void updateWithoutInput()  // 与用户输入无关的更新
{
	bird_x ++;
	bar1_y --;
	if (bird_y==bar1_y)
	{
		if ((bird_x>=bar1_xDown)&&(bird_x<=bar1_xTop))
			score++;
		else
		{
			printf("游戏失败\n");
			system("pause");
			exit(0);
		}
	}

	Sleep(150);
}

void updateWithInput()  // 与用户输入有关的更新
{	
	char input;
	if(kbhit())  // 判断是否有输入
	{
		input = getch();  // 根据用户的不同输入来移动,不必输入回车
		if (input == ' ')  
		{
			bird_x = bird_x - 2;
		}

	}
}

int main()
{
	startup();  // 数据初始化
	HideCursor();	
	while (1)  //  游戏循环执行
	{
		show();  // 显示画面
		updateWithoutInput();  // 与用户输入无关的更新
		updateWithInput();     // 与用户输入有关的更新
	}
	return 0;
}

效果:


5.障碍物循环出现

#include <cstdio>
#include <cstdlib>
#include <conio.h>
#include <windows.h>
// 全局变量
int high,width; // 游戏画面大小
int bird_x,bird_y; // 小鸟的坐标
int bar1_y,bar1_xDown,bar1_xTop; // 障碍物1的相关坐标
int score; // 得分,经过障碍物的个数

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 startup()  // 数据初始化
{
	high = 20;
	width = 20;
	bird_x = high/2;
	bird_y = 3;
	bar1_y = width;
	bar1_xDown = high/3;
	bar1_xTop = high/2;
	score = 0;
}

void show()  // 显示画面
{
	gotoxy(0,0);  // 清屏	
	int i,j;
	
	for (i=0;i<high;i++)
	{
		for (j=0;j<width;j++)
		{
			if ((i==bird_x) && (j==bird_y))
				printf("#");  //   输出小鸟
			else if ((j==bar1_y) && ((i<bar1_xDown)||(i>bar1_xTop)) )
				printf("||");  //   输出墙壁
			else
				printf(" ");  //   输出空格
		}
		printf("\n");
	}
	printf("得分:%d\n",score);
}	

void updateWithoutInput()  // 与用户输入无关的更新
{
	bird_x ++;
	bar1_y --;
	if (bird_y==bar1_y)
	{
		if ((bird_x>=bar1_xDown)&&(bird_x<=bar1_xTop))
			score++;
		else
		{
			printf("游戏失败\n");
			system("pause");
			exit(0);
		}
	}
	if (bar1_y<=0)  // 再新生成一个障碍物
	{
		bar1_y = width;
		int temp = rand()%int(high*0.8);
		bar1_xDown = temp - high/10;
		bar1_xTop = temp + high/10;
	}

	Sleep(150);
}

void updateWithInput()  // 与用户输入有关的更新
{	
	char input;
	if(kbhit())  // 判断是否有输入
	{
		input = getch();  // 根据用户的不同输入来移动,不必输入回车
		if (input == ' ')  
		{
			bird_x = bird_x - 2;
		}

	}
}

int main()
{
	startup();  // 数据初始化	
	HideCursor();
	while (1)  //  游戏循环执行
	{
		show();  // 显示画面
		updateWithoutInput();  // 与用户输入无关的更新
		updateWithInput();     // 与用户输入有关的更新
	}
	return 0;
}


效果:

扫描二维码推送至手机访问。

版权声明:本文由青少年编程知识记录发布,如需转载请注明出处。

分享给朋友:

相关文章

C++ 如何监听用户按下了哪个按键

想做一款小游戏,键盘事件是必须要了解的。前面的文章简单介绍过键盘事件,这篇文章简单实现了监听用户键盘的操作,主要监听“WASD”以及“上下左右”键参考代码#include<cstdio>...

C++如何在控制台不同区域显示不同颜色

C++如何在控制台不同区域显示不同颜色

0.前言在前面的文章中,我们介绍过让控制台”五彩斑斓“。但是有一个问题,就是使用system(“color A9”)这种方式,这种方式是一种全局的配置,会把原来的颜色给换掉,很难实现不同区域不同颜色的...

【C++图形化编程】EasyX函数~图形绘制相关函数(2)

【C++图形化编程】EasyX函数~图形绘制相关函数(2)

(1)ellipse  椭圆void ellipse( int left, int top, int right, int ...

C++实现弹窗效果

C++实现弹窗效果

1.格式C++实现弹窗效果需要用到messagebox,这个我在C#中用过,C++也有类似用法。messagebox函数,需要引入<windows.h>头文件2.简单用法#include&...

【算法】前缀和与差分(1)一维数组前缀和

【算法】前缀和与差分(1)一维数组前缀和

一、定义前缀和:是指某序列的前n项和。可以理解成数学上上的数列的前n项和。差分:是前缀和的逆运算。二、前缀和的分类可以分成一维数组的前缀和和二维 数组的前缀和一维数组前缀和  &n...

C++小游戏—弹跳小球

C++小游戏—弹跳小球

首先,要注意屏幕直角坐标系的问题,不然的话,后面移动过程中一定会出错。然后,利用printf函数在屏幕坐标(x,y)处显示一个静止的小球字符‘O’,注意屏幕坐标系的原点在左上方,参考代码#includ...