您好,登錄后才能下訂單哦!
小編給大家分享一下C語言怎么實現簡易版flappy bird小游戲,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
游戲界面如下:
首先,先畫出整個小游戲實現的流程圖,如下:
思路很簡單,整個游戲界面是由一個大的char類型數組構成,更新數組的值然后不停的打印出來就形成了動態效果。
由上圖看,大循環是保證游戲一直不斷的進行下去,小循環是讓小鳥的速度大于游戲界面里背景(由#構成的柱子)的速度(小鳥動四下柱子才動一下)。
下面是具體代碼(水平有限大家多多見諒,但是效果還是有的!)
Bird.c文件
#include <stdio.h> #include <windows.h> #include "Interface.h" int main(void) { InitialInterface(); for(;;) { newinterface(); scoring();//過一個柱子計一次分,所以和柱子更新速度一致 for (int i = 0; i < 4; i++)//小鳥的速度是柱子的4倍 { birdmove(); draw(); Sleep(50); } } return 0; }
Interface.h文件
#ifndef INTERFACE_H #define INTERFACE_H #define M 20 #define N 36 void InitialInterface(void); void newinterface(void); void birdmove(void); void scoring(void); void draw(void); #endif
Interface.c文件
#include <stdio.h> #include <stdlib.h> #include<conio.h> #include "interface.h" char interf[M][N] = {{ 32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35 }, { 32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35 }, { 32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35 }, { 32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35 }, { 32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35 }, { 32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35 }, { 32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35 }, { 32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35 }, { 32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,32,32 }, { 32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,32,32 }, { 38,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32 }, { 32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32 }, { 32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,35,35 }, { 32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,35,35 }, { 32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35 }, { 32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35 }, { 32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35 }, { 32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35 }, { 32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35 }, { 32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35 }, }; //初始界面矩陣,ASCII碼中“ ”是32,“&”是38表示小鳥,“#”是35用來畫柱子 int num = 0;//用于計數輸出并排兩列黑柱子同一位置 int black;//黑方塊位置 int p= M/2 ;//小鳥初始位置 int score = 0;//分數 /*初始化界面*/ void InitialInterface(void) { printf("\n 作者:xhyang,博客地址:http://blog.csdn.net/weixin_39449570\n"); printf(" 按\"w\"使小鳥跳起來,別落地,順利穿過盡可能多的柱子!\n"); for (int i = 0; i < M; i++) { printf(" "); for (int j = 0; j < N; j++) { printf("%c", interf[i][j]); } printf("\n"); } } /*更新界面各個柱子*/ void newinterface(void) { if (interf[0][1] == 35 && num==0)//當矩陣第二列為黑色方塊時,計算出下一次黑柱子上半部分的位置 { black = 5 + rand() % 5; num = 2;//黑柱子是兩列#組成,第二列與第一列位置一樣,用num保證兩列位置一致 } for (int i = 0; i < M; i++) { for (int j = 0; j < N - 1; j++) { interf[i][j] = interf[i][j + 1]; } if (interf[0][0] == 35 && (i < black || i>(black + 5)))//此時上面的第二列變成了第一列,更新下一個黑柱子,有了黑柱子上半部分位置+5即是下半部分的起始位置 { interf[i][N-1] = 35; } else { interf[i][N-1] = 32; } } if (num > 0) num--; } /*更新小鳥位置*/ void birdmove(void) { for (int a = 0; a < 3; a++) { if (a == 2 && p > 0)//減緩鳥的速度,使按鍵上跳速度是下落的4倍 { p = p + 1; } if (_kbhit()) { if (_getch() == 'w' || _getch() == 'W') { p = p - 3; } } } } /*計分*/ void scoring(void) { if (p > 20 || interf[p][0] == 35) { system("cls"); printf("\n\n 游戲結束!\n\n"); printf(" 最終得分:%d\n\n\n", score); system("pause"); } if (interf[0][0] == 35 && interf[0][1] == 32 ) score++; } /*重畫界面*/ void draw(void) { system("cls"); printf("\n 作者:xhyang,博客地址:http://blog.csdn.net/weixin_39449570\n"); printf(" 按\"w\"使小鳥跳起來,別落地,順利穿過盡可能多的柱子!\n"); for (int i = 0; i < M; i++) { printf(" "); for (int j = 0; j < N; j++) { if (i == p && j == 0 && interf[p][0] != 35) printf("%c", 38); else printf("%c", interf[i][j]); } printf("\n"); } printf(" 得分:%d \n", score); }
以上是“C語言怎么實現簡易版flappy bird小游戲”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。