用easyx实现的2048,一个简单的c++小游戏

#include <graphics.h>
#include <bits/stdc++.h>
#include <conio.h>

#define SPACE 11	//小矩形间距
#define SIDEL 130	//小矩形边长
#define BKLEN 575	//大矩形边长
#define X 200
#define Y 100
//font consolas
//需要用到的颜色
enum Color {
    bk1 = RGB(46, 40, 14),
    bk2 = RGB(63, 54, 45),
    bk3 = RGB(58, 48, 37),
    two1 = RGB(50, 37, 23),
    two2 = RGB(63, 48, 21),
    two3 = RGB(120, 62, 12),
    two4 = RGB(136, 52, 9),
    two5 = RGB(139, 33, 8),
    two6 = RGB(161, 36, 7),
    two7 = RGB(120, 95, 15),
    two8 = RGB(131, 104, 15),
    two9 = RGB(93, 93, 22),
    two10 = RGB(15, 28, 24),
    two11 = RGB(130, 130, 101),
    font = RGB(171, 178, 191),
};

//方向
enum Dirction { up = 'w', down = 's', left = 'a', right = 'd' };
//键盘上下左右 方向键的键码(keyCode)是38,40,37和39

class My2048 {
public:
    //constructor
    My2048() :_matrix{} {}
    //初始化游戏界面
    void init() {
        bk2();
        setFont();
    }
    //在空位产生2或者4
    void newNumber(bool judge) {
        if (judge)
        {
            srand((unsigned int)time(0));
            int x, y;
            int a = rand() % 100;
            //找到一个空位
            do {
                x = rand() % 4;
                y = rand() % 4;
            } while (_matrix[x][y] != 0);
            _matrix[x][y] = (a > 80) ? 2 : 1;
        }
    }
    //在对应的位置上画出小方块
    void drawSmallRect() {	
        int x, y;
        y = Y + SPACE;
        for (int xj = 0; xj < 4; xj++)
        {
            x = X + SPACE;
            for (int yj = 0; yj < 4; yj++)
            {
                switch (_matrix[xj][yj])//设置小方块颜色
                {
                case 1:
                    setfillcolor(Color::two1);
                    break;
                case 2:
                    setfillcolor(Color::two2);
                    break;
                case 3:
                    setfillcolor(Color::two3);
                    break;
                case 4:
                    setfillcolor(Color::two4);
                    break;
                case 5:
                    setfillcolor(Color::two5);
                    break;
                case 6:
                    setfillcolor(Color::two6);
                    break;
                case 7:
                    setfillcolor(Color::two7);
                    break;
                case 8:
                    setfillcolor(Color::two8);
                    break;
                case 9:
                    setfillcolor(Color::two9);
                    break;
                case 10:
                    setfillcolor(Color::two10);
                    break;
                case 11:
                    setfillcolor(Color::two11);
                    break;
                default:
                    setfillcolor(Color::bk3);
                    break;
                }//
                solidroundrect(x, y, x + SIDEL, y + SIDEL, 7, 7);
                
                //设置字体
                LOGFONT f;
                gettextstyle(&f);
                f.lfQuality = ANTIALIASED_QUALITY;        //抗锯齿
                _tcscpy_s(f.lfFaceName, TEXT("consolas"));//字体类型
                setbkmode(TRANSPARENT);
                settextcolor(Color::font);

                if (_matrix[xj][yj] < 4) {
                    f.lfHeight = 90;				  //字体高度
                    settextstyle(&f);
                    switch (_matrix[xj][yj])
                    {
                    case 1:
                        outtextxy(x + 45, y + 20, TEXT("2"));
                        break;
                    case 2:
                        outtextxy(x + 45, y + 20, TEXT("4"));
                        break;
                    case 3:
                        outtextxy(x + 45, y + 20, TEXT("8"));
                        break;
                    }
                }
                else if (_matrix[xj][yj] < 7) {
                    f.lfHeight = 90;				  //字体高度
                    settextstyle(&f);
                    switch (_matrix[xj][yj])
                    {
                    case 4:
                        outtextxy(x+25, y+15, TEXT("16"));
                        break;
                    case 5:
                        outtextxy(x+25, y+15, TEXT("32"));
                        break;
                    case 6:
                        outtextxy(x+25,y+15, TEXT("64"));
                        break;
                    }
                }
                else if (_matrix[xj][yj] < 10) {
                    f.lfHeight = 80;				  //字体高度
                    settextstyle(&f);
                    switch (_matrix[xj][yj])
                    {
                    case 7:
                        outtextxy(x+10, y+20, TEXT("128"));
                        break;
                    case 8:
                        outtextxy(x+10, y+20, TEXT("256"));
                        break;
                    case 9:
                        outtextxy(x+10, y+20, TEXT("512"));
                        break;
                    }
                }
                else if (_matrix[xj][yj] < 10) {
                    f.lfHeight = 90;				  //字体高度
                    settextstyle(&f);
                    switch (_matrix[xj][yj])
                    {
                    case 10:
                        outtextxy(x,y, TEXT("1024"));
                        break;
                    case 11:
                        outtextxy(x,y, TEXT("2048"));
                        break;
                    }
                }
                x += SIDEL + SPACE;
            }
            y += SIDEL + SPACE;
        }
    }

    bool moveRect(int di) {//移动方块到指定位置
        bool isMove = false;
        switch (di)
        {
        case Dirction::up: 
            isMove = up();
            break;
        case Dirction::down: 
            isMove = down();
            break;
        case Dirction::left:
            isMove = left();
            break;
        case Dirction::right:
            isMove = right();
            break;
        }
        return isMove;
    }
    bool isWon() {
        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                if (_matrix[i][j]==2048)
                {
                    return true;
                }
            }
        }
        return false;
    }
    bool isFail() {
        for (int i = 0; i < 4; i++) 
            for (int j = 0; j < 3; j++) {
                // 横向和纵向比较挨着的两个元素是否相等,若有相等则游戏不结束 
                if (_matrix[i][j] == _matrix[i][j + 1] || _matrix[j][i] == _matrix[j + 1][i])
                    return false;
            }
        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                if (_matrix[i][j] == 0)
                    return false;
            }
        }
        return true;
    }
    char overPict() {
        LOGFONT f;
        gettextstyle(&f);
        f.lfQuality = ANTIALIASED_QUALITY;//抗锯齿
        f.lfHeight = 100;				  //字体高度
        _tcscpy_s(f.lfFaceName, TEXT("黑体"));//字体类型
        settextstyle(&f);
        Sleep(200);
        cleardevice();
        outtextxy(X + SPACE + SIDEL + 50, 200, TEXT("你失败了"));
        f.lfHeight = 50;
        settextstyle(&f);
        outtextxy(X + SPACE + SIDEL + 50, 300, TEXT("输入y再来一次"));
        return (char)_getch();
    }
private:
    bool up() {
        bool ret = false;
        for (int x = 0; x < 4; x++)  //逐列处理
        {
            for (int i=0,j=1; j < 4; j++) //从头开始查找是否能合并
            {
                if (_matrix[j][x] != 0) {
                    if (_matrix[j][x] == _matrix[i][x])//相等
                    {
                        _matrix[i][x] ++; ret = true;
                        _matrix[j][x] = 0;
                        i++;
                    }
                    else if (_matrix[i][x] == 0) {  //移动
                        _matrix[i][x] = _matrix[j][x]; ret = true;
                        _matrix[j][x] = 0;
                    }
                    else {                          //碰撞
                        _matrix[i+1][x] = _matrix[j][x];
                        if ((i+1)!=j)
                        {
                            _matrix[j][x] = 0; ret = true;
                        }
                        i++;
                    }
                }
            }
        }
        return ret;
    }
    bool down() {
        bool ret = false;
        for (int x = 0; x < 4; x++)  //逐列处理
        {
            for (int i = 3, j = 2; j >= 0; j--) //从头开始查找是否能合并
            {
                if (_matrix[j][x] != 0) {
                    if (_matrix[j][x] == _matrix[i][x])//相等
                    {
                        _matrix[i][x] ++; ret = true;
                        _matrix[j][x] = 0;
                        i--;
                    }
                    else if (_matrix[i][x] == 0) {  //移动
                        _matrix[i][x] = _matrix[j][x]; ret = true;
                        _matrix[j][x] = 0;
                    }
                    else {                          //碰撞
                        _matrix[i-1][x] = _matrix[j][x];
                        if ((i - 1) != j)
                        {
                            _matrix[j][x] = 0; ret = true;
                        }
                        i--;
                    }
                }
            }
        }
        return ret;
    }
    bool left() {
        bool ret = false;
        for (int y = 0; y < 4; y++)  //逐行处理
        {
            for (int i = 0, j = 1; j < 4; j++) //从头开始查找是否能合并
            {
                if (_matrix[y][j] != 0) {
                    if (_matrix[y][j] == _matrix[y][i])//相等
                    {
                        _matrix[y][i] ++; ret = true;
                        _matrix[y][j] = 0;
                        i++;
                    }
                    else if (_matrix[y][i] == 0) {  //移动
                        _matrix[y][i] = _matrix[y][j]; ret = true;
                        _matrix[y][j] = 0;
                    }
                    else {                          //碰撞
                        _matrix[y][i+1] = _matrix[y][j];
                        if ((i + 1) != j)
                        {
                            _matrix[y][j] = 0; ret = true;
                        }
                        i++;
                    }
                }
            }
        }
        return ret;
    }
    bool right() {
        bool ret = false;
        for (int y = 0; y < 4; y++)  //逐行处理
        {
            for (int i = 3, j = 2; j >=0; j--) //从头开始查找是否能合并
            {
                if (_matrix[y][j] != 0) {
                    if (_matrix[y][j] == _matrix[y][i])//相等
                    {
                        _matrix[y][i] ++; ret = true;
                        _matrix[y][j] = 0;
                        i--;
                    }
                    else if (_matrix[y][i] == 0) {  //移动
                        _matrix[y][i] = _matrix[y][j]; ret = true;
                        _matrix[y][j] = 0;
                    }
                    else {                          //碰撞
                        _matrix[y][i-1] = _matrix[y][j];
                        if ((i - 1) != j)
                        {
                            _matrix[y][j] = 0; ret = true;
                        }
                        i--;
                    }
                }
            }
        }
        return ret;
    }
    
    void bk2() {												//设置背景
        setfillcolor(Color::bk2);
        solidroundrect(X, Y, X + BKLEN, Y + BKLEN, 20, 20);
        int x, y;
        y = Y + SPACE;
        for (int i = 0; i < 4; i++)
        {
            x = X + SPACE;
            for (int j = 0; j < 4; j++)
            {
                setfillcolor(Color::bk3);
                solidroundrect(x, y, x + SIDEL, y + SIDEL, 7, 7);
                x += SIDEL + SPACE;
            }
            y += SIDEL + SPACE;
        }
    }
    void setFont() {
        LOGFONT f;
        gettextstyle(&f);
        f.lfQuality = ANTIALIASED_QUALITY;//抗锯齿
        f.lfHeight = 100;				  //字体高度
        _tcscpy_s(f.lfFaceName, TEXT("黑体"));//字体类型
        settextstyle(&f);
        outtextxy(X + SPACE + SIDEL + 50, 0, TEXT("2048"));
        f.lfHeight = 20;
        settextstyle(&f);
        outtextxy(0, 0, TEXT("请切换英文输入法,小写 a w s d 操作"));
    }

    int _matrix[4][4];
};

int main()
{
    // 初始化图形窗口
    initgraph(1000, 700);
    setbkcolor(Color::bk1);
    cleardevice();
    //画出游戏主界面
A:
    My2048 a;
    a.init();
    //游戏循环
    a.newNumber(true);
    a.newNumber(true);
    a.drawSmallRect();
    bool judge;
    int ctr;
    while ((ctr = _getch()) == Dirction::up ||ctr == Dirction::down
        || ctr  == Dirction::left || ctr == Dirction::right)
    {
        judge=a.moveRect(ctr);
        a.drawSmallRect();
        Sleep(150);
        a.newNumber(judge);
        a.drawSmallRect();
        if (a.isWon()) {
            ShellExecute(NULL, TEXT("open"), TEXT("https://paste.ubuntu.com/p/FQV7YDmJBq/"), NULL, NULL, SW_SHOWNORMAL);
            Sleep(3000);
            break;
        }
        if (a.isFail()) {
            Sleep(1000);
            char e = a.overPict();
            if (e=='y')
            {
                goto A;
            }
            else {
                cleardevice();
                outtextxy(X + SPACE + SIDEL + 50, 200, TEXT("真遗憾"));
                Sleep(2000);
                break;
            }
        }
        Sleep(200);
    }

    closegraph();
    return 0;
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值