www.gusucode.com > VC++方格益智小游戏-源码程序 > VC++方格益智小游戏-源码程序\code\LightGameView.cpp

    // LightGameView.cpp : implementation of the CLightGameView class
// Download by http://www.NewXing.com

#include "stdafx.h"
#include "LightGame.h"

#include "LightGameDoc.h"
#include "LightGameView.h"
#include <stdlib.h>
#include <time.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CLightGameView

IMPLEMENT_DYNCREATE(CLightGameView, CView)

BEGIN_MESSAGE_MAP(CLightGameView, CView)
	//{{AFX_MSG_MAP(CLightGameView)
	ON_WM_LBUTTONUP()
	ON_WM_TIMER()
	ON_WM_CREATE()
	ON_UPDATE_COMMAND_UI(ID_FILE_NEW, OnUpdateFileNew)
	ON_UPDATE_COMMAND_UI(ID_FILE_OPEN, OnUpdateFileOpen)
	ON_UPDATE_COMMAND_UI(ID_FILE_PRINT, OnUpdateFilePrint)
	ON_UPDATE_COMMAND_UI(ID_FILE_SAVE, OnUpdateFileSave)
	ON_WM_RBUTTONDOWN()
	ON_COMMAND(IDM_TIP, OnTip)
	ON_COMMAND(IDM_CLOSE, OnClose)
	ON_COMMAND(IDM_FRESH, OnFresh)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLightGameView construction/destruction

CLightGameView::CLightGameView()
{
	// TODO: add construction code here	
}

CLightGameView::~CLightGameView()
{
}

BOOL CLightGameView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CLightGameView drawing

void CLightGameView::OnDraw(CDC* pDC)
{
	
	CLightGameDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
		CClientDC dc(this);	
		int i,j;

		if (fresh) {				//
	srand( (unsigned)time(NULL));
	for (i=1;i<6;i++)
		for (j=1;j<7;j++)
		puzzle[i][j]=rand()%2;
		fresh=false;
		m_time=0;
}
	for (i=1;i<6;i++)			//
		for (j=1;j<7;j++) {
			CBrush	paintbrush(RGB(200*puzzle[i][j],100,0));
				dc.FillRect(CRect(CPoint(120*j,120*(i-1)),
				CSize(119,119)),&paintbrush);
		}
	
	
	CBrush brush(RGB(200,0,0));

	dc.FillRect(CRect(CPoint(0,0),CSize(120,600)),&brush);
	dc.FillRect(CRect(CPoint(840,0),CSize(120,600)),&brush);
	
	CFont font,font1;
	font.CreatePointFont(170,"华文行楷",NULL);
	CFont *poldFont=dc.SelectObject(&font);

	dc.TextOut(20,60,"您已用了");
	dc.TextOut(40,100,"     秒");

	dc.TextOut(845,250,"游戏规则:");
	dc.SelectObject(poldFont);
	
	font1.CreatePointFont(150,"楷体",NULL);
	poldFont=dc.SelectObject(&font1);
	dc.TextOut(850,280,"鼠标点击中");
	dc.TextOut(850,300,"间方块,其");
	dc.TextOut(850,320,"四周方块将");
	dc.TextOut(850,340,"会改变颜色");
	dc.TextOut(850,360,"。当方块全");
	dc.TextOut(850,380,"为绿色时,");
	dc.TextOut(845,410,"游戏过关..");
	dc.TextOut(845,430,"..点鼠标右");
	dc.TextOut(845,450,"键或选择菜");
	dc.TextOut(845,470,"单可查看. ");
	dc.TextOut(845,490,"关闭提示, ");
	dc.TextOut(845,510,"选刷新可重");
	dc.TextOut(845,530,"新开始新游");
	dc.TextOut(845,550,"戏......  ");
	dc.SelectObject(poldFont);
}

/////////////////////////////////////////////////////////////////////////////
// CLightGameView printing

BOOL CLightGameView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CLightGameView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CLightGameView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CLightGameView diagnostics

#ifdef _DEBUG
void CLightGameView::AssertValid() const
{
	CView::AssertValid();
}

void CLightGameView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CLightGameDoc* CLightGameView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CLightGameDoc)));
	return (CLightGameDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CLightGameView message handlers

void CLightGameView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CView::OnLButtonUp(nFlags, point);

	Point=point;
	bool flag=true;
		int i,j;
	CString str="";
	CClientDC dc(this);

	int pos_y=(Point.x-120)/120+1,pos_x=Point.y/120+1;
	if (Point.x>120&&pos_y<=6&&pos_x<6) {
	puzzle[pos_x][pos_y]=1-puzzle[pos_x][pos_y];
	puzzle[pos_x-1][pos_y]=1-puzzle[pos_x-1][pos_y];
	puzzle[pos_x+1][pos_y]=1-puzzle[pos_x+1][pos_y];
	puzzle[pos_x][pos_y-1]=1-puzzle[pos_x][pos_y-1];
	puzzle[pos_x][pos_y+1]=1-puzzle[pos_x][pos_y+1];
	}

	for (i=1;i<6;i++)			//
		for (j=1;j<7;j++) {
			CBrush	paintbrush(RGB(200*puzzle[i][j],100,0));
				dc.FillRect(CRect(CPoint(120*j,120*(i-1)),
				CSize(119,119)),&paintbrush);
		}

	for (i=1;i<6;i++)			//
		for (j=1;j<7;j++)
			if (puzzle[i][j]==1)
				flag=false;
			if (flag==true) {
			CBrush brush(RGB(200,0,0));
			CClientDC dc(this);
			dc.FillRect(CRect(CPoint(0,300),CSize(120,240)),&brush);
			MessageBox("恭喜您成功过关!");
			fresh=true;
			}
		
}

void CLightGameView::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	m_time++;
	CString str="";
	str.Format("%d ",m_time);
	CClientDC dc(this);

	CFont font;
	font.CreatePointFont(170,"华文行楷",NULL);
	CFont *poldFont=dc.SelectObject(&font);
	dc.TextOut(35,100,str);

	COleDateTime t;
	t = COleDateTime::GetCurrentTime();
	str.Format("%d年%d月",t.GetYear(),t.GetMonth());
	dc.TextOut(850,60,str);	
	str.Format("%d:%d",t.GetHour(),t.GetMinute());
	dc.TextOut(870,190,str);	
	dc.SelectObject(poldFont);
	
	CFont font1;
	font1.CreatePointFont(550,"华文行楷",NULL);
	poldFont=dc.SelectObject(&font1);
	str.Format("%d",t.GetDay());
	dc.TextOut(860,100,str);
	dc.SelectObject(poldFont);

		int i,j;

		if (fresh) {				//
	srand( (unsigned)time(NULL));
	for (i=1;i<6;i++)
		for (j=1;j<7;j++)
		puzzle[i][j]=rand()%2;
		fresh=false;
		m_time=0;
		CBrush brush(RGB(200,0,0));
		dc.FillRect(CRect(CPoint(0,300),CSize(120,240)),&brush);
	}
	for (i=1;i<6;i++)			//
		for (j=1;j<7;j++) {
			CBrush	paintbrush(RGB(200*puzzle[i][j],100,0));
				dc.FillRect(CRect(CPoint(120*j,120*(i-1)),
				CSize(119,119)),&paintbrush);
		}

	CView::OnTimer(nIDEvent);
}

int CLightGameView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	SetTimer(1,1000,NULL);
	OnFresh();
	return 0;
}

void CLightGameView::OnUpdateFileNew(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(FALSE);
}

void CLightGameView::OnUpdateFileOpen(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(FALSE);
}

void CLightGameView::OnUpdateFilePrint(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(FALSE);
}

void CLightGameView::OnUpdateFileSave(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(FALSE);
}

void CLightGameView::OnRButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CMenu menu;
	menu.LoadMenu(IDR_MENU1);
	CMenu *popmenu=menu.GetSubMenu(0);
	ClientToScreen(&point);
	popmenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,
		point.x,point.y,this);
	CView::OnRButtonDown(nFlags, point);
}

void CLightGameView::OnTip() 
{
	// TODO: Add your command handler code here
	int c,i,j;
	bool flag;

	for ( i = 0; i < 6; i++ )
		press[i][0] = press[i][7] = 0;
	for ( i = 1; i < 7; i++ )
		press[0][i] = 0;

	for ( i = 1; i < 7; i++)
		press[1][i] = 0;

 while(true) {
	 for ( i = 1; i < 5; i++ )
		for ( j = 1; j < 7; j++ )
			press[i+1][j] =(puzzle[i][j] + press[i][j] + press[i-1][j] + 
				press[i][j-1] + press[i][j+1]) % 2;
		flag=true;
		for(i=1; i<7; i++) {
			if ( (press[5][i-1] + press[5][i] + press[5][i+1] + 
			press[4][i]) % 2 != puzzle[5][i] ) {
			flag=false;
			break;
		}
}

	if (flag) break;

	press[1][1]++;
	c = 1;
	while ( press[1][c] > 1 ) {
		press[1][c] = 0;
		c++;
		press[1][c]++;
	}
 }
	CString str="";
	CClientDC dc(this);

/*	CFont font;
	font.CreatePointFont(170,"华文行楷",NULL);
	CFont *poldFont=dc.SelectObject(&font);
	dc.TextOut(10,300,"Tips:");
 for ( i = 1; i < 6; i++ )	
	 for ( j = 1; j < 7; j++ ) {
		str.Format("%d ",press[i][j]);
		dc.TextOut(10+j*12,320+i*20,str);
	}
	 dc.TextOut(9,470,"对应位置");
	 dc.TextOut(9,490,"1:点击       ");
	 dc.TextOut(9,510,"0:不点      ");
	 dc.SelectObject(poldFont);*/
}

void CLightGameView::OnClose() 
{
	// TODO: Add your command handler code here
	CBrush brush(RGB(200,0,0));
	CClientDC dc(this);
	dc.FillRect(CRect(CPoint(0,300),CSize(120,240)),&brush);
	
}

void CLightGameView::OnFresh() 
{
	// TODO: Add your command handler code here
	fresh=true;
	
}