www.pudn.com > elsfk.rar > elsfkDlg.cpp
// elsfkDlg.cpp : implementation file
//
#include "stdafx.h"
#include "elsfk.h"
#include "elsfkDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CElsfkDlg dialog
CElsfkDlg::CElsfkDlg(CWnd* pParent /*=NULL*/)
: CDialog(CElsfkDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CElsfkDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
IfNewRect=1;
}
void CElsfkDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CElsfkDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CElsfkDlg, CDialog)
//{{AFX_MSG_MAP(CElsfkDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_KEYUP()
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CElsfkDlg message handlers
BOOL CElsfkDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
MoveType.RemoveAll();
MoveType.Add(CPoint(1,1));
MoveType.Add(CPoint(2,1));
MoveType.Add(CPoint(2,2));
MoveType.Add(CPoint(3,1));
MoveType.Add(CPoint(3,2));
MoveType.Add(CPoint(4,1));
MoveType.Add(CPoint(4,2));
MoveType.Add(CPoint(4,3));
MoveType.Add(CPoint(4,4));
MoveType.Add(CPoint(5,1));
MoveType.Add(CPoint(5,2));
MoveType.Add(CPoint(5,3));
MoveType.Add(CPoint(5,4));
MoveType.Add(CPoint(6,1));
MoveType.Add(CPoint(7,1));
MoveType.Add(CPoint(7,2));
MoveType.Add(CPoint(7,3));
MoveType.Add(CPoint(7,4));
//初始化填充区域
for(int i=0;i<15;i++)
for(int j=0;j<12;j++)
FillArray[i][j]=0;
GetClientRect(&CLientRect);
IfNewRect=TRUE;
GetNewRect();
SetTimer(0,1000,NULL);
return TRUE; // return TRUE unless you set the focus to a control
}
void CElsfkDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CElsfkDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CPaintDC dc(this);
DrawRect(dc,MoveRect);
//填充区域
CRect FillRect;
for(int i=0;i<15;i++)
for(int j=0;j<12;j++)
{
if(FillArray[i][j]==1)
{
FillRect.left=j*20;
FillRect.top=i*20;
FillRect.right=FillRect.left+20;
FillRect.bottom=FillRect.top+20;
dc.Rectangle(&FillRect);
}
}
// CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CElsfkDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CElsfkDlg::RemoveOneLine()
{
//从下往上
BOOL IsAll1;
for(int i=14;i>=0;i--)
{
IsAll1=TRUE;
for(int j=0;j<12;j++)
{
if(FillArray[i][j] !=1)
{
IsAll1=FALSE;
break;
}
}
if(IsAll1)//消除这一行
{
for(int p=i-1;p>=0;p--)
for(int q=0;q<12;q++)
FillArray[p+1][q]=FillArray[p][q];
for(int q=0;q<12;q++)
FillArray[0][q]=0;
Invalidate();
i++;
}
}
}
void CElsfkDlg::DrawRect(CPaintDC &dc, CRect rect)
{
BottomMoveRect.RemoveAll();
LeftMoveRect.RemoveAll();
RightMoveRect.RemoveAll();
if(CurrentMoveType>=0)
{
CPoint CurrentPoint;
CurrentPoint = MoveType.GetAt(CurrentMoveType);
switch(CurrentPoint.x) {
case 1:
dc.Rectangle(&rect);
BottomMoveRect.Add(rect);
LeftMoveRect.Add(rect);
RightMoveRect.Add(rect);
break;
case 2:
switch(CurrentPoint.y) {
case 1:
dc.Rectangle(&rect);
BottomMoveRect.Add(rect);
LeftMoveRect.Add(rect);
rect.left+=20;
rect.right+=20;
dc.Rectangle(&rect);
BottomMoveRect.Add(rect);
rect.left+=20;
rect.right+=20;
dc.Rectangle(&rect);
BottomMoveRect.Add(rect);
RightMoveRect.Add(rect);
break;
case 2:
dc.Rectangle(&rect);
BottomMoveRect.Add(rect);
LeftMoveRect.Add(rect);
RightMoveRect.Add(rect);
rect.top-=20;
rect.bottom-=20;
dc.Rectangle(&rect);
LeftMoveRect.Add(rect);
RightMoveRect.Add(rect);
rect.top-=20;
rect.bottom-=20;
dc.Rectangle(&rect);
LeftMoveRect.Add(rect);
RightMoveRect.Add(rect);
break;
}
break;
case 3:
switch(CurrentPoint.y) {
case 1:
dc.Rectangle(&rect);
BottomMoveRect.Add(rect);
LeftMoveRect.Add(rect);
rect.left+=20;
rect.right+=20;
dc.Rectangle(&rect);
BottomMoveRect.Add(rect);
RightMoveRect.Add(rect);
break;
case 2:
dc.Rectangle(&rect);
BottomMoveRect.Add(rect);
RightMoveRect.Add(rect);
LeftMoveRect.Add(rect);
rect.top-=20;
rect.bottom-=20;
dc.Rectangle(&rect);
RightMoveRect.Add(rect);
LeftMoveRect.Add(rect);
break;
}
break;
case 4:
switch(CurrentPoint.y) {
case 1:
dc.Rectangle(&rect);
BottomMoveRect.Add(rect);
LeftMoveRect.Add(rect);
RightMoveRect.Add(rect);
rect.top-=20;
rect.bottom-=20;
dc.Rectangle(&rect);
LeftMoveRect.Add(rect);
rect.left+=20;
rect.right+=20;
dc.Rectangle(&rect);
BottomMoveRect.Add(rect);
rect.left+=20;
rect.right+=20;
dc.Rectangle(&rect);
BottomMoveRect.Add(rect);
RightMoveRect.Add(rect);
break;
case 2:
dc.Rectangle(&rect);
BottomMoveRect.Add(rect);
LeftMoveRect.Add(rect);
rect.left+=20;
rect.right+=20;
dc.Rectangle(&rect);
BottomMoveRect.Add(rect);
RightMoveRect.Add(rect);
rect.left-=20;
rect.right-=20;
rect.top-=20;
rect.bottom-=20;
dc.Rectangle(&rect);
LeftMoveRect.Add(rect);
RightMoveRect.Add(rect);
rect.top-=20;
rect.bottom-=20;
dc.Rectangle(&rect);
LeftMoveRect.Add(rect);
RightMoveRect.Add(rect);
break;
case 3:
dc.Rectangle(&rect);
BottomMoveRect.Add(rect);
LeftMoveRect.Add(rect);
rect.left+=20;
rect.right+=20;
dc.Rectangle(&rect);
BottomMoveRect.Add(rect);
rect.left+=20;
rect.right+=20;
dc.Rectangle(&rect);
BottomMoveRect.Add(rect);
RightMoveRect.Add(rect);
rect.top-=20;
rect.bottom-=20;
dc.Rectangle(&rect);
LeftMoveRect.Add(rect);
RightMoveRect.Add(rect);
break;
case 4:
dc.Rectangle(&rect);
BottomMoveRect.Add(rect);
LeftMoveRect.Add(rect);
RightMoveRect.Add(rect);
rect.top-=20;
rect.bottom-=20;
dc.Rectangle(&rect);
LeftMoveRect.Add(rect);
RightMoveRect.Add(rect);
rect.top-=20;
rect.bottom-=20;
dc.Rectangle(&rect);
RightMoveRect.Add(rect);
rect.left-=20;
rect.right-=20;
dc.Rectangle(&rect);
BottomMoveRect.Add(rect);
LeftMoveRect.Add(rect);
break;
}
break;
case 5:
switch(CurrentPoint.y) {
case 1:
dc.Rectangle(&rect);
BottomMoveRect.Add(rect);
LeftMoveRect.Add(rect);
RightMoveRect.Add(rect);
rect.top-=20;
rect.bottom-=20;
dc.Rectangle(&rect);
RightMoveRect.Add(rect);
rect.left-=20;
rect.right-=20;
dc.Rectangle(&rect);
BottomMoveRect.Add(rect);
rect.left-=20;
rect.right-=20;
dc.Rectangle(&rect);
BottomMoveRect.Add(rect);
LeftMoveRect.Add(rect);
break;
case 2:
dc.Rectangle(&rect);
BottomMoveRect.Add(rect);
LeftMoveRect.Add(rect);
RightMoveRect.Add(rect);
rect.top-=20;
rect.bottom-=20;
dc.Rectangle(&rect);
LeftMoveRect.Add(rect);
RightMoveRect.Add(rect);
rect.top-=20;
rect.bottom-=20;
dc.Rectangle(&rect);
LeftMoveRect.Add(rect);
rect.left+=20;
rect.right+=20;
dc.Rectangle(&rect);
BottomMoveRect.Add(rect);
RightMoveRect.Add(rect);
break;
case 3:
dc.Rectangle(&rect);
BottomMoveRect.Add(rect);
LeftMoveRect.Add(rect);
rect.left+=20;
rect.right+=20;
dc.Rectangle(&rect);
BottomMoveRect.Add(rect);
rect.left+=20;
rect.right+=20;
dc.Rectangle(&rect);
BottomMoveRect.Add(rect);
RightMoveRect.Add(rect);
rect.top-=20;
rect.bottom-=20;
rect.left-=40;
rect.right-=40;
dc.Rectangle(&rect);
LeftMoveRect.Add(rect);
RightMoveRect.Add(rect);
break;
case 4:
dc.Rectangle(&rect);
BottomMoveRect.Add(rect);
LeftMoveRect.Add(rect);
rect.left+=20;
rect.right+=20;
dc.Rectangle(&rect);
BottomMoveRect.Add(rect);
RightMoveRect.Add(rect);
rect.top-=20;
rect.bottom-=20;
dc.Rectangle(&rect);
LeftMoveRect.Add(rect);
RightMoveRect.Add(rect);
rect.top-=20;
rect.bottom-=20;
dc.Rectangle(&rect);
LeftMoveRect.Add(rect);
RightMoveRect.Add(rect);
break;
}
break;
case 6:
dc.Rectangle(&rect);
BottomMoveRect.Add(rect);
LeftMoveRect.Add(rect);
RightMoveRect.Add(rect);
rect.top-=20;
rect.bottom-=20;
dc.Rectangle(&rect);
rect.top-=20;
rect.bottom-=20;
dc.Rectangle(&rect);
LeftMoveRect.Add(rect);
RightMoveRect.Add(rect);
rect.left-=20;
rect.right-=20;
rect.top+=20;
rect.bottom+=20;
dc.Rectangle(&rect);
BottomMoveRect.Add(rect);
LeftMoveRect.Add(rect);
rect.left+=40;
rect.right+=40;
dc.Rectangle(&rect);
BottomMoveRect.Add(rect);
RightMoveRect.Add(rect);
break;
case 7:
switch(CurrentPoint.y) {
case 1:
dc.Rectangle(&rect);
BottomMoveRect.Add(rect);
LeftMoveRect.Add(rect);
RightMoveRect.Add(rect);
rect.top-=20;
rect.bottom-=20;
dc.Rectangle(&rect);
LeftMoveRect.Add(rect);
rect.left+=20;
rect.right+=20;
dc.Rectangle(&rect);
BottomMoveRect.Add(rect);
RightMoveRect.Add(rect);
break;
case 2:
dc.Rectangle(&rect);
BottomMoveRect.Add(rect);
LeftMoveRect.Add(rect);
rect.top-=20;
rect.bottom-=20;
dc.Rectangle(&rect);
LeftMoveRect.Add(rect);
RightMoveRect.Add(rect);
rect.left+=20;
rect.right+=20;
rect.top+=20;
rect.bottom+=20;
dc.Rectangle(&rect);
BottomMoveRect.Add(rect);
RightMoveRect.Add(rect);
break;
case 3:
dc.Rectangle(&rect);
BottomMoveRect.Add(rect);
LeftMoveRect.Add(rect);
rect.left+=20;
rect.right+=20;
dc.Rectangle(&rect);
BottomMoveRect.Add(rect);
RightMoveRect.Add(rect);
rect.top-=20;
rect.bottom-=20;
dc.Rectangle(&rect);
BottomMoveRect.Add(rect);
LeftMoveRect.Add(rect);
RightMoveRect.Add(rect);
break;
case 4:
dc.Rectangle(&rect);
BottomMoveRect.Add(rect);
LeftMoveRect.Add(rect);
RightMoveRect.Add(rect);
rect.top-=20;
rect.bottom-=20;
dc.Rectangle(&rect);
RightMoveRect.Add(rect);
rect.left-=20;
rect.right-=20;
dc.Rectangle(&rect);
BottomMoveRect.Add(rect);
LeftMoveRect.Add(rect);
break;
}
break;
}
}
}
void CElsfkDlg::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
//37左 38上 39右 40下
switch(nChar) {
case 37:
MoveLeft();
break;
case 39:
MoveRight();
break;
case 40:
MoveDown();
break;
case 38:
Turn();
break;
}
CDialog::OnKeyUp(nChar, nRepCnt, nFlags);
}
void CElsfkDlg::MoveLeft()
{
//是否碰到障碍
BOOL IsStop=FALSE;int LineNum,RowNum;
for(int i=0;ileft<=CLientRect.left)
return;
if( ((x==4)&&(y==4)) || ((x==6)&&(y==1)) || ((x==7)&&(y==4)) )
{
if(MoveRect->left-20<=CLientRect.left)
return;
}
if((x==5 && y==1))
{
if(MoveRect->left-40 <=CLientRect.left)
return;
}
MoveRect->left-=20;
MoveRect->right-=20;
Invalidate();
}
void CElsfkDlg::MoveRight()
{
//是否碰到障碍
BOOL IsStop=FALSE;int LineNum,RowNum;
for(int i=0;iright>=CLientRect.right)
{
return;
}
CPoint CurrentPoint = MoveType.GetAt(CurrentMoveType);
int x,y;
x=CurrentPoint.x;
y=CurrentPoint.y;
if((x==4 &&y==2) ||(x==5&& y==2) || (x==5 && y==4) ||(x==7&&y==1) ||(x==7 &&y==2) ||(x==7 &&y==3) || (x==3 &&y==1))
{
if(MoveRect->right+20 >= CLientRect.right)
return;
}
if((x==4 && y==1) || (x==4 && y==3) || (x==5 && y==3) || (x==2 && y==1))
{
if(MoveRect->right+40 >= CLientRect.right)
return;
}
MoveRect->left+=20;
MoveRect->right+=20;
Invalidate();
}
void CElsfkDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
RemoveOneLine();
MoveDown();
CDialog::OnTimer(nIDEvent);
}
void CElsfkDlg::MoveDown()
{
//是否碰到障碍
BOOL IsStop=FALSE;int LineNum,RowNum;
for(int i=0;ibottom>=CLientRect.bottom)//是否到客户区域的底部
{
FillArrayFun(CurrentMoveType,MoveRect);
Invalidate();
RemoveOneLine();
if(MoveRect!=NULL)
delete MoveRect;
MoveRect=NULL;
IfNewRect=TRUE;
if(IfNewRect)
{
GetNewRect();
return;
}
return;
}
else{
MoveRect->bottom+=20;
MoveRect->top+=20;
Invalidate();
}
}
void CElsfkDlg::GetNewRect()
{
if(IfNewRect==TRUE)
{
MoveRect=new CRect(CLientRect.right/2-20,0,CLientRect.right/2-20+20,20);
srand((unsigned int) time(NULL));
CurrentMoveType = rand()%MoveType.GetSize();
IfNewRect=FALSE;
}
}
void CElsfkDlg::FillArrayFun(int Type,CRect MoveRect)
{
CPoint CurrentPoint;
int LineNum,RowNum;
LineNum = MoveRect.top/20;
RowNum = MoveRect.left/20;
CurrentPoint = MoveType.GetAt(CurrentMoveType);
switch(CurrentPoint.x) {
case 1:
FillArray[LineNum][RowNum]=1;
break;
case 2:
switch(CurrentPoint.y) {
case 1:
FillArray[LineNum][RowNum]=1;
FillArray[LineNum][RowNum+1]=1;
FillArray[LineNum][RowNum+2]=1;
break;
case 2:
FillArray[LineNum][RowNum]=1;
FillArray[LineNum-1][RowNum]=1;
FillArray[LineNum-2][RowNum]=1;
break;
}
break;
case 3:
switch(CurrentPoint.y) {
case 1:
FillArray[LineNum][RowNum]=1;
FillArray[LineNum][RowNum+1]=1;
break;
case 2:
FillArray[LineNum][RowNum]=1;
FillArray[LineNum-1][RowNum]=1;
break;
}
break;
case 4:
switch(CurrentPoint.y) {
case 1:
FillArray[LineNum][RowNum]=1;
FillArray[LineNum-1][RowNum]=1;
FillArray[LineNum-1][RowNum+1]=1;
FillArray[LineNum-1][RowNum+2]=1;
break;
case 2:
FillArray[LineNum][RowNum]=1;
FillArray[LineNum-1][RowNum]=1;
FillArray[LineNum-2][RowNum]=1;
FillArray[LineNum][RowNum+1]=1;
break;
case 3:
FillArray[LineNum][RowNum]=1;
FillArray[LineNum][RowNum+1]=1;
FillArray[LineNum][RowNum+2]=1;
FillArray[LineNum-1][RowNum+2]=1;
break;
case 4:
FillArray[LineNum][RowNum]=1;
FillArray[LineNum-1][RowNum]=1;
FillArray[LineNum-2][RowNum]=1;
FillArray[LineNum-2][RowNum-1]=1;
break;
}
break;
case 5:
switch(CurrentPoint.y) {
case 1:
FillArray[LineNum][RowNum]=1;
FillArray[LineNum-1][RowNum]=1;
FillArray[LineNum-1][RowNum-1]=1;
FillArray[LineNum-1][RowNum-2]=1;
break;
case 2:
FillArray[LineNum][RowNum]=1;
FillArray[LineNum-1][RowNum]=1;
FillArray[LineNum-2][RowNum]=1;
FillArray[LineNum-2][RowNum+1]=1;
break;
case 3:
FillArray[LineNum][RowNum]=1;
FillArray[LineNum-1][RowNum]=1;
FillArray[LineNum][RowNum+1]=1;
FillArray[LineNum][RowNum+2]=1;
break;
case 4:
FillArray[LineNum][RowNum]=1;
FillArray[LineNum][RowNum+1]=1;
FillArray[LineNum-1][RowNum+1]=1;
FillArray[LineNum-2][RowNum+1]=1;
break;
}
break;
case 6:
FillArray[LineNum][RowNum]=1;
FillArray[LineNum-1][RowNum]=1;
FillArray[LineNum-2][RowNum]=1;
FillArray[LineNum-1][RowNum-1]=1;
FillArray[LineNum-1][RowNum+1]=1;
break;
case 7:
switch(CurrentPoint.y) {
case 1:
FillArray[LineNum][RowNum]=1;
FillArray[LineNum-1][RowNum]=1;
FillArray[LineNum-1][RowNum+1]=1;
break;
case 2:
FillArray[LineNum][RowNum]=1;
FillArray[LineNum][RowNum+1]=1;
FillArray[LineNum-1][RowNum]=1;
break;
case 3:
FillArray[LineNum][RowNum]=1;
FillArray[LineNum][RowNum+1]=1;
FillArray[LineNum-1][RowNum+1]=1;
break;
case 4:
FillArray[LineNum][RowNum]=1;
FillArray[LineNum-1][RowNum]=1;
FillArray[LineNum-1][RowNum-1]=1;
break;
}
break;
}
}
void CElsfkDlg::Turn()
{
CPoint CurrentPoint;
CurrentPoint = MoveType.GetAt(CurrentMoveType);
int x=CurrentPoint.x;
int y=CurrentPoint.y;
switch(x) {
case 1:
case 6:
break;
case 2:
case 3:
if(y==1)
CurrentMoveType++;
else
CurrentMoveType--;
break;
case 4:
case 5:
case 7:
if(y==4)
CurrentMoveType-=3;
else
CurrentMoveType++;
break;
}
Invalidate();
}