C語言用函數(shù)實現(xiàn)反彈球消磚塊
本文實例為大家分享了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)文章
詳細(xì)分析C++ 數(shù)據(jù)封裝和數(shù)據(jù)抽象
這篇文章主要介紹了C++ 數(shù)據(jù)封裝和數(shù)據(jù)抽象的的相關(guān)資料,文中代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-06-06Pipes實現(xiàn)LeetCode(194.轉(zhuǎn)置文件)
這篇文章主要介紹了Pipes實現(xiàn)LeetCode(194.轉(zhuǎn)置文件),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08