吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 7366|回复: 25
收起左侧

[C&C++ 原创] 分享一个C++写的俄罗斯方块游戏

  [复制链接]
Ingran 发表于 2019-3-24 15:29
本帖最后由 Ingran 于 2019-3-25 23:31 编辑

分享一下当初学习C++时候写的一个图形化的俄罗斯方块,

QQ截图20190324150524.png

部分代码:
[C++] 纯文本查看 复制代码
void CManage::InitGame()
{
        SetGameSpeed();
        m_MapDate.InitMap();
        m_BlockDate.InitBlock();
        m_begTime = timeGetTime() / 1000.0f;
        
}

void CManage::UpdateGame()
{
        //清零,移动,下落(落不动消行),写入新方块
        _GetKey();
        MyPos const* temp = m_BlockDate.GetBlockHead();
        //清零
        for (int i = 0; i < BLOCK_NUM; i++)
                m_MapDate.SetMapVal((temp + i)->x, (temp + i)->y, 0);
        m_endTime = timeGetTime() / 1000.0f;

        //移动
        switch (key)
        {
        case 'w':
        case 'W':
        {
                bool isChange = true;
                switch (m_BlockDate.GetBlockType())
                {
                case 0://长条
                        switch (m_BlockDate.GetBlockChangeType())
                        {
                        case 0:
                                if (m_MapDate.GetMapVal((temp + 0)->x + 1, (temp + 0)->y) != 0
                                        || m_MapDate.GetMapVal((temp + 1)->x + 1, (temp + 1)->y) != 0
                                        || m_MapDate.GetMapVal((temp + 1)->x - 1, (temp + 1)->y) != 0
                                        || m_MapDate.GetMapVal((temp + 1)->x - 2, (temp + 1)->y) != 0
                                        || m_MapDate.GetMapVal((temp + 2)->x - 1, (temp + 2)->y) != 0
                                        || m_MapDate.GetMapVal((temp + 2)->x - 2, (temp + 2)->y) != 0
                                        || m_MapDate.GetMapVal((temp + 3)->x - 1, (temp + 3)->y) != 0)
                                        isChange = false;

                                break;
                        case 1:
                                if (m_MapDate.GetMapVal((temp + 0)->x, (temp + 0)->y - 1) != 0
                                        || m_MapDate.GetMapVal((temp + 1)->x, (temp + 1)->y - 1) != 0
                                        || m_MapDate.GetMapVal((temp + 1)->x, (temp + 1)->y + 1) != 0
                                        || m_MapDate.GetMapVal((temp + 1)->x, (temp + 1)->y + 2) != 0
                                        || m_MapDate.GetMapVal((temp + 2)->x, (temp + 2)->y + 1) != 0
                                        || m_MapDate.GetMapVal((temp + 2)->x, (temp + 2)->y + 2) != 0
                                        || m_MapDate.GetMapVal((temp + 3)->x, (temp + 3)->y + 1) != 0)
                                        isChange = false;
                                break;
                        }
                        break;
                case 1://田字
                        //不变
                        break;
                case 2://Z字
                        switch (m_BlockDate.GetBlockChangeType())
                        {
                        case 0:
                                if (m_MapDate.GetMapVal((temp + 0)->x + 1, (temp + 0)->y) != 0
                                        || m_MapDate.GetMapVal((temp + 1)->x, (temp + 1)->y + 1) != 0
                                        || m_MapDate.GetMapVal((temp + 1)->x - 1, (temp + 1)->y + 1) != 0  
                                        )
                                        isChange = false;
                                break;
                        case 1:
                                if (m_MapDate.GetMapVal((temp + 0)->x, (temp + 0)->y - 1) != 0
                                        || m_MapDate.GetMapVal((temp + 1)->x, (temp + 1)->y - 1) != 0
                                        || m_MapDate.GetMapVal((temp + 2)->x + 1, (temp + 2)->y) != 0
                                        )
                                        isChange = false;
                                break;
                        }
                        break;
                case 3://L字
                        switch (m_BlockDate.GetBlockChangeType())
                        {
                        case 0:
                                if (m_MapDate.GetMapVal((temp + 0)->x + 1, (temp + 0)->y) != 0
                                        || m_MapDate.GetMapVal((temp + 1)->x, (temp + 1)->y + 1) != 0
                                        || m_MapDate.GetMapVal((temp + 1)->x, (temp + 1)->y + 2) != 0
                                        || m_MapDate.GetMapVal((temp + 2)->x, (temp + 2)->y + 1) != 0
                                        || m_MapDate.GetMapVal((temp + 2)->x, (temp + 2)->y + 2) != 0
                                        || m_MapDate.GetMapVal((temp + 3)->x, (temp + 3)->y + 1) != 0
                                        )
                                isChange = false;
                                break;
                        case 1:
                                if (m_MapDate.GetMapVal((temp + 0)->x, (temp + 0)->y + 1) != 0
                                        || m_MapDate.GetMapVal((temp + 1)->x - 1, (temp + 1)->y) != 0
                                        || m_MapDate.GetMapVal((temp + 1)->x - 2, (temp + 1)->y) != 0
                                        || m_MapDate.GetMapVal((temp + 2)->x - 1, (temp + 2)->y) != 0
                                        || m_MapDate.GetMapVal((temp + 2)->x - 2, (temp + 2)->y) != 0
                                        || m_MapDate.GetMapVal((temp + 3)->x - 1, (temp + 3)->y) != 0
                                        )
                                        isChange = false;
                                break;
                        case 2:
                                if (m_MapDate.GetMapVal((temp + 0)->x - 1, (temp + 0)->y) != 0
                                        || m_MapDate.GetMapVal((temp + 1)->x, (temp + 1)->y - 1) != 0
                                        || m_MapDate.GetMapVal((temp + 1)->x, (temp + 1)->y - 2) != 0
                                        || m_MapDate.GetMapVal((temp + 2)->x, (temp + 2)->y - 1) != 0
                                        || m_MapDate.GetMapVal((temp + 2)->x, (temp + 2)->y - 2) != 0
                                        || m_MapDate.GetMapVal((temp + 3)->x, (temp + 3)->y - 1) != 0
                                        )
                                        isChange = false;
                                break;
                        case 3:
                                if (m_MapDate.GetMapVal((temp + 0)->x, (temp + 0)->y - 1) != 0
                                        || m_MapDate.GetMapVal((temp + 1)->x + 1, (temp + 1)->y) != 0
                                        || m_MapDate.GetMapVal((temp + 1)->x + 2, (temp + 1)->y) != 0
                                        || m_MapDate.GetMapVal((temp + 2)->x + 1, (temp + 2)->y) != 0
                                        || m_MapDate.GetMapVal((temp + 2)->x + 2, (temp + 2)->y) != 0
                                        || m_MapDate.GetMapVal((temp + 3)->x + 1, (temp + 3)->y) != 0
                                        )
                                        isChange = false;
                                break;
                        }
                        break;
                }
                if (isChange)
                        m_BlockDate.SetBlockChange();
        }
                
                key = 0;
                break;
        case 'a':
        case 'A':
                if (isMove(0, -1))
                        m_BlockDate.SetBlockLeft();
                key = 0;
                break;
        case 'd':
        case 'D':
                if (isMove(0, 1))
                        m_BlockDate.SetBlockLeft(false);
                key = 0;
                break;
        case 's':
        case 'S':
                for (int i = 0; i < 2; i++)
                {
                        if (isMove(1, 0))
                                m_BlockDate.SetBlockDown();
                        else
                                break;
                }
                key = 0;
                break;
        }
        
        //下落
        if (m_endTime - m_begTime >= m_fSpeed)
        {
                if (isMove(1, 0))
                        m_BlockDate.SetBlockDown();
                else
                {
                        for (int i = 0; i < BLOCK_NUM; i++)
                                m_MapDate.SetMapVal((temp + i)->x, (temp + i)->y, 2);
                        //消行
                        m_MapDate.ClearBlock();
                        ////覆盖之前分数
                        //setbkmode(OPAQUE);
                        //settextcolor(BLACK);
                        //outtextxy(25 * 2, 25 * ROW, " ");
        

                        m_BlockDate.InitBlock();
                }
                m_begTime = m_endTime;
        }
        
        //写入新方块
        for (int i = 0; i < BLOCK_NUM; i++)
                m_MapDate.SetMapVal((temp + i)->x, (temp + i)->y, 2);



}

void CManage::ShowGame()
{
        m_MapDate.ShowMap();
}

void CManage::SetGameSpeed()
{
        switch (m_Gamelv)
        {
        case 1:
                m_fSpeed = 1.0f;
                break;
        case 2:
                m_fSpeed = 0.6f;
                break;
        case 3:
                m_fSpeed = 0.2f;
                break;
        }

}


源代码下载:
C++俄罗斯方块.rar (7.59 KB, 下载次数: 162)

graphics.h头文件:
easyX.txt (41 Bytes, 下载次数: 63)

免费评分

参与人数 10吾爱币 +14 热心值 +9 收起 理由
易萧 + 1 + 1 用心讨论,共获提升!
吾在江湖 + 1 用心讨论,共获提升!
lefeng + 1 + 1 我很赞同!
a156905240 + 1 + 1 热心回复!
筱轩孜 + 1 + 1 谢谢@Thanks!
缥缈的心情 + 1 + 1 热心回复!
能一直 + 1 + 1 用心讨论,共获提升!
金龙 + 1 + 1 热心回复!
苏紫方璇 + 5 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
CyanCloud + 1 + 1 热心回复!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

Loveleaveme 发表于 2019-3-26 10:52
看起来很清爽
 楼主| Ingran 发表于 2019-3-25 23:27
xxdg 发表于 2019-3-25 20:19
错误        4        error C1083: 无法打开包括文件: “graphics.h”: No such file or directory        stdafx.h        17        1        C++俄 ...

需要下载easyX图像库
hui00000 发表于 2019-3-24 15:40
Peter.c 发表于 2019-3-24 16:01
支持一个
Nevatu 发表于 2019-3-24 16:19

感谢分享,
头像被屏蔽
tlf 发表于 2019-3-24 16:32
提示: 作者被禁止或删除 内容自动屏蔽
 楼主| Ingran 发表于 2019-3-24 22:25
被置顶了,受宠若惊。
缥缈的心情 发表于 2019-3-24 22:36
下载下来学习一下. 感觉蛮精炼的.结构也很清晰.
 楼主| Ingran 发表于 2019-3-24 23:30
缥缈的心情 发表于 2019-3-24 22:36
下载下来学习一下. 感觉蛮精炼的.结构也很清晰.

谢谢支持
qoovoop 发表于 2019-3-26 09:43
支持分享精神!
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-25 17:12

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表