小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

使用c語(yǔ)言實(shí)現(xiàn)飛機(jī)游戲

 GhostTrees 2019-05-19

在這里,我主要使用scanf函數(shù)和printf函數(shù)來(lái)實(shí)現(xiàn)一個(gè)簡(jiǎn)單的飛機(jī)游戲,并且通過(guò)函數(shù)的形式實(shí)現(xiàn)。

整體思路:main函數(shù)

在這里,主要是使用一個(gè)簡(jiǎn)易的游戲框架,來(lái)減小開(kāi)發(fā)游戲時(shí)的難度

int main()

{

    startup();//初始化

    while (1//游戲循環(huán)進(jìn)行

    {

        show();//顯示畫(huà)面  

        updateWitoutIput();//與用戶輸入無(wú)關(guān)的更新

        updateWithInput(); //與用戶輸入有關(guān)的更新

    }

    system('pause');

    return 0;

}

全局變量的定義

為了方便之后代碼的書(shū)寫(xiě),在這里,我們使用宏定義代替部分全局變量

//全局變量的定義

#define HIGH 20  //游戲界面高度

#define WIDTH 30  // 游戲界面寬度

#define NUM 20  //敵機(jī)下落速度



int position_x, position_y;  //飛機(jī)位置

int bullet_x, bullet_y;  //子彈位置

int enemy_x, enemy_y;  //敵機(jī)位置

int score;  //得分

全局變量的初始化

void startup()//數(shù)據(jù)的初始化

{

    //初始化飛機(jī)

    position_x = HIGH / 2//高度

    position_y = WIDTH / 2//寬度



    //初始化子彈

    bullet_x = -1;  //使子彈出現(xiàn)在屏幕外

    bullet_y = position_y;



    //初始化敵機(jī)

    enemy_x = 0;

    enemy_y = position_y;



    //初始化得分

    score = 0;

}

顯示函數(shù)show()

輸出飛機(jī)、敵機(jī)、子彈等

使用循環(huán)語(yǔ)句,在滿足飛機(jī)、敵機(jī)、子彈位置條件時(shí)輸出對(duì)應(yīng)的圖案(飛機(jī): * 敵機(jī): @ 子彈: | ),其余位置輸出‘ ’或‘\n’

//輸出每一行每一列

for (i = 0; i < HIGH; i++) //行  

    {

        for (j = 0; j < WIDTH; j++) //列

        {

            if (i == position_x && j == position_y)//輸出飛機(jī)

            {

                printf('*');

            }

            else if (i == bullet_x && j == bullet_y)//輸出子彈

            {

                printf('|');

            }

            else if (i == enemy_x && j == enemy_y)//輸出敵機(jī)

            {

                printf('@');

            }

            else

            {

                printf(' ');

            }

        }

        printf('\n');

    }

輸出得分

printf('得分:%d', score);

清屏

在輸出輸出飛機(jī)等圖標(biāo)時(shí),需要使用清屏函數(shù)使得光標(biāo)回到 (0,0) 處,方便下一次輸入

清屏函數(shù)

#include <windows.h>

system('cls');

使光標(biāo)回到 (0,0)

gotoxy(00); 
void gotoxy(int x, int y) //將光標(biāo)調(diào)整到(x,y)的位置

{

    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);

    COORD pos;

    pos.X = x;

    pos.Y = y;

    SetConsoleCursorPosition(handle, pos);

}

隱藏光標(biāo)顯示

為了防止光標(biāo)閃爍的過(guò)于頻繁,我們使用隱藏光標(biāo)顯示函數(shù)隱藏光標(biāo)

oid HideCursor()//隱藏光標(biāo)顯示函數(shù)

{

    CONSOLE_CURSOR_INFO cursor_info = { 10 };

    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);

}

//該函數(shù)只需調(diào)用一次即可

與用戶輸入無(wú)關(guān)的部分

子彈上落

if (bullet_x > -1//讓子彈向上落

{

    bullet_x--;

}

敵機(jī)下落,到達(dá)底部后,生成新的敵機(jī)

//創(chuàng)建一個(gè)靜態(tài)變量,當(dāng)靜態(tài)變量滿足一定條件時(shí),敵機(jī)下落一次,這樣可以控制敵機(jī)下降的速度同時(shí)不影響飛機(jī)移動(dòng)的速度

static int speed = 0//控制敵機(jī)下落速度

if (speed < NUM)  //每進(jìn)行NUM次敵機(jī)下落一次

{

    speed++;

}

else

{

    enemy_x++;  //敵機(jī)下落一次

    speed = 0;

}

if (enemy_x > HIGH)  //敵機(jī)一直下落到底部

{

    enemy_x = -1;   //生成新的飛機(jī)

    enemy_y = rand() % WIDTH;

}

命中敵機(jī),得分+1,同時(shí)生成新的敵機(jī)

if (bullet_x == enemy_x && bullet_y == enemy_y)  //命中

{

    score++;  //得分+1



    enemy_x = -1;   //生成新的飛機(jī)

    enemy_y = rand() % WIDTH;



    bullet_x = -1;  //讓子彈直接出現(xiàn)屏幕外,直到下一次發(fā)射子彈

}

與用戶輸入有關(guān)的部分

在這里,我們可以使用scanf函數(shù)或者getch函數(shù)實(shí)現(xiàn)在用戶輸入 ‘w’ ‘s’ ‘a(chǎn)’ ‘d’ 時(shí)對(duì)上下左右的控制

scanf函數(shù)

scanf('%c', &input);

if (input == 'w')

    position_x--;

if (input == 's')

    position_x++;

if (input == 'a')

    position_y--;

if (input == 'd')

    position_y++;

if (input == ' ')

{

    bullet_x = position_x - 1;

    bullet_y = position_y;

}

getch函數(shù)

if (_kbhit())  //kbhit()函數(shù):檢查當(dāng)前是否有鍵盤(pán)輸入,若有則返回一個(gè)非0值,否則返回0,頭文件<conio.h>

{

    input = _getch();  //getch()是一個(gè)不回顯函數(shù),當(dāng)用戶按下某個(gè)字符時(shí),函數(shù)自動(dòng)讀取,但是不會(huì)顯示在屏幕上,無(wú)需按回車(chē),

    if (input == 'w')

        position_x--;

    if (input == 's')

        position_x++;

    if (input == 'a')

        position_y--;

    if (input == 'd')

        position_y++;

    if (input == ' ')

    {

        bullet_x = position_x - 1;

        bullet_y = position_y;

    }

}

在這里,我們選擇使用getch()函數(shù),從而使得程序更加方便(減少用戶回車(chē)的輸入及屏幕上的顯示)

注意事項(xiàng)

在實(shí)現(xiàn)代碼時(shí)需要注意在使用各個(gè)函數(shù)時(shí)對(duì)頭文件的調(diào)用盡量減少對(duì)全局變量的創(chuàng)建

完整代碼

#include <stdio.h>

#include <windows.h>

#include <stdlib.h>

#include <conio.h>

#include <time.h>



//全局變量的定義

#define HIGH 20  //游戲界面高度

#define WIDTH 30  // 游戲界面寬度

#define NUM 10  //敵機(jī)下落速度



int position_x, position_y;  //飛機(jī)位置

int bullet_x, bullet_y;  //子彈位置

int enemy_x, enemy_y;  //敵機(jī)位置

int score;  //得分





void gotoxy(int x, int y) //將光標(biāo)調(diào)整到(x,y)的位置

{

    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);

    COORD pos;

    pos.X = x;

    pos.Y = y;

    SetConsoleCursorPosition(handle, pos);

}



void HideCursor()  //隱藏光標(biāo)顯示

{

    CONSOLE_CURSOR_INFO cursor_info = { 10 };

    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);

}



void startup()//數(shù)據(jù)的初始化

{

    //初始化飛機(jī)

    position_x = HIGH / 2//高度

    position_y = WIDTH / 2//寬度



    //初始化子彈

    bullet_x = -1;

    bullet_y = position_y;



    //初始化敵機(jī)

    enemy_x = 0;

    enemy_y = position_y;



    //初始化得分

    score = 0;

}



void show()//顯示畫(huà)面

{

    //system('cls'); //清屏函數(shù)  

    gotoxy(00); //使光標(biāo)回到0,0



    int i, j;

    for (i = 0; i < HIGH; i++) //行

    {

        for (j = 0; j < WIDTH; j++) //列

        {

            if (i == position_x && j == position_y)//輸出飛機(jī)

            {

                printf('*');

            }

            else if (i == bullet_x && j == bullet_y)//輸出子彈

            {

                printf('|');

            }

            else if (i == enemy_x && j == enemy_y)//輸出敵機(jī)

            {

                printf('@');

            }

            else

            {

                printf(' ');

            }

        }

        printf('\n');

    }

    printf('得分:%d', score);

}



void updateWitoutIput()//與用戶輸入無(wú)關(guān)的更新

{

    if (bullet_x > -1//讓子彈向上落

    {

        bullet_x--;

    }



    if (bullet_x == enemy_x && bullet_y == enemy_y) //命中敵機(jī)

    {

        score++;  //得分+1



        enemy_x = -1;   //生成新的飛機(jī)

        enemy_y = rand() % WIDTH;



        bullet_x = -1;  //讓子彈直接出現(xiàn)屏幕外,直到下一次發(fā)射子彈

    }



    static int speed = 0//控制敵機(jī)下落速度

    if (speed < NUM)  //每進(jìn)行NUM次敵機(jī)下落一次

    {

        speed++;

    }

    else

    {

        enemy_x++;

        speed = 0;

    }

    if (enemy_x > HIGH)  //敵機(jī)一直下落到底部

    {

        enemy_x = -1

        enemy_y = rand() % WIDTH;

    }



}



void updateWithInput()//與用戶輸入有關(guān)的更新

{

    //用戶輸入

    char input;

    if (_kbhit())

    {

        input = _getch();

        if (input == 'w')

            position_x--;

        if (input == 's')

            position_x++;

        if (input == 'a')

            position_y--;

        if (input == 'd')

            position_y++;

        if (input == ' ')

        {

            bullet_x = position_x - 1;

            bullet_y = position_y;

        }

    }

}

int main()

{

    startup();//初始化

    HideCursor();

    srand((unsigned)time(NULL));

    while (1)

    {

        show();//顯示畫(huà)面  

        updateWitoutIput();//與用戶輸入無(wú)關(guān)的更新  //更新數(shù)據(jù)

        updateWithInput(); //與用戶輸入有關(guān)的更新  //輸入分析

    }

    system('pause');

    return 0;

}

程序效果

總結(jié)

雖然完成了一個(gè)簡(jiǎn)單的飛機(jī)游戲,但是很多的功能都未能實(shí)現(xiàn),如改變飛機(jī)的形狀,增加游戲的難度,飛機(jī)生命的減少等等,任需要繼續(xù)的努力。

--------------------- 

作者:木頭i 

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多