C++之俄罗斯方块

代码内容:

#pragma once

#include "afxwin.h"
#include "Resource.h"

const int BLOCK_LINE = 20; //当前地图的行数
const int BLOCK_ROW = 10; //当前地图的列数
class CBlock
{
public:
    CBlock(void);
    ~CBlock(void);//什么意思?
private:     
    //当前方块形状
    int m_BlockShape;
    //下一个方块形状
    int m_nextBlockShape;
    //当前方块状态
    int m_BlockState;
    //等级
    int m_BlockLevel;
    //当前可消行数
    int m_BlockCurDelLine;
    //游戏是否在执行
    bool m_BlockRunning;
    //当前方块位置
    POINT m_BlockPos[4];
    //下个方块位置
    POINT m_nextBlockPos[4];
    //当前屏幕状态
    int m_Blockmap[BLOCK_LINE][BLOCK_ROW];
public:
    //初始化游戏
    void BlockInit(void);
private:
    //记录方块图形
    CBitmap m_bmpBlock[7];
public:
    void CreateNewBlock(void);
    //临时内存DC
    void MakeNewBlock(int shape,POINT pt[],int xPos);
    //CDC memDC;
    void DrawBlock(CDC* pDC);
    void BlockDropDown(CDC* pDC);
    //隐藏方块
    void HideBlock(CDC* pDC);
private:
    //背景图片
    CBitmap m_bmpBKGND;
public:
    //画下个方块的框框
    void DrawNextBlock(CDC* pDC);
    void BlockLeft(CDC* pDC);
    void BlockRight(CDC* pDC);
    void BlockFastDown(CDC* pDC);
    void ChangeState(CDC* pDC);
    //消除一行
    void DeleteLine(CDC* pDC,int LineNum,bool start);
    //游戏是否结束
    bool BlockGameOver(void);
    //计分板
    void BlockScore(int del,CDC* pDC);
    bool BlockLevelUp(void);
    int BlockSpeed(void);

};
#include "StdAfx.h"
#include "Block.h"

CBlock::CBlock(void)
{
    srand((unsigned)time(NULL));
    //当前方块形状
    m_BlockShape = 0;
    //下一个方块形状
    m_nextBlockShape = rand()%7;
    //当前方块状态
    m_BlockState = 0;
    //等级
    m_BlockLevel = 1;
    //当前可消行数
    m_BlockCurDelLine = 0;
    //游戏是否在执行
    m_BlockRunning = false;
    //当前的屏幕状态
    memset(m_Blockmap,0,sizeof(m_Blockmap));

}

CBlock::~CBlock(void)
{

}

//初始化游戏
void CBlock::BlockInit(void)
{
    //读取方块图片
    for(int i=0;i<7;i++)
    {
        m_bmpBlock[i].LoadBitmapW(IDB_BLOCKBAR+i);
    }
    m_bmpBKGND.LoadBitmapW(IDB_BMPBACK);
}

void CBlock::CreateNewBlock(void)
{
    m_BlockShape = m_nextBlockShape;
    m_nextBlockShape = rand()%7;
    m_BlockState = 0;
    MakeNewBlock(m_BlockShape,m_BlockPos,4);
}

//shape为形状,pt是坐标位置,xPos是偏移量
//生成新的方块
void CBlock::MakeNewBlock(int shape,POINT pt[],int xPos)
{
    switch(shape)
    {
    case 0: //长条形
        for(int i=0;i<4;i++)
        {
            pt[i].x=i+xPos;
            pt[i].y=0;
        }
        break;
    case 1: //三角形
         pt[0].x = 1+xPos;
         pt[0].y = 0;
         for(int i=1; i<4; i++)
         {
             pt[i].x = i-1+xPos;
             pt[i].y = 1;
         }
         break;
     case 2: // Z形
         pt[0].x = 0+xPos;
         pt[0].y = 0;
        
         pt[1].x = 1+xPos;
         pt[1].y = 0;
        
         pt[2].x = 1+xPos;
         pt[2].y = 1;
        
         pt[3].x = 2+xPos;
         pt[3].y =     1;
         break;
     case 3: //倒Z形
         pt[0].x = 0+xPos;
         pt[0].y = 1;
        
         pt[1].x = 1+xPos;
         pt[1].y = 1;
        
         pt[2].x = 1+xPos;
         pt[2].y = 0;
        
         pt[3].x = 2+xPos;
         pt[3].y = 0;
         break;
     case 4: //倒L形
         pt[0].x = 0+xPos;
         pt[0].y = 0;
         for(int i=1; i<4; i++)
         {
             pt[i].x = i-1+xPos;
             pt[i].y = 1;
         }
         break;
     case 5: //L形
         pt[0].x = 2+xPos;
         pt[0].y = 0;
         for(int i=1; i<4; i++)
         {
             pt[i].x = i-1+xPos;
             pt[i].y = 1;
         }
         break;
     case 6:
         for(int i=0; i<2; i++)
         {
             pt[i].x = i+xPos;
             pt[i].y = 0;
             pt[i+2].x = i+xPos;
             pt[i+2].y = 1;
        }
         break;
     default:
         break;
    }
}

void CBlock::DrawBlock(CDC* pDC)
{
    //BlockInit();
    //MakeNewBlock(m_BlockShape,m_BlockPos,4);
    CDC memDC;
    memDC.CreateCompatibleDC(pDC);
    memDC.SelectObject(&m_bmpBlock[m_BlockShape]);
    int x = BLOCK_X;
    int y = BLOCK_Y;
    for(int i=0;i<4;i++)
    {
        x = BLOCK_X + m_BlockPos[i].x*BLOCK_WIDTH;
        y = BLOCK_Y + m_BlockPos[i].y*BLOCK_HEIGHT;
        pDC->BitBlt(x,y,20,20,&memDC,0,0,SRCCOPY);
    }
    //for(int i=0;i<4;i++)
    // {
    //   m_BlockPos[i].y++;
    // }
    //memDC.DeleteDC();
}

void CBlock::BlockDropDown(CDC* pDC)
{
    int i,j;
    bool bAbledrop = true;
    //判断能否下落 没看懂
    for(i=0;i<4;i++)
    {
        if(19 == m_BlockPos[i].y || 0 != m_Blockmap[m_BlockPos[i].y+1][m_BlockPos[i].x])
        {
            bAbleDrop = false;
            break;
        }
    }
    //能下落
    if(true == bAbleDrop)
    {
        HideBlock(pDC);
        for(i=0;i<4;i++)
        {
            m_BlockPos[i].y++;
        }
        DrawBlock(pDC);
    }
    //不能下落
    else
    {
        for(i=0;i<4;i++)
        {
            m_Blockmap[m_BlockPos[i].y][m_BlockPos[i].x] = m_BlockShape+1;
        }
        for(i=0;i<20;i++)
        {
            for(j=0;j<10;j++)
            {
                if(0==m_Blockmap[i][j])
                {
                    break;
                }
            }
            if(10==j)//能消一行
            {
                DeleteLine(pDC,i,false);
                m_BlockCurDelLine++;
                BlockScore(1,pDC);
            }
        }
        CreateNewBlock();
        MakeNewBlock(m_BlockShape,m_BlockPos,4);
        DrawBlock(pDC);
        DrawNextBlock(pDC0;

    }
}

//隐藏方块
void CBlock::HideBlock(CDC* pDC)
{
    CDC bkDC;
    bkDC.CreateCompatibleDC(pDC);
    bkDC.SelectObject(m_bmpBKGND);
    int x,y;
    for(int i=0;i<4;i++)
    {
        x = BLOCK_X+BLOCK_WIDTH*m_BlockPos[i].x;
        y = BLOCK_Y+BLOCK_HEIGHT*m_BlockPos[i].y;
        pDC->BitBlt(x,y,BLOCK_WIDTH,BLOCK_HEIGHT,&bkDC,x,y,SRCCOPY);
    }
    bkDC.DeleteDC();
}

//画下个方块的框框
void CBlock::DrawNextBlock(CDC* pDC)
{
    CDC bkDC;
    bkDC.CreateCompatibleDC(pDC);
    bkDC.SelectObject(m_bmpBKGND);
    int x,y;
    pDC->BitBlt(NEXTBLOCK_X,NEXTBLOCK_Y,NEXTBLOCK_WIDTH,NEXTBLOCK_HEIGHT,
        &bkDC,NEXTBLOCK_X,NEXTBLOCK_Y,SRCCOPY);
    MakeNewBlock(m_nextBlockShape,m_nextBlockPos,0);
    bkDC.SelectObject(m_bmpBlock[m_nextBlockShape]);
    //是长条的时候
    if( 0 == m_nextBlockShape )
     {
         for( int i=0; i<4; i++ )
         {
             x = NEXTBLOCK_X + 15 + BLOCK_WIDTH*m_nextBlockPos[i].x;
             y = NEXTBLOCK_Y + 47 + BLOCK_HEIGHT*m_nextBlockPos[i].y;
             pDC->BitBlt( x, y, BLOCK_WIDTH, BLOCK_HEIGHT, &bkDC, 0, 0, SRCCOPY );
         }
     }
     //是方形的时候
     else if( 6 == m_nextBlockShape )
     {
         for( int i=0; i<4; i++ )
         {
             x = NEXTBLOCK_X + 35 + BLOCK_WIDTH*m_nextBlockPos[i].x;
             y = NEXTBLOCK_Y + 38 + BLOCK_HEIGHT*m_nextBlockPos[i].y;
             pDC->BitBlt( x, y, BLOCK_WIDTH, BLOCK_HEIGHT, &bkDC, 0, 0, SRCCOPY );
         }
     }
     //其他的
     else
     {
         for( int i=0; i<4; i++ )
         {
             x = NEXTBLOCK_X + 24 + BLOCK_WIDTH*m_nextBlockPos[i].x;
             y = NEXTBLOCK_Y + 38 + BLOCK_HEIGHT*m_nextBlockPos[i].y;
             pDC->BitBlt( x, y, BLOCK_WIDTH, BLOCK_HEIGHT, &bkDC, 0, 0, SRCCOPY );
         }
     }
}

//左移
void CBlock::BlockLeft(CDC* pDC)
{
    for(int i=0;i<4;i++)
    {
        if(m_BlockPos[i].x<=0) //判断是否到最左边
            return;
        if(0 != m_Blockmap[m_BlockPos[i].y][m_BlockPos[i].x-1])
            return;
    }
    HideBlock(pDC);
    for(int i=0;i<4;i++)
    {
        m_BlockPos[i].x-=1;
    }
    DrawBlock(pDC);
}

void CBlock::BlockRight(CDC* pDC)
{
    for(int i=0;i<4;i++)
    {
        if(m_BlockPos[i].x>=9) //判断是否到最右边
            return;
        if(0 != m_Blockmap[m_BlockPos[i].y][m_BlockPos[i].x+1]) //判断右边是否有方块
            return;
    }
    HideBlock(pDC);
    for(int i=0;i<4;i++)
    {
        m_BlockPos[i].x+=1;
    }
    DrawBlock(pDC);
}

void CBlock::BlockFastDown(CDC* pDC)
{
    for(int i=0;i<4;i++)
    {
      if(m_BlockPos[i].y>=19) //判断是否到最右边
          return;
      if(0 != m_Blockmap[m_BlockPos[i].y+1][m_BlockPos[i].x])
          return;
    }
    HideBlock(pDC);
    for(int i=0;i<4;i++)
    {
        m_BlockPos[i].y+=1;
    }
    DrawBlock(pDC);
}

#include "StdAfx.h"
#include "Block.h"

CBlock::CBlock(void)
{
    srand((unsigned)time(NULL));
    //当前方块形状
    m_BlockShape = 0;
    //下一个方块形状
    m_nextBlockShape = rand()%7;
    //当前方块状态
    m_BlockState = 0;
    //等级
    m_BlockLevel = 1;
    //当前可消行数
    m_BlockCurDelLine = 0;
    //游戏是否在执行
    m_BlockRunning = false;
    //当前屏幕状态
    memset(m_Blockmap,0,sizeof(m_Blockmap));

}

CBlock::~CBlock(void)
{

}

//初始化游戏
void CBlock::BlockInit(void)
{
    //读取方块图片
    for(int i=0;i<7;i++)
    {
        m_bmpBlock[i].LoadBitmapW(IDB_BLOCKBAR+i);
    }
    m_bmpBKGND.LoadBitmapW(IDB_BMPBACK);
}

void CBlock::CreateNewBlock(void)
{
    m_BlockShape = m_nextBlockShape;
    m_nextBlockShape = rand()%7;
    m_BlockState = 0;
    MakeNewBlock(m_BlockShape,m_BlockPos,4);
}

//shape为形状,pt是坐标位置,xPos是偏移量
//生成新的方块
void CBlock::MakeNewBlock(int shape, POINT pt[], int xPos)
{
     switch(shape)
     {
     case 0://长条形
         for(int i=0; i<4; i++)
         {
             pt[i].x=i+xPos;
             pt[i].y=0;
         }
         break;
     case 1://三角形
         pt[0].x = 1+xPos;
         pt[0].y = 0;
         for(int i=1; i<4; i++)
         {
             pt[i].x = i-1+xPos;
             pt[i].y = 1;
         }
         break;
     case 2://Z形
         pt[0].x = 0+xPos;
         pt[0].y = 0;
        
       pt[1].x = 1+xPos;
         pt[1].y = 0;
        
         pt[2].x = 1+xPos;
         pt[2].y = 1;
        
         pt[3].x = 2+xPos;
         pt[3].y = 1;
         break;
case 3://鍊抁褰?
         pt[0].x = 0+xPos;
         pt[0].y = 1;
        
         pt[1].x = 1+xPos;
         pt[1].y = 1;
        
         pt[2].x = 1+xPos;
         pt[2].y = 0;
        
         pt[3].x = 2+xPos;
         pt[3].y = 0;
         break;

     case 4://倒L形
          pt[0].x = 0+xPos;
         pt[0].y = 0;
         for(int i=1; i<4; i++)
         {
             pt[i].x = i-1+xPos;
             pt[i].y = 1;
         }
         break;
     case 5://L形
         pt[0].x = 2+xPos;
         pt[0].y = 0;
         for(int i=1; i<4; i++)
         {
             pt[i].x = i-1+xPos;
             pt[i].y = 1;
         }
         break;

     case 6:
         for(int i=0;i<2;i++)
         {
             pt[i].x = i+xPos;
             pt[i].y = 0;
             pt[i+2].x = i+xPos;
             pt[i+2].y = 1;
         }
         break;
       default;
         break;
     }
}

void CBlock::DrawBlock(CDC* pDC)
{
    //BlockInit();
    //MakeNewBlock(m_BlockShape,m_BlockPos,4);
    CDC memDC;
    memDC.CreateCompatibleDC(pDC);
    memDC.SelectObject(&m_bmpBlock[m_BlockShape]);
    int x = BLOCK_X;
    int y = BLOCK_Y;
    for(int i=0;i<4;i++)
    {
        x = BLOCK_X + m_BlockPos[i].x*BLOCK_WIDTH;
        y = BLOCK_Y + m_BlockPos[i].y*BLOCK_HEIGHT;
        pDC->BitBlt(x,y,20,20,&memDC,0,0,SRCCOPY);
    }
    //for(int i=0;i<4;i++)
    // {
    //   m_BlockPos[i].y++;
    // }
    //memDC.DeleteDC();
}

void CBlock::BlockDropDown(CDC* pDC)
{
    int i,j;
    bool bAbleDrop = true;
    //判断能否下落
    for(i=0;i<4;i++)
    {
        if(19 == m_BlockPos[i].y || 0 != m_Blockmap[m_BlockPos[i].y+1][m_BlockPos[i].x])
        {
           bAbleDrop = false;
           break;
        }
    }
    //能下落
    if(true==bAbleDrop)
    {
        HideBlock(pDC);
        for(i=0;i<4;i++)
        {
            m_BlockPos[i].y++;
        }
        DrawBlock(pDC);
    }
    //不能下落
    else
    {
        for(i=0;i<4;i++)
        {
            m_Blockmap[m_BlockPos[i].y][m_BlockPos[i].x] = m_BlockShape+1;
        }
        for(i=0;i<20;i++)
        {
            for(j=0;j<10;j++)
            {
                if(0==m_Blockmap[i][j])
                {
                    break;
                }
            }
            if(10==j) //能消一行
            {
                DeleteLine(pDC,i,false);
                m_BlockCurDelLine++;
                BlockScore(1,pDC);
            }
        }
        CreateNewBlock();
        MakeNewBlock(m_BlockShape,m_BlockPos,4);
        DrawBlock(pDC);
        DrawNextBlock(pDC);

    }
}

//隐藏方块
void CBlock::HideBlock(CDC* pDC)
{
     CDC bkDC;
     bkDC.CreateCompatibleDC(pDC);
     bkDC.SelectObject(m_bmpBKGND);
     int x, y;
     for( int i=0; i<4; i++ )
     {
         x = BLOCK_X+BLOCK_WIDTH*m_BlockPos[i].x;
         y = BLOCK_Y+BLOCK_HEIGHT*m_BlockPos[i].y;
         pDC->BitBlt( x, y, BLOCK_WIDTH, BLOCK_HEIGHT, &bkDC, x, y, SRCCOPY );
     }
     bkDC.DeleteDC();
}

//画下个方块的框框
void CBlock::DrawNextBlock( CDC* pDC )
{
     CDC bkDC;
     bkDC.CreateCompatibleDC( pDC );
     bkDC.SelectObject( m_bmpBKGND );
     int x ,y;
     pDC->BitBlt( NEXTBLOCK_X, NEXTBLOCK_Y, NEXTBLOCK_WIDTH, NEXTBLOCK_HEIGHT,
         &bkDC, NEXTBLOCK_X, NEXTBLOCK_Y, SRCCOPY );

  MakeNewBlock( m_nextBlockShape, m_nextBlockPos, 0 );
     bkDC.SelectObject( m_bmpBlock[m_nextBlockShape] );
     //是长条的时候
     if(0 == m_nextBlockShape)
     {
         for(int i=0;i<4;i++)
         {
             x = NEXTBLOCK_X + 15 + BLOCK_WIDTH*m_nextBlockPos[i].x;
             y = NEXTBLOCK_Y + 47 + BLOCK_HEIGHT*m_nextBlockPos[i].y;
             pDC->BitBlt( x, y, BLOCK_WIDTH, BLOCK_HEIGHT, &bkDC, 0, 0, SRCCOPY );
         }
     }
     //是方形的时候
     else if( 6 == m_nextBlockShape )
     {
         for( int i=0; i<4; i++ )
         {
             x = NEXTBLOCK_X + 35 + BLOCK_WIDTH*m_nextBlockPos[i].x;
             y = NEXTBLOCK_Y + 38 + BLOCK_HEIGHT*m_nextBlockPos[i].y;
             pDC->BitBlt( x, y, BLOCK_WIDTH, BLOCK_HEIGHT, &bkDC, 0, 0, SRCCOPY );
         }
     }
     //其他的情况
     else
     {
         for( int i=0; i<4; i++ )
         {
             x = NEXTBLOCK_X + 24 + BLOCK_WIDTH*m_nextBlockPos[i].x;
             y = NEXTBLOCK_Y + 38 + BLOCK_HEIGHT*m_nextBlockPos[i].y;
             pDC->BitBlt( x, y, BLOCK_WIDTH, BLOCK_HEIGHT, &bkDC, 0, 0, SRCCOPY );
         }
     }
}

//左移

void CBlock::BlockLeft(CDC* pDC)
{    
     for( int i=0; i<4; i++ )
     {
         if( m_BlockPos[i].x <= 0 )   //判断是否到最左边
             return;
         if( 0 != m_Blockmap[m_BlockPos[i].y][m_BlockPos[i].x-1] )   //判断左边是否有方块
             return;
     }
     HideBlock( pDC );
     for( int i=0; i<4; i++ )
     {
         m_BlockPos[i].x -= 1;
     }
     DrawBlock( pDC );
}

void CBlock::BlockRight(CDC* pDC)
{
     for( int i=0; i<4; i++ )
     {
         if( m_BlockPos[i].x >= 9 )   //判断是否到最右边
             return;
         if( 0 != m_Blockmap[m_BlockPos[i].y][m_BlockPos[i].x+1] )   //判断右边是否有方块
             return;
     }
     HideBlock( pDC );
     for( int i=0; i<4; i++ )
     {
         m_BlockPos[i].x += 1;
     }
     DrawBlock( pDC );
}

void CBlock::BlockFastDown(CDC* pDC)
{
     for( int i=0; i<4; i++ )
     {
         if( m_BlockPos[i].y >= 19 )   //
             return;
         if( 0 != m_Blockmap[m_BlockPos[i].y+1][m_BlockPos[i].x] )   //
             return;
     }
     HideBlock( pDC );
     for( int i=0; i<4; i++ )
     {
         m_BlockPos[i].y += 1;
     }
     DrawBlock( pDC );

}

总结:第一次简单的C++开发

问题:

 

#pragma once :这是一个比较常用的C/C++杂注,只要在头文件的最开始加入这条             杂注,就能够保证头文件只被编译一次。

#pragma once是编译器相关的,就是说即使这个编译系统上有效,但在其他编译系统也不一定可以,不过现在基本上已经是每个编译器都有这个杂注了。

#ifndef#define#endifC/C++语言中的宏定义,通过宏定义避免文件多次编译。所以在所有支持C++语言的编译器上都是有效的,如果写的程序要跨平台,最好使用这种方式。

 

Const:是一个C语言的关键字,它限定一个变量不允许被改变。使用const在一定程度上可以提高程序的安全性和可靠性。另外,在观看别人代码的时候,清晰理解const所起的作用,对理解对方的程序也有一些帮助。另外CONST在其他编程语言中也有出现,如C++PHP5C#.netHC08 C

::1)表示域操作符
  例:声明了一个类A,类A里声明了一个成员函数void f(),但没有在类的声明里给出f的定义,那么在类外定义f时,就要写成void A::f(),表示这个f()函数是类A成员函数
 (2)直接用在全局函数前,表示是全局函数
  例:在VC里,你可以在调用API 函数里,在API函数名前加::
 (3)表示引用成员函数及变量,作用域成员运算符
  例:System::Math::Sqrt() 相当于System.Math.Sqrt()

在一个新的类中 先定义变量 定义孩子类 定义方法 然后重写具体的类中变量和作用

void CBlock::MakeNewBlock(int shape,POINT pt[],int xPos)


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值