整体思路
在开始游戏之前,我们先了解一些辅助函数
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/lw/article/details/
完整代码
#include
#include
#include
#include
#include
//辅助函数 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游戏已经实现完了,多有不足之处,任需要继续改进。
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/218613.html原文链接:https://javaforall.net
