用C语言实现FlappyBird

在《FlappyBird》这款游戏中,玩家需要用一根手指来操控小鸟往上飞,不断的点击屏幕就会不断的往高处飞。放松手指,则会快速下降。所以玩家要控制小鸟一直向前飞行,然后注意躲避途中高低不平的管子得分。
在这里,我们主要使用C语言中的printf和scanf函数实现一个简单的FlappyBird游戏

整体思路

在开始游戏之前,我们先了解一些辅助函数

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);
}

主函数

我们使用一个简单的游戏框架,来减小游戏实现的难度

int main()
{
	startup();	//初始化变量 
	while (1)
	{
		show(); //显示画面
		updateWithoutInput();//与用户无关的更新
		updateWithInput();//与用户输入有关的更新
	}
	return 0;
}

全局变量的定义

//游戏画面大小
int high;	//行
int width;	//列

//小鸟的坐标
int bird_x;	  //行
int bird_y;	  //列

//障碍物的相关坐标	
int bar1_y;	//障碍物与左边界的距离
int bar1_xTop;	//上边障碍物底部与上边界的距离
int bar1_xDown;	//下边障碍物顶部与上边界的距离

int score;	//得分

在这里插入图片描述

数据的初始化

//游戏界面大小 
high = 15;	
width = 25;
	
//小鸟的初始位置 
bird_x = high / 2;	
bird_y = width / 4;
	
//障碍物的初始位置
bar1_y = width - 1;	
bar1_xTop = high / 4;
bar1_xDown = high / 2;	
	
//得分 
score = 0;

显示画面

每一次循环,都需要重新输出一次画面,在这里,我们使用 @ 表示小鸟, * 表示障碍物

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_xTop) || (i > bar1_xDown)))	//输出障碍物
		{
			//只有在合适的列时才需要去判断是否需要输出*,所以需要利用&&的性质,第一个条件成立之后才去判断第二个条件
			printf("*");
		}
		else
		{
			printf(" ");
		}
	}
	printf("\n");
}
printf("得分:%d\n", score);

与用户无关的更新

实现小鸟的下落和障碍物的移动
bird_x++;	//小鸟向下落
bar1_y--;  //小鸟只是在上下移动,障碍物向左移动
判断游戏的得分与失败
if (bird_y == bar1_y)  //障碍物与小鸟在同一列
{
	if ((bird_x >= bar1_xTop) && (bird_x <= bar1_xDown))	//得分
	{
		score++;
	}
	else	//不能撞墙
	{
		printf("游戏失败!!!");
		return -1;
	}
}
else
{
	if (bird_x > high)	//小鸟不能落到high以下
	{
		printf("游戏失败!!!");
		return -1;
	}
}
重新生成障碍物
if (bar1_y <= 0)  //新生成一个障碍物
{
	bar1_y = width - 1;
	//随机生成障碍物与开口大小
	int upside = rand() % (int)(high * 0.6) + 1;	//防止上侧障碍物出现在边界
	bar1_xTop = upside;	
	int opening = rand() % (int)(high * 0.2) + 2;	  //开口大小随机,且开口不能太小
	while ((bar1_xDown = bar1_xTop + opening) > high - 2)	//防止下侧障碍物过小
	{
		opening = rand() % (int)(high * 0.2) + 2;
	}
}

与用户输入有关的更新

游戏在运行的过程中,用户需要输入空格来使小鸟向上跳动

if (_kbhit())
{
	input = _getch();
	if (input == ' ' && bird_x > 0)
	{
		bird_x = bird_x - 2;
	}
}

关于_kbhit()和_getch()函数可以在《使用c语言实现飞机游戏》中getch函数部分查看,链接:https://blog.csdn.net/lw13572259173/article/details/81662103

完整代码

#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
//辅助函数
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);
}

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

void startup() //数据初始化
{
	//游戏界面大小 
	high = 15;	
	width = 25;
	//小鸟的初始位置 
	bird_x = high / 2;
	bird_y = width / 4;
	//障碍物的初始位置
	bar1_y = width - 1;	
	bar1_xTop = high / 4;
	bar1_xDown = high / 2;
	//得分 
	score = 0;
}

void show()  //显示画面
{
	gotoxy(0, 0);	//将光标调整到(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_xTop) || (i > bar1_xDown)))	//输出障碍物	
			{
				printf("*");
			}
			else
			{
				printf(" ");
			}
		}
		printf("\n");
	}
	printf("得分:%d\n", score);
}

int updateWithoutInput()//与用户无关的更新
{
	bird_x++;	
	bar1_y--;
	if (bird_y == bar1_y)
	{
		if ((bird_x >= bar1_xTop) && (bird_x <= bar1_xDown))	
		{
			score++;
		}
		else
		{
			printf("游戏失败!!!");
			return -1;
		}
	}
	else
	{
		if (bird_x > high)
		{
			printf("游戏失败!!!");
			return -1;
		}
	}
	if (bar1_y <= 0)
	{
		bar1_y = width - 1;
		int upside = rand() % (int)(high * 0.6) + 1;
		bar1_xTop = upside;
		int opening = rand() % (int)(high * 0.2) + 2;
		while ((bar1_xDown = bar1_xTop + opening) > high - 2)
		{
			opening = rand() % (int)(high * 0.2) + 2;
		}
	}
	Sleep(150);
	return 0;
 }
 
void updateWithInput()//与用户输入有关的更新
{
	char input;
	if (_kbhit())
	{
		input = _getch();
		if (input == ' ' && bird_x > 0)
		{
			bird_x = bird_x - 2;
		}
	}
}
int main()
{
	srand((unsigned)time(NULL));
	HideCursor();
again:
	startup();	//初始化变量 
	while (1)
	{
		show(); //显示画面
		int ret = updateWithoutInput();//与用户无关的更新
		if (ret == -1)
		{
			system("CLS");
			printf("1.重新开始\n0.退出\n请选择:");
			int input = 0;
			scanf("%d", &input);
			if (input)
			{
				goto again;
			}
			else
				return 0;
		}
		updateWithInput();//与用户输入有关的更新
	}
	return 0;
}

效果演示

在这里,我们从游戏失败之后重新开始演示
在这里插入图片描述

总结

到这里,我们的FlappyBird游戏已经实现完了,多有不足之处,任需要继续改进。

  • 23
    点赞
  • 125
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值