C语言实现Flappy Bird小游戏
更新时间:2020年4月25日 17:26 点击:1981
本文实例为大家分享了C语言实现Flappy Bird小游戏的具体代码,供大家参考,具体内容如下
#include<stdio.h> #include<stdlib.h> #include<conio.h> #include<time.h> #include<Windows.h> /********函数变量声明********/ #define PR_Box printf("■") #define PR_Gold printf("★") #define PR_Ag printf("☆") #define PR_FBird printf("Ю") #define PR_DBird printf("Ф") #define PR_Land printf("┳┳┯") #define PR_Bg_TL printf("╔") #define PR_Bg_TR printf("╗") #define PR_Bg_DL printf("╚") #define PR_Bg_DR printf("╝") #define PR_Bg_X printf("═") #define PR_Bg_Y printf("║") #define PR_Blank printf(" "); int Grade = 1, C_Gold = 0, C_Ag = 0, Score = 0, Delay_time = 1000, Max_blank = 9, Distance = 18; typedef struct Birds { int x, y; int condition; }Birds; Birds * Bird; typedef struct Bg { int x, y; int l_blank; int reward[9]; struct Bg * pri; struct Bg * next; }Bg; Bg * Bg1; void Position(int x, int y) { COORD pos = { x - 1, y - 1 }; HANDLE Out = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(Out, pos); } void CreatBird() { Bird -> x = 41; Bird -> y = 10; Bird -> condition = 0; } void CreatBg() { Bg * Bg2 = (Bg * ) malloc(sizeof(Bg)); Bg1 -> x = 90; Bg1 -> y = 8; Bg2 -> x = Bg1 -> x + Distance; Bg2 -> y = 9; Bg1 -> l_blank = Max_blank - Grade; Bg2 -> l_blank = Max_blank - Grade; Bg1 -> next = Bg2; Bg1 -> pri = Bg2; Bg2 -> next = Bg1; Bg2 -> pri = Bg1; } void InsertBg(Bg * p) { int temp; Bg * Bgs = (Bg * ) malloc(sizeof(Bg)); Bgs -> x = p -> pri -> x + Distance; Bgs -> l_blank = Max_blank - Grade; srand((int) time(0)); temp = rand(); if (temp % 2 == 0) //++ { if ((temp % 4 + p -> pri -> y + Max_blank - Grade) < 21) Bgs -> y = p -> pri -> y + temp % 4; else Bgs -> y = p -> pri -> y; } else { if ((p -> pri -> y - temp % 4) > 2) Bgs -> y = p -> pri -> y - temp % 4; else Bgs -> y = p -> pri -> y; } Bgs -> pri = p -> pri; Bgs -> next = p; p -> pri -> next = Bgs; p -> pri = Bgs; } void Check_Bg(Bg * q) { Bg * p = q; int i = 0, temp; while (++i <= 5) { if (p -> x > -4) p = p -> next; else { srand((int) time(0)); temp = rand(); if (temp % 2 == 0) //++ { if ((temp % 4 + p -> y + Max_blank - Grade) < 21) p -> y = p -> y + temp % 4; else p -> y = p -> y; p -> x = p -> pri -> x + Distance; p -> l_blank = Max_blank - Grade; } else { if ((p -> y - temp % 4) > 2) p -> y = p -> y - temp % 4; else p -> y = p -> y; p -> x = p -> pri -> x + Distance; p -> l_blank = Max_blank - Grade; } } } } void Loop_Bg(Bg * q) { Bg * p = q; int i = 0; while (++i <= 5) { p -> x = p -> x - 1; p = p -> next; if (Bird -> x == p -> x) { Score += 1; if (Score % 4 == 0 && Grade < 4) Grade++; } } } void Prt_Bg(Bg * q) { Bg * p = q; int i = 0, k, j; while (++i <= 5) { if (p -> x > 0 && p -> x <= 78) { for (k = 2; k < p -> y; k++) { Position(p -> x + 1, k); PR_Box; PR_Box; PR_Blank } Position(p -> x, p -> y); PR_Box; PR_Box; PR_Box; PR_Blank; Position(p -> x, p -> y + p -> l_blank); PR_Box; PR_Box; PR_Box; PR_Blank; k = k + p -> l_blank + 1; for (k; k <= 22; k++) { Position(p -> x + 1, k); PR_Box; PR_Box; PR_Blank; } Position(p -> x, 23); for (k = 1; k < Distance / 3 - 2; k++) PR_Land; } p = p -> next; if (p -> x == 0) { for (j = 2; j < p -> y; j++) { Position(p -> x + 1, j); PR_Blank; PR_Blank; } Position(p -> x + 1, p -> y); PR_Blank; PR_Blank; PR_Blank; Position(p -> x + 1, p -> y + Max_blank - Grade); PR_Blank; PR_Blank; PR_Blank; j = j + Max_blank - Grade + 1; for (j; j <= 22; j++) { Position(p -> x + 1, j); PR_Blank; PR_Blank; } } } } void PrtBg() { int i; Position(1, 1); PR_Bg_TL; Position(79, 1); PR_Bg_TR; Position(1, 24); PR_Bg_DL; Position(79, 24); PR_Bg_DR; for (i = 3; i <= 78; i += 2) { Position(i, 1); PR_Bg_X; Position(i, 24); PR_Bg_X; } } void PrtBird() { Position(Bird -> x, Bird -> y - 1); PR_Blank; Position(Bird -> x, Bird -> y); PR_FBird; Position(38, 2); printf("Score:%d", Score); } int CheckYN(Bg * q) { Bg * p = q; int i = 0; while (++i <= 5) { if (Bird -> y > 23) return 0; if (Bird -> x == p -> x && Bird -> y <= p -> y) return 0; if ((Bird -> x == p -> x || Bird -> x == p -> x + 1 || Bird -> x == p -> x + 2) && Bird -> y == p -> y) return 0; if (Bird -> x == p -> x && Bird -> y > p -> y + p -> l_blank) return 0; if ((Bird -> x == p -> x || Bird -> x == p -> x + 1 || Bird -> x == p -> x + 2) && Bird -> y == p -> y + p -> l_blank) return 0; p = p -> next; } return 1; } void Prtfirst() { printf("══════════════════════════════════════\n"); printf(" ■■ ■■\n"); printf(" ■■ ■■\n"); printf(" ■■ ■■ C语言版 Flappy Bird\n"); printf(" ■■ ■■ 瞎搞人:yyposs\n"); printf(" ■■ ■■ 瞎搞日期:2014.2\n"); printf(" ■■ ■■ 耗时:4小时\n"); printf(" ■■■ ■■ 游戏说明:\n"); printf(" ■■ 1-按上箭头使鸟起飞\n"); printf(" ■■ 2-等级越高,难度越大!\n"); printf(" Ю ■■■\n"); printf("\n"); printf(" ■■■ 欢迎各路大神一起探讨\n"); printf(" ■■\n"); printf(" ■■\n"); printf(" ■■ ■■■ 【无版权,随意修改】\n"); printf(" ■■ ■■\n"); printf(" ■■ Ф ■■\n"); printf(" ■■ ■■\n"); printf(" ■■ ■■\n"); printf(" ■■ ■■\n"); printf(" ■■ ■■\n"); printf(" ┳┳┯┳┳┯┳┳┯┳┳┯┳┳┯┳┳┯┳┳┯┳┳┯┳┳┯┳┳┯┳┳┯┳┳┯┳\n"); system("pause"); Position(1, 1); int i = 0; while (i++ < 40 * 25) PR_Blank; } void main() { int i = 0; Bird = (Birds * ) malloc(sizeof(Birds)); Bg1 = (Bg * ) malloc(sizeof(Bg)); Prtfirst(); PrtBg(); CreatBg(); InsertBg(Bg1); InsertBg(Bg1); InsertBg(Bg1); CreatBird(); while (1) { if (!CheckYN(Bg1)) break; Check_Bg(Bg1); Prt_Bg(Bg1); PrtBird(); Loop_Bg(Bg1); Bird -> y = Bird -> y + 1; if (GetAsyncKeyState(VK_UP)) { Position(Bird -> x, Bird -> y - 1); PR_Blank; Bird -> y = Bird -> y - 4; } while (i++ < 500); { Sleep(100); } i = 0; } Position(38, 10); printf("You Lost!"); Position(1, 25); system("pause"); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。
上一篇: C++中指针函数与函数指针的使用
相关文章
- 这篇文章主要为大家详细介绍了C语言实现放烟花的程序,有音乐播放,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-02-23
- 本篇文章主要介绍C语言中char的知识,并附有代码实例,以便大家在学习的时候更好的理解,有需要的可以看一下...2020-04-25
- 这篇文章主要介绍了详解如何将c语言文件打包成exe可执行程序,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-25
- free函数是释放之前某一次malloc函数申请的空间,而且只是释放空间,并不改变指针的值。下面我们就来详细探讨下...2020-04-25
- 这篇文章主要介绍了C语言中计算正弦的相关函数总结,包括正弦和双曲线正弦以及反正弦的函数,需要的朋友可以参考下...2020-04-25
详解C语言中的rename()函数和remove()函数的使用方法
这篇文章主要介绍了详解C语言中的rename()函数和remove()函数的使用方法,是C语言入门学习中的基础知识,需要的朋友可以参考下...2020-04-25- 这篇文章主要介绍了C语言中求和、计算平均值、方差和标准差的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-12-10
- 本篇文章主要讲解C语言 基本语法,这里提供简单的示例和代码来详细讲解C语言的基本语法,开始学习C语言的朋友可以看一下,希望能够给你带来帮助...2021-09-18
- 这篇文章主要介绍了C语言中send()函数和sendto()函数的使用方法,是C语言入门学习中的基础知识,需要的朋友可以参考下...2020-04-25
- 今天小编就为大家分享一篇C语言实现从文件读入一个3*3数组,并计算每行的平均值,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-04-25
- 这篇文章主要介绍了C语言中memcpy 函数的用法详解的相关资料,需要的朋友可以参考下...2020-04-25
- 这篇文章主要介绍了使用C语言操作文件的基本函数整理,包括创建和打开以及关闭文件的操作方法,需要的朋友可以参考下...2020-04-25
- 这篇文章主要介绍了C语言中查找字符在字符串中出现的位置的方法,分别是strchr()函数和strrchr()函数的使用,需要的朋友可以参考下...2020-04-25
- 很多同学在学习c语言的时候是不是会碰到a++和++a都有甚么作用啊。今天我们就来探讨下...2020-04-25
- 这篇文章主要对C语言中const关键字的用法进行了详细的分析介绍,需要的朋友可以参考下...2020-04-25
- 下面小编就为大家带来一篇C语言实现时间戳转日期的算法(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-04-25
- 这篇文章主要介绍了C语言之整数划分问题(递归法)实例代码的相关资料,需要的朋友可以参考下...2020-04-25
- 本文给大家简单介绍下c实现linux下的数据库备份的方法和具体的源码,十分的实用,有需要的小伙伴可以参考下。...2020-04-25
C语言正则表达式详解 regcomp() regexec() regfree()用法详解
C语言处理正则表达式常用的函数有regcomp()、regexec()、regfree()和regerror(),这里就为大家介绍一下,需要的朋友可以参考一下啊...2020-04-25- 这篇文章主要介绍了c语言实现找最大值最小值位置查找,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-04