C语言小游戏:飞翔的小鸟(完整版),素材源码都准备好了!
每天一个C语言小项目,提升你的编程能力!
《Flappy Bird》是曾经一款流行的一款手机游戏,你只要让小鸟保持飞行,不要碰到绿色的管道就可以啦。操作虽然简单,但是非常具有挑战!本次我们也是自己动手来实现这样一款游戏的高仿版,大家不妨自己先读一遍代码然后动手试试!
该程序使用 C 语言实现的 FlappyBird 的电脑版,玩法和手机版的相同。
程序通过 alpha 域实现透明贴图,并且通过双缓冲绘图防止刷新闪屏。
程序执行效果如下:
本程序设计了三种不同颜色的小鸟(可以实现三人对战)另外也有白天模式和夜晚模式,不同模式和不同小鸟是在上次死亡后随机刷新,避免产生视觉疲劳。
简单了解游戏后我们就来试试吧!
本项目编译环境:Visual Studio 2019/2022,EasyX插件
必备素材:
网盘链接:https://pan.baidu.com/s/10ALP-QdcTu0UV4p8alTE4w?pwd=3p3s 提取码:3p3s
代码展示:(有五百多行代码,每一个功能实现都会给出对应注释)
#include
#include
#include
#include
#include
#include
// 初始背景图 白天、黑夜
IMAGE BK[2];
// 全局画板
IMAGE bk;
// 飞鸟资源 橘、蓝、红
IMAGE BIRD[3][3];
// 管道资源 绿、红
IMAGE PIPE[2][2];
// 三种数字字体
IMAGE NUMBER[3][10];
// 奖牌资源
IMAGE MEDAL[4];
// 地面资源
IMAGE LAND;
// 开始游戏按钮
IMAGE PLAY;
// 得分面板
IMAGE PANEL;
// 以下为五种游戏文字
// 游戏结束 游戏预备 游戏标题 游戏提示 最高分
IMAGE OVER;
IMAGE READY;
IMAGE TITLE;
IMAGE TUTORIAL;
IMAGE NEW;
// 结束时状态
IMAGE OVEIMG;
// 是否为白天
int isDay = 0;
// 鸟的颜色
int birdColor = 0;
// 最高分
int best = 0;
// 游戏初始时间
long startTime = 0;
// 游戏基本属性
// 重力加速度
const double g = 6.5;
// 管道的移动速度
double vx = 11;
// 鸟的下落速度
double vy = 10;
// 鸟的当前位置
double y = 220;
const double x = 288 / 2 - (41 + 8) / 2 - 80;
// 管道的横向坐标
double pipeX[2];
// 管道的纵向坐标
int pipeY[2];
// 当前得分
int score;
// 鸟的矩形判断区域
const int top = 12;
const int left = 10;
const int right = 37;
const int buttom = 33;
// 奖牌显示位置
const int medalXY[2] = { 0, 0 };
// 陆地坐标
const double landY = 420;
double landX = -20;
// 飞鸟姿态
int pose = 0;
// 飞行间隔
const int diff = 110;
// 管道上补偿
const int pipeUp = -280;
// 管道下补偿
const int pipeDown = 140;
// 游戏预处理
void start();
// 游戏函数
void game();
// 结束游戏函数
void end();
// 移动函数
void move(long time);
// 绘制函数
void draw();
// 重置画布
void reset();
// 绘制画布
void put();
// 加载资源
void loadRes();
// 根据透明度绘图
void drawAlpha(IMAGE* dstimg, int x, int y, IMAGE* srcimg);
// 绘制分数
void drawScore(int y, int sc, int dif, int se, int st, int type);
// 绘制管道
void drawPipe();
// 判断鸟死亡
bool isDie();
// 初始化游戏资源
void init();
int main()
{
// 游戏开局页面
init();
while (true)
{
// 游戏初始化
start();
// 游戏进行
game();
// 显示得分
end();
}
return 0;
}
bool GetControl()
{
bool res = false;
if (_kbhit())
{
char ch = _getch();
if (ch == ' ')
{
res = true;
}
}
MOUSEMSG msg;
while (MouseHit())
{
msg = GetMouseMsg();
if (msg.mkLButton)
{
res = true;
}
}
return res;
}
// 游戏预处理
void start()
{
// 初始化数据
isDay = rand() % 2;
birdColor = rand() % 3;
pipeX[0] = 288 + 30;
pipeX[1] = 288 + 30 + 190;
pipeY[0] = rand() % 250;
pipeY[1] = rand() % 250;
pose = 0;
landX = 0;
score = 0;
y = 220;
vy = 0;
// 游戏初始时间
clock_t time = clock();
// 开场动画
clock_t t = clock();
while (true)
{
reset();
drawScore(60, score, 13, 26, 144, 0);
drawAlpha(&bk, 50, 120, &READY);
drawAlpha(&bk, (int)x, (int)y, &BIRD[birdColor][pose]);
drawAlpha(&bk, 90, 220, &TUTORIAL);
drawAlpha(&bk, (int)landX, (int)landY, &LAND);
landX -= (clock() - t) * vx / 100;
t = clock();
pose = ((clock() - time) / diff) % 3;
put();
if (landX < -44)
{
landX = -20;
}
if (GetControl())
{
break;
}
Sleep(10);
}
}
// 游戏函数
void game()
{
// 根据毫秒
startTime = clock();
long time = clock();
while (!isDie())
{
// 移动
move(clock() - time);
time = clock();
// 控制
if (GetControl())
{
vy = -26;
}
// 绘制
draw();
Sleep(10);
}
startTime = clock();
while (clock() - startTime < 1000);
vy = -30;
time = clock();
while (y < 520 y - time vy 100 vy clock - time 100 time='clock();' draw sleep10 drawalphaoveimg 0 0 bk void flush while mousehit getmousemsg while _kbhit _getch void end starttime='clock();' reset drawpipe drawalphabk intlandx intlandy land drawalphabk 0 0 oveimg drawalphabk 25 150 panel int r='(score' - 50 50 if score> 50)
{
if (r > 3)
{
r = 3;
}
drawAlpha(&bk, 57, 195, &MEDAL[r]);
}
// 打印当前分
drawScore(189, score, 16, 16, 240, 2);
// 写最高分
int tmp = best;
if (best < score)
{
best = score;
}
// 打印最高分
drawScore(231, best, 16, 16, 240, 2);
// 如果当前分超过最高分 则显示新分数图标
if (tmp < score drawalphabk 165 210 new flush put while true if getcontrol break sleep10 void drawscoreint y int sc int dif int se int st int type int t='sc;' int num8 int length='0;' do numlength='t' 10 length t while t int s='st' - dif length for int i='length' - 1 i>= 0; i--)
{
drawAlpha(&bk, s, y, &NUMBER[type][num[i]]);
s += se;
}
}
// 管道、主角、地面移动
void move(long time)
{
y += time * vy / 100;
vy += g * time / 100;
pipeX[0] -= time * vx / 100;
pipeX[1] -= time * vx / 100;
landX -= time * vx / 100;
pose = ((clock() - startTime) / diff) % 3;
if (landX < -44)
{
landX = -20;
}
if (pipeX[0] < -52)
{
pipeX[0] = pipeX[1] + 190;
pipeY[0] = rand() % 250;
}
if (pipeX[1] < -52)
{
pipeX[1] = pipeX[0] + 190;
pipeY[1] = rand() % 250;
}
if (y < 0 - top)
{
y = -top;
}
score = (int)(((clock() - startTime) * vx / 100 - (288 - x + 30)) / 190 + 1);
if (score < 0 score='0;' void draw drawalphabk 0 0 bkisday drawpipe drawalphabk intlandx intlandy land drawalphabk intx inty birdbirdcolorpose drawscore60 score 13 26 144 0 put void drawalphaimage dstimg int x int y image srcimg if dstimg='= NULL)' return dword dst='GetImageBuffer(dstimg);' dword src='GetImageBuffer(srcimg);' int src_width='srcimg-'>getwidth();
int src_height = srcimg->getheight();
int dst_width = dstimg->getwidth();
int dst_height = dstimg->getheight();
// 实现透明贴图 可优化
for (int iy = 0; iy < src_height; iy++)
{
for (int ix = 0; ix < src_width ix int srcx='ix' iy src_width int sa='((src[srcX]' 0xff000000>> 24);
int sr = ((src[srcX] & 0xff0000) >> 16);
int sg = ((src[srcX] & 0xff00) >> 8);
int sb = src[srcX] & 0xff;
if (x + ix >= 0 && x + ix < dst_width y iy>= 0 && y + iy < dst_height int dstx='(x' ix y iy dst_width int dr='((dst[dstX]' 0xff0000>> 16);
int dg = ((dst[dstX] & 0xff00) >> 8);
int db = dst[dstX] & 0xff;
dst[dstX] = ((sr * sa / 255 + dr * (255 - sa) / 255) << 16)
| ((sg * sa / 255 + dg * (255 - sa) / 255) << 8 sb sa 255 db 255 - sa 255 void drawpipe drawalphabk intpipex0 pipey0 pipeup pipeisday0 drawalphabk intpipex0 pipey0 pipedown pipeisday1 drawalphabk intpipex1 pipey1 pipeup pipeisday0 drawalphabk intpipex1 pipey1 pipedown pipeisday1 bool isdie if y buttom> landY)
return true;
if (x + right > pipeX[0] && x + left < pipeX[0] + 52)
{
if (y + top < pipey0 40 y buttom> pipeY[0] + 140)
return true;
}
if (x + right > pipeX[1] && x + left < pipeX[1] + 52)
{
if (y + top < pipey1 40 y buttom> pipeY[1] + 140)
return true;
}
return false;
}
void reset()
{
drawAlpha(&bk, 0, 0, &BK[isDay]);
}
void put()
{
putimage(0, 0, &bk);
}
// 初始化游戏资源
void init()
{
// 加载图形资源
loadRes();
// 初始化图形界面
initgraph(288, 512);
// 初始化随机数种子
srand((unsigned int)time(NULL));
// 初始化变量
best = 0;
isDay = rand() % 2;
birdColor = rand() % 3;
// 游戏初始时间
clock_t time = clock();
// 开场动画
while (true)
{
reset();
drawAlpha(&bk, 60, 120, &TITLE);
drawAlpha(&bk, 125, 200, &BIRD[birdColor][pose]);
drawAlpha(&bk, 90, 270, &PLAY);
pose = ((clock() - time) / diff) % 3;
put();
if (GetControl())
{
break;
}
Sleep(10);
}
}
// 加载图片资源
void loadRes()
{
loadimage(&BK[0], _T("res\\bg_day.png"));
loadimage(&BK[1], _T("res\\bg_night.png"));
loadimage(&bk, _T("res\\bg_day.png"));
loadimage(&OVEIMG, _T("res\\bg_day.png"));
loadimage(&BIRD[0][0], _T("res\\bird0_0.png"));
loadimage(&BIRD[0][1], _T("res\\bird0_1.png"));
loadimage(&BIRD[0][2], _T("res\\bird0_2.png"));
loadimage(&BIRD[1][0], _T("res\\bird1_0.png"));
loadimage(&BIRD[1][1], _T("res\\bird1_1.png"));
loadimage(&BIRD[1][2], _T("res\\bird1_2.png"));
loadimage(&BIRD[2][0], _T("res\\bird2_0.png"));
loadimage(&BIRD[2][1], _T("res\\bird2_1.png"));
loadimage(&BIRD[2][2], _T("res\\bird2_2.png"));
loadimage(&PIPE[0][0], _T("res\\pipe_down.png"));
loadimage(&PIPE[0][1], _T("res\\pipe_up.png"));
loadimage(&PIPE[1][0], _T("res\\pipe2_down.png"));
loadimage(&PIPE[1][1], _T("res\\pipe2_up.png"));
loadimage(&NUMBER[0][0], _T("res\\font_048.png"));
loadimage(&NUMBER[0][1], _T("res\\font_049.png"));
loadimage(&NUMBER[0][2], _T("res\\font_050.png"));
loadimage(&NUMBER[0][3], _T("res\\font_051.png"));
loadimage(&NUMBER[0][4], _T("res\\font_052.png"));
loadimage(&NUMBER[0][5], _T("res\\font_053.png"));
loadimage(&NUMBER[0][6], _T("res\\font_054.png"));
loadimage(&NUMBER[0][7], _T("res\\font_055.png"));
loadimage(&NUMBER[0][8], _T("res\\font_056.png"));
loadimage(&NUMBER[0][9], _T("res\\font_057.png"));
loadimage(&NUMBER[1][0], _T("res\\number_context_00.png"));
loadimage(&NUMBER[1][1], _T("res\\number_context_01.png"));
loadimage(&NUMBER[1][2], _T("res\\number_context_02.png"));
loadimage(&NUMBER[1][3], _T("res\\number_context_03.png"));
loadimage(&NUMBER[1][4], _T("res\\number_context_04.png"));
loadimage(&NUMBER[1][5], _T("res\\number_context_05.png"));
loadimage(&NUMBER[1][6], _T("res\\number_context_06.png"));
loadimage(&NUMBER[1][7], _T("res\\number_context_07.png"));
loadimage(&NUMBER[1][8], _T("res\\number_context_08.png"));
loadimage(&NUMBER[1][9], _T("res\\number_context_09.png"));
loadimage(&NUMBER[2][0], _T("res\\number_score_00.png"));
loadimage(&NUMBER[2][1], _T("res\\number_score_01.png"));
loadimage(&NUMBER[2][2], _T("res\\number_score_02.png"));
loadimage(&NUMBER[2][3], _T("res\\number_score_03.png"));
loadimage(&NUMBER[2][4], _T("res\\number_score_04.png"));
loadimage(&NUMBER[2][5], _T("res\\number_score_05.png"));
loadimage(&NUMBER[2][6], _T("res\\number_score_06.png"));
loadimage(&NUMBER[2][7], _T("res\\number_score_07.png"));
loadimage(&NUMBER[2][8], _T("res\\number_score_08.png"));
loadimage(&NUMBER[2][9], _T("res\\number_score_09.png"));
loadimage(&MEDAL[0], _T("res\\medals_3.png"));
loadimage(&MEDAL[1], _T("res\\medals_2.png"));
loadimage(&MEDAL[2], _T("res\\medals_1.png"));
loadimage(&MEDAL[3], _T("res\\medals_0.png"));
loadimage(&LAND, _T("res\\land.png"));
loadimage(&PLAY, _T("res\\button_play.png"));
loadimage(&OVER, _T("res\\text_game_over.png"));
loadimage(&READY, _T("res\\text_ready.png"));
loadimage(&TITLE, _T("res\\title.png"));
loadimage(&TUTORIAL, _T("res\\tutorial.png"));
loadimage(&PANEL, _T("res\\score_panel.png"));
loadimage(&NEW, _T("res\\new.png"));
}
大家赶紧去动手试试吧!
此外,我也给大家分享我收集的其他资源,从最零基础开始的教程到C语言C++项目案例,帮助大家在学习C语言的道路上披荆斩棘!
编程学习书籍分享:
编程学习项目分享:
整理分享(多年学习的源码、项目实战视频、项目笔记,基础入门教程)最重要的是你可以在群里面交流提问编程问题哦!
对于C/C++感兴趣可以关注小编在后台私信我:【编程交流】一起来学习哦!可以领取一些C/C++的项目学习视频资料哦!已经设置好了关键词自动回复,自动领取就好了!