📄 gamefield.cpp
字号:
#include "stdafx.h"
#include "GameField.h"
#include <cstdlib>
using namespace std;
int GameField::bitEmpty = 0x0;
int GameField::bitFull =0xffff;
int GameField::Height = HEIGHT ;
int GameField::Width = WIDTH;
int GameField::SquareSize = SIZE;
HDC* GameField::hdc;
DOCINFO * GameField::info;
int GameField::arrBitGameField[HEIGHT];
Square GameField::arrGameField[WIDTH][HEIGHT];
GameField::GameField(HDC &Hdc,DOCINFO &docinfo)
{
GameField::hdc = & Hdc;
GameField::info = & docinfo;
/*Width = 16;
Height = 30;*/
/*SquareSize = 10;*/
//this->arrGameField = new Square[Width,Height];
/*this->arrGameField = new Square*[Width];
for(int i=0;i<Width;i++)
{
this->arrGameField[i] = new Square[Height];
}
this->arrBitGameField = new int[Height];*/
}
GameField::~GameField(void)
{
/*if( this->arrBitGameField != NULL)
delete [] this->arrBitGameField;
for(int i=0;i<Width;i++)
{
delete[] this->arrGameField[i];
}*/
}
bool GameField::IsEmpty(const int &x, const int &y)
{
if((y < 0 || y >= Height) || ( x < 0 || x >= Width ))
return false;
else if((arrBitGameField[y] & (1 << x ) ) != 0)
{
return false;
}
return true;
}
int GameField::CheckLines()
{
int CheckLines_result = 0;
int y = Height -1;
while(y >= 0)
{
if(arrBitGameField[y] == bitEmpty)
y = 0;
if(arrBitGameField[y] == bitFull )
{
CheckLines_result++;
for(int index = y; index >= 0; index--)
{
if(index > 0)
{
arrBitGameField[index] = arrBitGameField[index-1];
for(int x =0;x < Width;x++)
{
arrGameField[x][index] = arrGameField[x][index -1];
if( arrGameField[x][index].IsNull() )
{
arrGameField[x][index]._Location.Y += GameField::SquareSize;
}
}
}
else
{
arrBitGameField[index] = bitEmpty;
for(int x=0;x<Width;x++)
{
arrGameField[x][index].MakeNull();
}
}
}
}
else
{
y--;
}
}
return CheckLines_result;
}
void GameField::StopSquare(const Square &square, const int &x, const int &y)
{
arrBitGameField[y] = arrBitGameField[y] | (1 << x);
arrGameField[x][y] = square;
}
void GameField::Redraw()
{
for(int y = Height -1;y >= 0;y--)//TODO y = height -1;
{
if(arrBitGameField[y] != bitEmpty)
for(int x= Width -1;x>=0;x--)
{
if( arrGameField[x][y].IsNull())
arrGameField[x][y].Show(*hdc,*info);
}
}
}
void GameField::Reset()
{
for(int i=0;i< Height; i++)
{
arrBitGameField[i] = bitEmpty;
for(int j =0;j<Width;j++)
{
arrGameField[j][i].MakeNull();
}
}
}
void GameField::ReDrawAll()
{
for(int i =0;i< Width;i++)
{
for(int j=0;j<Height;j++)
{
if(! arrGameField[i][j].IsNull())
{
arrGameField[i][j].Show(*hdc,*info);
}
}
}
}
void GameField::Clear()
{
for(int i=0;i<Width;i++)
{
for(int j=0;j<Height;j++)
{
arrGameField[i][j].MakeNull();
}
}
}
int Random(int const& start,int const& end)
{
int temp = end - start +1;
srand(unsigned (time(NULL)));
int randNumber = rand()%temp;
randNumber += start;
return randNumber;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -