欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

在進(jìn)入更復(fù)雜的學(xué)習(xí)之前,我們最后實(shí)現(xiàn)一個(gè)小游戲——像素鳥。

下落的小鳥

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

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

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

void gotoxy(int x, int y)?? ?//移動(dòng)光標(biāo)便于清屏重畫
{
?? ?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;
}

顯示障礙物

我們?cè)谏弦徊降幕A(chǔ)上完成障礙物的繪制,使用全局變量barl_y, barl_xTop, barl_xDown描述相關(guān)量,如圖:

加入以下代碼片段:

數(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("*");
...

障礙物移動(dòng)(在updateWithoutInput里)

barl_y --;

判定碰撞

接下來判定當(dāng)障礙物 y 坐標(biāo)到達(dá)小鳥位置時(shí)是否有碰撞發(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),因?yàn)椴还茉趺礃右膊粦?yīng)該只有一個(gè)障礙物吧!同時(shí),在此還將利用 rand() 隨機(jī)障礙物空隙坐標(biāo)。

加入以下代碼段:

if(barl_y <= 0)
{
?? ?barl_y = width;
?? ?int temp =rand() % int(high * 0.8);?? ?//使用臨時(shí)變量?jī)?chǔ)存障礙物坐標(biāo)信息
?? ?barl_xDown = temp + high/10;
?? ?barl_xTop = temp - high/10;
}

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

小結(jié)

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

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

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

void gotoxy(int x, int y)?? ?//移動(dòng)光標(biāo)便于清屏重畫
{
?? ?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);?? ?//使用臨時(shí)變量?jī)?chǔ)存障礙物坐標(biāo)信息
?? ??? ?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;
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C++中幾種將整數(shù)轉(zhuǎn)換成二進(jìn)制輸出的方法總結(jié)

    C++中幾種將整數(shù)轉(zhuǎn)換成二進(jìn)制輸出的方法總結(jié)

    下面小編就為大家?guī)硪黄狢++中幾種將整數(shù)轉(zhuǎn)換成二進(jìn)制輸出的方法總結(jié)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-09-09
  • C C++ LeetCode題解在二叉樹中增加一行示例詳解

    C C++ LeetCode題解在二叉樹中增加一行示例詳解

    這篇文章主要為大家介紹了C C++ LeetCode題解在二叉樹中增加一行示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • C/C++中可變參數(shù)的用法詳細(xì)解析

    C/C++中可變參數(shù)的用法詳細(xì)解析

    可變參數(shù)的使用方法遠(yuǎn)遠(yuǎn)不止以下介紹的幾種,不過在C,C++中使用可變參數(shù)時(shí)要小心,在使用printf()等函數(shù)時(shí)傳入的參數(shù)個(gè)數(shù)一定不能比前面的格式化字符串中的’%’符號(hào)個(gè)數(shù)少,否則會(huì)產(chǎn)生訪問越界,運(yùn)氣不好的話還會(huì)導(dǎo)致程序崩潰
    2013-09-09
  • C++開源庫nlohmann/json的介紹和使用詳解

    C++開源庫nlohmann/json的介紹和使用詳解

    nlohmann/json?是一個(gè)C++實(shí)現(xiàn)的JSON解析器,使用非常方便直觀,這篇文章主要為大家詳細(xì)介紹了nlohmann/json的簡(jiǎn)介和使用,需要的可以參考下
    2023-12-12
  • ReSharper 的安裝使用詳細(xì)教程

    ReSharper 的安裝使用詳細(xì)教程

    resharper安裝教程是關(guān)于vs2012一個(gè)非常好用的插件的安裝教程,建議大家嘗試安裝,今天通過本教程幫助大家學(xué)習(xí)ReSharper 的安裝使用詳細(xì)教程,感興趣的朋友一起看看吧
    2021-06-06
  • 基于C++中sprintf的錯(cuò)誤總結(jié)詳解

    基于C++中sprintf的錯(cuò)誤總結(jié)詳解

    本篇文章是對(duì)C++中sprintf的錯(cuò)誤進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • 一文詳解C++中的引用與關(guān)鍵字auto

    一文詳解C++中的引用與關(guān)鍵字auto

    引用就是給一個(gè)已經(jīng)存在的變量取一個(gè)別名,與變量共用一段內(nèi)存空間。關(guān)鍵字auto一般可以用來自動(dòng)識(shí)別類型,本文主要來講講二者的相關(guān)知識(shí),需要的可以參考一下
    2023-04-04
  • 深入解析C++編程中類的封裝特性

    深入解析C++編程中類的封裝特性

    這篇文章主要介紹了深入解析C++編程中類的封裝特性,是C++入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-09-09
  • C++中浮點(diǎn)類型的具體使用

    C++中浮點(diǎn)類型的具體使用

    C++提供了不同精度的浮點(diǎn)類型,主要有?float、double?和?long?double,這些浮點(diǎn)類型具有不同的字節(jié)大小和范圍,用于滿足不同應(yīng)用場(chǎng)景的精度要求,本文主要介紹了C++中浮點(diǎn)類型的具體使用,感興趣的可以了解一下
    2023-08-08
  • C++實(shí)現(xiàn)LeetCode(110.平衡二叉樹)

    C++實(shí)現(xiàn)LeetCode(110.平衡二叉樹)

    這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(110.平衡二叉樹),本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07

最新評(píng)論