项目: 用函数实现反弹球消砖块
生活随笔
收集整理的這篇文章主要介紹了
项目: 用函数实现反弹球消砖块
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
- 一、項目描述和最終的成果展示
- 二、封裝后的彈跳小球
- 三、顯示移動擋板
- 四、反彈小球
- 五、添加磚塊并實現打磚塊操作
一、項目描述和最終的成果展示
這是在上一次彈跳小項目上進行了一系列的優化和封裝。項目: 彈跳的小球
上次沒有用函數進行的封裝。這次在上次的基礎上進行封裝和一些功能的優化。
最終效果圖如下:
二、封裝后的彈跳小球
代碼如下:
#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("-");//輸出下邊框elseprintf(" ");//輸出空格}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("*");elseprintf(" ");//輸出空格}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("*");elseprintf(" ");//輸出空格}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");//輸出磚塊1else if( (i==block_x2) && (j==block_y2) )printf("B");//輸出磚塊2else if( (i==block_x3) && (j==block_y3) )printf("C");//輸出磚塊3elseprintf(" ");//輸出空格}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++;//分數加1block_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++;//分數加1block_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++;//分數加1block_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; }效果圖如下:
總結
以上是生活随笔為你收集整理的项目: 用函数实现反弹球消砖块的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 项目: 用封装的方法实现飞机大战游戏
- 下一篇: 项目: flappy bird