# 反弹消砖块 **Repository Path**: wjj912409/rebound_brick ## Basic Information - **Project Name**: 反弹消砖块 - **Description**: No description available - **Primary Language**: C - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2018-11-05 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README #include #include #include #include // 全局变量 int high,width; //游戏画面尺寸 int x,y; //小球的位置 int vx,vy; //小球的速度 int position_x,position_y;//挡板中心坐标 int radius; //挡板半径 int left; int right; //挡板的左右边界 int number; //反弹小球数 int zhuan_x,zhuan_y; //砖块的位置 int score; //游戏分数 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 HideCursor() // 用于隐藏光标 { CONSOLE_CURSOR_INFO cursor_info = {1, 0}; // 第二个值为0表示隐藏光标 SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); } void startup() // 数据初始化 { high = 18,width = 25; x=1,y=width/2; vx=1,vy=1; position_x=high,position_y=width/2; radius=5; left=position_y-radius; right=position_y+radius; number=0; zhuan_x=1,zhuan_y=3; score=0; HideCursor(); // 隐藏光标 } void show() // 显示画面 { gotoxy(0,0); // 光标移动到原点位置,以下重画清屏 int i,j; for (i=0;i<=high+1;i++) { for (j=0;j<=width;j++) { if ((i==x)&&(j==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==zhuan_x)&&(j==zhuan_y)) printf("*"); else printf(" "); // 输出空格 } printf("\n"); } printf("反弹小球数:%d\n",number); printf("得分:%d",score); } void updateWithoutInput() // 与用户输入无关的更新 { if((zhuan_x==x)&&(zhuan_y==y)) { zhuan_x=1,zhuan_y=rand()%width; score++; } if(x==high-1) { if((y>=left)&&(y<=right)) { number++; vy=-vy; } else { printf("Game Over!"); exit(0); } } //根据速度更新小球的位置 x=x+vx; y=y+vy; //碰到边界,改变速度的方向,实现反弹 if((x==0)||(x==high-1)) vx=-vx; if((y==0)||(y==width-1)) vy=-vy; Sleep(50); } void updateWithInput() // 与用户输入有关的更新 { char input; if(kbhit()) // 判断是否有输入 { input = getch(); // 根据用户的不同输入来移动,不必输入回车 if(input=='a') { position_y--; left=position_y-radius; right=position_y+radius; } if(input=='d') { position_y++; left=position_y-radius; right=position_y+radius; } } } int main() { startup(); // 数据初始化 while (1) // 游戏循环执行 { show(); // 显示画面 updateWithoutInput(); // 与用户输入无关的更新 updateWithInput(); // 与用户输入有关的更新 } return 0; }