c++ 小游戏(EasyX)

这是我的第n个鸡肋小游戏

#include<graphics.h>
#include<conio.h>

const int WINDOW_HEIGHT = 600;  //定义窗口的高
const int WINDOW_WIDE = 400;    //定义窗口的宽

class Bricks
{
public:
    int bricks[12][12];                 //用二维数组保存所有砖块
    int count;                          //记录砖块总数
    const int x = 10, y = 5;            //确定砖块有几排(y)几列(x)
    const int length = WINDOW_WIDE / x; //计算每个砖块的长和宽
    const int wide = 20;

    //构造函数
    Bricks()
    {
        memset(bricks, 0, sizeof(bricks));  //初始化 0表示有砖块
        count = x * y;                        //计算砖块总数
    }

    //画出所有的砖块
    void drawallbricks()
    {
        setfillcolor(YELLOW);   //设置砖块颜色
        setlinecolor(BLACK);    //设置边框颜色
        for (int i = 0; i < y; i++)
            for (int j = 0; j < x; j++)
                fillrectangle(j * length, i * wide, (j + 1) * length, (i + 1) * wide);
    }
};

class Board                 //定义木板类
{
public:
    int x, y;               //定义板的坐标
    const int length = 120;  //定义板的长度
    const int wide = 15;    //定义板的宽度

    //构造函数 将木板的坐标初始化在中心位置
    Board()
    {
        x = WINDOW_WIDE / 2 - length / 2;
        y = WINDOW_HEIGHT - wide;
    }

    //木板移动函数
    void Move()
    {
        int ch;         //接受一个键值
        ch = _getch();

        setfillcolor(BLACK);    //将木板当前位置用背景色黑色覆盖
        solidrectangle(x, y, x + length, y + wide);

        switch (ch)
        {
        case 75:        //每次左移木板长度的1/3
        case 'A':
        case 'a':
            x -= length / 3;
            break;
        case 77:        //每次右移木板长度的1/3
        case 'D':
        case 'd':
            x += length / 3;
            break;
        }
        //木板左右移动的边界限制
        if (x <= 0) x = 0;
        if (x >= WINDOW_WIDE - length) x = WINDOW_WIDE - length;
        setfillcolor(BLUE);     //更新坐标后画新木板
        solidrectangle(x, y, x + length, y + wide);
    }
};

class Ball
{
public:
    const int r = 20;//定义球的半径
    const int speed = 3;    //定义球的飞行速度
    int ballx, bally;       //定义球的坐标
    int addx, addy;         //表示球的飞行方向

    //设置两个个标志量
    bool go;            //小球是否发射
    bool iscatch;       //木板是否捕捉到了小球

    //构造函数  使小球初始化时位于木板中心上
    Ball(int board_wide)
    {
        ballx = WINDOW_WIDE / 2;
        bally = WINDOW_HEIGHT - board_wide - r - 1;
        //初始时小球向右上发射
        addx = 1;
        addy = -1;

        go = 0;         //初始化状态 小球未发射出去
        iscatch = 1;    //初始化状态 球被木板接住
    };

    //小球移动函数
    void Move(Bricks& brick, Board& board)
    {
        BeginBatchDraw();       //开启批量画图模式

        //处理边界:左,右,上边界要反弹
        if (ballx >= WINDOW_WIDE - r || ballx <= r) { addx *= -1; }
        if (bally <= r) { addy *= -1; }
        //若小球触及下边界 说明木板板没有接住小球
        if (bally >= WINDOW_HEIGHT - r) { iscatch = 0; return; }
        //判断小球和木板的碰撞(小球发射出去后才能判断 即go = 1)
        if (go && ballx + 1 >= board.x - r && ballx - 1 <= board.x + board.length + r && bally + 1 >= board.y - r)
        {
            go = 0;                             //小球未发射
            if (bally + 1 <= board.y)           //接住了 小球反向
                addy *= -1;
            else if (bally < WINDOW_HEIGHT - r) //这里是对小球碰撞到木板左右侧面的情况进行处理
            {
                addx *= -1;
                addy *= -1;
            }
        }
        int flag = 0;   //表示未小球击中任一砖块
        for (int i = 0; i < brick.y && !flag; i++)
        {
            for (int j = 0; j < brick.x && !flag; j++)
            {
                //此处有砖块 且小球在该砖块的碰撞范围内
                if (brick.bricks[i][j] == 0 && ballx + 1 >= j * brick.length - r && ballx - 1 <= (j + 1) * brick.length + r && bally + 1 >= i * brick.wide - r && bally - 1 <= (i + 1) * brick.wide + r)
                {
                    //左右两边
                    if (bally + 1 > i * brick.wide - r && bally - 1 < (i + 1) * brick.wide)
                        addx *= -1;
                    //上下两边
                    else if (ballx + 1 >= j * brick.length && ballx - 1 <= (j + 1) * brick.length)
                        addy *= -1;
                    //四个顶角处
                    else
                        continue;
                    brick.bricks[i][j] = 1; //此处砖块被打掉了
                    brick.count--;          //砖块数减一
                    flag = 1;               //击中了 不用继续遍历
                    setfillcolor(BLACK);    //将击中的砖块用黑色覆盖掉
                    fillrectangle(j * brick.length, i * brick.wide, (j + 1) * brick.length, (i + 1) * brick.wide);
                }
            }//
        }//for
        setfillcolor(BLACK);    //擦除小球当前位置
        solidcircle(ballx, bally, r);
        ballx += addx * speed;    //更新位置
        bally += addy * speed;
        if (bally + 1 < board.y - r)
            go = 1;             //小球成功发射
        setfillcolor(RED);      //在新位置画小球
        solidcircle(ballx, bally, r);

        FlushBatchDraw();       //把之前所有的绘图内容显示出来
        Sleep(3);               //休眠,就是暂停,使小球慢慢地运动
    }
};

int Gaming()
{
    Bricks brick;
    brick.drawallbricks();

    Board board;
    setfillcolor(BLUE);
    solidrectangle(board.x, board.y, board.x + board.length, board.y + board.wide);

    Ball ball(board.wide);
    setfillcolor(RED);
    solidcircle(ball.ballx, ball.bally, ball.r);

    while (1)
    {
        //游戏结束条件
        if (!ball.iscatch || brick.count == 0)
        {
            //本局结束后把当前小球和木板清除掉
            setfillcolor(BLACK);
            solidcircle(ball.ballx, ball.bally, ball.r);
            solidrectangle(board.x, board.y, board.x + board.length, board.y + board.wide);

            if (brick.count > 0)
                return MessageBox(NULL, L"You Lose!", L"打砖块", MB_RETRYCANCEL);
            else if (brick.count == 0)
                return MessageBox(NULL, L"You Win!", L"打砖块", MB_RETRYCANCEL);
        }

        if (_kbhit())   //判断你是否按下键 按下返回1 没有返回0
        {
            board.Move();
        }
        ball.Move(brick, board);
    }
}

int main()
{
    initgraph(WINDOW_WIDE, WINDOW_HEIGHT);  //初始化窗口
    while (1)
    {
        if (Gaming() == IDCANCEL)           //点击 取消
            return 0;
    }
}

本代码借鉴了一个忘了叫啥名的博主

---------------------------------------------------------------------------------------------------------------------------------

..........................................................................................................................................................._____________________________________________________________________________

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值