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

C語言用函數(shù)實現(xiàn)反彈球消磚塊

 更新時間:2022年05月12日 11:41:28   作者:輝小歌  
這篇文章主要為大家詳細(xì)介紹了C語言用函數(shù)實現(xiàn)反彈球消磚塊,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了C語言用函數(shù)實現(xiàn)反彈球消磚塊的具體代碼,供大家參考,具體內(nèi)容如下

一、項目描述和最終的成果展示

這是在上一次彈跳小項目上進(jìn)行了一系列的優(yōu)化和封裝。項目: 彈跳的小球
上次沒有用函數(shù)進(jìn)行的封裝。這次在上次的基礎(chǔ)上進(jìn)行封裝和一些功能的優(yōu)化。

最終效果圖如下:

二、封裝后的彈跳小球

代碼如下:

#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<windows.h>

//全局變量
int high,width; //游戲畫面大小
int ball_x,ball_y;//小球的坐標(biāo)
int ball_vx,ball_vy;//小球的速度

void gotoxy(int x,int y)//將光標(biāo)移動到(x,y)位置
{
?? ?HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
?? ?COORD pos;
?? ?pos.X = x;
?? ?pos.Y = y;
?? ?SetConsoleCursorPosition(handle,pos);
}

void startup()//數(shù)據(jù)的初始化
{
?? ?high = ?15;
?? ?width = 20;
?? ?ball_x = 0;
?? ?ball_y = width/2;
?? ?ball_vx = 1;
?? ?ball_vy = 1;
}

void show()//顯示畫面
{
?? ?gotoxy(0,0);//光標(biāo)移動到原點位置,以下重畫清屏
?? ?int i,j;
?? ?for(i=0;i<=high;i++)
?? ?{
?? ??? ?for(j=0;j<=width;j++)
?? ??? ?{
?? ??? ??? ?if( ( i == ball_x) && ( j == ball_y ) )
? ? ? ? ? ? ? ? printf("O");//輸出小球
?? ??? ??? ?else if( j == width)
?? ??? ??? ??? ?printf("+");//輸出右邊框
?? ??? ??? ?else if( i == high)
?? ??? ??? ??? ?printf("-");//輸出下邊框
?? ??? ??? ?else
?? ??? ??? ??? ?printf(" ");//輸出空格
?? ??? ?}
?? ??? ?printf("\n");
?? ?}
}

void updateWithoutInput()//與用戶輸入無關(guān)的更新
{
?? ?ball_x = ball_x + ball_vx;
?? ?ball_y = ball_y + ball_vy;

?? ?if( (ball_x == 0 ) || (ball_x == high-1 ))
?? ??? ?ball_vx = -ball_vx;
?? ?if( (ball_y == 0 ) || (ball_y == width-1 ))
?? ??? ?ball_vy = -ball_vy;

?? ?Sleep(50);
}

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

}
int main(void)
{
?? ?startup();//數(shù)據(jù)的初始化
?? ?while(1)
?? ?{
?? ??? ?show();//顯示畫面
?? ??? ?updateWithoutInput();//與用戶輸入無關(guān)的更新
?? ??? ?updateWithInput();//與用戶輸入有關(guān)的更新
?? ?}
?? ?return 0;
}

效果圖如下:

三、顯示移動擋板

代碼如下:

#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<windows.h>

//全局變量
int high,width; //游戲畫面大小
int ball_x,ball_y;//小球的坐標(biāo)
int ball_vx,ball_vy;//小球的速度
int position_x,position_y;//擋板的中心坐標(biāo)
int ridus;//擋板的半徑大小
int left,right;//擋板的左右位置

void gotoxy(int x,int y)//將光標(biāo)移動到(x,y)位置
{
?? ?HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
?? ?COORD pos;
?? ?pos.X = x;
?? ?pos.Y = y;
?? ?SetConsoleCursorPosition(handle,pos);
}

void startup()//數(shù)據(jù)的初始化
{
?? ?high = ?15;
?? ?width = 20;
?? ?ball_x = 0;
?? ?ball_y = width/2;
?? ?ball_vx = 1;
?? ?ball_vy = 1;
?? ?ridus = 5;
?? ?position_x = high;
?? ?position_y = width/2;
?? ?left = position_y -ridus;
?? ?right = position_y + ridus;
}

void show()//顯示畫面
{
?? ?gotoxy(0,0);//光標(biāo)移動到原點位置,以下重畫清屏
?? ?int i,j;
?? ?for(i=0;i<=high+1;i++)
?? ?{
?? ??? ?for(j=0;j<=width;j++)
?? ??? ?{
?? ??? ??? ?if( ( i == ball_x) && ( j == ball_y ) )
? ? ? ? ? ? ? ? printf("O");//輸出小球
?? ??? ??? ?else if( j == width)
?? ??? ??? ??? ?printf("+");//輸出右邊框
?? ??? ??? ?else if( i == high+1)
?? ??? ??? ??? ?printf("-");//輸出下邊框
?? ??? ??? ?else if ( (i==high)&&(j>=left)&&(j<=right))
?? ??? ??? ??? ?printf("*");
?? ??? ??? ?else
?? ??? ??? ??? ?printf(" ");//輸出空格
?? ??? ?}
?? ??? ?printf("\n");
?? ?}
}

void updateWithoutInput()//與用戶輸入無關(guān)的更新
{
?? ?ball_x = ball_x + ball_vx;
?? ?ball_y = ball_y + ball_vy;

?? ?if( (ball_x == 0 ) || (ball_x == high-1 ))
?? ??? ?ball_vx = -ball_vx;
?? ?if( (ball_y == 0 ) || (ball_y == width-1 ))
?? ??? ?ball_vy = -ball_vy;

?? ?Sleep(50);
}

void updateWithInput()//與用戶輸入有關(guān)的更新
{
?? ?char input;
?? ?if(kbhit())
?? ?{
?? ??? ?input = getch();
?? ??? ?if( input == 'a' || input == 'A' )
?? ??? ?{
?? ??? ??? ?position_y--;//位置左移
?? ??? ??? ?left = position_y-ridus;
?? ??? ??? ?right = position_y+ridus;
?? ??? ?}
?? ??? ?if( input == 'd' || input == 'D' )
?? ??? ?{
?? ??? ??? ?position_y++;
?? ??? ??? ?left = position_y - ridus;
?? ??? ??? ?right = position_y + ridus;
?? ??? ?}
?? ?}
}
int main(void)
{
?? ?startup();//數(shù)據(jù)的初始化
?? ?while(1)
?? ?{
?? ??? ?show();//顯示畫面
?? ??? ?updateWithoutInput();//與用戶輸入無關(guān)的更新
?? ??? ?updateWithInput();//與用戶輸入有關(guān)的更新
?? ?}
?? ?return 0;
}

效果圖如下:

四、反彈小球

代碼如下:

#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<windows.h>

//全局變量
int high,width; //游戲畫面大小
int ball_x,ball_y;//小球的坐標(biāo)
int ball_vx,ball_vy;//小球的速度
int position_x,position_y;//擋板的中心坐標(biāo)
int ridus;//擋板的半徑大小
int left,right;//擋板的左右位置
int ball_number;//反彈小球的次數(shù)

void gotoxy(int x,int y)//將光標(biāo)移動到(x,y)位置
{
?? ?HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
?? ?COORD pos;
?? ?pos.X = x;
?? ?pos.Y = y;
?? ?SetConsoleCursorPosition(handle,pos);
}

void startup()//數(shù)據(jù)的初始化
{
?? ?high = ?15;
?? ?width = 20;
?? ?ball_x = 0;
?? ?ball_y = width/2;
?? ?ball_vx = 1;
?? ?ball_vy = 1;
?? ?ridus = 5;
?? ?position_x = high;
?? ?position_y = width/2;
?? ?left = position_y -ridus;
?? ?right = position_y + ridus;
?? ?ball_number=0;
}

void show()//顯示畫面
{
?? ?gotoxy(0,0);//光標(biāo)移動到原點位置,以下重畫清屏
?? ?int i,j;
?? ?for(i=0;i<=high+1;i++)
?? ?{
?? ??? ?for(j=0;j<=width;j++)
?? ??? ?{
?? ??? ??? ?if( ( i == ball_x) && ( j == ball_y ) )
? ? ? ? ? ? ? ? printf("O");//輸出小球
?? ??? ??? ?else if( j == width)
?? ??? ??? ??? ?printf("+");//輸出右邊框
?? ??? ??? ?else if( i == high+1)
?? ??? ??? ??? ?printf("-");//輸出下邊框
?? ??? ??? ?else if ( (i==high)&&(j>=left)&&(j<=right))
?? ??? ??? ??? ?printf("*");
?? ??? ??? ?else
?? ??? ??? ??? ?printf(" ");//輸出空格
?? ??? ?}
?? ??? ?printf("\n");
?? ?}
?? ?printf("反彈小球數(shù):%d\n",ball_number);
}

void updateWithoutInput()//與用戶輸入無關(guān)的更新
{
?? ?if( ball_x == high -1)
?? ?{
?? ??? ?if( (ball_y>=left) && (ball_y<=right) )
?? ??? ?{
?? ??? ??? ?ball_number++;
?? ??? ??? ?printf("\a");//響鈴
?? ??? ?}
?? ??? ?else
?? ??? ?{
?? ??? ??? ?printf("游戲失敗\n");
?? ??? ??? ?system("pause");
?? ??? ??? ?exit(0);
?? ??? ?}
?? ?}
?? ?
?? ?ball_x = ball_x + ball_vx;
?? ?ball_y = ball_y + ball_vy;

?? ?if( (ball_x == 0 ) || (ball_x == high-1 ))
?? ??? ?ball_vx = -ball_vx;
?? ?if( (ball_y == 0 ) || (ball_y == width-1 ))
?? ??? ?ball_vy = -ball_vy;

?? ?Sleep(50);
}

void updateWithInput()//與用戶輸入有關(guān)的更新
{
?? ?char input;
?? ?if(kbhit())
?? ?{
?? ??? ?input = getch();
?? ??? ?if( input == 'a' || input == 'A' )
?? ??? ?{
?? ??? ??? ?position_y--;//位置左移
?? ??? ??? ?left = position_y-ridus;
?? ??? ??? ?right = position_y+ridus;
?? ??? ?}
?? ??? ?if( input == 'd' || input == 'D' )
?? ??? ?{
?? ??? ??? ?position_y++;
?? ??? ??? ?left = position_y - ridus;
?? ??? ??? ?right = position_y + ridus;
?? ??? ?}
?? ?}
}
int main(void)
{
?? ?startup();//數(shù)據(jù)的初始化
?? ?while(1)
?? ?{
?? ??? ?show();//顯示畫面
?? ??? ?updateWithoutInput();//與用戶輸入無關(guān)的更新
?? ??? ?updateWithInput();//與用戶輸入有關(guān)的更新
?? ?}
?? ?return 0;
}

效果圖如下:

五、添加磚塊并實現(xiàn)打磚塊操作

代碼如下:

#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<windows.h>

//全局變量
int high,width; //游戲畫面大小
int ball_x,ball_y;//小球的坐標(biāo)
int ball_vx,ball_vy;//小球的速度
int position_x,position_y;//擋板的中心坐標(biāo)
int ridus;//擋板的半徑大小
int left,right;//擋板的左右位置
int ball_number;//反彈小球的次數(shù)
int block_x1,block_y1;//磚塊1的位置
int block_x2,block_y2;//磚塊2的位置
int block_x3,block_y3;//磚塊3的位置
int score;//消掉磚塊的個數(shù)

void gotoxy(int x,int y)//將光標(biāo)移動到(x,y)位置
{
?? ?HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
?? ?COORD pos;
?? ?pos.X = x;
?? ?pos.Y = y;
?? ?SetConsoleCursorPosition(handle,pos);
}

void startup()//數(shù)據(jù)的初始化
{
?? ?high = ?15;
?? ?width = 20;
?? ?ball_x = 0;
?? ?ball_y = width/2;
?? ?ball_vx = 1;
?? ?ball_vy = 1;
?? ?ridus = 5;
?? ?position_x = high;
?? ?position_y = width/2;
?? ?left = position_y -ridus;
?? ?right = position_y + ridus;
?? ?ball_number=0;
?? ?block_x1 = 0;
?? ?block_y1 = 1;
?? ?block_x2 = 0;
?? ?block_y2 = 2;
?? ?block_x3 = 0;
?? ?block_y3 = 3;
?? ?score=0;
}

void show()//顯示畫面
{
?? ?gotoxy(0,0);//光標(biāo)移動到原點位置,以下重畫清屏
?? ?int i,j;
?? ?for(i=0;i<=high+1;i++)
?? ?{
?? ??? ?for(j=0;j<=width;j++)
?? ??? ?{
?? ??? ??? ?if( ( i == ball_x) && ( j == ball_y ) )
? ? ? ? ? ? ? ? printf("O");//輸出小球
?? ??? ??? ?else if( j == width)
?? ??? ??? ??? ?printf("+");//輸出右邊框
?? ??? ??? ?else if( i == high+1)
?? ??? ??? ??? ?printf("-");//輸出下邊框
?? ??? ??? ?else if ( (i==high)&&(j>=left)&&(j<=right))
?? ??? ??? ??? ?printf("*");
?? ??? ??? ?else if( (i==block_x1) && (j==block_y1) )
?? ??? ??? ??? ?printf("A");//輸出磚塊1
?? ??? ??? ?else if( (i==block_x2) && (j==block_y2) )
?? ??? ??? ??? ?printf("B");//輸出磚塊2
?? ??? ??? ?else if( (i==block_x3) && (j==block_y3) )
?? ??? ??? ??? ?printf("C");//輸出磚塊3
?? ??? ??? ?else
?? ??? ??? ??? ?printf(" ");//輸出空格
?? ??? ?}
?? ??? ?printf("\n");
?? ?}
?? ?printf("反彈小球數(shù):%d\n",ball_number);
?? ?printf("消掉的磚塊數(shù): %d\n",score);
}

void updateWithoutInput()//與用戶輸入無關(guān)的更新
{
?? ?if( ball_x == high -1)
?? ?{
?? ??? ?if( (ball_y>=left) && (ball_y<=right) )//被擋板擋住了
?? ??? ?{
?? ??? ??? ?ball_number++;
?? ??? ??? ?printf("\a");//響鈴
?? ??? ?}
?? ??? ?else
?? ??? ?{
?? ??? ??? ?printf("游戲失敗\n");
?? ??? ??? ?system("pause");
?? ??? ??? ?exit(0);
?? ??? ?}
?? ?}
?? ?
?? ?if( (ball_x == block_x1) && (ball_y ==block_y1) )//小球擊中磚塊1
?? ?{
?? ??? ?score++;//分?jǐn)?shù)加1
?? ??? ?block_y1=rand()%width;//產(chǎn)生新的磚塊
?? ??? ?while((block_y1==block_y2) || ( block_y1==block_y3))
?? ??? ?//當(dāng)新產(chǎn)生的磚塊和其他磚塊重合時
?? ??? ?{
?? ??? ??? ?block_y1=rand()%width;//產(chǎn)生新的磚塊
?? ??? ?}
?? ?}

?? ?if( (ball_x == block_x2) && (ball_y ==block_y2) )//小球擊中磚塊2
?? ?{
?? ??? ?score++;//分?jǐn)?shù)加1
?? ??? ?block_y2=rand()%width;//產(chǎn)生新的磚塊
?? ??? ?while((block_y2==block_y1) || ( block_y2==block_y3))
?? ??? ?//當(dāng)新產(chǎn)生的磚塊和其他磚塊重合時
?? ??? ?{
?? ??? ??? ?block_y2=rand()%width;//產(chǎn)生新的磚塊
?? ??? ?}
?? ?}

?? ?if( (ball_x == block_x3) && (ball_y ==block_y3) )//小球擊中磚塊3
?? ?{
?? ??? ?score++;//分?jǐn)?shù)加1
?? ??? ?block_y3=rand()%width;//產(chǎn)生新的磚塊
?? ??? ?while((block_y3==block_y1) || ( block_y3==block_y2))
?? ??? ??? ?//當(dāng)新產(chǎn)生的磚塊和其他磚塊重合時
?? ??? ?{
?? ??? ??? ?block_y3=rand()%width;//產(chǎn)生新的磚塊
?? ??? ?}
?? ?}

?? ?ball_x = ball_x + ball_vx;
?? ?ball_y = ball_y + ball_vy;

?? ?if( (ball_x == 0 ) || (ball_x == high-1 ))
?? ??? ?ball_vx = -ball_vx;
?? ?if( (ball_y == 0 ) || (ball_y == width-1 ))
?? ??? ?ball_vy = -ball_vy;

?? ?Sleep(66);
}

void updateWithInput()//與用戶輸入有關(guān)的更新
{
?? ?char input;
?? ?if(kbhit())
?? ?{
?? ??? ?input = getch();
?? ??? ?if( input == 'a' || input == 'A' )
?? ??? ?{
?? ??? ??? ?position_y--;//位置左移
?? ??? ??? ?left = position_y-ridus;
?? ??? ??? ?right = position_y+ridus;
?? ??? ?}
?? ??? ?if( input == 'd' || input == 'D' )
?? ??? ?{
?? ??? ??? ?position_y++;
?? ??? ??? ?left = position_y - ridus;
?? ??? ??? ?right = position_y + ridus;
?? ??? ?}
?? ?}
}
int main(void)
{
?? ?startup();//數(shù)據(jù)的初始化
?? ?while(1)
?? ?{
?? ??? ?show();//顯示畫面
?? ??? ?updateWithoutInput();//與用戶輸入無關(guān)的更新
?? ??? ?updateWithInput();//與用戶輸入有關(guān)的更新
?? ?}
?? ?return 0;
}

效果圖如下:

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

相關(guān)文章

  • C語言修煉之路函數(shù)篇真題訓(xùn)練下

    C語言修煉之路函數(shù)篇真題訓(xùn)練下

    函數(shù)是一組一起執(zhí)行一個任務(wù)的語句。每個 C 程序都至少有一個函數(shù),即主函數(shù) main() ,所有簡單的程序都可以定義其他額外的函數(shù)
    2022-03-03
  • 詳細(xì)分析C++ 數(shù)據(jù)封裝和數(shù)據(jù)抽象

    詳細(xì)分析C++ 數(shù)據(jù)封裝和數(shù)據(jù)抽象

    這篇文章主要介紹了C++ 數(shù)據(jù)封裝和數(shù)據(jù)抽象的的相關(guān)資料,文中代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • C語言編程C++自定義個性化類型

    C語言編程C++自定義個性化類型

    這篇文章主要介紹了C語言編程中如何來自定義C++個性化類型,文中附含詳細(xì)的示例代碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-09-09
  • 淺談C++中char型變量的地址輸出

    淺談C++中char型變量的地址輸出

    下面小編就為大家?guī)硪黄獪\談C++中char 型變量的地址輸出。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • Pipes實現(xiàn)LeetCode(194.轉(zhuǎn)置文件)

    Pipes實現(xiàn)LeetCode(194.轉(zhuǎn)置文件)

    這篇文章主要介紹了Pipes實現(xiàn)LeetCode(194.轉(zhuǎn)置文件),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • C++11并發(fā)編程:多線程std::thread

    C++11并發(fā)編程:多線程std::thread

    今天小編就為大家分享一篇關(guān)于C++11并發(fā)編程:多線程std::thread,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • 利用C語言玩轉(zhuǎn)魔方陣實例教程

    利用C語言玩轉(zhuǎn)魔方陣實例教程

    這篇文章主要給大家介紹了關(guān)于利用C語言玩轉(zhuǎn)魔方陣的相關(guān)資料,文中詳細(xì)介紹了關(guān)于奇數(shù)魔方陣和4N 魔方陣的實現(xiàn)方法,通過示例代碼讓大家更好的參考學(xué)習(xí),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-11-11
  • C語言中函數(shù)返回字符串的方法匯總

    C語言中函數(shù)返回字符串的方法匯總

    C語言返回字符串函數(shù)共有四種方式,分別如下:使用堆空間,返回申請的堆地址,注意釋放、函數(shù)參數(shù)傳遞指針,返回該指針、返回函數(shù)內(nèi)定義的靜態(tài)變量(共享)、返回全局變量
    2017-05-05
  • C語言異常處理機制案例講解

    C語言異常處理機制案例講解

    這篇文章主要介紹了C語言異常處理機制案例講解,本文講解了異常處理機制所用的函數(shù)和具體的代碼實現(xiàn)等,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • C++淺析虛函數(shù)使用方法

    C++淺析虛函數(shù)使用方法

    對C++了解的人都應(yīng)該知道虛函數(shù)(Virtual Function)是通過一張?zhí)摵瘮?shù)表(Virtual Table)來實現(xiàn)的。簡稱為V-Table。本文就將詳細(xì)講講虛函數(shù)表的原理與使用,需要的可以參考一下
    2022-08-08

最新評論