第3章 flappy bird作业、SVN、GIT、码云

第3章别碰白块讲解后(童晶:3 别碰白块(《C和C++游戏趣味编程》配套教学视频)),请同学们做了一个flappy bird的练习:

在别碰白块代码基础上,实现flappy bird游戏效果。码云(https://gitee.com/)上新建公开仓库,提交分步骤实现代码,在作业栏中列出码云仓库地址,让其他同学查看、评分。

v2-41a5f2fdcf94ddf703b7ff416bbbca63_b.jpg

参考实现步骤:

1 别碰白块原始代码

2 修改小球为空中起跳

3 添加上面的障碍物

4 两个障碍物向左移动

5 完善游戏失败的判定

6 最终代码


以下为一位同学在码云上的代码,感兴趣的可以参考:

gitee.com/JinJunQiang/t

v2-0f2f62de9cf85be36e0c26e3d8602b75_b.jpg
#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

v2-ff0dc75de901e2cf4506e24e44186d22_b.jpg

v2-eb880a7820d3a2715613be9464c8e697_b.jpg

v2-80fcd1fdbd61dd153a9e8e8e24217217_b.jpg

v2-81fa587172483411d971e42ed3178885_b.jpg

v2-499d958554a56b4052c64bde4c814022_b.jpg

初学者,可以使用在线的代码版本管理工具,推荐使用国内的码云:

Gitee - 基于 Git 的代码托管和研发协作平台

注册账号后,大家就可以参考帮助文档,在线创建你的第一个项目了:

创建你的第一个仓库 - Gitee.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值