码迷,mamicode.com
首页 > 编程语言 > 详细

vc++微游戏

时间:2015-03-14 17:03:40      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:vc++   visual c++   mfc   

先上图:

技术分享

使用vc++6.0开发

源码:http://download.csdn.net/detail/h1023417614/8501223


//view类的头文件
// game1View.h : interface of the CGame1View class
//
/////////////////////////////////////////////////////////////////////////////

#if !defined(AFX_GAME1VIEW_H__C9459B4E_5B14_4A9E_8D36_0AC5CB7A3E58__INCLUDED_)
#define AFX_GAME1VIEW_H__C9459B4E_5B14_4A9E_8D36_0AC5CB7A3E58__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000


class CGame1View : public CView
{
protected: // create from serialization only
	CGame1View();
	DECLARE_DYNCREATE(CGame1View)

// Attributes
public:
	CGame1Doc* GetDocument();

// Operations
public:

// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CGame1View)
	public:
	virtual void OnDraw(CDC* pDC);  // overridden to draw this view
	virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
	virtual BOOL PreTranslateMessage(MSG* pMsg);
	protected:
	virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
	virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
	virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
	//}}AFX_VIRTUAL

// Implementation
public:
	virtual ~CGame1View();
#ifdef _DEBUG
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif

protected:

// Generated message map functions
protected:
	//{{AFX_MSG(CGame1View)
	afx_msg void OnTimer(UINT nIDEvent);
	afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
	afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()

private://012色号代表红绿蓝
	int background;//背景色号
	int movecolor;//移动块色号
	int movecolorselect;//移动块选择位置012代表左右下

	int haveselect;//判断是否有按下方向键
	int select;//玩家所选号

    int kuan;//客户区的宽度
	int leftkuan;//左客户区的宽度
	int selectkuan;//选择块的宽度
	int x,y;//移动块的位置

	int color[3];
	int left,right,below;//左右下的颜色值

	void setupcolor();//设置所有块的颜色
	void draw(CDC*,CRect &);//画图
	void overgame();//gameover

	int score;//分数

};

#ifndef _DEBUG  // debug version in game1View.cpp
inline CGame1Doc* CGame1View::GetDocument()
   { return (CGame1Doc*)m_pDocument; }
#endif

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_GAME1VIEW_H__C9459B4E_5B14_4A9E_8D36_0AC5CB7A3E58__INCLUDED_)



//view类cpp
// game1View.cpp : implementation of the CGame1View class
//

#include "stdafx.h"
#include "game1.h"


#include "game1Doc.h"
#include "game1View.h"
#include "tishikuang.h"

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

/////////////////////////////////////////////////////////////////////////////
// CGame1View

IMPLEMENT_DYNCREATE(CGame1View, CView)

BEGIN_MESSAGE_MAP(CGame1View, CView)
	//{{AFX_MSG_MAP(CGame1View)
	ON_WM_TIMER()
	ON_WM_CREATE()
	ON_WM_CANCELMODE()
	ON_WM_KEYDOWN()
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CGame1View construction/destruction

CGame1View::CGame1View()
{
	// TODO: add construction code here
	haveselect=0;
	x=0;
	y=0;
	for(int i=0;i<3;++i)color[i]=0;

	 background=0;//背景色号
	 movecolor=0;//移动块色号
	 movecolorselect=0;//移动块选择位置012代表左右下

	 haveselect=0;//判断是否有按下方向键
	 select=0;//玩家所选号

     kuan=0;//客户区的宽度
	 leftkuan=0;//左客户区的宽度
	 selectkuan=0;//选择块的宽度

	 left=0;right=0;below=0;//左右下的颜色值
	 score=0;
}

CGame1View::~CGame1View()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CGame1View drawing

void CGame1View::OnDraw(CDC* pDC)
{
	CGame1Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	static double speed=0;//静态数据,
	int i;

	CRect rc;
	GetClientRect(&rc);
	ScreenToClient(&rc);
	kuan=rc.Width();//设置一些块的宽度
	leftkuan=kuan * 0.8;
	selectkuan=kuan*0.05;

	if(1==haveselect || 0==speed)//如果有选择或者初始化时
	{
		if (0==speed)
		{
			speed=1;//初始化
		}
		if(1==haveselect)
		{
			haveselect=0;
			if(movecolorselect != select){//选择错了
				overgame();
				speed=1;
				score=-1;
			}
			speed=speed * 1.1;//加速
			++score;	//加分
		}
		setupcolor();//设置颜色
		x=rand() % (int)(kuan * 0.8 * 0.9);//随机设置移动块的位置
		y=0;   
	}

	draw(pDC,rc);//画图
	
    y=y+speed;//移动块移动
	if(y>rc.Height())overgame();//越过区域
	
}

/////////////////////////////////////////////////////////////////////////////
// CGame1View printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CGame1View diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CGame1View message handlers

void CGame1View::OnTimer(UINT nIDEvent) 
{

    Invalidate(0);//刷图
	CView::OnTimer(nIDEvent);
}


int CGame1View::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
    MessageBox("使用左右下方向键选择移动块对应的颜色");

	srand((unsigned)time(NULL));//时间种子
    SetTimer(1, 3, 0);//设置定时刷图
	
	return 0;
}


BOOL CGame1View::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class

      switch(pMsg->message)
      {
      case WM_KEYDOWN:
			{    
			   switch(pMsg->wParam)//左右下方向键按下了
			   {
					   case VK_LEFT:
						   {  
							  select=0;
							  haveselect=1;	
						  }	
						   break;
					   case VK_RIGHT:
						  {  
							  select=1;
							   haveselect=1;
							
						  }	
						  break;
						case VK_DOWN:
						  {  
							  select=2;
							   haveselect=1;
							
						  }	
						  break;
					   default:
						  break;
			   }
       
        }
		break;
      default:
        break;
      }
   
	
	return CView::PreTranslateMessage(pMsg);
}

void CGame1View::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
    //最好不要使用这里来处理按键

	CView::OnKeyDown(nChar, nRepCnt, nFlags);
}



void CGame1View::setupcolor()
{
		background=rand()%3;
		do{
		movecolor=rand()%3;//随机的移动块的颜色
		}
		while(movecolor==background);

		
		movecolorselect=rand()%3;//随机的移动块的选择位置

		left=0;right=0;below=0;//以下是设置左右下块的颜色
		if (movecolorselect==0)
		{
			left =movecolor;
			while(right==left){++right;}
			while(below==right || below==left){++below;}
		}
		else if (movecolorselect==1)
		{
			right=movecolor;
			while(right==left){++left;}
			while(below==right || below==left){++below;}
		}
		else	
		{
			below=movecolor;
		while(right==below){++right;}
		while(left==right || below==left){++left;}
		}

}

void CGame1View::draw(CDC *pDC,CRect &rc)
{
	CBrush brush1;   // Must initialize!
	CBrush* pTempBrush = NULL;
	CBrush OrigBrush;
    brush1.GetLogBrush;
    
	int i;
    for( i=0;i<3;++i)color[i]=0;
    color[background]=255;
    brush1.CreateSolidBrush(RGB(color[0], color[1], color[2]));   // brush.
	pTempBrush = (CBrush*)pDC->SelectObject(brush1);
	OrigBrush.FromHandle((HBRUSH)pTempBrush);// Save original brush.
	// Paint upper left corner with blue brush.
	pDC->Rectangle(0, 0, kuan * 0.8, rc.Height());

	// These constructors throw resource exceptions.
	try
	{	
		for( i=0;i<3;++i)color[i]=0;
    	color[movecolor]=255;
        brush1.DeleteObject();
		brush1.CreateSolidBrush(RGB(color[0], color[1], color[2]));   // brush.
		(CBrush*)pDC->SelectObject(brush1);
		pDC->Rectangle(x, y, x+kuan * 0.8 * 0.1,
			y+kuan * 0.8 * 0.1);

		for( i=0;i<3;++i)color[i]=0;
    	color[left]=255;
		brush1.DeleteObject();
		brush1.CreateSolidBrush(RGB(color[0], color[1], color[2]));   // brush.
		(CBrush*)pDC->SelectObject(brush1);
		pDC->Rectangle(leftkuan+selectkuan, 5, leftkuan+2*selectkuan,
			5+selectkuan);

		for( i=0;i<3;++i)color[i]=0;
    	color[right]=255;
		brush1.DeleteObject();
		brush1.CreateSolidBrush(RGB(color[0], color[1], color[2]));   // brush.
		(CBrush*)pDC->SelectObject(brush1);
		pDC->Rectangle(leftkuan+2*selectkuan, 5, leftkuan + 3*selectkuan,
			5+selectkuan);

		for( i=0;i<3;++i)color[i]=0;
   		 color[below]=255;
		brush1.DeleteObject();
		brush1.CreateSolidBrush(RGB(color[0], color[1], color[2]));   // brush.
		(CBrush*)pDC->SelectObject(brush1);
		pDC->Rectangle(leftkuan+1.5*selectkuan, 5+selectkuan,leftkuan+2.5*selectkuan,
			5+2*selectkuan);

	}
	catch (CResourceException* e)
	{
		e->ReportError();
		e->Delete();
	}
	pDC->SelectObject(&OrigBrush);
}
void CGame1View::overgame()
{
	KillTimer(1);//参数1是settimer的标号
    CString s;
	s.Format(_T("你的分数是: %d  "), score);
	
	tishikuang tishi;
	tishi.m_tishi=s;
	if(tishi.DoModal()==IDOK)
	{
		SetTimer(1,3,0);

	}
	else PostQuitMessage(0);
}












vc++微游戏

标签:vc++   visual c++   mfc   

原文地址:http://blog.csdn.net/h1023417614/article/details/44260075

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!