jds_snakeView.cpp
资源名称:snake.rar [点击查看]
上传用户:ywxcgood
上传日期:2022-06-03
资源大小:1959k
文件大小:7k
源码类别:
其他游戏
开发平台:
Visual C++
- // jds_snakeView.cpp : implementation of the CJds_snakeView class
- //
- #include "stdafx.h"
- #include "jds_snake.h"
- #include "jds_snakeDoc.h"
- #include "jds_snakeView.h"
- #include "MainFrm.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CJds_snakeView
- IMPLEMENT_DYNCREATE(CJds_snakeView, CView)
- BEGIN_MESSAGE_MAP(CJds_snakeView, CView)
- //{{AFX_MSG_MAP(CJds_snakeView)
- ON_WM_TIMER()
- ON_WM_CREATE()
- ON_WM_KEYDOWN()
- ON_COMMAND(ID_DIFFICULTY_EASY, OnDifficultyEasy)
- ON_COMMAND(ID_DIFFICULTY_HARD, OnDifficultyHard)
- ON_COMMAND(ID_DIFFICULTY_NORMAL, OnDifficultyNormal)
- ON_COMMAND(ID_DIFFICULTY_VERYHARD, OnDifficultyVeryhard)
- ON_COMMAND(ID_BUTTON_START, OnButtonStart)
- //}}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()
- /////////////////////////////////////////////////////////////////////////////
- // CJds_snakeView construction/destruction
- CJds_snakeView::CJds_snakeView()
- {
- // TODO: add construction code here
- start=0;
- difficulty=10;
- count=4;
- }
- CJds_snakeView::~CJds_snakeView()
- {
- }
- BOOL CJds_snakeView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- return CView::PreCreateWindow(cs);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CJds_snakeView drawing
- void CJds_snakeView::OnDraw(CDC* pDC)
- {
- CJds_snakeDoc* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
- // TODO: add draw code for native data here
- }
- /////////////////////////////////////////////////////////////////////////////
- // CJds_snakeView printing
- BOOL CJds_snakeView::OnPreparePrinting(CPrintInfo* pInfo)
- {
- // default preparation
- return DoPreparePrinting(pInfo);
- }
- void CJds_snakeView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add extra initialization before printing
- }
- void CJds_snakeView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add cleanup after printing
- }
- /////////////////////////////////////////////////////////////////////////////
- // CJds_snakeView diagnostics
- #ifdef _DEBUG
- void CJds_snakeView::AssertValid() const
- {
- CView::AssertValid();
- }
- void CJds_snakeView::Dump(CDumpContext& dc) const
- {
- CView::Dump(dc);
- }
- CJds_snakeDoc* CJds_snakeView::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CJds_snakeDoc)));
- return (CJds_snakeDoc*)m_pDocument;
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CJds_snakeView message handlers
- void CJds_snakeView::OnTimer(UINT nIDEvent)
- {
- // TODO: Add your message handler code here and/or call default
- KillTimer(1);
- CString str_count;
- str_count.Format("分数:%d",count-4);
- aliveordie(); //生死判官
- CDC* pDC=GetDC();
- CDC MemDC;
- CBitmap MemBitmap;
- MemDC.CreateCompatibleDC(NULL);
- MemBitmap.CreateCompatibleBitmap(pDC,1000,1000);
- CBitmap *pOldBit=MemDC.SelectObject(&MemBitmap);
- MemDC.FillSolidRect(0,0,1000,1000,RGB(255,255,255));
- MemDC.TextOut(500,200,str_count);
- MemDC.Rectangle(CRect(10,10,440,440));
- MemDC.Rectangle(CRect(20,20,430,430));
- if (start==1)
- {
- b.x=a[0].x;
- b.y=a[0].y;
- if (aspect==0)
- {
- a[0].x=a[0].x+10;
- } else if (aspect==1)
- {
- a[0].y=a[0].y+10;
- }else if (aspect==2)
- {
- a[0].x=a[0].x-10;
- }else if (aspect==3)
- {
- a[0].y=a[0].y-10;
- }
- for (int i=1;i<count-1;i++)
- {
- a[count-i].x=a[count-i-1].x;
- a[count-i].y=a[count-i-1].y;
- }
- a[1].x=b.x;
- a[1].y=b.y;
- CBrush head(RGB(255,0,0));
- MemDC.SelectObject(head);
- rct=new CRect(a[0].x,a[0].y,a[0].x+10,a[0].y+10);
- MemDC.Rectangle(rct);
- for(i=1;i<count;i++)
- {
- CBrush body(RGB(255,255,0));
- MemDC.SelectObject(body);
- rct=new CRect(a[i].x,a[i].y,a[i].x+10,a[i].y+10);
- MemDC.Rectangle(rct);
- MemDC.SelectStockObject(BLACK_BRUSH);
- }
- if (a[0].x==xx && a[0].y==yy)
- {
- can_new=1;
- count++;
- a[count-1].x=a[count-2].x;
- a[count-1].y=a[count-2].y;
- // MessageBox("eat");
- }
- if (can_new==1)
- {
- int px=int(random(2,42));
- int py=int(random(2,42));
- xx=px*10;
- yy=px*10;
- for (int i=0;i<count;i++)
- {
- while (xx==a[i].x && yy==a[i].y)
- {
- int px=int(random(2,42));
- int py=int(random(2,42));
- xx=px*10;
- yy=px*10;
- }
- }
- can_new=0;
- }
- CBrush foot(RGB(0,255,0));
- MemDC.SelectObject(foot);
- MemDC.Rectangle(xx,yy,xx+10,yy+10);
- MemDC.SelectStockObject(BLACK_BRUSH);
- }
- pDC->BitBlt(0,0,1000,1000,&MemDC,0,0,SRCCOPY);
- MemBitmap.DeleteObject();
- MemDC.DeleteDC();
- OnDraw(pDC);
- int m_timer=SetTimer(1,difficulty*30,NULL);
- CView::OnTimer(nIDEvent);
- }
- int CJds_snakeView::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CView::OnCreate(lpCreateStruct) == -1)
- return -1;
- // TODO: Add your specialized creation code here
- /*
- CString str;
- str.Format("%d",difficulty);
- MessageBox(str);
- */
- SetTimer(1,10,NULL);
- srand(unsigned(time(0)));
- return 0;
- }
- void CJds_snakeView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- // TODO: Add your message handler code here and/or call default
- last_aspect=aspect;
- if (nChar==37 && last_aspect!=0)
- {
- aspect=2;
- }else if (nChar==38 && last_aspect!=1)
- {
- aspect=3;
- }else if (nChar==39 && last_aspect!=2)
- {
- aspect=0;
- }else if (nChar==40 && last_aspect!=3)
- {
- aspect=1;
- }
- CView::OnKeyDown(nChar, nRepCnt, nFlags);
- }
- double CJds_snakeView::random(double start, double end)
- {
- return start+(end-start)*rand()/(RAND_MAX + 1.0);
- }
- void CJds_snakeView::aliveordie()
- {
- for (int i=1;i<count;i++)
- {
- if (a[0].x==a[i].x && a[0].y==a[i].y)
- {
- start=0;
- }
- else if (a[0].x<20 || a[0].x>420 ||a[0].y<20 ||a[0].y>420)
- {
- start=0;
- }
- }
- }
- void CJds_snakeView::Init()
- {
- aspect=0;
- last_aspect=0;
- count=4;
- can_new=1;
- a[0].x=90;
- a[0].y=80;
- a[1].x=80;
- a[1].y=80;
- a[2].x=70;
- a[2].y=80;
- a[3].x=60;
- a[3].y=80;
- }
- void CJds_snakeView::OnDifficultyEasy()
- {
- // TODO: Add your command handler code here
- difficulty=10;
- CString str;
- str.Format("%d",difficulty);
- // MessageBox(str);
- }
- void CJds_snakeView::OnDifficultyHard()
- {
- // TODO: Add your command handler code here
- difficulty=3;
- }
- void CJds_snakeView::OnDifficultyNormal()
- {
- // TODO: Add your command handler code here
- difficulty=6;
- CString str;
- str.Format("%d",difficulty);
- // MessageBox(str);
- }
- void CJds_snakeView::OnDifficultyVeryhard()
- {
- // TODO: Add your command handler code here
- difficulty=2;
- CString str;
- str.Format("%d",difficulty);
- // MessageBox(str);
- }
- void CJds_snakeView::OnButtonStart()
- {
- // TODO: Add your command handler code here
- start=1;
- Init();
- }