最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

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

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

最終效果圖如下:

二、封裝后的彈跳小球

代碼如下:

#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()//數(shù)據(jù)的初始化
{
?? ?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();//數(shù)據(jù)的初始化
?? ?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()//數(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);//光標移動到原點位置,以下重畫清屏
?? ?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();//數(shù)據(jù)的初始化
?? ?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;//反彈小球的次數(shù)

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()//數(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);//光標移動到原點位置,以下重畫清屏
?? ?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()//與用戶輸入無關的更新
{
?? ?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();//數(shù)據(jù)的初始化
?? ?while(1)
?? ?{
?? ??? ?show();//顯示畫面
?? ??? ?updateWithoutInput();//與用戶輸入無關的更新
?? ??? ?updateWithInput();//與用戶輸入有關的更新
?? ?}
?? ?return 0;
}

效果圖如下:

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

代碼如下:

#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;//反彈小球的次數(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)//將光標移動到(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);//光標移動到原點位置,以下重畫清屏
?? ?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()//與用戶輸入無關的更新
{
?? ?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++;//分數(shù)加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++;//分數(shù)加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++;//分數(shù)加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();//數(shù)據(jù)的初始化
?? ?while(1)
?? ?{
?? ??? ?show();//顯示畫面
?? ??? ?updateWithoutInput();//與用戶輸入無關的更新
?? ??? ?updateWithInput();//與用戶輸入有關的更新
?? ?}
?? ?return 0;
}

效果圖如下:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論

信丰县| 阿合奇县| 隆子县| 沂水县| 河间市| 绿春县| 乌兰浩特市| 随州市| 米林县| 招远市| 平凉市| 阳原县| 二手房| 麦盖提县| 罗甸县| 信宜市| 凤阳县| 嘉祥县| 孙吴县| 清河县| 广南县| 逊克县| 明光市| 忻州市| 达州市| 孙吴县| 宁都县| 聂荣县| 乌兰浩特市| 阜城县| 阳春市| 扬州市| 灵山县| 集安市| 安庆市| 长沙市| 遂平县| 长顺县| 三明市| 洞头县| 游戏|