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

📄 block.cpp

📁 俄罗斯方块 采用MFC和GDI+编写的,自己感觉写得不是很好,不过看上去还行
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include "stdafx.h"
#include "Block.h"

Block::Block(void)
{
}

Color Block::backColors[8] = {Color(),Color::Red,Color::Blue,Color::Red,
		Color::Yellow,Color::Green,Color::White,Color::Black};

Color Block::foreColor[8] = {Color(),Color::Purple,Color::LightBlue,
		Color::Yellow,Color::Red,Color::LightGreen,Color::Black,Color::White};


Block::~Block(void)
{
	if(this->square1 != NULL)
		delete square1;
	if(this->square2 != NULL)
		delete square2;
	if(this->square3 != NULL)
		delete square3;
	if(this->square4 != NULL)
		delete square4;
}
Block::Block(Point location, BlockTypes newBlockType,GameField &engine)
{
	//
	//
	this->_engine = & engine;
	this->squareSize = engine.SquareSize;
	this->StatusRotation = RotationDirecttions::NORTH;
	int createType = Random(1,7);
	if(newBlockType == BlockTypes::Undefined)
	{
		BlockType = (BlockTypes)createType;
	}
	else
	{
		BlockType = newBlockType;
	}
	
	this->square1 = new Square(Size(this->squareSize,this->squareSize),backColors[(int)BlockType],foreColor[(int)BlockType]);
	this->square2 = new Square(Size(this->squareSize,this->squareSize),backColors[(int)BlockType],foreColor[(int)BlockType]);
	this->square3 = new Square(Size(this->squareSize,this->squareSize),backColors[(int)BlockType],foreColor[(int)BlockType]);
	this->square4 = new Square(Size(this->squareSize,this->squareSize),backColors[(int)BlockType],foreColor[(int)BlockType]);

	switch(BlockType)
	{
	case BlockTypes::Square_:
		this->square1->_Location = location;
		this->square2->_Location.X = location.X + squareSize;
		this->square2->_Location.Y = location.Y;
		this->square3->_Location.X = location.X;
		this->square3->_Location.Y = location.Y + squareSize;
		this->square4->_Location.X = location.X + squareSize;
		this->square4->_Location.Y = location.Y + squareSize;
		break;
	case BlockTypes::Line:
		this->square1->_Location = location;
		this->square2->_Location.X = location.X;
		this->square2->_Location.Y = location.Y + squareSize;
		this->square3->_Location.X = location.X;
		this->square3->_Location.Y = location.Y +2*squareSize;
		this->square4->_Location.X = location.X;
		this->square4->_Location.Y = location.Y + 3*squareSize;
		break;
	case BlockTypes::J:
		this->square1->_Location = location;
		this->square2->_Location.X = location.X - squareSize;
		this->square2->_Location.Y = location.Y;
		this->square3->_Location.X = location.X;
		this->square3->_Location.Y = location.Y + squareSize;
		this->square4->_Location.X = location.X ;
		this->square4->_Location.Y = location.Y + 2*squareSize;
		break;
	case BlockTypes::L:
		this->square1->_Location = location;
		this->square2->_Location.X = location.X + squareSize;
		this->square2->_Location.Y = location.Y;
		this->square3->_Location.X = location.X ;
		this->square3->_Location.Y = location.Y + squareSize;
		this->square4->_Location.X = location.X;
		this->square4->_Location.Y = location.Y + 2*squareSize;
		break;
	case BlockTypes::S:
		this->square1->_Location = location;
		this->square2->_Location.X = location.X -squareSize;
		this->square2->_Location.Y = location.Y;
		this->square3->_Location.X = location.X;
		this->square3->_Location.Y = location.Y + squareSize;
		this->square4->_Location.X = location.X + squareSize;
		this->square4->_Location.Y = location.Y +squareSize;
		break;
	case BlockTypes::T:
		this->square1->_Location = location;
		this->square2->_Location.X = location.X -squareSize;
		this->square2->_Location.Y = location.Y;
		this->square3->_Location.X = location.X;
		this->square3->_Location.Y = location.Y - squareSize;
		this->square4->_Location.X = location.X + squareSize;
		this->square4->_Location.Y = location.Y;
		break;
	case BlockTypes::Z:
		this->square1->_Location = location;
		this->square2->_Location.X = location.X -squareSize;
		this->square2->_Location.Y = location.Y;
		this->square3->_Location.X = location.X;
		this->square3->_Location.Y = location.Y - squareSize;
		this->square4->_Location.X = location.X + squareSize;
		this->square4->_Location.Y = location.Y -squareSize;
		break;
	default:
		break;
	}
}

bool Block::Down()
{
	if(GameField::IsEmpty(square1->_Location.X / squareSize,square1->_Location.Y /squareSize + 1)
		&&GameField::IsEmpty(square2->_Location.X / squareSize,square2->_Location.Y /squareSize +1)
		&&GameField::IsEmpty(square3->_Location.X / squareSize,square3->_Location.Y /squareSize +1)
		&&GameField::IsEmpty(square4->_Location.X / squareSize,square4->_Location.Y /squareSize +1) )
	{
		Hide();
		square1->_Location.Y += squareSize;
		square2->_Location.Y += squareSize;
		square3->_Location.Y += squareSize;
		square4->_Location.Y += squareSize;
		Show();
		return true;
	}
	else
	{
		GameField::StopSquare(*square1,square1->_Location.X / squareSize,square1->_Location.Y / squareSize);
		GameField::StopSquare(*square2,square2->_Location.X / squareSize,square2->_Location.Y / squareSize);
		GameField::StopSquare(*square3,square3->_Location.X / squareSize,square3->_Location.Y / squareSize);
		GameField::StopSquare(*square4,square4->_Location.X / squareSize,square4->_Location.Y / squareSize);
		return false;
	}
}

bool Block::Right()
{
	if(GameField::IsEmpty((int)square1->_Location.X / squareSize + 1,(int)square1->_Location.Y  / squareSize)
		&&GameField::IsEmpty((int)square2->_Location.X / squareSize + 1,(int)square2->_Location.Y  / squareSize)
		&&GameField::IsEmpty((int)square3->_Location.X / squareSize + 1,(int)square3->_Location.Y  / squareSize)
		&&GameField::IsEmpty((int)square4->_Location.X / squareSize + 1,(int)square4->_Location.Y  / squareSize) )
	{
		Hide();
		square1->_Location.X += squareSize;
		square2->_Location.X += squareSize;
		square3->_Location.X += squareSize;
		square4->_Location.X += squareSize;
		Show();
		return true;
	}
	else
	{
		return false;
	}
}

bool Block::Left()
{
	if(GameField::IsEmpty((int)square1->_Location.X / squareSize - 1,(int)square1->_Location.Y  / squareSize)
		&&GameField::IsEmpty((int)square2->_Location.X / squareSize - 1,(int)square2->_Location.Y  / squareSize)
		&&GameField::IsEmpty((int)square3->_Location.X / squareSize - 1,(int)square3->_Location.Y  / squareSize)
		&&GameField::IsEmpty((int)square4->_Location.X / squareSize - 1,(int)square4->_Location.Y  / squareSize) )
	{
		Hide();
		square1->_Location.X -= squareSize;
		square2->_Location.X -= squareSize;
		square3->_Location.X -= squareSize;
		square4->_Location.X -= squareSize;
		Show();
		return true;
	}
	else
	{
		return false;
	}
}

void Block::Rotate()
{
	Point OldPosition1 = square1->_Location;
	Point OldPosition2 = square2->_Location;
	Point OldPosition3 = square3->_Location;
	Point OldPosition4 = square4->_Location;
	RotationDirecttions OldStatusRotation = this->StatusRotation;
	Hide();
	switch(this->BlockType)
	{
	case BlockTypes::Square_:
		Show();
		return;
	case BlockTypes::L:
		switch(this->StatusRotation)
		{
		case RotationDirecttions::NORTH:
			this->StatusRotation = RotationDirecttions::EAST;
			square2->_Location.X = square1->_Location.X;
			square2->_Location.Y = square1->_Location.Y - squareSize;
			square3->_Location.X = square1->_Location.X + squareSize;
			square3->_Location.Y = square1->_Location.Y ;
			square4->_Location.X = square1->_Location.X + squareSize*2;
			square4->_Location.Y = square1->_Location.Y ;
			break;
		case RotationDirecttions::EAST:
			this->StatusRotation = RotationDirecttions::SOUTH;
			square2->_Location.X = square1->_Location.X - squareSize;
			square2->_Location.Y = square1->_Location.Y ;
			square3->_Location.X = square1->_Location.X ;
			square3->_Location.Y = square1->_Location.Y - squareSize;
			square4->_Location.X = square1->_Location.X ;
			square4->_Location.Y = square1->_Location.Y - squareSize*2;
			break;
		case RotationDirecttions::SOUTH:
			this->StatusRotation = RotationDirecttions::WEST;
			square2->_Location.X = square1->_Location.X;
			square2->_Location.Y = square1->_Location.Y + squareSize;
			square3->_Location.X = square1->_Location.X - squareSize;
			square3->_Location.Y = square1->_Location.Y ;
			square4->_Location.X = square1->_Location.X - squareSize*2;
			square4->_Location.Y = square1->_Location.Y ;

⌨️ 快捷键说明

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