vc++微游戏

来源:互联网 发布:网络机顶盒app应用软件 编辑:程序博客网 时间:2024/06/13 16:38

先上图:


使用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 > 1000class CGame1View : public CView{protected: // create from serialization onlyCGame1View();DECLARE_DYNCREATE(CGame1View)// Attributespublic:CGame1Doc* GetDocument();// Operationspublic:// Overrides// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CGame1View)public:virtual void OnDraw(CDC* pDC);  // overridden to draw this viewvirtual 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// Implementationpublic:virtual ~CGame1View();#ifdef _DEBUGvirtual void AssertValid() const;virtual void Dump(CDumpContext& dc) const;#endifprotected:// Generated message map functionsprotected://{{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_MSGDECLARE_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();//gameoverint score;//分数};#ifndef _DEBUG  // debug version in game1View.cppinline 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_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// CGame1ViewIMPLEMENT_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 commandsON_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/destructionCGame1View::CGame1View(){// TODO: add construction code herehaveselect=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 csreturn CView::PreCreateWindow(cs);}/////////////////////////////////////////////////////////////////////////////// CGame1View drawingvoid CGame1View::OnDraw(CDC* pDC){CGame1Doc* pDoc = GetDocument();ASSERT_VALID(pDoc);// TODO: add draw code for native data herestatic 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 printingBOOL CGame1View::OnPreparePrinting(CPrintInfo* pInfo){// default preparationreturn 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 _DEBUGvoid 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 handlersvoid 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);}












0 0
原创粉丝点击