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

📄 russiaview.cpp

📁 一个俄罗斯方块的vc源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// RussiaView.cpp : implementation of the CRussiaView class
//

#include "stdafx.h"
#include "Russia.h"

#include "RussiaDoc.h"
#include "RussiaView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


static const TCHAR szSection[] = _T("Settings");
static const TCHAR clrCube[] = _T("方块颜色");
static const TCHAR clrDeclareCube[] = _T("声明区域的方块颜色");
static const TCHAR clrMaxRect[] = _T("最大区域颜色");
static const TCHAR clrDeclareRect[] = _T("声明区域颜色");
static const TCHAR clrDiamondsRect[] = _T("运行区域颜色");
static const TCHAR clrLine[] = _T("方格线颜色");
static const TCHAR clrDeclareFont[] = _T("声明区域的字体颜色");
static const TCHAR clrGameOverFont[] = _T("游戏结束字体的颜色");
static const TCHAR bRandomCubeColor[] = _T("随机产生运动方块颜色");
static const TCHAR Speed[] = _T("速度");
static const TCHAR Difficult[] = _T("难度");
static const TCHAR Music[] = _T("音乐开关");



extern theApp;
/////////////////////////////////////////////////////////////////////////////
// CRussiaView

IMPLEMENT_DYNCREATE(CRussiaView, CView)

BEGIN_MESSAGE_MAP(CRussiaView, CView)
	//{{AFX_MSG_MAP(CRussiaView)
	ON_WM_TIMER()
	ON_COMMAND(ID_MENU_START, OnMenuStart)
	ON_COMMAND(ID_MENU_PAUSE, OnMenuPause)
	ON_WM_CREATE()
	ON_WM_KEYDOWN()
	ON_UPDATE_COMMAND_UI(ID_MENU_PAUSE, OnUpdateMenuPause)
	ON_COMMAND(ID_MENU_SETUP, OnMenuSetup)
	ON_COMMAND(ID_MENU_SAVE, OnMenuSave)
	ON_COMMAND(ID_MENU_READ, OnMenuRead)
	ON_COMMAND(ID_MENU_NETSTART, OnMenuNetstart)
	ON_COMMAND(ID_MENU_DOUBLESTART, OnMenuDoublestart)
	ON_COMMAND(ID_MENU_HELP, OnMenuHelp)
	ON_COMMAND(ID_MENU_COLOR, OnMenuColor)
	ON_COMMAND(ID_MENU_PLAYER, OnMenuPlayer)
	ON_MESSAGE(WM_HOTKEY,OnHotKey)//kiler adds
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
	// Standard printing commands

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRussiaView construction/destruction

CRussiaView::CRussiaView()
{
	// TODO: add construction code here
	m_bPause=false;
	m_bStart=false;
	m_bMusic=TRUE;
	CurrentGameFlag=1;//1为单人游戏,2为双人游戏,3为网络游戏
}

CRussiaView::~CRussiaView()
{

}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CRussiaView drawing

void CRussiaView::OnDraw(CDC* pDC)
{
//	CRussiaDoc* pDoc = GetDocument();
//	ASSERT_VALID(pDoc);

	// TODO: add draw code for native data here
	Game.Draw(pDC);
	if(CurrentGameFlag==2)
		Game2.Draw(pDC);
}

/////////////////////////////////////////////////////////////////////////////
// CRussiaView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CRussiaView message handlers
void CRussiaView::OnTimer(UINT nIDEvent) 
{
	if((m_bPause==TRUE)||(m_bStart==FALSE)) return;
	int speed=Game.GetSpeed();
	switch(CurrentGameFlag)
	{
	case 1:
		Game.MoveNext();
		Game.Draw(GetDC());
		if(Game.EndFlag==TRUE)
		{
			m_bStart=FALSE;
			if(GetMinScore()>=Game.m_Score) break;
			CDialogNameInput dlg;
			if(dlg.DoModal()==IDOK)
			{
				SavePlayer(Game.m_Score,dlg.m_Player);
				OnMenuPlayer();
			}
			CTipDlg dlg2;
			dlg2.DoModal();
		}
		break;
	case 2:
		Game.MoveNext();
		Game.Draw(GetDC());
		Game2.MoveNext();
		Game2.Draw(GetDC());
		break;
	case 3:
		break;
	}

	if(speed<Game.GetSpeed())
	{
		OnMenuPause();
		OnMenuPause();
		//KillTimer(9999);
		//SetTimer(9999,50*(11-Game.GetSpeed()),NULL);
	}
	CView::OnTimer(nIDEvent);
}

void CRussiaView::OnMenuStart() 
{
	CWinApp* pApp = AfxGetApp();
	int dx=pApp->GetProfileInt("Settings","方块大小",20);
	int m1=111+dx*10;
	int m2=85+dx*24;

	if(CurrentGameFlag!=1)
	{
		//调整窗口大小
		AfxGetMainWnd() ->SetWindowPos(NULL,0,0,m1,m2,SWP_NOMOVE|SWP_NOZORDER );
		AfxGetMainWnd()->CenterWindow();
	}
	
	CurrentGameFlag=1;
	//1为单人游戏,2为双人游戏,3为网络游戏
	
	m_bPause=false;		//暂停标志为假
	m_bStart=true;		//开始标志为真
	
	//Game2.SetPosition(320,0);
	Game.SetPosition(0,0);
	SetTimer(9999,50*(11-Game.GetSpeed()),NULL);
	Game.Start();
	Game.Draw(GetDC());

	//播放声音
//	if(m_bMusic==TRUE)
//		sndPlaySound("Russia.wav",SND_LOOP|SND_ASYNC);
	Game.PlayMusic(m_bMusic);
}

void CRussiaView::OnMenuPause() 
{
	m_bPause=!m_bPause;
	if(m_bPause)
	{
		KillTimer(9999);
		sndPlaySound(NULL,SND_ASYNC);
		Game.PlayMusic(FALSE);
		Game2.PlayMusic(FALSE);
	}

	else
	{
		SetTimer(9999,50*(12-Game.GetSpeed()),NULL);
		//播放声音
		//if(m_bMusic==TRUE)
		//	sndPlaySound("Russia.wav",SND_LOOP|SND_ASYNC);
		Game.PlayMusic(m_bMusic);
		if(CurrentGameFlag==2) Game2.PlayMusic(m_bMusic);

	}
}

//保存前五名游戏者
void CRussiaView::SavePlayer(int score,CString player)
{
	TCHAR szDir [MAX_PATH];
	:: GetWindowsDirectory (szDir, MAX_PATH);

	CString str,s[6];
	int i,m[6];
	
	CStdioFile file;
	for(i=0;i<6;i++)
	{
		s[i]="swallow";
		m[i]=50;
	}	//初始值置为50

	
	//先从文件中把成绩和姓名读出存入m[i]和s[i]中
	str.Format("%s\\kiler.ini",szDir);
	if(file.Open(str,CFile::modeRead)!=0)
	{
		CArchive ar(&file,CArchive::load);
		
		for(i=0;i<5;i++)
		{
			ar.ReadString(str);
			char ss[20];
			sscanf(str,"%d-%s",&m[i],ss);
			s[i].Format("%s",ss);
		}

		file.Close();
		ar.Close();
	}

	//把成绩最小的一位游戏者挤出去
	int k=5;
	for(i=0;i<5;i++)
		if((m[i]<score)&&(k==5))
		{
			k=i;
			break;
		}

	if(k<5)
	{
		for(i=4;i>k;i--)
		{
			s[i]=s[i-1];
			m[i]=m[i-1];
		}
		s[k]=player;
		m[k]=score;
	}

	//重新写入文件
	if(file.Open("c:\\windows\\kiler.ini",CFile::modeCreate|CFile::modeWrite|CFile::typeText)==0)
	{
		AfxMessageBox("save error!");
		return;
	}   
	for(i=0;i<5;i++)
	{
		str.Format("%d-%s\n",m[i],LPCSTR(s[i]));
		file.WriteString(str);
	}

}
 
LRESULT CRussiaView::OnHotKey(WPARAM wParam,LPARAM lParam)
{
	if(wParam==1001||wParam==1002)
	{
		MessageBox("Hello ,my friends!");
	// 	CRect rect;
	//	AfxGetMainWnd()->GetWindowRect(&rect);
	//	AfxGetMainWnd()->OnSize(0,rect.Width(),rect.Height());
		AfxGetMainWnd()->RestoreWaitCursor();
	}
	return(0);


}


int CRussiaView::OnCreate(LPCREATESTRUCT lpCreateStruct) 

⌨️ 快捷键说明

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