C語言用數(shù)組實(shí)現(xiàn)反彈球消磚塊
本文項(xiàng)目為大家分享了C語言用數(shù)組實(shí)現(xiàn)反彈球消磚塊的具體代碼,供大家參考,具體內(nèi)容如下
一、效果展示:
二、代碼如下:
#include<stdio.h> #include<string.h> #include<conio.h> #include<Windows.h> #include<time.h> #define High 24 ? //游戲畫面尺寸 #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ǔ)游戲畫布中對(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()//顯示畫面 { ?? ?gotoxy(0,0);//將光標(biāo)移動(dòng)到原點(diǎn)位置,以下重畫清屏 ?? ?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()//與用戶輸入無關(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();//顯示畫面 ?? ??? ?updateWithoutInput();//與用戶輸入無關(guān)的更新 ?? ??? ?updateWithInput();//與用戶輸入有關(guān)的更新 ?? ?} ?? ?return 0; }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Qt串口通信開發(fā)之Qt串口通信模塊QSerialPort開發(fā)完整實(shí)例(串口助手開發(fā))
這篇文章主要介紹了Qt串口通信開發(fā)之Qt串口通信模塊QSerialPort開發(fā)完整實(shí)例(串口助手開發(fā)),需要的朋友可以參考下2020-03-03C語言中調(diào)用Swift函數(shù)實(shí)例詳解
這篇文章主要介紹了C語言中調(diào)用Swift函數(shù)實(shí)例詳解的相關(guān)資料,實(shí)現(xiàn)該功能可以通過定義全局的指向Blocks的對(duì)象指針來實(shí)現(xiàn),需要的朋友可以參考下2017-07-07Cocos2d-x學(xué)習(xí)筆記之Hello World!
這篇文章主要介紹了Cocos2d-x學(xué)習(xí)筆記之Hello World!本文基于vs2010和C++語言開發(fā),需要的朋友可以參考下2014-09-09C語言遞歸應(yīng)用實(shí)現(xiàn)掃雷游戲
這篇文章主要為大家詳細(xì)介紹了C語言遞歸應(yīng)用實(shí)現(xiàn)掃雷游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06exec()函數(shù)在C++中的應(yīng)用及其用法
exec()函數(shù)在C++中是一個(gè)進(jìn)程控制函數(shù),用于創(chuàng)建新進(jìn)程執(zhí)行其他程序或命令行指令。exec()函數(shù)可以替換當(dāng)前進(jìn)程的代碼和數(shù)據(jù),創(chuàng)建新的進(jìn)程運(yùn)行其他程序。exec()函數(shù)有多個(gè)版本,例如execl、execv、execle、execve等,根據(jù)不同的參數(shù)類型和個(gè)數(shù)來使用2023-05-05C語言中設(shè)置用戶識(shí)別碼的相關(guān)函數(shù)的簡(jiǎn)單講解
這篇文章主要介紹了C語言中設(shè)置用戶識(shí)別碼的相關(guān)函數(shù)的簡(jiǎn)單講解,包括setuid()函數(shù)和setreuid()函數(shù)以及setfsuid()函數(shù),需要的朋友可以參考下2015-08-08C++中的四個(gè)默認(rèn)成員函數(shù)與運(yùn)算符重載詳解
這篇文章主要給大家介紹了關(guān)于C++中四個(gè)默認(rèn)成員函數(shù)與運(yùn)算符重載的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來跟著小編一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08C++ Boost PropertyTree示例超詳細(xì)講解
Boost是為C++語言標(biāo)準(zhǔn)庫提供擴(kuò)展的一些C++程序庫的總稱。Boost庫是一個(gè)可移植、提供源代碼的C++庫,作為標(biāo)準(zhǔn)庫的后備,是C++標(biāo)準(zhǔn)化進(jìn)程的開發(fā)引擎之一,是為C++語言標(biāo)準(zhǔn)庫提供擴(kuò)展的一些C++程序庫的總稱2022-11-11