c 打砖块,可以无限关卡 ,显示关卡信息和得分

本文档介绍了一个使用C语言编写的打砖块游戏,具备无限关卡功能,能够显示关卡信息和玩家得分。游戏界面包括木板、小球和墙,通过键盘控制木板移动,小球自动移动并碰撞检测。游戏会根据消除的砖块数量自动进入下一关,同时更新得分。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<iostream>
#include<graphics.h>
#include<Windows.h>
#include<conio.h>
#include<time.h>


#define WIN_WIDHT 1100   //窗体宽度
#define WIN_HEIGHT 800   //窗体高度
#define WALL_WIDTH  90   //墙宽度
#define WALL_HEIGHT  25   //墙高度
#define BOARD_WIDTH 200   //挡板宽度
#define GAME_WIDTH  800   //游戏主界面宽度
#define GAME_HEIGHT  (WIN_HEIGHT)   //游戏主界面高度
using namespace std;

//木板结构体
typedef struct Board
{
    int x;
    int y;
    int width;
    int height;
    int speed;
    COLORREF  color;
}Board;

//球的结构体
typedef struct Ball
{
    int x;
    int y;
    int r;
    int dx;  //移动x坐标值
    int dy;  //移动y坐标值
    COLORREF  color;
}Ball;

//墙的结构体
typedef struct Wall
{
    int x;
    int y;
    int width;
    int height;
    COLORREF  color;
}Wall;

int map[10][10] = { 0 }; //墙集合
int row = 6, col = 6;   //行数和列数
bool  death = false;   //是否死亡标记
int all_board_num = row * col;//砖块的总数
int kit_board_num = 0; //销毁砖块的总数   
int score = 0;//分数
int game_num = 1;//关卡
//初始化
void init();
//创建木板
Board* createBoard();

//创建球
Ball* createBall(Board* board);

//创建墙
void createWall(int row, int col);


//画木板
void drawBoard(Board* board);

//画球
void drawBall(Ball* ball);

//画墙
void drawWall();
//键盘按键按下
void keyDown(Board* board, Ball* ball);

//小球移动
void moveBall(Ball* ball, Board* board);
//小球移动定时器
bool timer_ball(int num);

//小球碰到木板
bool kitboard(Board* board, Ball* ball);
//小球碰到砖块
bool kitwall(Ball* ball);
//画分割线
void drawcutline();
//显示信息
void outinfo(int x, int y, string info);
int main()
{


    //1初始化
    init();
    Board* pboard; //墙指针
    Ball* pball;//球指针
    while (true)//这层循环控制关卡
    {

        pboard = createBoard();
        pball = createBall(pboard);
        createWall(row, col);

        static char temp_game_num[10]{};   //临时变量,用于拼接数字和字母,方便输出到窗体
        static char temp_score[10]{};   //临时变量,用于拼接数字和字母,方便输出到窗体
        static char temp_all_board_num[10]{};   //临时变量,用于拼接数字和字母,方便输出到窗体

        BeginBatchDraw();//批量赋值标记
        while (1) //这个循环是更新当前关卡信息
        {
            if (death)//判断小球是否死亡
                break;
            cleardevice();  //清屏
            drawWall();      //画墙
            drawcutline();    //画分割线

            outinfo(GAME_WIDTH + 20, 30, "游戏关卡: ");
            outtextxy(GAME_WIDTH + 20, 50, "积分: ");


            itoa(game_num, temp_game_num, 10);
            itoa(score, temp_score, 10);
            outtextxy(GAME_WIDTH + 100, 30, temp_game_num);
            outtextxy(GAME_WIDTH + 100, 50, temp_score);

            drawBoard(pboard);
            drawBall(pball);
            if (timer_ball(10)) //用于球循环移动条件
                moveBall(pball, pboard);
            keyDown(pboard, pball);//键盘按键

            FlushBatchDraw();

            //判断是否通关d
            if (kit_board_num == all_board_num)
            {
                //
                row = rand() % 6 + 1;
                col = rand() % 6 + 1;
                all_board_num = row * col;
                kit_board_num = 0;

                game_num++;
                Sleep(100);
                break;
            }

        }
        EndBatchDraw();

        //跳出循环
        if (death)//
            break;
    }

    outtextxy(GAME_WIDTH / 2, GAME_HEIGHT / 2, "Game Over");
    outtextxy(GAME_WIDTH / 2, GAME_HEIGHT / 2 + 50, "Press any key to exit the game");
    system("pause");
    return 0;
}

//初始化
void init()
{
    initgraph(WIN_WIDHT, WIN_HEIGHT, true);

    srand((unsigned)time(NULL));
}
//创木板
Board* createBoard()
{
    Board* pboard = (Board*)malloc(sizeof(Board));
    pboard->width = BOARD_WIDTH;
    pboard->height = 50;
    pboard->speed = 2;
    pboard->color = RED;
    pboard->x = GAME_WIDTH / 2 - pboard->width / 2;
    pboard->y = GAME_HEIGHT - 100;
    return pboard;
}

//创建球
Ball* createBall(Board* board)
{

    Ball* pball = (Ball*)malloc(sizeof(Ball));
    pball->color = BLUE;
    pball->r = 10;
    //pball->x = board->x + board->width / 2;
    //pball->y = board->y - pball->r * 2;

    pball->x = board->x + board->width + 30;
    pball->y = board->y - pball->r * 2;
    pball->dx = -3;
    pball->dy = -3;

    return pball;
}

//创建墙
void createWall(int row, int col)
{
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            map[i][j] = rand() % 3 + 1;
        }
    }


}


//画木板
void drawBoard(Board* board)
{
    auto coler = getfillcolor();
    setfillcolor(board->color);
    fillrectangle(board->x, board->y, board->x + board->width, board->y + board->height);
    setfillcolor(coler);
}

//画球
void drawBall(Ball* ball)
{
    auto coler = getfillcolor();
    setfillcolor(ball->color);
    fillcircle(ball->x, ball->y, ball->r);
    setfillcolor(coler);
}

//画墙
void drawWall()
{
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {

            int x = j * 100;
            int y = i * 30;
            switch (map[i][j])
            {
            case 0:
                break;
            case 1:
                setfillcolor(RED);
                fillrectangle(x, y, x + WALL_WIDTH, y + WALL_HEIGHT);
                break;
            case 2:
                setfillcolor(BLUE);
                fillrectangle(x, y, x + WALL_WIDTH, y + WALL_HEIGHT);
                break;
            case 3:
                setfillcolor(YELLOW);
                fillrectangle(x, y, x + WALL_WIDTH, y + WALL_HEIGHT);
                break;
            default:
                break;
            }
        }
    }
}

//键盘按键按下
void keyDown(Board* board, Ball* ball)
{

    if (GetAsyncKeyState('A') || GetAsyncKeyState(VK_LEFT) && board->x > 0)
    {
        board->x -= board->speed;

    }
    else if (GetAsyncKeyState('D') || GetAsyncKeyState(VK_RIGHT) && board->x + board->width < GAME_WIDTH - 2)
    {
        board->x += board->speed;

    }
    else
    {

    }
}

//小球移动
void moveBall(Ball* ball, Board* board)
{
    if (ball->x - ball->r <= 0 || ball->x + ball->r >= GAME_WIDTH)
    {
        ball->dx = -ball->dx;
    }

    if (ball->y + ball->r > board->y && (ball->x < board->x || ball->x > board->x + BOARD_WIDTH))
    {
        death = true;
        return;
    }
    else
    {
        if (ball->y - ball->r <= 0 || ball->y - ball->r == board->y || kitboard(board, ball) || kitwall(ball))
        {
            ball->dy = -ball->dy;
        }
    }

    ball->x += ball->dx;
    ball->y += ball->dy;
}

//小球移动定时器
bool timer_ball(int num)
{
    static time_t start[1];
    time_t end = clock();

    if (end - start[0] >= num)
    {
        start[0] = end;
        return true;
    }
    return false;

}

//小球碰到木板
bool kitboard(Board* board, Ball* ball)
{
    if (ball->y + ball->r == board->y && (ball->x > board->x) && ball->x < board->x + BOARD_WIDTH)
    {
        ball->y -= ball->dy;
        return true;
    }
    return false;
}
//小球碰到砖块
bool kitwall(Ball* ball)
{
    int ballj = ball->x / WALL_WIDTH;
    int  balli = ball->y / WALL_HEIGHT;

    if (ballj < col && balli < row && map[ballj][balli] != 0)
    {
        map[ballj][balli] = 0;
        ++kit_board_num;
        score += 50;
        return true;
    }

    return false;
}

//画分割线
void drawcutline()
{
    //画游戏区域和信息显示区域分割线
    setfillcolor(BLUE);
    line(GAME_WIDTH + 10, 0, GAME_WIDTH + 10, WIN_HEIGHT);
}

//显示信息
void outinfo(int x, int y, string info)
{
    cout << info << endl;
    settextcolor(WHITE);
    outtextxy(x, y, info.c_str());
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值