⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 打字游戏view.cpp

📁 这是一个打字游戏哦
💻 CPP
字号:
// 打字游戏View.cpp : implementation of the CMyView class
//

#include "stdafx.h"
#include "打字游戏.h"

#include "打字游戏Doc.h"
#include "打字游戏View.h"
#include "mmsystem.h"
#include "HeroDlg.h"
#include "MainFrm.h"
#include "InputDlg.h"

#include "MyMenu1.h"
#pragma comment(lib,"winmm.lib")

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//--------------------------------------------------------------------
#define ID_TIMER	1
UINT nInterval[9]={110,110,110,55,55,55,50,50,50};
int  nStep[9]={6,8,10,6,7,8,6,7,8};
//------------------------------------------------
extern CMyApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CMyView

IMPLEMENT_DYNCREATE(CMyView, CView)

BEGIN_MESSAGE_MAP(CMyView, CView)
	ON_WM_CONTEXTMENU()
	//{{AFX_MSG_MAP(CMyView)
	ON_COMMAND(ID_START, OnStart)
	ON_UPDATE_COMMAND_UI(ID_START, OnUpdateStart)
	ON_COMMAND(ID_STOP, OnStop)
	ON_UPDATE_COMMAND_UI(ID_STOP, OnUpdateStop)
	ON_COMMAND(ID_SOUND_OFF, OnSoundOff)
	ON_UPDATE_COMMAND_UI(ID_SOUND_OFF, OnUpdateSoundOff)
	ON_COMMAND(ID_SOUND_ON, OnSoundOn)
	ON_UPDATE_COMMAND_UI(ID_SOUND_ON, OnUpdateSoundOn)
	ON_WM_TIMER()
	ON_WM_ERASEBKGND()
	ON_WM_CHAR()
	ON_COMMAND(ID_HERO, OnHero)
	ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
	ON_COMMAND(ID_FULL_SCREEN, OnFullScreen)
	ON_COMMAND(ID_FILE_NEW, OnFileNew)
	ON_COMMAND(ID_APP_EXIT, OnAppExit)
	//}}AFX_MSG_MAP
	ON_UPDATE_COMMAND_UI(IDS_HITCOUNT, OnHitCount)
	ON_UPDATE_COMMAND_UI(IDS_MISSCOUNT, OnMissCount)
	ON_UPDATE_COMMAND_UI(IDS_ERRORCOUNT, OnErrorCount)
	ON_UPDATE_COMMAND_UI(IDS_LEVEL, OnUpdateLevel)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyView construction/destruction

CMyView::CMyView()
{
	// TODO: add construction code here

}

CMyView::~CMyView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMyView drawing

void CMyView::OnDraw(CDC* pDC)
{
	CMyDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CMyView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CMyView message handlers

void CMyView::OnStart() 
{
	m_bRunning=TRUE;
	SetTimer(ID_TIMER,nInterval[m_nLevel],NULL);
}

void CMyView::OnUpdateStart(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(!m_bRunning);
}

void CMyView::OnStop() 
{

	m_bRunning=FALSE;
	KillTimer(ID_TIMER);
}

void CMyView::OnUpdateStop(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(m_bRunning);
}

void CMyView::OnInitialUpdate() 
{
	CView::OnInitialUpdate();
	int i;
	if(m_bRunning)
	{
		KillTimer(ID_TIMER);
		m_bRunning=FALSE;
	
	}
	for(i=0;i<10;i++)
	{
		if(::IsWindow(m_wndBmp[i].m_hWnd))
			m_wndBmp[i].DestroyWindow();
	}
	m_bRunning=FALSE;
	m_bSoundOn=TRUE;
	m_nLevel=0;
	m_nHitCount=0;
	m_nMissCount=0;
	m_nErrorCount=0;
	CString str;
	str=AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW,
		AfxGetApp()->LoadCursor(IDC_WAIT),(HBRUSH)(COLOR_WINDOW+1),NULL);
	srand(::GetTickCount());
	CRect rect;
	GetClientRect(&rect);
	int nAverageWidth=(rect.Width()-20)/10;
	for(i=0;i<10;i++)
	{
		if(!::IsWindow(m_wndBmp[i].m_hWnd))
		{
			int nPos=-rand()%rect.Height()+rect.Height()/3;
			m_wndBmp[i].Create(str,NULL,WS_CHILD|WS_VISIBLE,CRect(20+i*nAverageWidth,nPos,i*nAverageWidth+nAverageWidth,nPos+60),this,IDB_AB+rand()%52,NULL);
		}
	}
}
void CMyView::OnSoundOff() 
{
	m_bSoundOn=FALSE;
	
}
void CMyView::OnUpdateSoundOff(CCmdUI* pCmdUI) 
{
	pCmdUI->SetRadio(!m_bSoundOn);
	
}

void CMyView::OnSoundOn() 
{
	m_bSoundOn=TRUE;
	
}
void CMyView::OnUpdateSoundOn(CCmdUI* pCmdUI) 
{
	pCmdUI->SetRadio(m_bSoundOn);
	
}

void CMyView::OnTimer(UINT nIDEvent) 
{
	int i;
	CRect rect;
	GetClientRect(&rect);
	CRect rect1;
	int nAverageWidth=(rect.Width()-20)/10;
	for(i=0;i<10;i++)
	{
		if(::IsWindow(m_wndBmp[i].m_hWnd))
		{
			m_wndBmp[i].GetWindowRect(&rect1);
			ScreenToClient(&rect1);
			UINT nID=::GetWindowLong(m_wndBmp[i].m_hWnd,GWL_ID);
			if(nID==IDB_BAOZHA)
				m_wndBmp[i].ShowWindow(SW_HIDE);
			if(rect1.top+10>=rect.bottom)
			{
				
				UINT nID=::GetWindowLong(m_wndBmp[i].m_hWnd,GWL_ID);
				if(nID!=IDB_BAOZHA)
					m_nMissCount++;
				m_wndBmp[i].DestroyWindow();
			}
			else
				m_wndBmp[i].MoveWindow(20+i*nAverageWidth,(rect1.top+nStep[m_nLevel]),rect1.Width(),rect1.Height());
		}
		else
		{
			CString str;
			str=AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW,
				AfxGetApp()->LoadCursor(IDC_WAIT),(HBRUSH)(COLOR_WINDOW+1),NULL);
			int nPos=-rand()%rect.Height();
			m_wndBmp[i].Create(str,NULL,WS_CHILD|WS_VISIBLE,CRect(20+i*70,nPos,i*70+69,nPos+60),this,IDB_AB+rand()%52,NULL);
		}

	}
	if(m_nMissCount>=20)
	{
		OnStop();
		if(!SaveToFile())
		{
			MessageBox("挂了吧,傻瓜!\n重新开始吧!");
		}
		OnInitialUpdate();
	}
	CView::OnTimer(nIDEvent);
}

BOOL CMyView::OnEraseBkgnd(CDC* pDC) 
{
	CBrush brush(RGB(0,0,0x66));
	CRect rect;
	GetClientRect(&rect);
	pDC->FillRect(&rect,&brush);
	return TRUE;
}
void CMyView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	int i;
	CRect rect1;
	for(i=0;i<10;i++)
	{
		if(::IsWindow(m_wndBmp[i].m_hWnd))
		{
			UINT nID=::GetWindowLong(m_wndBmp[i].m_hWnd,GWL_ID);
			m_wndBmp[i].GetWindowRect(&rect1);
			ScreenToClient(&rect1);
			if(nChar>='A'&&nChar<='Z')
			{
				if((nID-IDB_AB+'A')==nChar)
				{
					if(rect1.top>-30)
					{
						::SetWindowLong(m_wndBmp[i].m_hWnd,GWL_ID,(LONG)IDB_BAOZHA);
						m_wndBmp[i].Invalidate();
						if(m_bSoundOn)
							::PlaySound((LPCTSTR)IDR_DEL,NULL,SND_RESOURCE|SND_ASYNC);
						m_nHitCount++;
						if(m_nHitCount%20==0)
						{
							KillTimer(ID_TIMER);
							m_nLevel=min(m_nLevel+1,8);
							SetTimer(ID_TIMER,nInterval[m_nLevel],NULL);
						}
						return ;
					}
				}
			}
			if(nChar>='a'&&nChar<='z')
			{
				if((nID-IDB_AS+'a')==nChar)
				{
					if(rect1.top>-5)
					{
						::SetWindowLong(m_wndBmp[i].m_hWnd,GWL_ID,(LONG)IDB_BAOZHA);
						m_wndBmp[i].Invalidate();
						if(m_bSoundOn)
							::PlaySound((LPCTSTR)IDR_DEL,NULL,SND_RESOURCE|SND_ASYNC);
						m_nHitCount++;
						if(m_nHitCount%20==0)
						{
							KillTimer(ID_TIMER);
							m_nLevel=min(m_nLevel+1,8);
							SetTimer(ID_TIMER,nInterval[m_nLevel],NULL);
						}
						return ;
					}
				}
			}
		}
	}
	if(m_bRunning)
	{
		m_nErrorCount++;
		if(m_bSoundOn)
			::PlaySound((LPCTSTR)IDR_TYPE,NULL,SND_RESOURCE|SND_ASYNC);
	}
	CView::OnChar(nChar, nRepCnt, nFlags);
}

void CMyView::OnHero() 
{
	if(_access("ScoreData.bsn",0)== -1)
	{
		MessageBox("还没有记录,你是第一个玩家!");
		return ;
	}
	CHeroDlg dlg;
	dlg.DoModal();
}
void CMyView::OnHitCount(CCmdUI* pCmdUI) 
{
	CString str;
	str.Format(" 打中:%d",m_nHitCount);
	pCmdUI->SetText(str);
}
void CMyView::OnMissCount(CCmdUI* pCmdUI) 
{
	CString str;
	str.Format(" 遗漏:%d",m_nMissCount);
	pCmdUI->SetText(str);
}
void CMyView::OnErrorCount(CCmdUI* pCmdUI) 
{
	CString str;
	str.Format(" 打错:%d",m_nErrorCount);
	pCmdUI->SetText(str);
}
void CMyView::OnUpdateLevel(CCmdUI* pCmdUI)
{
	CString str;
	str.Format(" 级别:%d",m_nLevel);
	pCmdUI->SetText(str);
}

BOOL CMyView::SaveToFile()
{
	FILE *pFile;
	HEROS heros[10];
	HEROS tempHero;
	SYSTEMTIME SysTime;
	char pszTime[40]="";
	int	i;
	CInputDlg Inputdlg;
	CHeroDlg  HeroDlg;
	CString strPlayName;
	memset(heros,0,sizeof(heros));
	memset(&tempHero,0,sizeof(HEROS));
	GetLocalTime(&SysTime);
	wsprintf(pszTime,"%4d/%02d/%02d--%02d:%02d:%02d",SysTime.wYear,SysTime.wMonth,SysTime.wDay,SysTime.wHour,SysTime.wMinute,SysTime.wSecond);
	int nScore=10*m_nHitCount-2*m_nErrorCount;
	if(_access("ScoreData.bsn",0)== -1)
	{
		if(m_bSoundOn)
			::PlaySound((LPCTSTR)IDR_CLAP,NULL,SND_RESOURCE|SND_ASYNC);
		if(Inputdlg.DoModal()==IDOK)
		{
			if(Inputdlg.m_strName==_T(""))
				strPlayName=strPlayName=CString("匿名");
			else
				strPlayName=Inputdlg.m_strName;
		}
		else
		{
			strPlayName=CString("匿名");
		}

		pFile=fopen("ScoreData.bsn","w+");
		if(pFile == NULL)
		{
			MessageBox("Can't create file for write!");
			return FALSE;
		}
		strcpy(tempHero.name,strPlayName);
		wsprintf(tempHero.score,"%d",nScore);
		wsprintf(tempHero.hitcount,"%d",m_nHitCount);
		wsprintf(tempHero.errorcount,"%d",m_nErrorCount);
		strcpy(tempHero.playtime,pszTime);

		fwrite(&tempHero,sizeof(HEROS),1,pFile);
		for(i=0;i<9;i++)
		{
			strcpy(heros[i].hitcount,"0");
			strcpy(heros[i].errorcount,"0");
			strcpy(heros[i].score,"0");
			strcpy(heros[i].name,"匿名");
			strcpy(heros[i].playtime,"0000/00/00--00:00:00");
			fwrite(&heros[i],sizeof(HEROS),1,pFile);
		}
		fclose(pFile);
		HeroDlg.DoModal();
		return TRUE;
	}
	else
	{
		pFile=fopen("ScoreData.bsn","r+");
		if(pFile == NULL)
		{
			MessageBox("Can't open file for write!");
			return FALSE;
		}
		for(i=0;i<10;i++)
		{
			fread(&heros[i],sizeof(HEROS),1,pFile);
		}
		for(i=0;i<10;i++)
		{
			if(atoi(heros[i].score) < nScore)
			{
				if(m_bSoundOn)
					::PlaySound((LPCTSTR)IDR_CLAP,NULL,SND_RESOURCE|SND_ASYNC);
				if(Inputdlg.DoModal()==IDOK)
				{
					if(Inputdlg.m_strName==_T(""))
						strPlayName=strPlayName=CString("匿名");
					else
						strPlayName=Inputdlg.m_strName;
				}
				else
				{
					strPlayName=CString("匿名");
				}
				strcpy(tempHero.name,strPlayName);
				wsprintf(tempHero.score,"%d",nScore);
				wsprintf(tempHero.hitcount,"%d",m_nHitCount);
				wsprintf(tempHero.errorcount,"%d",m_nErrorCount);
				strcpy(tempHero.playtime,pszTime);
				memmove(heros+i+1,heros+i,(9-i)*sizeof(HEROS));
				heros[i]=tempHero;
				fseek(pFile,0L,SEEK_SET);
				fwrite(heros,sizeof(HEROS),10,pFile);
				fclose(pFile);
				HeroDlg.DoModal();
				return TRUE;
			}
		}
		fclose(pFile);
		return FALSE;
	}

}

void CMyView::OnContextMenu(CWnd*, CPoint point)
{
	CMyMenu *menu = new CMyMenu;
	menu->CreateMenu(IDB_MENU);
	menu->SetColor(RGB(0,0,255),RGB(100,200,255),RGB(255,0,0));
	menu->InsertMenu(ID_FILE_NEW,"新建(&N)\t\t\tCtrl+N",IDI_NEW);
	menu->InsertMenu(ID_START,"开始(&S)\t\t\tEnter",IDI_START);
	menu->InsertMenu(ID_STOP,"停止(&P)\t\t\tSpace",IDI_STOP);
	menu->InsertMenu(ID_SOUND_ON,"声音开(&U)\t\tF3",IDI_SOUND_ON);
	menu->InsertMenu(ID_SOUND_OFF,"声音关(&D)\t\tF4",IDI_SOUND_OFF);
	if(((CMainFrame *)AfxGetMainWnd())->m_bFullScreen==TRUE)
		menu->InsertMenu(ID_FULL_SCREEN,"取消全屏显示(&L)\tAlt+Enter",IDI_FULL_SCREEN);
	else
		menu->InsertMenu(ID_FULL_SCREEN,"全屏显示(&L)\t\tAlt+Enter",IDI_FULL_SCREEN);

	menu->InsertMenu(ID_APP_ABOUT,"关于 打字游戏(&A)...\tF1",IDI_ABOUT);
	menu->InsertMenu(ID_HERO,"英雄榜(&H)...\t\tF2",IDI_HERO);
	menu->InsertMenu(ID_APP_EXIT,"退出(&X)\t\t\tAlt+X",IDI_EXIT);
	menu->TrackPopupMenu(TPM_LEFTALIGN,point.x,point.y,this);
	delete menu;
}

void CMyView::OnAppAbout() 
{
	theApp.OnAppAbout();
}

void CMyView::OnFileNew() 
{
	OnInitialUpdate();
	
}

void CMyView::OnFullScreen() 
{
	// TODO: Add your command handler code here
	((CMainFrame *)AfxGetMainWnd())->OnFullScreen();
	
}


void CMyView::OnAppExit() 
{
	// TODO: Add your command handler code here
	((CMainFrame *)AfxGetMainWnd())->SendMessage(WM_CLOSE);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -