栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c++ 小游戏(EasyX)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c++ 小游戏(EasyX)

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

#include
#include

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;
    }
}

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

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

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/948473.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号