// movieWnd.cpp : implementation file // //#include "stdafx.h" #include "movie.h" #include "movieWnd.h" #include #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // movieWnd IMPLEMENT_DYNCREATE(movieWnd, CFrameWnd) int x=0,y=0; int maze[8][8]={ 1,1,1,1,1,1,2,1, //第一行,值为2的元素代表入口 3,0,0,1,0,0,0,1, //第二行,值为3的元素代表出口 1,1,0,0,0,1,1,1, //第三行 1,0,0,1,0,0,0,1, //第四行 1,1,1,1,0,1,1,1, //第五行 1,0,0,0,0,0,0,1, //第六行 1,0,1,0,1,0,0,1, //第七行 1,1,1,1,1,1,1,1 //第八行 }; int m,n; int k,p; BOOL Start=true; struct list { int m; int n; int x; int y; struct list*next; struct list*back; CBitmap*bitmap; }; list*ptr,*preptr,*first; BOOL go=false; //int lastm,lastn; int number=0; movieWnd::movieWnd() { Create(NULL,"绘图视窗",WS_OVERLAPPEDWINDOW,CRect(0,0,320,320));// char ch[8]; for (int i=0;i<4;i++) { for (int j=0;j<4;j++) { sprintf(ch,"%d.%d.bmp",i+1,j+1); bitmap[i][j]=new CBitmap; bitmap[i][j]->m_hObject=LoadImage(NULL,ch,IMAGE_BITMAP,40,40,LR_LOADFROMFILE); } } mdc=new CDC; CClientDC dc(this); mdc->CreateCompatibleDC(&dc); mdc->SelectObject(bitmap[0][0]); dir=0,index=0; for( i=0;i<8;i++) { for (int j=0;j<8;j++) { if (maze[i][j]==2) { m=i; n=j; break; } } } ptr=(list*)malloc(sizeof(list)); ptr->m=m; ptr->n=n; ptr->next=NULL; ptr->back=NULL; //ptr->bitmap=new CBitmap; ptr->bitmap=bitmap[0][0]; first=ptr; wall=new CBitmap; wall->m_hObject=LoadImage(NULL,"wall.bmp",IMAGE_BITMAP,40,40,LR_LOADFROMFILE); } movieWnd::~movieWnd() { } BEGIN_MESSAGE_MAP(movieWnd, CFrameWnd) //{{AFX_MSG_MAP(movieWnd) ON_WM_PAINT() ON_WM_CREATE() ON_WM_TIMER() ON_WM_KEYDOWN() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // movieWnd message handlers void movieWnd::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: Add your message handler code here //dc.BitBlt(0,0,40,40,mdc,0,0,SRCCOPY); // Do not call CFrameWnd::OnPaint() for painting messages } int movieWnd::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CFrameWnd::OnCreate(lpCreateStruct) == -1) return -1; // TODO: Add your specialized creation code here SetTimer(1,40,NULL); return 0; } void movieWnd::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default if (Start) { start(); } else { if (go) { Go(); } } /* CClientDC dc(this); if (dir>=4) { dir=0; } if (index<4) { mdc->SelectObject(bitmap[dir][index]); index++; } else { index=0; dir++; mdc->SelectObject(bitmap[dir][index]); } dc.BitBlt(0,0,40,40,mdc,0,0,SRCCOPY); */ CFrameWnd::OnTimer(nIDEvent); } void movieWnd::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) { // TODO: Add your message handler code here and/or call default CClientDC dc(this); dc.BitBlt(n*40+x,m*40+y,40,40,mdc,0,0,WHITENESS); if (nChar==VK_DOWN) { y+=40; k=(int)floor((n*40+x)/40); p=(int)floor((m*40+y)/40); if (maze[p][k]==1) { y-=40; } if (dir==0) { index++; if (index==4) { index=0; } } else { index=0; dir=0; } ptr->next=(list*)malloc(sizeof(list)); ptr->next->m=m; ptr->next->n=n; ptr->next->x=x; ptr->next->y=y; ptr->next->bitmap=bitmap[dir][index]; preptr=ptr; ptr->next->next=NULL; ptr=ptr->next; ptr->back=preptr; } if (nChar==VK_UP) { y-=40; k=((n*40+x)/40); p=((m*40+y)/40); if (maze[p][k]==1) { y+=40; //return; } if (dir==3) { index++; if (index==4) { index=0; } } else { index=0; dir=3; } ptr->next=(list*)malloc(sizeof(list)); ptr->next->m=m; ptr->next->n=n; ptr->next->x=x; ptr->next->y=y; ptr->next->bitmap=bitmap[dir][index]; preptr=ptr; ptr->next->next=NULL; ptr=ptr->next; ptr->back=preptr; } if (nChar==VK_LEFT) { x-=40; k=((n*40+x)/40); p=((m*40+y)/40); if (maze[p][k]==1) { x+=40; //return; } if (dir==1) { index++; if (index==4) { index=0; } } else { index=0; dir=1; } ptr->next=(list*)malloc(sizeof(list)); ptr->next->m=m; ptr->next->n=n; ptr->next->x=x; ptr->next->y=y; ptr->next->bitmap=bitmap[dir][index]; preptr=ptr; ptr->next->next=NULL; ptr=ptr->next; ptr->back=preptr; } if (nChar==VK_RIGHT) { x+=40; k=((n*40+x)/40); p=((m*40+y)/40); if (maze[p][k]==1) { x-=40; //return; } if (dir==2) { index++; if (index==4) { index=0; } } else { index=0; dir=2; } ptr->next=(list*)malloc(sizeof(list)); ptr->next->m=m; ptr->next->n=n; ptr->next->x=x; ptr->next->y=y; ptr->next->bitmap=bitmap[dir][index]; preptr=ptr; ptr->next->next=NULL; ptr=ptr->next; ptr->back=preptr; } k=(int)floor((n*40+x)/40); p=(int)floor((m*40+y)/40); if (maze[p][k]==3) { dc.TextOut(120,350,"出去了"); ptr->next=(list*)malloc(sizeof(list)); ptr->next->m=m; ptr->next->n=n; ptr->next->x=x; ptr->next->y=y; ptr->next->bitmap=bitmap[dir][index]; preptr=ptr; ptr->next->next=NULL; ptr=ptr->next; ptr->back=preptr; go=true; return; } mdc->SelectObject(bitmap[dir][index]); dc.BitBlt(ptr->n*40+ptr->x,ptr->m*40+ptr->y,40,40,mdc,0,0,SRCCOPY); CFrameWnd::OnKeyDown(nChar, nRepCnt, nFlags); } void movieWnd::start() { CClientDC dc(this); mdc->SelectObject(wall); for (int i=0;i<8;i++) { for (int j=0;j<8;j++) { if (maze[i][j]==1) { dc.BitBlt(j*40,i*40,40,40,mdc,0,0,SRCCOPY); } } } //mdc->SelectObject(bitmap[0][0]); mdc->SelectObject(ptr->bitmap); dc.BitBlt(ptr->n*40,ptr->m*40,40,40,mdc,0,0,SRCCOPY); Start=false; // lastm=ptr->m; // lastn=ptr->n; } void movieWnd::Go() { CClientDC dc(this); mdc->SelectObject(wall); for (int i=0;i<8;i++) { for (int j=0;j<8;j++) { if (maze[i][j]==1) { dc.BitBlt(j*40,i*40,40,40,mdc,0,0,SRCCOPY); } } } mdc->SelectObject(first->bitmap); if (number==0) { dc.BitBlt(first->n*40+first->x,first->m*40+first->y,40,40,mdc,0,0,SRCCOPY); } else { dc.BitBlt(first->back->n*40+first->back->x,first->back->m*40+first->back->y,40,40,mdc,0,0,WHITENESS); dc.BitBlt(first->n*40+first->x,first->m*40+first->y,40,40,mdc,0,0,SRCCOPY); } number++; if (first->next==NULL) { go=false; } else first=first->next; }