C語言用數(shù)組實現(xiàn)反彈球消磚塊
更新時間:2022年05月12日 11:46:47 作者:輝小歌
這篇文章主要為大家詳細介紹了C語言用數(shù)組實現(xiàn)反彈球消磚塊,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文項目為大家分享了C語言用數(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;//小球的坐標 int ball_vx,ball_vy;//小球的速度 int canvas[High][Width]={0}; int position_x,position_y;//擋板的中心坐標 int ridus;//擋板的半徑大小 int left,right;//擋板的左右大小 int score=0;//分數(shù) //二維數(shù)組存儲游戲畫布中對應(yīng)的元素 //0為空格,1為小球 2為擋板 3為磚塊(1分) ? 4為磚塊(2分) ?5為磚塊(3分) 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ù)的初始化 { ? ? 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;//隨機的初始化磚塊類型 ?? ??? ?} ?? ?} } //定義隱藏光標函數(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);//將光標移動到原點位置,以下重畫清屏 ?? ?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("#、+、$ 一個分別為1、2、3分\n"); ?? ?printf("分數(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)//起到延時的效果 ?? ??? ?speed++; ?? ?if(speed==5) ?? ?{ ?? ??? ?speed=0; ?? ??? ?canvas[ball_x][ball_y] = 0; ?? ??? ?//更新小球的坐標 ?? ??? ?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; }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Qt串口通信開發(fā)之Qt串口通信模塊QSerialPort開發(fā)完整實例(串口助手開發(fā))
這篇文章主要介紹了Qt串口通信開發(fā)之Qt串口通信模塊QSerialPort開發(fā)完整實例(串口助手開發(fā)),需要的朋友可以參考下2020-03-03Cocos2d-x學(xué)習(xí)筆記之Hello World!
這篇文章主要介紹了Cocos2d-x學(xué)習(xí)筆記之Hello World!本文基于vs2010和C++語言開發(fā),需要的朋友可以參考下2014-09-09exec()函數(shù)在C++中的應(yīng)用及其用法
exec()函數(shù)在C++中是一個進程控制函數(shù),用于創(chuàng)建新進程執(zhí)行其他程序或命令行指令。exec()函數(shù)可以替換當前進程的代碼和數(shù)據(jù),創(chuàng)建新的進程運行其他程序。exec()函數(shù)有多個版本,例如execl、execv、execle、execve等,根據(jù)不同的參數(shù)類型和個數(shù)來使用2023-05-05C語言中設(shè)置用戶識別碼的相關(guān)函數(shù)的簡單講解
這篇文章主要介紹了C語言中設(shè)置用戶識別碼的相關(guān)函數(shù)的簡單講解,包括setuid()函數(shù)和setreuid()函數(shù)以及setfsuid()函數(shù),需要的朋友可以參考下2015-08-08