⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 +Ȧ

📁 迷宫源代码
💻
字号:
// GameWnd.cpp : implementation file
//

//#include "stdafx.h"
#include "Game32.h"
#include "GameWnd.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// GameWnd

IMPLEMENT_DYNCREATE(GameWnd, CFrameWnd)
int i;
int dir,index;
int x,y;
int maze[8][8]={
	1,1,1,1,1,1,2,1,
    3,0,0,1,0,0,0,1,
	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 n,m;
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 number=0; 
GameWnd::GameWnd()
{
	Create(NULL,"创建窗口",WS_OVERLAPPEDWINDOW,CRect(0,0,864,928));
	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,108,116,LR_LOADFROMFILE);
		}
	}
	//bitmap=new CBitmap;
	//bitmap->m_hObject=LoadImage(NULL,"1.bmp",IMAGE_BITMAP,224,306,LR_LOADFROMFILE);
	mdc=new CDC;
	CClientDC dc(this);
	mdc->CreateCompatibleDC(&dc);
	mdc->SelectObject(bitmap[0][0]);
	for(i=0;i<8;i++)
	{
		for(int j=0;j<8;j++)
		{
			if(maze[i][j]==2)
			{
				m=i;
				n=j;
				break;
			}

		}
	}
    wall=new CBitmap;
	wall->m_hObject=LoadImage(NULL,"wall.bmp",IMAGE_BITMAP,108,116,LR_LOADFROMFILE);
    i=0;
	dir=0;
	index=0;
	x=0,y=0;
    ptr=(list*)malloc(sizeof(list));
	ptr->m=m;
	ptr->n=n;
	ptr->bitmap=bitmap[0][0];
	ptr->next=NULL;
	ptr->back=NULL;
	first=ptr;


}

GameWnd::~GameWnd()
{
}


BEGIN_MESSAGE_MAP(GameWnd, CFrameWnd)
	//{{AFX_MSG_MAP(GameWnd)
	ON_WM_PAINT()
	ON_WM_CREATE()
	ON_WM_TIMER()
	ON_WM_KEYDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// GameWnd message handlers

void GameWnd::OnPaint() 
{
//	CPaintDC dc(this); // device context for painting
	CPaintDC dc(this);
	// TODO: Add your message handler code here
//	if(i++<=300)
//	{
//	   dc.BitBlt(0+i,0,224,306,mdc,0,0,SRCCOPY);
//	}
//	else
//		i=0;

	// Do not call CFrameWnd::OnPaint() for painting messages
}

int GameWnd::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	SetClassLong(m_hWnd, GCL_HBRBACKGROUND, (LONG)GetStockObject(BLACK_BRUSH));//改背景色为黑色
	SetTimer(1,100,NULL);
	return 0;
}

void GameWnd::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
/*	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,108,116,mdc,0,0,SRCCOPY);*/

	if(start)
	{
		Start();
	}
	else
	{
		if(go)
		{
			Go();
		}
	}
	CFrameWnd::OnTimer(nIDEvent);


}

void GameWnd::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	CClientDC dc(this);
	dc.BitBlt(n*108+x,m*116+y,108,116,mdc,0,0,BLACKNESS);	//消去残影
	if(nChar==VK_DOWN)
	{
		y+=116;
		k=(n*108+x)/108;
		p=(m*116+y)/116;
		if(maze[p][k]==1)
		{
			y-=116;
		}
		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-=116;
	    k=(n*108+x)/108;
		p=(m*116+y)/116;
		if(maze[p][k]==1)
		{
			y+=116;
		}
		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;
	}
	if(nChar==VK_LEFT)
	{
		x-=108;
	    k=(n*108+x)/108;
		p=(m*116+y)/116;
		if(maze[p][k]==1)
		{
			x+=108;
		}
		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+=108;
	    k=(n*108+x)/108;
		p=(m*116+y)/116;
		if(maze[p][k]==1)
		{
			x-=108;
		}
		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;
	}
		k=(n*108+x)/108;
		p=(m*116+y)/116;
		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(n*108+x,m*116+y,108,116,mdc,0,0,SRCCOPY);			
	
	CFrameWnd::OnKeyDown(nChar, nRepCnt, nFlags);
}

void GameWnd::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*108,i*116,108,116,mdc,0,0,SRCCOPY);
			}
		}
	}
	mdc->SelectObject(bitmap[0][0]);
	dc.BitBlt(n*108,m*116,108,116,mdc,0,0,SRCCOPY);
	start=false;
}

void GameWnd::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*108,i*116,108,116,mdc,0,0,SRCCOPY);
			}
		}
	}
	mdc->SelectObject(first->bitmap);
	if(number==0)
	{
		dc.BitBlt(first->n*108+first->x,first->m*116+first->y,108,116,mdc,0,0,SRCCOPY);
	}
	else
	{
	    dc.BitBlt(first->back->n*108+first->back->x,first->back->m*116+first->back->y,108,116,mdc,0,0,BLACKNESS);
	    dc.BitBlt(first->n*108+first->x,first->m*116+first->y,108,116,mdc,0,0,SRCCOPY);
	}
	
	number++;
	if(first->next==NULL)
	{
		go=false;
	}
	else
	{
        first=first->next;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -