Brickbreaker is the name of an action video game. The player operates an on-screen horizontal “stick” so that a bouncing “ball” does not fall to the bottom of the screen on its way to hitting the “brick” that is eliminated as the target of the clearance. Today we are going to do a little game of brick breaking.
Void DrawZhuanKuai(){for (int I = 0; i < ROW; i++){ for (int j = 0; j < COL; If (ZhuanKuai[I][j] == 0){if ((I + j) % 2 == 0){setFillcolor (LIGHTCYAN); // Rectangle (j * 40, I * 20, j * 40 + 40, I * 20 + 20); } else{// Set the filling color to: setFillColor (LIGHTGRAY); // Rectangle (j * 40, I * 20, j * 40 + 40, I * 20 + 20); } } } } }Copy the code
First, draw the brick. Here, you just need to draw it as a simple rectangle. You can fill it with whatever color you like. Of course, the placement can also be set up by yourself.
Void MuBan(){// Use BLACK to erase the previous board // set the fillcolor: BLACK setFillColor (BLACK); // Draw a solidRectangle (boardx, boardy, boardx + 60, boardy + 10); // switch (getch()){case 75:// Move boardx -= 15; break; Case 77:// Move right to boardx += 15; break; default:break; } // if (boardx <= 0){boardx = 0; } if (boardx >= (400 - 60)) { boardx = (400 - 60); } // redraw the board // set the fillcolor: YELLOW setfillcolor(YELLOW); // Draw a solidRectangle (boardx, boardy, boardx + 60, boardy + 10); }Copy the code
The above part of the code is to write the control of the baffle, which is actually moving left and right, and the need to prevent the baffle from going out of bounds to make the screen size limit.
/ / small ball to touch the wall / / the sphere is: 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; {// int x = j * 40; int y = i * 20; If (ZhuanKuai[I][j] == 0 - ballx <= 10 - ballx-x <= 50 - bally-y >= 10 - bally-y <= 30) {addy = -1 * addy; ZhuanKuai[i][j] = 1; flag = 1; // Set the fillcolor: BLACK setFillColor (BLACK); // Draw a solidRectangle (x, y, x + 40, y + 20); break; }} if (flag){break; If (starting &&boardx-ballx <= 10 &&boardy-bally <= 10) {addy = -1 * addy; }Copy the code
The top part is the core logic, the game logic that describes the ball. They hit the wall, they hit the brick, they hit the baffle. Everything would be so much simpler if we just sorted out these three logics.
If you want to learn how to make games, you can pay attention to the public number: code like poetry, find me to learn together.