C与C++游戏项目练习5:Flappy Bird简易版

本文介绍了如何使用C语言实现FlappyBird的简化版本,包括小鸟下落速度的动态调整和障碍物生成的优化策略。作者分享了纯C版本的代码片段,以及C++中使用类和对象管理柱子的升级版。通过实例展示了如何通过编程技巧增加游戏挑战性和可玩性。
摘要由CSDN通过智能技术生成

C与C++游戏项目练习5:Flappy Bird简易版

难得从项目里忙里偷闲,今天继续来钻研这本童晶老师的好书:
在这里插入图片描述
因为本次改动较大,所以放上两版代码:
纯C的版本,只实现了2.3.6的课后第一题的解答,每次小鸟下落时增加的距离除了固定的1之外,再加一个纵坐标除以10,这样小鸟飞得越下面,下落得就越快了。

bird_x=bird_x+1+bird_x/10;//小鸟往下飞,并且越往高度下(bird_x越大)飞的越快

例如,小鸟纵坐标是20,那他下一次就会落到20+1+20/10=23;
小鸟坐标是30,那他下一次就会落到30+1+30/10=34

具体下落多块可以调,看自己快乐咯~~

以下是C语言写的代码,只有课后第一题

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#include<ctime>
int height, width;
int bird_x, bird_y;
int bar1_y, bar1_xDown, bar1_xTop;//障碍物,这里1我写的是数字1,one,不是字母l(L),问就是我自己也没看出书上是啥
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 StartUp()
{
	height = 15;
	width = 20;
	bird_x = height/2;
	bird_y = width / 3;
	bar1_y = width*2/ 3;//柱子的横坐标在画面的2/3处
	bar1_xDown = height / 3;//柱子纵坐标
	bar1_xTop = height / 2;
	score = 0;
}

void Show()
{
	gotoxy(0, 0);
	for (int i = 0; i < height; i++)
	{
		for (int 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("score = %d", score);
}

void UpdateWithOutInput()//与用户输入无关的更新
{
	bird_x=bird_x+1+bird_x/10;//小鸟往下飞,并且越往高度下(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)(height * 0.8);//height(int)*0.8(float)会得到float类型结果,所以要强制类型转换int
		bar1_xDown = temp - height / 10;
		bar1_xTop = temp + height / 10;
	}
	Sleep(150);
}

void UpdateWithInput()
{
	char input;
	if (_kbhit())
	{
		input = _getch();//遇到空格,就让小鸟往上飞两个单位
		if (input == ' ')
		{
			bird_x = bird_x - 2;
		}
	}
}

int main()
{
	srand((unsigned)time(NULL));
	StartUp();
	while (1)
	{
		Show();
		UpdateWithOutInput();
		UpdateWithInput();
	}
	return 0;
}


课后第二题我是用的C++,定义一个柱子类Bar,然后设定好N=5的柱子数组,每次用构造函数在Start()函数里随机生成五个柱子。

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#include<ctime>
#define N 5//暂定五根柱子
int height, width;
int bird_x, bird_y;
int score;
class Bar//柱子类
{
public:
	int bar1_y, bar1_xDown, bar1_xTop;//障碍物,这里1我写的是数字1,one,不是字母l(L),问就是我自己也没看出书上是啥
	Bar() {};//默认构造函数
	Bar(int y)//传入的参数y是生成柱子的位置(注意柱子要在小鸟的前方生成)
	{
		bar1_y = y;
		int temp = rand() % (int)(height * 0.8);//height(int)*0.8(float)会得到float类型结果,所以要强制类型转换int
		bar1_xDown = temp - height / 10;
		bar1_xTop = temp + height / 10;
	}
	void Move()
	{
		bar1_y--;//往左移动
	}
};
Bar bar[N];//创建柱子数组的全局变量

void gotoxy(int x, int y)
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(handle, pos);
}
void StartUp()
{
	height = 15;
	width = 100;
	bird_x = height/2;
	bird_y = width / 10;
	
	for (int i = 0; i < N ; i++)
	{
		bar[i].bar1_y = rand() % width + bird_y;//柱子要生成在小鸟的前面,所以不能小于bird_y
		if (i > 0)//从第一根柱子开始,每根柱子与前一根柱子之间至少隔两列的距离,不然小鸟没法飞
		{
			bar[i].bar1_y = (abs(bar[i].bar1_y - bar[i - 1].bar1_y) > 2) ? bar[i].bar1_y : bar[i].bar1_y + 2;
			//判断第i根柱子和i-1根柱子之间是不是至少隔了两列,如果是的话就不变,如果不是的话就把距离往前加两列,可多不可少
		}
		int temp = rand() % (int)(height * 0.8);//height(int)*0.8(float)会得到float类型结果,所以要强制类型转换int
		bar[i].bar1_xDown = temp - height / 10;
		bar[i].bar1_xTop = temp + height / 10;
	}
	score = 0;
}

void Show()
{
	gotoxy(0, 0);
	for (int i = 0; i < height; i++)
	{
		for (int j = 0; j < width; j++)
		{
			if (i == bird_x && j == bird_y)
			{
				printf("@");//输出小鸟
			}
			else //剩下的情况不是柱子就是空格,把柱子输出了,剩下的肯定是空格
			{	
				int flag = 0;
				for (int k = 0; k < N; k++)
				{		
					if (j == bar[k].bar1_y && ((i >= bar[k].bar1_xTop) || (i <= bar[k].bar1_xDown)))
					{
						flag = 1;
						printf("*");
						break;
					}
				}
				if (flag == 0)
				{
					printf(" ");//输出空格
				}
			}
			
		}
		printf("\n");
	}
	printf("score = %d", score);
}

void UpdateWithOutInput()//与用户输入无关的更新
{
	bird_x=bird_x+1+bird_x/10;//小鸟往下飞,并且越往高度下(bird_x越大)飞的越快
	for (int i = 0; i < N; i++)
	{
		bar[i].bar1_y--;//障碍物从右到左移动
		if (bird_y == bar[i].bar1_y)//柱子与小鸟位于同一直线范围内
		{
			if (bird_x > bar[i].bar1_xDown && bird_x < bar[i].bar1_xTop)//小鸟从缝隙间成功穿过,过柱子成功,得分加一
			{
				score++;
			}
			else
			{
			printf("游戏失败!\n");
			system("pause");
			exit(0);
			}
		}
		if (bar[i].bar1_y < 0)//当前这个柱子移到了屏幕外面,需要重新生成
		{
			bar[i].bar1_y = width;
			int temp = rand() % (int)(height * 0.8);//height(int)*0.8(float)会得到float类型结果,所以要强制类型转换int
			bar[i].bar1_xDown = temp - height / 10;
			bar[i].bar1_xTop = temp + height / 10;
		}
	
	}
	Sleep(100);
}

void UpdateWithInput()
{
	char input;
	if (_kbhit())
	{
		input = _getch();//遇到空格,就让小鸟往上飞两个单位
		if (input == ' ')
		{
			bird_x = bird_x - 2;
		}
	}
}

int main()
{
	srand((unsigned)time(NULL));
	StartUp();
	while (1)
	{
		Show();
		UpdateWithOutInput();
		UpdateWithInput();
	}
	return 0;
}




最理想的效果如图:
在这里插入图片描述
有一点很重要,要保证前后两根柱子之间至少有一到两根柱子的距离,不然小鸟没法飞,直接过了这根就撞上下一根柱子了。

比如下图最右边的两根柱子:
在这里插入图片描述
我自己试着优化了一下代码,从第二根柱子开始,如果和前一根柱子的距离的绝对值小于2,就往前移动两个单位。


void StartUp()
{
	height = 15;
	width = 100;
	bird_x = height/2;
	bird_y = width / 10;
	
	for (int i = 0; i < N ; i++)
	{
		bar[i].bar1_y = rand() % width + bird_y;//柱子要生成在小鸟的前面,所以不能小于bird_y
		if (i > 0)//从第一根柱子开始,每根柱子与前一根柱子之间至少隔两列的距离,不然小鸟没法飞
		{
			bar[i].bar1_y = (abs(bar[i].bar1_y - bar[i - 1].bar1_y) > 2) ? bar[i].bar1_y : bar[i].bar1_y + 2;
			//判断第i根柱子和i-1根柱子之间是不是至少隔了两列,如果是的话就不变,如果不是的话就把距离往前加两列,可多不可少
		}
		int temp = rand() % (int)(height * 0.8);//height(int)*0.8(float)会得到float类型结果,所以要强制类型转换int
		bar[i].bar1_xDown = temp - height / 10;
		bar[i].bar1_xTop = temp + height / 10;
	}
	score = 0;
}

为什么必须是绝对值呢?比如说原来第一根柱子坐标是1,第二根是2,第三根是3,第二根发现和第一根太近,就往前移动2个单位,坐标2+2=4,这样就又和坐标是3的第三根柱子挨在一起了,而第三根柱子只会判断跟第二根柱子之间的距离,这个时候等于-1,如果不是绝对值小于2的话,这个问题不会被修正。所以必须是绝对值小于2。

123----1 32

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值