當前位置:首頁 » 編程語言 » c語言版flappybird

c語言版flappybird

發布時間: 2023-12-31 21:59:50

c語言能用來做什麼

C語言是許多高級計算機語言的基礎,學好C語言,能更好地學習其他語言,為以後學習打基礎。它既具有高級語言的特點,又具有匯編語言的特點。它的應用廣泛,具備很強的數據處理能力,各類科研都需要用到C語言。它是面向過程的語言。學好是語言再學習使用其他語言也會有很大的幫助。

❷ c語言游戲編程,下落的小鳥 求代碼

下落的小鳥

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
#include<Windows.h>
int Grade = 1, Score = 0, Max_blank = 9, Distance = 18;
struct Birds{int x; int y;}; //定義一種Birds數據類型(含3個成員)
Birds *Bird = (Birds*)malloc(sizeof(Birds)); //定義Birds類型 指針變數Bird並賦初值
struct Bg{int x, y; int l_blank; Bg *pri; Bg *next;}; //定義一種Bg數據類型(含5個成員)
Bg *Bg1 = (Bg*)malloc(sizeof(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 Csh( ) //初始化界面

{

printf("══════════════════════════════════════ ");

printf(" ■■ ■■ C語言版 Flappy Bird ");

printf(" ■■ ■■ ");

printf(" ■■ ■■ ");

printf(" ■■ ■■ 瞎搞人:yyposs原創 ");

printf(" ■■ ■■ 瞎搞日期:2014.2 ");

printf(" ■■ ■■ ");

printf(" ■■ ■■ 改編:鳴蟬百2021.7 ");

printf(" ■■ ■■ 操作:按向上方向鍵讓小鳥起飛 ");

printf(" ■■ ");

printf(" ■■ ");

printf(" ■■ ■■ ");

printf(" ■■ ■■ ");

printf(" ■■ ■■ ");

printf(" ■■ ■■ ");

printf(" ■■ ■■ DEVc++運行通過 ");

printf("══════════════════════════════════════ ");

printf(" 按鍵繼續…");

getch( );

system("cls");

}

void PrFK( ) //輸出方框(游戲范圍區)

{int i;

Position(1, 1); printf("╔"); Position(79, 1); printf("╗");

Position(1, 24); printf("╚"); Position(79, 24); printf("╝");

for (i = 3; i <= 78; i += 2){Position(i, 1); printf("═"); Position(i, 24); printf("═");}

for(i=2;i<=23;i++)

{ Position(1,i); printf("║");if(i<11)printf("0%d",i-1);else printf("%d",i-1);

Position(79,i); printf("║");

}

Position(4, 25); printf("小鳥即將出現,請准備按鍵起飛… ");

getch( );

Position(4, 25); printf(" ");

}

void CreatBg( ) //創建障礙物坐標(便於列印輸出)
{Bg *Bg2 = (Bg*)malloc(sizeof(Bg));
Bg1->x = 90; Bg1->y = 8; //確定障礙物的一對基本坐標(此時值是在游戲框之外)
Bg2->x = Bg1->x + Distance; Bg2->y = 9; //下一障礙物的基本坐標x、y
Bg1->l_blank = Max_blank - Grade; //障礙物上下兩部分之間的空白距離l_blank
Bg2->l_blank = Max_blank - Grade;
Bg1->next = Bg2; Bg1->pri = Bg2;
Bg2->next = Bg1; Bg2->pri = Bg1;
}


void InsertBg(Bg *p) //隨機改變障礙物的y坐標,讓空白通道有上下變化
{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( ); //產生一個隨機數並賦值給temp
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 CreatBird( ) //建立小鳥的坐標(初始列印輸出小鳥的位置)
{Bird->x = 41; Bird->y = 10;}

int CheckYN(Bg *q) //判斷游戲結束與否(值為0是要結束,為1沒有要結束)
{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 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 Prt_Bg(Bg *q) //列印輸出障礙物(依據其x、y坐標進行相應輸出)
{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); printf("■"); printf("■"); printf(" ");}
Position(p->x, p->y);
printf("■"); printf("■"); printf("■"); printf(" ");
Position(p->x, p->y + p->l_blank);
printf("■"); printf("■"); printf("■"); printf(" ");
k = k + p->l_blank + 1;
for (k; k <= 23; k++){Position(p->x + 1, k); printf("■"); printf("■"); printf(" ");}
}
p = p->next;
if (p->x == 0)
{for (j = 2; j<p->y; j++){Position(p->x + 1, j); printf(" "); printf(" ");}
Position(p->x + 1, p->y);
printf(" "); printf(" "); printf(" ");
Position(p->x + 1, p->y + Max_blank - Grade);
printf(" "); printf(" "); printf(" ");
j = j + Max_blank - Grade + 1;
for (j; j <= 22; j++){Position(p->x + 1, j); printf(" "); printf(" ");}
}
}
}

void PrtBird( ) //列印輸出小鳥
{Position(Bird->x, Bird->y - 1); printf(" ");
Position(Bird->x, Bird->y); printf("Ю");
Position(38, 2); printf("Score:%d", Score);
}
void Loop_Bg(Bg *q) //障礙物x坐標左移,並記錄成績
{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++;
}
}
}

int main( )
{int i = 0; int t;

while (1)

{
Csh( );PrFK( );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);printf(" ");
Bird->y = Bird->y - 4;
}
Sleep(200);
//程序延時200毫秒(數值大小決定游戲速度快慢)
i = 0;
}
Position(6, 25);
printf("游戲結束! 請輸入:0.退出 1.重玩");

scanf("%d",&t);

if (t==0)break;

system("cls"); Score = 0;

}
return 0;
}

熱點內容
c語言大於小於 發布:2024-11-30 06:54:43 瀏覽:499
如何知道老婆微信和密碼 發布:2024-11-30 06:46:16 瀏覽:848
java計劃 發布:2024-11-30 06:44:04 瀏覽:942
linux查看ftp日誌 發布:2024-11-30 06:33:19 瀏覽:475
設置截屏存儲 發布:2024-11-30 06:29:00 瀏覽:394
jpg演算法 發布:2024-11-30 06:28:55 瀏覽:195
怎麼刪除u盤中的文件夾 發布:2024-11-30 06:28:20 瀏覽:216
iphone文件夾打開 發布:2024-11-30 06:13:43 瀏覽:298
如何配置Javaweb環境 發布:2024-11-30 06:09:24 瀏覽:121
怎麼使用Androidapi 發布:2024-11-30 06:08:43 瀏覽:61