当前位置: 代码迷 >> 综合 >> easyx----小球消砖块
  详细解决方案

easyx----小球消砖块

热度:22   发布时间:2023-12-18 11:27:48.0
#include <iostream>
#include <graphics.h>
#include <conio.h>
using namespace std;
#define High 480
#define Width 640
#define Bricknum 10 // 砖块的数量// 全局变量
int ball_x, ball_y; // 小球中心点位置
int ball_vx, ball_vy; // 小球的速度
int ball_r; // 小球的半径
int bar_x, bar_y; // 挡板的中心点
int bar_left, bar_top, bar_right, bar_bottom; // 确定挡板位置的四个点
int bar_high, bar_width; // 挡板的宽度int brick_high, brick_width; // 砖块的宽度
int is_brick_exist[Bricknum]; // 判断砖块是否存在void star() // 数据初始化
{
    ball_x = Width / 2;ball_y = High / 2;ball_vx = 2;ball_vy = 2;ball_r = 20;bar_high = High / 20;bar_width = Width / 2;bar_x = Width / 2;bar_y = High - bar_high / 2;bar_left = bar_x - bar_width / 2;bar_top = bar_y - bar_high / 2;bar_right = bar_x + bar_width / 2;bar_bottom = bar_y + bar_high / 2;brick_high = High / Bricknum;brick_width = Width / Bricknum;for (int i = 0; i < Bricknum; i++){
    is_brick_exist[i] = 1;}initgraph(Width, High);BeginBatchDraw();
}void gameover() // 游戏结束
{
    EndBatchDraw();closegraph();
}void clean() // 消除画面
{
    setcolor(BLACK);setfillcolor(BLACK);fillcircle(ball_x, ball_y, ball_r);bar(bar_left, bar_top, bar_right, bar_bottom);int brick_left, brick_top, brick_right, brick_bottom;for (int i = 0; i < Bricknum; i++){
    brick_left = i * brick_width;brick_top = 0;brick_right = brick_left + brick_width;brick_bottom = brick_high;if (!is_brick_exist[i]){
    fillrectangle(brick_left, brick_top, brick_right, brick_bottom);}}
}void update_without_input() // 与用户输入无关的更新
{
    if (ball_y + ball_r == High) // 判断小球是否触碰到墙底部{
    gameover();exit(0);}// 判断小球与挡板是否碰撞if ((ball_y + ball_r == bar_top) || (ball_y - ball_r == bar_bottom)) {
    if ((ball_x >= bar_left) && (ball_x <= bar_right)){
    ball_vy = -ball_vy;}}// 更新小球位置ball_x += ball_vx;ball_y += ball_vy;// 限制小球越界if ((ball_x <= ball_r) || (ball_x >= Width - ball_r)){
    ball_vx = -ball_vx;}if ((ball_y <= ball_r) || (ball_y >= High - ball_r)){
    ball_vy = -ball_vy;}// 判断小球是否与砖块相撞int brick_left, brick_top, brick_right, brick_bottom;for (int i = 0; i < Bricknum; i++){
    if (is_brick_exist[i]){
    brick_left = i * brick_width;brick_top = 0;brick_right = brick_left + brick_width;brick_bottom = brick_high;if ((ball_y - ball_r == brick_bottom) && (ball_x >= brick_left) && (ball_x <= brick_right)){
    is_brick_exist[i] = 0;ball_vy = -ball_vy;}}}
}void update_withinput() // 与用户输入有关的更新
{
    char input;if (_kbhit()){
    input = _getch();if (input == 'w' && bar_top > 0){
    bar_y -= 15;bar_top = bar_y + bar_high / 2;bar_bottom = bar_y - bar_high / 2;}if (input == 's' && bar_bottom < High){
    bar_y += 15;bar_top = bar_y - bar_high / 2;bar_bottom = bar_y + bar_high / 2;}if (input == 'a' && bar_left > 0){
    bar_x -= 15;bar_left = bar_x - bar_width / 2;bar_right = bar_x + bar_width / 2;}if (input == 'd' && bar_right < Width){
    bar_x += 15;bar_left = bar_x - bar_width / 2;bar_right = bar_x + bar_width / 2;}}}void show() // 显示更新后的画面
{
    setcolor(YELLOW);setfillcolor(BGR(0x7373B9));fillcircle(ball_x, ball_y, ball_r);bar(bar_left, bar_top, bar_right, bar_bottom);int brick_left, brick_top, brick_right, brick_bottom;for (int i = 0; i < Bricknum; i++){
    brick_left = i * brick_width;brick_top = 0;brick_right = brick_left + brick_width;brick_bottom = brick_high;if (is_brick_exist[i]){
    setcolor(WHITE);setfillcolor(RED);fillrectangle(brick_left, brick_top, brick_right, brick_bottom);}}FlushBatchDraw();Sleep(2);
}int main()
{
    star();while (1){
    clean();update_without_input();update_withinput();show();}return 0;
}