您好,登錄后才能下訂單哦!
本篇內容介紹了“C語言中怎么用函數實現反彈球消磚塊”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
這是在上一次彈跳小項目上進行了一系列的優化和封裝。項目: 彈跳的小球
上次沒有用函數進行的封裝。這次在上次的基礎上進行封裝和一些功能的優化。
最終效果圖如下:
代碼如下:
#include<stdio.h> #include<string.h> #include<conio.h> #include<windows.h> //全局變量 int high,width; //游戲畫面大小 int ball_x,ball_y;//小球的坐標 int ball_vx,ball_vy;//小球的速度 void gotoxy(int x,int y)//將光標移動到(x,y)位置 { HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); COORD pos; pos.X = x; pos.Y = y; SetConsoleCursorPosition(handle,pos); } void startup()//數據的初始化 { high = 15; width = 20; ball_x = 0; ball_y = width/2; ball_vx = 1; ball_vy = 1; } void show()//顯示畫面 { gotoxy(0,0);//光標移動到原點位置,以下重畫清屏 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()//與用戶輸入無關的更新 { 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()//與用戶輸入有關的更新 { } int main(void) { startup();//數據的初始化 while(1) { show();//顯示畫面 updateWithoutInput();//與用戶輸入無關的更新 updateWithInput();//與用戶輸入有關的更新 } return 0; }
效果圖如下:
代碼如下:
#include<stdio.h> #include<string.h> #include<conio.h> #include<windows.h> //全局變量 int high,width; //游戲畫面大小 int ball_x,ball_y;//小球的坐標 int ball_vx,ball_vy;//小球的速度 int position_x,position_y;//擋板的中心坐標 int ridus;//擋板的半徑大小 int left,right;//擋板的左右位置 void gotoxy(int x,int y)//將光標移動到(x,y)位置 { HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); COORD pos; pos.X = x; pos.Y = y; SetConsoleCursorPosition(handle,pos); } void startup()//數據的初始化 { 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);//光標移動到原點位置,以下重畫清屏 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()//與用戶輸入無關的更新 { 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()//與用戶輸入有關的更新 { 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();//數據的初始化 while(1) { show();//顯示畫面 updateWithoutInput();//與用戶輸入無關的更新 updateWithInput();//與用戶輸入有關的更新 } return 0; }
效果圖如下:
代碼如下:
#include<stdio.h> #include<string.h> #include<conio.h> #include<windows.h> //全局變量 int high,width; //游戲畫面大小 int ball_x,ball_y;//小球的坐標 int ball_vx,ball_vy;//小球的速度 int position_x,position_y;//擋板的中心坐標 int ridus;//擋板的半徑大小 int left,right;//擋板的左右位置 int ball_number;//反彈小球的次數 void gotoxy(int x,int y)//將光標移動到(x,y)位置 { HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); COORD pos; pos.X = x; pos.Y = y; SetConsoleCursorPosition(handle,pos); } void startup()//數據的初始化 { 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);//光標移動到原點位置,以下重畫清屏 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("反彈小球數:%d\n",ball_number); } void updateWithoutInput()//與用戶輸入無關的更新 { 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()//與用戶輸入有關的更新 { 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();//數據的初始化 while(1) { show();//顯示畫面 updateWithoutInput();//與用戶輸入無關的更新 updateWithInput();//與用戶輸入有關的更新 } return 0; }
效果圖如下:
代碼如下:
#include<stdio.h> #include<string.h> #include<conio.h> #include<windows.h> //全局變量 int high,width; //游戲畫面大小 int ball_x,ball_y;//小球的坐標 int ball_vx,ball_vy;//小球的速度 int position_x,position_y;//擋板的中心坐標 int ridus;//擋板的半徑大小 int left,right;//擋板的左右位置 int ball_number;//反彈小球的次數 int block_x1,block_y1;//磚塊1的位置 int block_x2,block_y2;//磚塊2的位置 int block_x3,block_y3;//磚塊3的位置 int score;//消掉磚塊的個數 void gotoxy(int x,int y)//將光標移動到(x,y)位置 { HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); COORD pos; pos.X = x; pos.Y = y; SetConsoleCursorPosition(handle,pos); } void startup()//數據的初始化 { 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);//光標移動到原點位置,以下重畫清屏 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("反彈小球數:%d\n",ball_number); printf("消掉的磚塊數: %d\n",score); } void updateWithoutInput()//與用戶輸入無關的更新 { 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++;//分數加1 block_y1=rand()%width;//產生新的磚塊 while((block_y1==block_y2) || ( block_y1==block_y3)) //當新產生的磚塊和其他磚塊重合時 { block_y1=rand()%width;//產生新的磚塊 } } if( (ball_x == block_x2) && (ball_y ==block_y2) )//小球擊中磚塊2 { score++;//分數加1 block_y2=rand()%width;//產生新的磚塊 while((block_y2==block_y1) || ( block_y2==block_y3)) //當新產生的磚塊和其他磚塊重合時 { block_y2=rand()%width;//產生新的磚塊 } } if( (ball_x == block_x3) && (ball_y ==block_y3) )//小球擊中磚塊3 { score++;//分數加1 block_y3=rand()%width;//產生新的磚塊 while((block_y3==block_y1) || ( block_y3==block_y2)) //當新產生的磚塊和其他磚塊重合時 { block_y3=rand()%width;//產生新的磚塊 } } 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()//與用戶輸入有關的更新 { 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();//數據的初始化 while(1) { show();//顯示畫面 updateWithoutInput();//與用戶輸入無關的更新 updateWithInput();//與用戶輸入有關的更新 } return 0; }
效果圖如下:
“C語言中怎么用函數實現反彈球消磚塊”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。