本文小编为大家详细介绍“C语言怎么用数组实现反弹球消砖块”,内容详细,步骤清晰,细节处理妥当,希望这篇“C语言怎么用数组实现反弹球消砖块”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
一、效果展示:
二、代码如下:
#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;//分数//二维数组存储游戏画布中对应的元素//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() //数据的初始化{ 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;//随机的初始化砖块类型 } }}//定义隐藏光标函数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("分数为:%d\n",score);}void updateWithoutInput()//与用户输入无关的更新{ 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()//与用户输入有关的更新{ 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();//数据的初始化 HideCursor(); while(1) { show();//显示画面 updateWithoutInput();//与用户输入无关的更新 updateWithInput();//与用户输入有关的更新 } return 0;}
读到这里,这篇“C语言怎么用数组实现反弹球消砖块”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注编程网行业资讯频道。