最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C語言實現(xiàn)像素鳥游戲

 更新時間:2022年05月13日 08:45:51   作者:無限的菜鳥  
這篇文章主要為大家詳細介紹了C語言實現(xiàn)像素鳥游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了C語言實現(xiàn)像素鳥游戲的具體代碼,供大家參考,具體內容如下

在進入更復雜的學習之前,我們最后實現(xiàn)一個小游戲——像素鳥。

下落的小鳥

首先我們寫好游戲代碼框架并實現(xiàn)小鳥下落和上升(按空格)的功能:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <cwindow.h>

//全局變量
int high,width;?? ?//畫面尺寸
int bird_x,bird_y;?? ?//小鳥坐標
int barl_y,barl_xTop,barl_xDowm;?? ?//障礙物

void gotoxy(int x, int y)?? ?//移動光標便于清屏重畫
{
?? ?HANDLE handle = GetStdHandle(STD_UOTPUT_HANDLE);
?? ?CROOD pos;
?? ?pos.X = x;
?? ?pos.Y = y;
?? ?SetConsoleCursorPosition(handle, pos);
}

void startup()?? ??? ?//數(shù)據(jù)初始化
{
?? ?high = 15;
?? ?width = 20;
?? ?bird_x = 0;
?? ?bird_y = width/3;
}

void show()?? ??? ?//顯示畫面
{
?? ?gotoxy(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
?? ??? ??? ??? ?printf(" ");
?? ??? ?}
?? ??? ?print("\n");?? ?//每經(jīng)一次行循環(huán)就換行
?? ?}
}

void updateWithoutInput()
{
?? ?bird_x ++;
?? ?sleep(150);
}

void updateWithInput()
{
?? ?char input;
?? ?if(kbhit())?? ?//判斷是否有輸入
?? ?{
?? ??? ?input = getch();
?? ??? ?if(input==' ')
?? ??? ??? ?bird_x = bird_x - 2;
?? ?}
}

int main()
{
?? ?startup();
?? ?while(1)
?? ?{
?? ??? ?show();
?? ??? ?updateWithoutInput();
?? ??? ?updateWithInput();
?? ?}
?? ?return 0;
}

顯示障礙物

我們在上一步的基礎上完成障礙物的繪制,使用全局變量barl_y, barl_xTop, barl_xDown描述相關量,如圖:

加入以下代碼片段:

數(shù)據(jù)初始化

barl_y = width/2;
barl_xDown = high/2;
barl_xTop = high/3;

輸出循環(huán)

...
else if ((j==barl_y)&&((i>barl_xDown)||(i<barl_xTop)))
?? ?printf("*");
...

障礙物移動(在updateWithoutInput里)

barl_y --;

判定碰撞

接下來判定當障礙物 y 坐標到達小鳥位置時是否有碰撞發(fā)生,若有,則游戲失敗,反之則得分加一。

加入如下代碼段:

int score; //全局變量,得分

void startup()
{
?? ?...
?? ?score = 0;?? ?
?? ?...
}

void updateWithoutInput()
{
?? ?...
?? ?if(bird_y == barl_y)
?? ?{
?? ??? ?if((bird_x>=barl_xTop)&&(bird_x<=barl_xDown))
?? ??? ??? ?score ++;
?? ??? ?else?
?? ??? ?{
?? ??? ??? ?printf("GG\n");
?? ??? ??? ?system("pause");
?? ??? ??? ?exit(0);
?? ??? ?}
?? ?}
?? ?...
}

循環(huán)障礙物

到這里我們就要是障礙物循環(huán)出現(xiàn),因為不管怎么樣也不應該只有一個障礙物吧!同時,在此還將利用 rand() 隨機障礙物空隙坐標。

加入以下代碼段:

if(barl_y <= 0)
{
?? ?barl_y = width;
?? ?int temp =rand() % int(high * 0.8);?? ?//使用臨時變量儲存障礙物坐標信息
?? ?barl_xDown = temp + high/10;
?? ?barl_xTop = temp - high/10;
}

這里對臨時變量加減高度除以十的操作是為了防止生成新障礙物的空隙在太過邊緣的位置。

小結

完整代碼如下(我是打字機器):

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <cwindow.h>

//全局變量
int high,width;?? ?//畫面尺寸
int bird_x,bird_y;?? ?//小鳥坐標
int barl_y,barl_xTop,barl_xDowm;?? ?//障礙物
int score; ?? ??? ?//得分

void gotoxy(int x, int y)?? ?//移動光標便于清屏重畫
{
?? ?HANDLE handle = GetStdHandle(STD_UOTPUT_HANDLE);
?? ?CROOD pos;
?? ?pos.X = x;
?? ?pos.Y = y;
?? ?SetConsoleCursorPosition(handle, pos);
}

void startup()?? ??? ?//數(shù)據(jù)初始化
{
?? ?high = 15;
?? ?width = 20;
?? ?score = 0;?? ?
?? ?bird_x = 0;
?? ?bird_y = width/3;
?? ?barl_y = width/2;
?? ?barl_xDown = high/2;
?? ?barl_xTop = high/3;
}

void show()?? ??? ?//顯示畫面
{
?? ?gotoxy(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==barl_y)&&((i>barl_xDown)||(i<barl_xTop)))
?? ??? ??? ??? ?printf("*");
?? ??? ??? ?else
?? ??? ??? ??? ?printf(" ");
?? ??? ?}
?? ??? ?print("\n");?? ?//每經(jīng)一次行循環(huán)就換行
?? ?}
}

void updateWithoutInput()
{
?? ?bird_x ++;
?? ?barl_y --;
?? ?if(bird_y == barl_y)
?? ?{
?? ??? ?if((bird_x>=barl_xTop)&&(bird_x<=barl_xDown))
?? ??? ??? ?score ++;
?? ??? ?else?
?? ??? ?{
?? ??? ??? ?printf("GG\n");
?? ??? ??? ?system("pause");
?? ??? ??? ?exit(0);
?? ??? ?}
?? ?}
?? ?if(barl_y <= 0)
?? ?{
?? ??? ?barl_y = width;
?? ??? ?int temp =rand() % int(high * 0.8);?? ?//使用臨時變量儲存障礙物坐標信息
?? ??? ?barl_xDown = temp + high/10;
?? ??? ?barl_xTop = temp - high/10;
?? ?}
?? ?sleep(150);
}

void updateWithInput()
{
?? ?char input;
?? ?if(kbhit())?? ?//判斷是否有輸入
?? ?{
?? ??? ?input = getch();
?? ??? ?if(input==' ')
?? ??? ??? ?bird_x = bird_x - 2;
?? ?}
}

int main()
{
?? ?startup();
?? ?while(1)
?? ?{
?? ??? ?show();
?? ??? ?updateWithoutInput();
?? ??? ?updateWithInput();
?? ?}
?? ?return 0;
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論

扎兰屯市| 永州市| 汤阴县| 神池县| 英山县| 天祝| 兴隆县| 青田县| 卢氏县| 大邑县| 隆回县| 诸城市| 论坛| 新丰县| 尼玛县| 河池市| 老河口市| 三亚市| 莎车县| 元氏县| 井研县| 龙川县| 保定市| 兰考县| 霍邱县| 清苑县| 鹰潭市| 且末县| 富裕县| 青海省| 潮安县| 若羌县| 房产| 汶上县| 景谷| 上蔡县| 历史| 昔阳县| 孝昌县| 岢岚县| 乌兰浩特市|