📄 rsgame.cpp
字号:
// RsGame.cpp: implementation of the RsGame class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "RusGame.h"
#include "RsGame.h"
#include "windows.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
RsGame::RsGame()
{
Score=nLine=0;
CubNum=rand()%7;
memset(CurrentCub,0,sizeof(point)*4);
RotScale=-1;
memset(GameGrid,0,sizeof(bool)*nGridX*nGridY);
isStart=false;
isStop=true;
}
RsGame::~RsGame()
{
}
//////////////////////////////////////////////////////////////////////
// Family Functions
//////////////////////////////////////////////////////////////////////
//初始化游戏
void RsGame::GameStart()
{
Score=nLine=0;
isStart=true;
isStop=false;
for(int i=0;i<nGridY;i++)
for(int j=0;j<nGridX;j++)
GameGrid[i][j]=false;
}//RsGame::GameStart
//产生新方块
void RsGame::NewCub()
{
CurrentCubNum=CubNum;
for(int i=0;i<4;i++)
CurrentCub[i].x=Cub[CubNum][i].x+4,
CurrentCub[i].y=Cub[CubNum][i].y;
RotScale=-1;//初始化旋转角度为0
CubNum=rand()%7;//产生下个方块类型
}//RsGame::NewCub
//左移
void RsGame::MoveLt()
{
int x=CurrentCub[0].x;
for(int i=1;i<4;i++)
if(x>CurrentCub[i].x)
x=CurrentCub[i].x;
//当前方块不能左移,返回
if(x==0||
GameGrid[CurrentCub[0].x-1][CurrentCub[0].y]||
GameGrid[CurrentCub[1].x-1][CurrentCub[1].y]||
GameGrid[CurrentCub[2].x-1][CurrentCub[2].y]||
GameGrid[CurrentCub[3].x-1][CurrentCub[3].y])
return;
for(i=0;i<4;i++)
CurrentCub[i].x--;
}//RsGame::MoveLt
//右移
void RsGame::MoveRt()
{
int x=CurrentCub[0].x;
for(int i=1;i<4;i++)
if(x<CurrentCub[i].x)
x=CurrentCub[i].x;
//当前方块不能右移,返回
if(x==nGridX-1||
GameGrid[CurrentCub[0].y][CurrentCub[0].x+1]||
GameGrid[CurrentCub[1].y][CurrentCub[1].x+1]||
GameGrid[CurrentCub[2].y][CurrentCub[2].x+1]||
GameGrid[CurrentCub[3].y][CurrentCub[3].x+1])
return;
for(i=0;i<4;i++)
CurrentCub[i].x++;
}//RsGame::MoveRt
//旋转
void RsGame::Rot()
{
//顺时针旋转方块90度
RotScale=(RotScale+1)%4;
for(int i=0;i<4;i++)
CurrentCub[i].x+=Rotate[CurrentCubNum][RotScale][i].x,
CurrentCub[i].y+=Rotate[CurrentCubNum][RotScale][i].y;
//当前位置方块不能旋转,逆时针再旋转90度
if(CurrentCub[0].x<0||CurrentCub[0].x>=nGridX||CurrentCub[0].y<0||CurrentCub[0].y>=nGridY||
CurrentCub[1].x<0||CurrentCub[1].x>=nGridX||CurrentCub[1].y<0||CurrentCub[1].y>=nGridY||
CurrentCub[2].x<0||CurrentCub[2].x>=nGridX||CurrentCub[2].y<0||CurrentCub[2].y>=nGridY||
CurrentCub[3].x<0||CurrentCub[3].x>=nGridX||CurrentCub[3].y<0||CurrentCub[3].y>=nGridY||
GameGrid[CurrentCub[0].y][CurrentCub[0].x]||GameGrid[CurrentCub[1].y][CurrentCub[1].x]||
GameGrid[CurrentCub[2].y][CurrentCub[2].x]||GameGrid[CurrentCub[3].y][CurrentCub[3].x])
{
for(int i=0;i<4;i++)
CurrentCub[i].x-=Rotate[CurrentCubNum][RotScale][i].x,
CurrentCub[i].y-=Rotate[CurrentCubNum][RotScale][i].y;
RotScale=(RotScale+3)%4;
}
}//RsGame::Rot
//快速下降
bool RsGame::QuickDn()
{
//当前方块不能下降,返回false
if(isBottom())
return false;
for(int i=0;i<4;i++)
CurrentCub[i].y++;
return true;
}//RsGame::QuickDn
//判断方块是否落到底
bool RsGame::isBottom()
{
int y=CurrentCub[0].y;
for(int i=1;i<4;i++)
if(y<CurrentCub[i].y)
y=CurrentCub[i].y;
//当前位置已经到底,返回true
if(y==nGridY-1||
GameGrid[CurrentCub[0].y+1][CurrentCub[0].x]||
GameGrid[CurrentCub[1].y+1][CurrentCub[1].x]||
GameGrid[CurrentCub[2].y+1][CurrentCub[2].x]||
GameGrid[CurrentCub[3].y+1][CurrentCub[3].x])
{
for(i=0;i<4;i++)
GameGrid[CurrentCub[i].y][CurrentCub[i].x]=1;
return 1;
}
return 0;
}//RsGame::isBottom
//判断能否消去得分
int RsGame::CanGainScore()
{
int i,j,maxY,minY,count=0;//count能消去的行数
maxY=minY=CurrentCub[0].y;
for(i=1;i<4;i++)
{
if(maxY<CurrentCub[i].y)
maxY=CurrentCub[i].y;
if(minY>CurrentCub[i].y)
minY=CurrentCub[i].y;
}
for(i=0;i<=maxY-minY;i++)
{
//判断能否消去一行
for(j=0;j<nGridX&&GameGrid[maxY][j];j++);
//可以,cont自增
if(j==nGridX)
{
count++;
//刷新游戏
RsGame::refreshGameGrid(maxY);
}
}
//刷新分数,返回消去的行数
if(count)
{
nLine+=count;
Score+=count*PutScore(count);
return maxY;
}
return 0;
}//RsGame::CanGainScore
//消去第y行,并刷新游戏
void RsGame::refreshGameGrid(int y)
{
for(int i=y;i>0;i--)
for(int j=0;j<nGridX;j++)
GameGrid[i][j]=GameGrid[i-1][j];
for(i=0;i<nGridX;i++)
GameGrid[0][i]=false;
}//RsGame::refreshGameGrid
//打分
int RsGame::PutScore(int n)
{
int score;
switch(n)
{
case 1:score=10;break;
case 2:score=20;break;
case 3:score=30;break;
case 4:score=50;break;
}
return score;
}//RsGame::PutScore
//判断游戏是否结束
bool RsGame::isOver()
{
//游戏结束,返回true
for(int i=0;i<nGridX;i++)
if(GameGrid[0][i])
{
isStart=0;
isStop=1;
return 1;
}
return 0;
}//RsGame::isOver
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -