# c++编写打砖块小游戏

``````//绘制转块
void DrawZhuanKuai(){
for (int i = 0; i < ROW; i++){
for (int j = 0; j < COL; j++){
//判断是否有砖块
if (ZhuanKuai[i][j] == 0){
if ((i + j) % 2 == 0){
//设置填充颜色为：天蓝色
setfillcolor(LIGHTCYAN);
//画出填充矩形：左        上        右                下
solidrectangle(j * 40, i * 20, j * 40 + 40, i * 20 + 20);
}
else{
//设置填充颜色为：灰色
setfillcolor(LIGHTGRAY);
//画出填充矩形：左        上        右                下
solidrectangle(j * 40, i * 20, j * 40 + 40, i * 20 + 20);
}
}
}
}
}

``````//木板移动
void MuBan(){
//用黑色木板擦除之前的木板
//设置填充颜色：黑色
setfillcolor(BLACK);
//绘制实心木板
solidrectangle(boardx, boardy, boardx + 60, boardy + 10);
//让木板左右移动
switch (getch()){
case 75://向左移动
boardx -= 15;
break;
case 77://向右移动
boardx += 15;
break;
default:break;
}
//防止木板出界
if (boardx <= 0){
boardx = 0;
}
if (boardx >= (400 - 60)) {
boardx = (400 - 60);
}
//重新绘制木板
//设置填充颜色：黄色
setfillcolor(YELLOW);
//绘制实心木板
solidrectangle(boardx, boardy, boardx + 60, boardy + 10);
}

``````//小球碰墙壁
//小球半径为：10
if (ballx >= (400 - 10) || ballx <= (0 + 10)) {
addx = -1 * addx;
}
if (bally <= 10){
addy = -1 * addy;
}
//小球碰砖块
int flag = 0;//标记小球打到砖块
for (int i = 0; i < ROW; i++){
for (int j = 0; j < COL; j++){
//砖块坐标
int x = j * 40;
int y = i * 20;
//小球遇到砖块
if (ZhuanKuai[i][j] == 0 && x - ballx <= 10 && ballx - x <= 50 && bally - y >= 10 && bally - y <= 30) {
addy = -1 * addy;
ZhuanKuai[i][j] = 1;
flag = 1;//控制跳出外层循环
//让砖块消失
//设置填充颜色：黑色
setfillcolor(BLACK);
//绘制实心木板
solidrectangle(x, y, x + 40, y + 20);
break;//退出内层循环
}
}
if (flag){
break;//退出外层循环
}
}
//小球碰木板
if (starting&&boardx - ballx <= 10 && ballx - boardx <= 70 && boardy - bally <= 10) {
addy = -1 * addy;
}