C語(yǔ)言用數(shù)組實(shí)現(xiàn)反彈球消磚塊
更新時(shí)間:2022年05月12日 11:46:47 作者:輝小歌
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言用數(shù)組實(shí)現(xiàn)反彈球消磚塊,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文項(xiàng)目為大家分享了C語(yǔ)言用數(shù)組實(shí)現(xiàn)反彈球消磚塊的具體代碼,供大家參考,具體內(nèi)容如下
一、效果展示:

二、代碼如下:
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<Windows.h>
#include<time.h>
#define High 24 ? //游戲畫(huà)面尺寸
#define Width 36
//全局變量
int ball_x,ball_y;//小球的坐標(biāo)
int ball_vx,ball_vy;//小球的速度
int canvas[High][Width]={0};
int position_x,position_y;//擋板的中心坐標(biāo)
int ridus;//擋板的半徑大小
int left,right;//擋板的左右大小
int score=0;//分?jǐn)?shù)
//二維數(shù)組存儲(chǔ)游戲畫(huà)布中對(duì)應(yīng)的元素
//0為空格,1為小球 2為擋板 3為磚塊(1分) ? 4為磚塊(2分) ?5為磚塊(3分)
void gotoxy(int x,int y)//將光標(biāo)移動(dòng)到(x,y)位置
{
?? ?HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
?? ?COORD pos;
?? ?pos.X=x;
?? ?pos.Y=y;
?? ?SetConsoleCursorPosition(handle,pos);
}
void startup() //數(shù)據(jù)的初始化
{
? ? int k,i;
?? ?ridus=5;
?? ?position_x=High-1;
?? ?position_y=Width/2;
?? ?left=position_y-ridus;
?? ?right=position_y+ridus;
?? ?ball_x=position_x-1;
?? ?ball_y=position_y;
?? ?ball_vx=-1;
?? ?ball_vy=1;
?? ?canvas[ball_x][ball_y]=1;
?? ?for(k=left;k<=right;k++)//擋板
?? ??? ?canvas[position_x][k]=2;
?? ?srand(time(NULL));
?? ?for(k=0;k<Width;k++)//加幾排磚塊
?? ?{
?? ??? ?for(i=0;i<High/4;i++)
?? ??? ?{
?? ??? ??? ?canvas[i][k]=rand()%3+3;//隨機(jī)的初始化磚塊類型
?? ??? ?}
?? ?}
}
//定義隱藏光標(biāo)函數(shù)
void HideCursor()
{
?? ?CONSOLE_CURSOR_INFO cursor; ? ?
?? ?cursor.bVisible = FALSE; ? ?
?? ?cursor.dwSize = sizeof(cursor); ? ?
?? ?HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); ? ?
?? ?SetConsoleCursorInfo(handle, &cursor);
}
void show()//顯示畫(huà)面
{
?? ?gotoxy(0,0);//將光標(biāo)移動(dòng)到原點(diǎn)位置,以下重畫(huà)清屏
?? ?int i,j;
?? ?for(i=0;i<High;i++)
?? ?{
?? ??? ?for(j=0;j<Width;j++)
?? ??? ?{
?? ??? ??? ?if(canvas[i][j]==0)
?? ??? ??? ??? ?printf(" ");//輸出空格
?? ??? ??? ?else if(canvas[i][j]==1)
?? ??? ??? ??? ?printf("O");//輸出小球0
?? ??? ??? ?else if(canvas[i][j]==2)
?? ??? ??? ??? ?printf("*");//輸出擋板
?? ??? ??? ?else if(canvas[i][j]==3)
?? ??? ??? ??? ?printf("#");//輸出磚塊
?? ??? ??? ?else if(canvas[i][j]==4)
?? ??? ??? ??? ?printf("+");
?? ??? ??? ?else if(canvas[i][j]==5)
?? ??? ??? ??? ?printf("$");
?? ??? ?}
?? ??? ?printf("|\n");//顯示右邊界
?? ?}
?? ?for(j=0;j<Width;j++)
?? ??? ?printf("-");//顯示下邊界
?? ?printf("\n");
?? ?printf("#、+、$ 一個(gè)分別為1、2、3分\n");
?? ?printf("分?jǐn)?shù)為:%d\n",score);
}
void updateWithoutInput()//與用戶輸入無(wú)關(guān)的更新
{
?? ?static int speed=0;
?? ?if(ball_x==High-2)
?? ?{
?? ??? ?if( (ball_y>=left)&&(ball_y<=right) )//被擋板擋住
?? ??? ?{
?? ??? ??? ?printf("\a");//響鈴
?? ??? ?}
?? ??? ?else
?? ??? ?{
?? ??? ??? ?printf("游戲失敗\n");
?? ??? ??? ?system("pause");
?? ??? ??? ?exit(0);
?? ??? ?}
?? ?}
?? ?if(speed<5)//起到延時(shí)的效果
?? ??? ?speed++;
?? ?if(speed==5)
?? ?{
?? ??? ?speed=0;
?? ??? ?canvas[ball_x][ball_y] = 0;
?? ??? ?//更新小球的坐標(biāo)
?? ??? ?ball_x=ball_x+ball_vx;
?? ??? ?ball_y=ball_y+ball_vy;
?? ??? ?canvas[ball_x][ball_y] = 1;
?? ??? ?//碰到邊界后反彈
?? ??? ?if( (ball_x==0) || (ball_x==High-2) )
?? ??? ??? ?ball_vx = -ball_vx;
?? ??? ?if( (ball_y==0) || (ball_y==Width-1) )
?? ??? ??? ?ball_vy = -ball_vy;
?? ??? ?//碰到磚塊后反彈
?? ??? ?if(canvas[ball_x-1][ball_y]>=3&&canvas[ball_x-1][ball_y]<=5)
?? ??? ?{
?? ??? ??? ?if(canvas[ball_x-1][ball_y]==3)//判斷磚塊的類型
?? ??? ??? ??? ?score++;
?? ??? ??? ?if(canvas[ball_x-1][ball_y]==4)
?? ??? ??? ??? ?score=score+2;
?? ??? ??? ?if(canvas[ball_x-1][ball_y]==5)
?? ??? ??? ??? ?score=score+3;
?? ??? ??? ?ball_vx=-ball_vx;
?? ??? ??? ?canvas[ball_x-1][ball_y]=0;
?? ??? ??? ?printf("\a");
?? ??? ?}
?? ?}
}
void updateWithInput()//與用戶輸入有關(guān)的更新
{
?? ?char input;
?? ?if(kbhit())//判斷是否有輸入
?? ?{
?? ??? ?input=getch();
?? ??? ?if( ((input=='a')||(input=='A')) && (left>0) )
?? ??? ?{
?? ??? ??? ?canvas[position_x][right]=0;
?? ??? ??? ?position_y--;
?? ??? ??? ?left=position_y-ridus;
?? ??? ??? ?right=position_y+ridus;
?? ??? ??? ?canvas[position_x][left]=2;
?? ??? ?}
?? ??? ?if( ((input=='d')||(input=='D')) && (right<Width-1) )
?? ??? ?{
?? ??? ??? ?canvas[position_x][left]=0;
?? ??? ??? ?position_y++;
?? ??? ??? ?left=position_y-ridus;
?? ??? ??? ?right=position_y+ridus;
?? ??? ??? ?canvas[position_x][right]=2;
?? ??? ?}
?? ?} ?
}
int main()
{
?? ?system("title 消磚塊游戲中");
?? ?system("color 09");
?? ?startup();//數(shù)據(jù)的初始化
?? ?HideCursor();
?? ?while(1)
?? ?{
?? ??? ?show();//顯示畫(huà)面
?? ??? ?updateWithoutInput();//與用戶輸入無(wú)關(guān)的更新
?? ??? ?updateWithInput();//與用戶輸入有關(guān)的更新
?? ?}
?? ?return 0;
}以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++實(shí)現(xiàn)LeetCode(12.整數(shù)轉(zhuǎn)化成羅馬數(shù)字)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(12.整數(shù)轉(zhuǎn)化成羅馬數(shù)字),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C++實(shí)現(xiàn)簡(jiǎn)單版圖書(shū)管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)簡(jiǎn)單版圖書(shū)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
linux下C語(yǔ)言中的mkdir函數(shù)與rmdir函數(shù)
以下是對(duì)C語(yǔ)言中的mkdir函數(shù)與rmdir函數(shù)進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過(guò)來(lái)參考下2013-08-08
C++實(shí)現(xiàn)FTP綜合應(yīng)用詳解
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)FTP綜合應(yīng)用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
C語(yǔ)言實(shí)現(xiàn)自動(dòng)分配地址的示例
本文介紹了兩種自動(dòng)分配地址的方法,包括通過(guò)宏定義實(shí)現(xiàn)地址分配和將EE地址作為一個(gè)結(jié)構(gòu)體,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-11-11

