| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 915 人关注过本帖
标题:小弟新手 求助一贪吃蛇问题,蛇吃了豆子为什么没有变化,而且随机的豆子也未 ...
取消只看楼主 加入收藏
longyi2
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2012-9-6
收藏
 问题点数:0 回复次数:0 
小弟新手 求助一贪吃蛇问题,蛇吃了豆子为什么没有变化,而且随机的豆子也未出现
yView class
//
#include <stdio.h>

#include "stdafx.h"
#include "贪吃蛇.h"

#include "贪吃蛇Doc.h"
#include "贪吃蛇View.h"
#include "Ge.h"
#include "snake.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

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

IMPLEMENT_DYNCREATE(CMyView, CView)

BEGIN_MESSAGE_MAP(CMyView, CView)
    //{{AFX_MSG_MAP(CMyView)
    ON_COMMAND(IDM_START, OnStart)
    ON_COMMAND(IDM_PAUSE, OnPause)
    ON_WM_TIMER()
    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()

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

CMyView::CMyView()
{
    // TODO: add construction code here
    this->m_start=false;
    this->m_pause=false;

    //初始化蛇
    snake.n=4,snake.direction=2;
    snake.body[0].i=2,snake.body[0].j=5;
    snake.body[1].i=2,snake.body[1].j=4;
    snake.body[2].i=2,snake.body[2].j=3;
    snake.body[3].i=2,snake.body[3].j=2;
   
        //初始化豆子
    unsigned int seed=time(NULL);
    srand(seed);
    do
    {
    bean.i=rand()%29;
    bean.j=rand()%29;
    }   
    while(CSnakesnake()==true);   
}

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
    //画背景
   
 CClientDC dc(this);
 CPen pen1;
 CPen *oldPen;
 pen1.CreatePen(PS_SOLID,2,RGB(192,192,192));
 oldPen = dc.SelectObject(&pen1);
 dc.MoveTo(0,290);
 dc.LineTo(290,290);
 dc.SelectObject(oldPen);

    CDC dcmemory;
    dcmemory.CreateCompatibleDC(pDC);
    CBitmap bitmap;
    bitmap.LoadBitmap(IDB_BITMAP1);
    dcmemory.SelectObject(&bitmap);
    int i,j;
    for (i=0;i<=28;i++)
    {
    for (j=0;j<=28;j++)
    {
    pDC->StretchBlt(0+10*i,0+10*j,10,10,&dcmemory,0,0,48,48,SRCCOPY);
    }
    }
    bitmap.Detach();


    //画蛇头
    bitmap.LoadBitmap(IDB_BITMAP2);
    dcmemory.SelectObject(&bitmap);
    pDC->StretchBlt(0+10*snake.body[0].j,0+10*snake.body[0].i,10,10,&dcmemory,0,0,48,48,SRCCOPY);
    bitmap.Detach();

    //画蛇身
    bitmap.LoadBitmap(IDB_BITMAP3);
    dcmemory.SelectObject(&bitmap);
    for (i=1;i<snake.n;i++)
    {
    pDC->StretchBlt(0+10*snake.body[i].j,0+10*snake.body[i].i,10,10,&dcmemory,0,0,48,48,SRCCOPY);
    }
    bitmap.Detach();
    //画豆子
    bitmap.LoadBitmap(IDB_BITMAP4);
    dcmemory.SelectObject(&bitmap);
    pDC->StretchBlt(0+10*bean.i,0+10*bean.j,10,10,&dcmemory,0,0,48,48,SRCCOPY);
    bitmap.Detach();
            


}
/////////////////////////////////////////////////////////////////////////////
// CMyView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// 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()
{
    // TODO: Add your command handler code here
    this->m_start=true;
    SetTimer(1,100,NULL);   
}

void CMyView::OnPause()
{
    // TODO: Add your command handler code here
static t=1;
if(t%2==1)
{
this->m_pause=true;
}
else
{
this->m_pause=false;
}
    t++;
}

void CMyView::OnTimer(UINT nIDEvent)
{
    // TODO: Add your message handler code here and/or call default
    if(this->m_start==true && this->m_pause==false)
    {
    CGe tmp;
    CClientDC dc(this);
    CDC dcmemory;
    dcmemory.CreateCompatibleDC(&dc);
    CBitmap bitmap;
switch (snake.direction)
        {
case 1 :
                tmp=snake.forwardup();
             break;
case 2:
                 tmp=snake.forwardright();
        break;
case 3:
            tmp=snake.forwarddown();
            break;
case 4:
                tmp=snake.forwardleft();
                break;
            }

if(Issnakeeat()==true)
    {
    //初始化豆子
    unsigned int seed=time(NULL);
    srand(seed);
    do
    {
    bean.i=rand()%29;
    bean.j=rand()%29;
    }   
    while(CSnakesnake()==true);   

    bitmap.LoadBitmap(IDB_BITMAP4);
    dcmemory.SelectObject(&bitmap);
    dc.StretchBlt(0+10*bean.j,0+10*bean.i,10,10,&dcmemory,0,0,48,48,SRCCOPY);
    bitmap.Detach();

    snake.n++;//蛇头增加
    snake.body[snake.n-1].i=tmp.i;//蛇身不变
    snake.body[snake.n-1].j=tmp.j;


    }
            bitmap.LoadBitmap(IDB_BITMAP1);
            dcmemory.SelectObject(&bitmap);
            dc.StretchBlt(0+10*tmp.j,0+10*tmp.i,10,10,&dcmemory,0,0,48,48,SRCCOPY);
            bitmap.Detach();

            //画蛇头
            bitmap.LoadBitmap(IDB_BITMAP2);
            dcmemory.SelectObject(&bitmap);
            dc.StretchBlt(0+10*snake.body[0].j,0+10*snake.body[0].i,10,10,&dcmemory,0,0,48,48,SRCCOPY);
            bitmap.Detach();
            //画蛇身
            bitmap.LoadBitmap(IDB_BITMAP3);
            dcmemory.SelectObject(&bitmap);
            for (int i=1;i<snake.n;i++)
            {
            dc.StretchBlt(0+10*snake.body[i].j,0+10*snake.body[i].i,10,10,&dcmemory,0,0,48,48,SRCCOPY);
            }
            bitmap.Detach();   


    }
    CView::OnTimer(nIDEvent);
}

void CMyView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
    // TODO: Add your message handler code here and/or call default
              if(nChar==VK_UP && snake.direction!=3)
        {
        snake.direction=1;
        }
                if(nChar==VK_RIGHT && snake.direction!=4)
        {
        snake.direction=2;
        }

                    if(nChar==VK_DOWN && snake.direction!=1)
        {
        snake.direction=3;
        }
                  if(nChar==VK_LEFT && snake.direction!=2)
        {
        snake.direction=4;
        }


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

bool CMyView::CSnakesnake()
{
 bool flag=false;
 for(int i=0;i<snake.n;i++)
 {
    if(snake.body[i].i== bean.i&& bean.j==snake.body[i].j)
    {
flag=true;
    break;
    }
 }
 return flag;
}

bool CMyView::Issnakeeat()
{
    if(snake.body[snake.n].i==bean.i && snake.body[snake.n].j==bean.j)
    {
    return true;
    }
    else
    {
    return false;
    }
}
搜索更多相关主题的帖子: 贪吃蛇 include 而且 
2012-09-06 20:38
快速回复:小弟新手 求助一贪吃蛇问题,蛇吃了豆子为什么没有变化,而且随机的豆 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016572 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved