| 网站首页 | 业界新闻 | 群组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 编程论坛
大量收QQ微信精准粉/交友粉,非诚勿扰千里之行 始于足下
共有 242 人关注过本帖
标题:贪吃蛇程序出了问题,求大神指教
只看楼主 加入收藏
念星
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2017-12-28
  问题点数:0  回复次数:0   
贪吃蛇程序出了问题,求大神指教

// 2323View.cpp : CMy2323View 类的实现
//

#include "stdafx.h"
// SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的
// ATL 项目中进行定义,并允许与该项目共享文档代码。
#ifndef SHARED_HANDLERS
#include "2323.h"
#endif

#include "2323Doc.h"
#include "2323View.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#define UP 1
#define DOWN 2
#define LEFT 3
#define RIGHT 4


// CMy2323View

IMPLEMENT_DYNCREATE(CMy2323View, CView)

BEGIN_MESSAGE_MAP(CMy2323View, CView)
    // 标准打印命令
    ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
    ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
    ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CMy2323View::OnFilePrintPreview)
    ON_WM_CONTEXTMENU()
    ON_WM_RBUTTONUP()
    ON_WM_CHAR()
    ON_COMMAND(ID_32771, &CMy2323View::Onlevel1)
    ON_COMMAND(ID_32772, &CMy2323View::Onlevel2)
    ON_COMMAND(ID_32773, &CMy2323View::Onlevel3)
    ON_WM_TIMER()
END_MESSAGE_MAP()

// CMy2323View 构造/析构

CMy2323View::CMy2323View()
    : Snakelength(0)
    , head(0)
    , EndFlag(0)

    , level(0)
{
    // TODO: 在此处添加构造代码
    Snakelength=4;
    wall.SetRect(100,100,700,500);
    food.SetRect(0,0,0,0);
    int i;
    for(i=0;i<Snakelength;i++)
        SnakeBody[i].SetRect(200+i*5,200,205+i*5,205);
    head=LEFT;
    FoodFlag=1;
//    FoodFlag=1;
    level=0;

}

CMy2323View::~CMy2323View()
{
;

}

BOOL CMy2323View::PreCreateWindow(CREATESTRUCT& cs)
{
    // TODO: 在此处通过修改
    //  CREATESTRUCT cs 来修改窗口类或样式

    return CView::PreCreateWindow(cs);
}

// CMy2323View 绘制

void CMy2323View::OnDraw(CDC* pDC)
{
    CMy2323Doc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    if (!pDoc)
        return;
    // TODO: 在此处为本机数据添加绘制代码
    DrawWall(pDC);
    Drawsnake(pDC);
    if(FoodFlag==1)
    {
        SetFood();
        Drawfood(pDC);
        FoodFlag=0;
    }

}


// CMy2323View 打印


void CMy2323View::OnFilePrintPreview()
{
#ifndef SHARED_HANDLERS
    AFXPrintPreview(this);
#endif
}

BOOL CMy2323View::OnPreparePrinting(CPrintInfo* pInfo)
{
    // 默认准备
    return DoPreparePrinting(pInfo);
}

void CMy2323View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
    // TODO: 添加额外的打印前进行的初始化过程
}

void CMy2323View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
    // TODO: 添加打印后进行的清理过程
}

void CMy2323View::OnRButtonUp(UINT /* nFlags */, CPoint point)
{
    ClientToScreen(&point);
    OnContextMenu(this, point);
}

void CMy2323View::OnContextMenu(CWnd* /* pWnd */, CPoint point)
{
#ifndef SHARED_HANDLERS
    theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
#endif
}


// CMy2323View 诊断

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

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

CMy2323Doc* CMy2323View::GetDocument() const // 非调试版本是内联的
{
    ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMy2323Doc)));
    return (CMy2323Doc*)m_pDocument;
}
#endif //_DEBUG


// CMy2323View 消息处理程序


void CMy2323View::DrawWall(CDC* pDC)
{
    pDC->Rectangle(wall);

}


void CMy2323View::Drawfood(CDC* pDC)
{
    CBrush  red;
    red.CreateSolidBrush(RGB(255,0,0));
    pDC->FillRect(food,&red);
    FoodFlag=0;
}


void CMy2323View::Drawsnake(CDC* pDC)
{
    CBrush blue;
    blue.CreateSolidBrush(RGB(0,0,225));
    int i;
    for(i=0;i<Snakelength;i++)
        pDC->FillRect(SnakeBody[i],&blue);
}


void CMy2323View::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
    // TODO: 在此添加消息处理程序代码和/或调用默认值
    switch(nChar)
    {
    case 'w':if(head!=DOWN) BodyMove(UP);break;
    case 's':if(head!=UP)  BodyMove(DOWN);break;   
    case 'a':if(head!=RIGHT)  BodyMove(LEFT);break;   
    case 'd':if(head!=LEFT)  BodyMove(RIGHT);break;
    }
    if(EndFlag==1)
        Invalidate(TRUE);

}


int CMy2323View::HitTest(CRect temp)
{
    if(temp.left<100||temp.right>700||temp.top<0||temp.bottom>500)
    return 1;
    if(IsSnake(temp))
    return 1;
    if(temp==food)
    return 2;
    else
    return 0;

}


void CMy2323View::SetFood()
{
    do
    {
    food.left=100+rand()%120;
    food.top=100+rand()%80;
    food.right=food.left+5;
    food.bottom=food.top+5;
    }while(IsSnake(food));
}



int CMy2323View::IsSnake(CRect temp)
{
    int i;
    for(i=0;i<Snakelength;i++)
        if(SnakeBody[i]==temp)
            return 1;
    return 0;
}


void CMy2323View::GameOver(void)
{
CDC*pDC=GetDC();
CRect rect;
GetClientRect(&rect);
CBitmap map;
map.LoadBitmap(IDB_BITMAP1);
CDC memDC;
memDC.CreateCompatibleDC(pDC);
memDC.SelectObject(&map);
pDC->BitBlt(0,0,rect.Width(),rect.Height(),&memDC,0,0,SRCCOPY);
EndFlag=0;
}



void CMy2323View::BodyMove(int h)
{
    CRect temp;
    temp=SnakeBody[0];
    switch(h)
    {
    case UP:temp.top-=5;temp.bottom-=5;break;
    case DOWN:temp.top+=5;temp.bottom+=5;break;
    case LEFT:temp.left-=5;temp.right-=5;break;
    case RIGHT:temp.left+=5;temp.right+=5;break;
}
    int i,j;
    i=HitTest(temp);
    if(i==1)
        GameOver();
    else
    {
        CRect last;
        last=SnakeBody[Snakelength-1];
    for(j=Snakelength-1;j>0;j--)
        SnakeBody[j]=SnakeBody[j-1];
    SnakeBody[0]=temp;
    if(i==2)
    {
        Snakelength++;
        SnakeBody[Snakelength-1]=last;
    FoodFlag=1;}
      head=h;
   
    }}


        void CMy2323View::Onlevel1()
        {
            // TODO: 在此添加命令处理程序代码
            SetTimer(1,100,NULL);
            level=1;
        }


        void CMy2323View::Onlevel2()
        {
            // TODO: 在此添加命令处理程序代码
            SetTimer(2,50,NULL);
            level=2;
        }


        void CMy2323View::Onlevel3()
        {
            // TODO: 在此添加命令处理程序代码
            SetTimer(3,10,NULL);
            level=3;
        }


        void CMy2323View::OnTimer(UINT_PTR nIDEvent)
        {
            // TODO: 在此添加消息处理程序代码和/或调用默认值
            BodyMove(head);
            if(EndFlag==1)
                 Invalidate(TRUE);
            CView::OnTimer(nIDEvent);
        }
2017-12-28 13:31







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

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