Click here to Skip to main content
12,450,977 members (23,490 online)
Click here to Skip to main content
Articles » Multimedia » DirectX » Games » Downloads

Stats

223.4K views
8.4K downloads
71 bookmarked
Posted

Falling Blocks

, 17 Apr 2008 CPOL
A game written using Visual C++ and DirectX.
FallingBlocks.exe
FallingBlocks
Block are Falling.doc
DirectX.ico
Drowning.wav
Graphics.bmp
LineComplete.wav
Move.wav
Place.wav
Web Falling Blocks.doc
Block are Falling.doc
DirectX.ico
Drowning.wav
FallingBlocks.suo
Graphics.bmp
LineComplete.wav
Move.wav
Place.wav
Web Falling Blocks.doc
Block are Falling.doc
DirectX.ico
Drowning.wav
FallingBlocks.dsp
FallingBlocks.dsw
Graphics.bmp
LineComplete.wav
Move.wav
Place.wav
Web Falling Blocks.doc
// FlooredBlocks.cpp: implementation of the CFlooredBlocks class.
//
//////////////////////////////////////////////////////////////////////
#include <windows.h>
#include "BlockList.h"
#include "FlooredBlocks.h"
#include "Resource.h"

extern HINSTANCE g_hInstance;

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CFlooredBlocks::CFlooredBlocks(RECT rcBoundary)
{
	m_rcBoundary=rcBoundary;

}

CFlooredBlocks::~CFlooredBlocks()
{

}

bool CFlooredBlocks::IsOccupied(short nX, short nY)
{
	return CBlockList::IsOccupied(nX,nY);
}

void CFlooredBlocks::Display()
{
	CBlockList::Display();
}

bool CFlooredBlocks::Insert(CBlockList &BlockList)
{
	SNODE* pCurr = BlockList.m_pListHead->pNext;
	while (pCurr) {
		if (CBlockList::Insert(pCurr->Block) == false)
			return false;
		pCurr= pCurr->pNext;
	}
	return true;
}

bool CFlooredBlocks::IsGameOver()
{
	for (short nI=0; nI <= m_rcBoundary.right; nI++) {
		if (CBlockList::IsOccupied(nI, 1))
			return true;
	}
	return false;
}

////////////////////////////////////////////////////////
// Returns true if the give location already 
// contains a block
///////////////////////////////////////////////////////
bool CFlooredBlocks::IsOccupied(CBlockList &BlockList, short nX, short nY)
{
	SNODE* pCurr= BlockList.m_pListHead->pNext;
	while (pCurr) {
		if (IsOccupied(pCurr->Block.nX+nX,pCurr->Block.nY+nY)) 
			return true;
		pCurr = pCurr->pNext;
	}
	return false;
}

/////////////////////////////////////////
// Increment all Y < nY
// Used when the floored bricks must fall
/////////////////////////////////////////
void CFlooredBlocks::IncrementYabove(short nY)
{
	SNODE* pCurr = m_pListHead->pNext;
	while (pCurr) {
		if (pCurr->Block.nY < nY)
			pCurr->Block.nY++;
		pCurr= pCurr->pNext;
	}
}
//////////////////////////////////////////////////////////////////
//  Checks and removes any completed rows of blocks
//////////////////////////////////////////////////////////////////
short CFlooredBlocks::CheckAndRemoveContinuousBlocks()
{
	SNODE* pCurr= m_pListHead->pNext;
	SNODE* pPrev= m_pListHead;
	short nRowsDeleted=0;
	short nX1 = (short) m_rcBoundary.left;
	short nX2 = (short) m_rcBoundary.right;

	while (pCurr) {
		while (pCurr && pCurr->Block.nX != nX1) {
			pPrev=pCurr;
			pCurr = pCurr->pNext;
		}
		if (pCurr) {
			SNODE* pBegin= pPrev; // pBegin->Next actually contains the begining.
			while (pCurr && pCurr->pNext && pBegin->pNext->Block.nY == pCurr->Block.nY 
				&& (pCurr->Block.nX+1) == (pCurr->pNext->Block.nX)) {
				pPrev = pCurr;
				pCurr=pCurr->pNext;
			}
			if (pBegin->pNext->Block.nX == nX1 && 
				pBegin->pNext->Block.nY == pPrev->Block.nY &&
				pCurr->Block.nX == nX2)  {       // Found a row to remove
				nRowsDeleted++;
				SNODE* pDelete = pBegin->pNext;
				pCurr=pCurr->pNext;   // Move pointer to next location since the current will be deleted
				IncrementYabove(pBegin->pNext->Block.nY);
				while (pDelete && pDelete != pCurr) {
					SNODE* pDel= pDelete;
					pDelete=pDelete->pNext;
				//	delete pDel;
				}
				pBegin->pNext = pCurr;
	            pCurr= m_pListHead->pNext;
	            pPrev= m_pListHead;
			}
			pPrev = pCurr;
			pCurr=pCurr->pNext;
		}
	}
	if (nRowsDeleted) {
		::PlaySound(MAKEINTRESOURCE( IDR_SOUNDLINECOMPLETE), g_hInstance, SND_RESOURCE | SND_ASYNC);
	}
	return nRowsDeleted;

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Xavier John
Software Developer
United States United States
No Biography provided

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160826.1 | Last Updated 17 Apr 2008
Article Copyright 2001 by Xavier John
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid