第3章别碰白块讲解后(童晶:3 别碰白块(《C和C++游戏趣味编程》配套教学视频)),请同学们做了一个flappy bird的练习:
在别碰白块代码基础上,实现flappy bird游戏效果。码云(https://gitee.com/)上新建公开仓库,提交分步骤实现代码,在作业栏中列出码云仓库地址,让其他同学查看、评分。
![v2-41a5f2fdcf94ddf703b7ff416bbbca63_b.jpg](https://img-blog.csdnimg.cn/img_convert/08044944d32cec80f2de6594417fa19b.png)
参考实现步骤:
1 别碰白块原始代码
2 修改小球为空中起跳
3 添加上面的障碍物
4 两个障碍物向左移动
5 完善游戏失败的判定
6 最终代码
以下为一位同学在码云上的代码,感兴趣的可以参考:
https://gitee.com/JinJunQiang/tes-t1/commits/master/%E5%B0%8F%E9%B8%9F%E9%A3%9E.cpp
![v2-0f2f62de9cf85be36e0c26e3d8602b75_b.jpg](https://img-blog.csdnimg.cn/img_convert/7429b1dc22501d2c4b962e933639f462.png)
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
int main()
{
float width,height,gravity; // 游戏画面大小、重力加速度
float ball_x,ball_y,ball_vy,radius; // 小球圆心坐标、y方向速度、半径大小
float rect_left_x,rect_top_y,rect_width,rect_height,rect_vx; // 方块障碍物的相关参数
int score = 0; // 得分
// 小球是否在地面上,避免重复起跳
width = 600; // 游戏画面宽度
height = 400; // 游戏画面高度
gravity = 0.6; // 重力加速度
initgraph(width, height); // 新建一个画布
radius = 20; // 小球半径
ball_x = width/4; // 小球x位置
ball_y = height-radius; // 小球y位置
ball_vy = 0; // 小球初始y速度为0
rect_height = 100; // 方块高度
rect_width = 20; // 方块宽度
rect_left_x = width*3/4; // 方块左边x坐标
rect_top_y = height - rect_height; // 方块顶部y坐标
rect_vx = -3; // 方块x方向速度
while(1) // 一直循环
{
if (kbhit()) // 当按键时
{
char input = _getch(); // 获得输入字符
if (input==' ' ) // 当按下空格键,并且小球在地面上时
{
ball_vy = -15; // 给小球一个向上的速度
// 表示小球不在地面了,不能重复起跳
}
}
ball_vy = ball_vy + gravity; // 根据重力加速度更新小球y方向速度
ball_y = ball_y + ball_vy; // 根据小球y方向速度更新其y坐标
if (ball_y >= height-radius) // 如果小球落到地面上
{
ball_vy = 0; // y速度为0
ball_y = height-radius; // 规范其y坐标,避免落到地面下
// 表示小球在地面上
}
if(ball_y<=0){
ball_vy=0;
ball_y=radius;}
rect_left_x = rect_left_x + rect_vx; // 方块向左移
if (rect_left_x <= 0) // 如果方块跑到最左边
{
rect_left_x = width; // 在最右边重新出现
score = score + 1; // 得分+1
rect_height = rand() % int(height/4) + height/4; // 设置随机高度
rect_vx = rand()/float(RAND_MAX) *4 - 7; // 设置方块随机速度
}
// 如果小球碰到方块
if ((rect_left_x <= ball_x + radius)
&& (rect_left_x + rect_width >= ball_x - radius)
&& (height - rect_height <= ball_y + radius) || (ball_y-radius<=height - rect_height-100) && (rect_left_x <= ball_x + radius)
&& (rect_left_x + rect_width >= ball_x - radius))
{
Sleep(50); // 慢动作效果
score = 0; // 得分清零
}
cleardevice(); // 清空画面
fillcircle(ball_x, ball_y, radius); // 绘制小球
// 画方块
fillrectangle(rect_left_x, height - rect_height, rect_left_x + rect_width,height);
fillrectangle(rect_left_x, 0, rect_left_x + rect_width,height - rect_height-100);
TCHAR s[20]; // 定义字符串数组
_stprintf(s, _T("%d"), score); // 将score转换为字符串
settextstyle(40, 0, _T("宋体")); // 设置文字大小、字体
outtextxy(50, 30, s); // 输出得分文字
Sleep(10); // 暂停10毫秒
}
closegraph();
return 0;
}
当我们写的代码较复杂时,强烈建议同学们采用SVN、GIT等代码版本管理工具。以下为SVN的一些应用说明:
![v2-79e617246fc9f0d484ef10d60a669f63_b.jpg](https://img-blog.csdnimg.cn/img_convert/88bcb89c11d9bf3ef7aeee0482cafc74.png)
![v2-ff0dc75de901e2cf4506e24e44186d22_b.jpg](https://img-blog.csdnimg.cn/img_convert/2a4e131519edda40d3d2d470a2406bd2.png)
![v2-eb880a7820d3a2715613be9464c8e697_b.jpg](https://img-blog.csdnimg.cn/img_convert/40bd8991eec756a3fbd2d79439a20f5e.png)
![v2-80fcd1fdbd61dd153a9e8e8e24217217_b.jpg](https://img-blog.csdnimg.cn/img_convert/3e4740970486498f3844c202215578e9.png)
![v2-81fa587172483411d971e42ed3178885_b.jpg](https://img-blog.csdnimg.cn/img_convert/67c14d27ee419c8b857cce26c69d5f16.png)
![v2-499d958554a56b4052c64bde4c814022_b.jpg](https://img-blog.csdnimg.cn/img_convert/91458a07e0ec34cd0bdeee764d453744.png)
初学者,可以使用在线的代码版本管理工具,推荐使用国内的码云:
Gitee - 基于 Git 的代码托管和研发协作平台注册账号后,大家就可以参考帮助文档,在线创建你的第一个项目了:
创建你的第一个仓库 - Gitee.com