⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 game.cpp

📁 俄罗斯方块小游戏的源码 只实现了基本的功能
💻 CPP
字号:
// Game.cpp: implementation of the CGame class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Game.h"
#include "SmallBox.h"
#include "resource.h"
#include <stdio.h>
#include <time.h>
extern g_GameRow;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CGame::CGame()
:Disabletag(TRUE),score(0)
{

}

CGame::~CGame()
{

}

void CGame::InitGame(CSmallBox (*psb)[g_GameBgRow])
{
    srand((UINT)time(NULL));
    SetCurX((g_GameBgCol - 4)/2);
    SetCurY(1);
    for(int i = 0;i < g_GameBgCol;i++)
    {
        for(int j = 0 ;j < g_GameBgRow;j++)
        {
            if(i == 0 || j == 0 || i == g_GameBgCol - 1 || j == g_GameBgRow - 1)
            {
                psb[i][j].SetDisable();
            }
            else
            {
                psb[i][j].SetBackGround();
            }
        }
   }
}

void CGame::GamePaint(CSmallBox (*psb)[g_GameBgRow],HDC hdc)
{
    HICON hicon = NULL;
//    ICONINFO incoinfo;
    //GetIconInfo(hicon,&incoinfo);
    for(int x = 0;x < g_GameBgCol; x++)
    {
        for(int y = 0;y < g_GameBgRow;y++)
        {
            switch(psb[x][y].GetState())
            {
            case MOVE:
                hicon = LoadIcon(GetModuleHandle(NULL),MAKEINTRESOURCE(ICON_ENABLE));
                break;
            case DISABLEMOVE:
                hicon = LoadIcon(GetModuleHandle(NULL),MAKEINTRESOURCE(ICON_DISMOVE));
                break;
            case BACKGROUND:
                hicon = LoadIcon(GetModuleHandle(NULL),MAKEINTRESOURCE(ICON_BG));
                break;
            default:
                break;
            }
            
           DrawIconEx(hdc,x*16,
                y*16,
                hicon,16,
               16,0,NULL,DI_NORMAL);
           
           //MessageBox(NULL,NULL,NULL,MB_OK);
          //  DrawIcon(hdc,x*incoinfo.xHotspot,y*incoinfo.yHotspot,hicon);
        }
    }
    DrawBox(hdc,hicon);
        DeleteObject(hicon);
}

void CGame::WriteShape()
{
    FILE *fp;
    fp = fopen("shape.dat","rb");
    if(fp == NULL)
    {
        MessageBox(NULL,"读取形状文件失败","窗口",MB_OK);
    }
    fread(&m_shapebox,132,20,fp);
    //int i = sizeof(m_shapebox);
    fclose(fp);
}

void CGame::DrawBox(HDC hdc,HICON hicon)
{
    
    for(int i = 0;i < 4;i++)
    {
        for(int j = 0;j < 4;j++)
        {
             switch(m_gamebox.smallbox[i][j].GetState())
             {
             case BACKGROUND:
                 
                 break;
             case MOVE:
                 DrawIconEx(hdc,(GetCurX()+i)*16,(j+GetCurY())*16,
                     hicon,16,16,0,NULL,DI_NORMAL);
                 break;
             case DISABLEMOVE:
                 break;
             }
        }
    }
}

int CGame::GetCurX()
{
    return m_curx;
}

int CGame::GetCurY()
{
    return m_cury;
}

void CGame::SetCurX(int curx)
{
    m_curx = curx;
}

void CGame::SetCurY(int cury)
{
    m_cury = cury;
}

BOOL CGame::IsCanMOVE(CSmallBox (*psb)[g_GameBgRow],int curx,int cury)
{
    for(int i = 0;i < 4;i++)
    {
        for(int j = 0;j < 4;j++)
        {
            if(m_gamebox.smallbox[i][j].GetState() == MOVE)
            {
                if(psb[curx + i][cury + j].GetState() != BACKGROUND)
                {
                    return FALSE;
                }
            }
        }
    }
    return TRUE;
}


BOOL CGame::IsCanDisable(CSmallBox (*psb)[g_GameBgRow],int curx,int cury)
{
    for(int i = 0;i < 4;i++)
    {
        for(int j = 0;j < 4;j++)
        {
            if(m_gamebox.smallbox[i][j].GetState() == MOVE)
            {
                if(psb[curx + i][cury + j+1].GetState() == DISABLEMOVE)
                {
                    return TRUE;
                }
            }
        }
    }
    return FALSE;
}

void CGame::DisableBox(HWND hwnd,HDC hdc,CSmallBox (*psb)[g_GameBgRow],int curx,int cury,HICON hicon)
{
    if (cury == 1)
    {
        SetOver(TRUE);
    }
    for(int i = 0;i < 4;i++)
    {
        for(int j = 0;j < 4;j++)
        {
            if(m_gamebox.smallbox[i][j].GetState() == MOVE)
            {
                
                psb[curx + i][cury + j].SetDisable();
            }
        }
    }
//	index = 0;
// 	ReleaseBox(hwnd,psb,curx,cury);
	//GreateBox(hdc,hicon);
}

void CGame::GreateBox(HDC hdc,HICON hicon)
{
    SetCurX((g_GameBgCol - 4)/2);
    SetCurY(1);
	SetCurAry(rand()%5);
    m_gamebox = m_shapebox[GetCurAry()][0];
    //DrawBox(hdc,hicon);
}

BOOL CGame::IsCanRelease(CSmallBox (*psb)[g_GameBgRow],int line)
{
        for(int j = 1;j < g_GameBgRow-1;j++)
        {
            if(psb[j][line-1].GetState() != DISABLEMOVE)
            {
                return FALSE;
            }
        }
    return TRUE;
}

void CGame::ReleaseBox(HWND hwnd,CSmallBox (*psb)[g_GameBgRow],int curx,int cury)
{
    for(int i = g_GameBgCol - 1;i>=2;)
    {
        if(IsCanRelease(psb,i) == TRUE)
        {
            for(int x = 1;x < g_GameBgRow-1;x++)
            {
                for(int y = i-1;y > 1;y--)
                {
                    psb[x][y] = psb[x][y-1];
                }
                //psb[x][1].SetBackGround();
            }
			//score = score + 10;
        }
        else
        {
            i--;
        }
    }
	
}

BOOL CGame::IsCanChange(CSmallBox (*psb)[g_GameBgRow],int mx,int my)
{
    CBox temp = m_shapebox[GetCurAry()][(index + 1)%4];
    return IsCanMoveEx(psb,temp,mx,my);
}

void CGame::ChangeBox(CSmallBox (*psb)[g_GameBgRow],int curx,int cury)
{
      m_gamebox = m_shapebox[GetCurAry()][(++index)%4];
}

int CGame::GetIndex()
{
    return index;
}

void CGame::SetIndex(int number)
{
    index = number;
}

BOOL CGame::IsCanMoveEx(CSmallBox (*psb)[g_GameBgRow],CBox newbox,int curx,int cury)
{
    for(int i = 0;i < 4;i++)
    {
        for(int j = 0;j < 4;j++)
        {
            if(newbox.smallbox[i][j].GetState() == MOVE)
            {
                if(psb[curx + i][cury + j].GetState() != BACKGROUND)
                {
                    return FALSE;
                }
            }
        }
    }
    return TRUE;
}

void CGame::SetOver(BOOL m_bool)
{
    m_over = m_bool;
}

BOOL CGame::GetOver()
{
    return m_over;
}

void CGame::SetCurAry(int number)
{
	curindex = number;
}

int CGame::GetCurAry()
{
	return curindex;
}

UINT CGame::GetScore()
{
	return score;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -