// TcsGameDlg.cpp : implementation file
//
//BY 吴志刚 ewuzg@163.com,cn.fox@tom.com 2003//
#include "stdafx.h"
#include "TcsGame.h"
#include "TcsGameDlg.h"
#include "DlgVictory.h"
//Load Sound Lib//
#include <mmsystem.h>
#pragma comment(lib,"WINMM.LIB")
//==============================//
#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 HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnPaint();
//}}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)
ON_WM_CTLCOLOR()
ON_WM_LBUTTONDOWN()
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTcsGameDlg dialog
CTcsGameDlg::CTcsGameDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTcsGameDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTcsGameDlg)
// 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(IDI_ICOSNAKE);
}
void CTcsGameDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTcsGameDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTcsGameDlg, CDialog)
//{{AFX_MSG_MAP(CTcsGameDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_KEYDOWN()
ON_WM_TIMER()
ON_COMMAND(IDM_START, OnStart)
ON_COMMAND(IDM_EXIT, OnExit)
ON_COMMAND(IDM_ABOUT, OnAbout)
ON_COMMAND(IDM_PAUSE, OnPause)
ON_WM_CTLCOLOR()
ON_COMMAND(IDM_SOUND, OnSound)
ON_COMMAND(IDM_THROUGH, OnThrough)
ON_COMMAND(IDM_STOP, OnStop)
ON_WM_MOUSEMOVE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTcsGameDlg message handlers
BOOL CTcsGameDlg::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
srand(GetTickCount());
////////////////////////init status bar//////////////////////////////
m_wndStatusBar.Create(WS_CHILD|WS_VISIBLE|CCS_BOTTOM,CRect(0,0,0,0),this,102);
int iPartDim[3]={80,370,-1};
m_wndStatusBar.SetParts(2,iPartDim);
m_wndStatusBar.SetText(_T("停止游戏"),0,0);
m_wndStatusBar.SetText(_T("方向键控制方向,P暂停,F2新游戏"),1,0);
///////////////////////////////////////////////////
pNumBmp[0].LoadBitmap(IDB_BMPN0);
pNumBmp[1].LoadBitmap(IDB_BMPN1);
pNumBmp[2].LoadBitmap(IDB_BMPN2);
pNumBmp[3].LoadBitmap(IDB_BMPN3);
pNumBmp[4].LoadBitmap(IDB_BMPN4);
pNumBmp[5].LoadBitmap(IDB_BMPN5);
pNumBmp[6].LoadBitmap(IDB_BMPN6);
pNumBmp[7].LoadBitmap(IDB_BMPN7);
pNumBmp[8].LoadBitmap(IDB_BMPN8);
pNumBmp[9].LoadBitmap(IDB_BMPN9);
pNumBmp[10].LoadBitmap(IDB_BMPNBLACK);
///////////////////////////////////////////////////
ClearArray();
m_nSnakeLen=0;
srand(GetTickCount());
m_nLenX=15;
m_nLenY=20;
m_nBlockLen=16;
m_nLevel=0;
m_nSnakeLife=5;
m_nTotalScore=0;
m_nTimeInterval=180;
m_bEnableSound=FALSE;
m_bCanThrough=FALSE;
m_bSnakeDied=TRUE;
m_nMaxLevel=10;
m_nWaitType=WAIT_NONE;
////////////////////////
m_nInitX=5;
m_nInitY=45;
m_rcGame.SetRect(m_nInitX,m_nInitY,m_nInitX+m_nLenX*m_nBlockLen,m_nInitY+m_nLenY*m_nBlockLen);
m_ptDisplay.x=m_rcGame.right+25;
m_ptDisplay.y=m_rcGame.top;
int nWx,nWy;
nWx=m_rcGame.left+((m_rcGame.right-m_rcGame.left)-180)/2;
nWy=m_rcGame.top+((m_rcGame.bottom-m_rcGame.top)-80)/2;
m_rcWait.SetRect(nWx,nWy,nWx+180,nWy+80);
///////////////////INIT TOOLBAR 256COLORS//////////////////////////////////
if (!m_wndToolBar.CreateEx(this, TBSTYLE_BUTTON, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_wndToolBar.LoadToolBar(IDR_TOOLBAR1))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
//设置ToolBar的图标列表
m_ilTB.Create(32, 32, TRUE | ILC_COLOR8, 5, 0);
HICON hIcon = NULL;
hIcon = (HICON)::LoadImage(::AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDI_ICONNEW), IMAGE_ICON, 32, 32, 0);
m_ilTB.Add(hIcon);
hIcon = (HICON)::LoadImage(::AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDI_ICOPAUSE), IMAGE_ICON, 32, 32, 0);
m_ilTB.Add(hIcon);
hIcon = (HICON)::LoadImage(::AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDI_ICONSTOP), IMAGE_ICON, 32, 32, 0);
m_ilTB.Add(hIcon);
hIcon = (HICON)::LoadImage(::AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDI_ICOHELP), IMAGE_ICON, 32, 32, 0);
m_ilTB.Add(hIcon);
hIcon = (HICON)::LoadImage(::AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDI_ICONQUIT), IMAGE_ICON, 32, 32, 0);
m_ilTB.Add(hIcon);
m_wndToolBar.GetToolBarCtrl().SetImageList(&m_ilTB);
RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0);
///////////////////////////////////////////////////////////////////////////
return TRUE; // return TRUE unless you set the focus to a control
}
void CTcsGameDlg::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 CTcsGameDlg::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
{
DrawGame();
DrawARNumber(m_nLevel, m_ptDisplay.x+6*13,m_ptDisplay.y+25);
DrawARNumber(m_nSnakeLen,m_ptDisplay.x+6*13,m_ptDisplay.y+23+50);
DrawARNumber(m_nTotalScore,m_ptDisplay.x+6*13,m_ptDisplay.y+46+75);
DrawLifeCount(m_nSnakeLife,m_ptDisplay.x,m_ptDisplay.y+69+60+30);
DrawInfoText();
DrawWait(m_nWaitType,m_nWaitCount);
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CTcsGameDlg::OnQuery
评论0