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

📄 gameframe.cpp

📁 本人自己设计的rpg游戏
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// gameFrame.cpp : implementation file
//

#include "stdafx.h"
#include "gameapps.h"
#include "gameFrame.h"
#include "bgground.h"
#include "actor.h"

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

/////////////////////////////////////////////////////////////////////////////
// gameFrame

IMPLEMENT_DYNCREATE(gameFrame, CFrameWnd)
int i,d=0,c=0,xp=0,old_map_x,old_map_y; //人物走动参数
int xmon=300,ymon=150,xlast,ylast,xp1,xm,numm,nump,d1,i1,attack=0,go1=1,go2=1,go3=1,go4=1;
int speed,fresh=30;
int dis_map_x,dis_map_y;
int old_dis_x,old_dis_y;
int map_x,map_y;
int snowx,snowy;
Cgoodbox opengoods[40];
CRect selectrect[40];
CRect sbutton[10];
Cactor people[10];
snow snowflake[50];
	CRect hprange;
	CRect mprange;
	int linelength;
	CPen newPen1,newPen2,newPen3,*oldPen;
	CPoint lmousedk;
	

	

BOOL mouse_right=FALSE,mouse_left=FALSE;
BOOL ESCAPE=FALSE,UP=FALSE,DOWN=FALSE,LEFT=FALSE,RIGHT=FALSE,isattack=FALSE,go_on=FALSE,startpic=TRUE,goods=FALSE,drawbutton=TRUE;
BOOL people1_click=FALSE;
BOOL isdismap=TRUE;
BOOL isstart=FALSE;
CPoint mousepoint;
CDC* mdc;
CDC* mdc1;
CDC* bgcdc;
CDC* bgcdc1;
CDC* screendc;
CDC* att[10];
CDC* att1[10];
CBitmap* tempbmp;
CBitmap* tempbmp1;
CBitmap* tempbmp2;
CBitmap* tempbmp3;
CBitmap* tempbmp4;
CBitmap* attackbmp[10];
CBitmap* attackbmp1[10];
CBitmap* m[4];
CBitmap* a[4];
CBitmap* pm[4];
CBitmap* pn[4];
CBitmap* at[4];
CBitmap* tt[4];

CBitmap* drawmap[200];

int thismap[128][128];
int maps_sc_x,maps_sc_y;
int go_map_x,go_map_y;
int raintime=0;
CFile file;
CString msg;
CString sFile("map/1.map");
char filename[15];



	CBitmap* bitmap;
	BITMAP bmInfo;
	CDC* dcComp;
	


monster mon[3];//创建角色
Cman myself;
int count; //怪物数量
int clear=3;
Cman mainman;

gameFrame::gameFrame()
{
	
	RECT rect;
	Create(NULL,"Dreamsoft");
CClientDC dc(this);
	GetWindowRect( &rect );
	go_x=201;
	go_y=200;
	sc_x=150;
	sc_y=150;
	opengoods[0].select=1;
	go_map_x=(go_x+sc_x)/50;
	go_map_y=(go_y+sc_y)/50+1;
	dis_map_x=sc_x/50;
	dis_map_y=sc_y/50;

	mdc=new CDC;
	mdc1=new CDC;
	bgcdc=new CDC;
    bgcdc1=new CDC;
	screendc=new CDC;
	
	dcComp=new CDC;
	screendc->CreateCompatibleDC(&dc);
	bgcdc->CreateCompatibleDC(&dc);
    bgcdc1->CreateCompatibleDC(&dc);
	
	mdc->CreateCompatibleDC(&dc);
	mdc1->CreateCompatibleDC(&dc);
	dcComp->CreateCompatibleDC(&dc);
	tempbmp=new CBitmap;
	tempbmp1=new CBitmap;
	tempbmp2=new CBitmap;
	tempbmp3=new CBitmap;
	tempbmp4=new CBitmap;
	bitmap=new CBitmap;
	tempbmp->CreateCompatibleBitmap(&dc,1000,600);
	tempbmp1->CreateCompatibleBitmap(&dc,1000,600);
	tempbmp2->CreateCompatibleBitmap(&dc,1000,600);
	tempbmp3->CreateCompatibleBitmap(&dc,1000,600);
	tempbmp4->CreateCompatibleBitmap(&dc,1000,600);
	

		att[0]=new CDC;
		att1[0]=new CDC;
		attackbmp[0]=new CBitmap;
		attackbmp1[0]=new CBitmap;
		att[0]->CreateCompatibleDC(&dc);
		att1[0]->CreateCompatibleDC(&dc);
		attackbmp[0]->CreateCompatibleBitmap(&dc,100,80);
		attackbmp1[0]->CreateCompatibleBitmap(&dc,100,80);
	
		att[0]->SelectObject(attackbmp[0]);
		att1[0]->SelectObject(attackbmp1[0]);



		att[1]=new CDC;
		att1[1]=new CDC;
		attackbmp[1]=new CBitmap;
		attackbmp1[1]=new CBitmap;
		att[1]->CreateCompatibleDC(&dc);
		att1[1]->CreateCompatibleDC(&dc);
		attackbmp[1]->m_hObject=(HBITMAP)::LoadImage(NULL,"actor/a1.bmp",IMAGE_BITMAP,100,80,LR_LOADFROMFILE);
		attackbmp1[1]->m_hObject=(HBITMAP)::LoadImage(NULL,"actor/b1.bmp",IMAGE_BITMAP,100,80,LR_LOADFROMFILE);
	
		att[1]->SelectObject(attackbmp[1]);
		att1[1]->SelectObject(attackbmp1[1]);
///////////////////////////////////背景
		att[2]=new CDC;
		attackbmp[2]=new CBitmap;
		att[2]->CreateCompatibleDC(&dc);
		attackbmp[2]->CreateCompatibleBitmap(&dc,1024,150);
		att[2]->SelectObject(attackbmp[2]);

		att1[2]=new CDC;
		attackbmp1[2]=new CBitmap;
		att1[2]->CreateCompatibleDC(&dc);
		attackbmp1[2]->m_hObject=(HBITMAP)::LoadImage(NULL,"bg/1.bmp",IMAGE_BITMAP,24,600,LR_LOADFROMFILE);
		att1[2]->SelectObject(attackbmp1[2]);
///////////////////////////////////////////
/////////////////////////////////雪花
			att1[3]=new CDC;
		attackbmp1[3]=new CBitmap;
		att1[3]->CreateCompatibleDC(&dc);
		attackbmp1[3]->m_hObject=(HBITMAP)::LoadImage(NULL,"bg/SNOW.bmp",IMAGE_BITMAP,24,600,LR_LOADFROMFILE);
		att1[3]->SelectObject(attackbmp1[3]);
			att[3]=new CDC;
		attackbmp[3]=new CBitmap;
		att[3]->CreateCompatibleDC(&dc);
		attackbmp[3]->m_hObject=(HBITMAP)::LoadImage(NULL,"bg/MASK.bmp",IMAGE_BITMAP,24,600,LR_LOADFROMFILE);
		att[3]->SelectObject(attackbmp[3]);

//////////////////
//////////////////////////开始画
		att[4]=new CDC;
		attackbmp[4]=new CBitmap;
		att[4]->CreateCompatibleDC(&dc);
		attackbmp[4]->CreateCompatibleBitmap(&dc,1024,150);
		att[4]->SelectObject(attackbmp[4]);


		///////////////////////////////

	char str[11];
	char strp[13];
	char getinfo[3];
	char attack[16];
	
	for(i=0;i<4;i++)
	{
	sprintf(attack,"attack/at%d.bmp",i);
		at[i]=new CBitmap;
	    at[i]->m_hObject=(HBITMAP)::LoadImage(NULL,attack,IMAGE_BITMAP,100,80,LR_LOADFROMFILE);
	    sprintf(attack,"attack/tt%d.bmp",i);
		tt[i]=new CBitmap;
	    tt[i]->m_hObject=(HBITMAP)::LoadImage(NULL,attack,IMAGE_BITMAP,100,80,LR_LOADFROMFILE);


	}
	for(i=0;i<4;i++)
	{
		
		sprintf(str,"ren/mm%d.bmp",i);
		m[i]=new CBitmap;
		m[i]->m_hObject=(HBITMAP)::LoadImage(NULL,str,IMAGE_BITMAP,963,96,LR_LOADFROMFILE);
		sprintf(str,"ren/aa%d.bmp",i);
		a[i]=new CBitmap;
		a[i]->m_hObject=(HBITMAP)::LoadImage(NULL,str,IMAGE_BITMAP,963,96,LR_LOADFROMFILE);
		sprintf(strp,"guai/MM%d.bmp",i);
		pm[i]=new CBitmap;
	    pm[i]->m_hObject=(HBITMAP)::LoadImage(NULL,strp,IMAGE_BITMAP,544,82,LR_LOADFROMFILE);
	    sprintf(strp,"guai/M%d.bmp",i);
		pn[i]=new CBitmap;
	    pn[i]->m_hObject=(HBITMAP)::LoadImage(NULL,strp,IMAGE_BITMAP,544,82,LR_LOADFROMFILE);
		
		

	}
	
	sbutton[0].top=20;
	sbutton[0].bottom=sbutton[0].top+25;
	sbutton[0].left=700;
      sbutton[0].right=sbutton[0].left+60;
	for(i=1;i<10;i++)
	{
		sbutton[i].top=sbutton[i-1].top;
	sbutton[i].bottom=sbutton[i].top+30;
	sbutton[i].left=sbutton[i-1].left+32;
      sbutton[i].right=sbutton[i].left+30;
		
		
	}
screendc->SelectObject(tempbmp);
mdc->SelectObject(tempbmp4);
		mdc1->SelectObject(tempbmp1);
		bgcdc->SelectObject(tempbmp2);
		bgcdc1->SelectObject(tempbmp3);
	


	
	mdc->SelectObject(m[0]);
	mdc1->SelectObject(a[0]);

	
	dcComp->SelectObject(tempbmp4);
	




	if(!file.Open(sFile,CFile::modeRead))
	{
		msg.Format("faile to open file %s",sFile);
		AfxMessageBox(msg);
	}

	file.Read(getinfo,3);
	maps_sc_x=atoi(getinfo);

	file.Read(getinfo,3);
	maps_sc_y=atoi(getinfo);
	for(int xmap=0;xmap<maps_sc_x;xmap++)
{
	for(int ymap=0;ymap<maps_sc_y;ymap++)
	{
		
		file.Read(getinfo,3);
		thismap[ymap][xmap]=atoi(getinfo);
		
	}
}






file.Close();





for(i=0;i<50;i++)//拼地图
	{sprintf(strp,"mapcon/%d.bmp",i);
	drawmap[i]=new CBitmap;
	drawmap[i]->m_hObject=(HBITMAP)::LoadImage(NULL,strp,IMAGE_BITMAP,51,51,LR_LOADFROMFILE);
	}



	for(int map_x=0;map_x<21;map_x++)
	{
		for(int map_y=0;map_y<13;map_y++)
		{
			for(count=0;count<=50;count++)
			{
			if(thismap[maps_sc_y+map_y][maps_sc_x+map_x]==count)
			{
				bgcdc->SelectObject(drawmap[count]);
				dcComp->BitBlt(map_x*50,map_y*50,51,51,bgcdc,0,0,SRCCOPY);
			}
			}
		

		}
	}
//初始化怪物
	
	
	for(i=0;i<3;i++)
	{
	mon[i].p1.x=rand()%2048+2048;
	mon[i].p1.y=rand()%1024+2048;
	mon[i].hp=200;
	mon[i].alive=true;
	mon[i].isbeat=false;
	
	}

people[0].point.x=650;
people[0].point.y=400;	
people[0].actorect.top=people[0].point.y;
people[0].actorect.bottom=people[0].actorect.top+60;
people[0].actorect.left=people[0].point.x;
people[0].actorect.right=people[0].actorect.left+60;

	myself.alive=true;
	myself.attack=FALSE;
	myself.hp=500;
	myself.mp=800;
	myself.money=0;





hprange.top=620;
	hprange.bottom=hprange.top+18;
	hprange.left=100;
	hprange.right=hprange.left+151;

	mprange.top=640;
	mprange.bottom=mprange.top+18;
	mprange.left=100;
	mprange.right=mprange.left+151;

	newPen1.CreatePen(NULL,1,RGB(255,0,0));
	newPen2.CreatePen(NULL,1,RGB(0,0,255));
	newPen3.CreatePen(NULL,1,RGB(80,80,80));



dc.TextOut(100,250,"Welcome to play my first game!!!");









	
}

gameFrame::~gameFrame()
{
}


BEGIN_MESSAGE_MAP(gameFrame, CFrameWnd)
	//{{AFX_MSG_MAP(gameFrame)
	ON_WM_PAINT()
	ON_WM_KEYDOWN()
	ON_WM_CREATE()
	ON_WM_RBUTTONDOWN()
	ON_WM_RBUTTONUP()
	ON_WM_TIMER()
	ON_WM_MOUSEMOVE()
	ON_WM_KEYUP()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_LBUTTONDBLCLK()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// gameFrame message handlers



void gameFrame::OnPaint() 
{
	CPaintDC dc(this); // device context for painting


	// TODO: Add your message handler code here
	
	// Do not call CFrameWnd::OnPaint() for painting messages
	if(isstart)
	{
	
	dc.BitBlt(0,0,1024,668,screendc,0,0,SRCCOPY);
	char str[12];

	CBrush brushGrid(RGB(25,25,255));
	CBrush* pOldBrush=att[2]->SelectObject(&brushGrid);
	att[2]->SetTextColor(RGB(30,255,30));
	att[2]->SetBkColor(RGB(0,0,0));

	sprintf(str,"生命值:%d",myself.hp);
	att[2]->TextOut(10,45,str);
	sprintf(str,"魔法值:%d",myself.mp);
	att[2]->TextOut(10,65,str);
	
	att[2]->TextOut(300,20,"当前坐标");
	sprintf(str,"X:%d   ",go_map_x);
	att[2]->TextOut(380,20,str);
	sprintf(str,"Y:%d   ",go_map_y);
	att[2]->TextOut(420,20,str);


	sprintf(str,"地图大小X:%d  ",maps_sc_x);
	att[2]->TextOut(500,20,str);
	sprintf(str,"Y:%d  ",maps_sc_y);
	att[2]->TextOut(610,20,str);

	att[2]->SetTextColor(RGB(30,255,30));
	att[2]->SetBkColor(RGB(0,0,255));
	att[2]->Rectangle(sbutton[0]);
	att[2]->TextOut(sbutton[0].left+15,sbutton[0].top+2,"物品");
	
for(linelength=0;linelength<16;linelength++)
	{
	
	
	oldPen=att[2]->SelectObject(&newPen3);
	
	att[2]->MoveTo(101,45+linelength);
	att[2]->LineTo(100+150,45+linelength);
	
    
	att[2]->MoveTo(101,65+linelength);
	att[2]->LineTo(100+150,65+linelength);
	}

	for(linelength=0;linelength<16;linelength++)
	{
	
	
	oldPen=att[2]->SelectObject(&newPen1);
	
	att[2]->MoveTo(101,45+linelength);
	att[2]->LineTo(100+150*myself.hp/500,45+linelength);
	oldPen=att[2]->SelectObject(&newPen2);
    
	att[2]->MoveTo(101,65+linelength);
	att[2]->LineTo(100+150*myself.mp/800,65+linelength);
	}

	oldPen=att[2]->SelectObject(&newPen3);
	att[2]->MoveTo(0,1);
	att[2]->LineTo(1024,1);
	att[2]->SelectObject(pOldBrush);
	sixbox();
dc.BitBlt(0,601,1024,150,att[2],0,0,SRCCOPY);
dc.BitBlt(1001,0,24,600,att1[2],0,0,SRCCOPY);
ReleaseDC(att[2]);
	}

	else
	{
		isbegin();
		dc.BitBlt(0,100,1024,150,att[4],0,0,SRCCOPY);
	}
	


}

void gameFrame::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	
		if(nChar==VK_UP)
	UP=TRUE;
	if(nChar==VK_DOWN)
    DOWN=TRUE;
	if(nChar==VK_LEFT)
    LEFT=TRUE;
	if(nChar==VK_RIGHT)
	RIGHT=TRUE;
	if(nChar==VK_F1)
	isstart=TRUE;
	if(nChar==VK_ESCAPE)
	{ESCAPE=TRUE;
	PostMessage(WM_CLOSE);
	}
	
	
	
	Invalidate(FALSE);
	CFrameWnd::OnKeyDown(nChar, nRepCnt, nFlags);
}





int gameFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	SetTimer(1,300,NULL);
	SetTimer(2,500,NULL);
	SetTimer(3,300,NULL);
	SetTimer(4,100,NULL);
	SetTimer(5,500,NULL);
	SetTimer(6,1000,NULL);
	return 0;
}



void gameFrame::OnRButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	mouse_right=TRUE;
	if(mouse_right){
	mousepoint.x=point.x;
	mousepoint.y=point.y;

	
	}

	CFrameWnd::OnRButtonDown(nFlags, point);
}


void gameFrame::OnRButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	mouse_right=FALSE;
	CFrameWnd::OnRButtonUp(nFlags, point);
}

void gameFrame::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	
	CClientDC dc(this);
if(isdismap)
{
	for(int map_x=0;map_x<21;map_x++)
	{
		for(int map_y=0;map_y<13;map_y++)
		{
			for(count=0;count<=50;count++)
			{
			if(thismap[dis_map_y+map_y][dis_map_x+map_x]==count)
			{
				bgcdc->SelectObject(drawmap[count]);
				dcComp->BitBlt((map_x)*50,(map_y)*50,51,51,bgcdc,0,0,SRCCOPY);
			}
			}
		

		}
	}



isdismap=FALSE;
}
	
	//鼠标移动
if(nIDEvent==6)
{if(thismap[go_map_y][go_map_x]==5)
		Load(filename);
}
	
if(nIDEvent==3)
{
	old_dis_x=dis_map_x;
	old_dis_y=dis_map_y;
	
	if(mouse_right)
	{
	
		
		if(go_map_x>=0&&go_map_x<maps_sc_x&&go_map_y>=0&&go_map_y<maps_sc_y)
		{




			if(go_y-mousepoint.y>40&&abs(mousepoint.y-go_y)>abs(mousepoint.x-go_x))
	{	
			mdc->SelectObject(m[0]);

		if(sc_y>=0&&go_y>0)
		{

			if(thismap[go_map_y-1][go_map_x]<6||(go_map_y-1)*51<go_y)
			{
	if(go_y>200)
	{
	    go_y=go_y-50;
        
		go_map_y=(go_y+sc_y)/50+1;
	}
	else if(sc_y>0&&go_y<=200)
	{
		
		sc_y=sc_y-50;
		go_map_y=(go_y+sc_y)/50+1;
		
	}
	else 
	{
		go_y=go_y-50;
				
		go_map_y=(go_y+sc_y)/50+1;
	}
	

			}
			dis_map_x=sc_x/50;
	dis_map_y=sc_y/50;

	
		
		
		
		if(d==0)
		{
			c=c+1;
			if(c>=9)
			c=2;
		}
		else 
			c=2;
		d=0;
		xp=96*c;
		}
	    
			mdc1->SelectObject(a[0]);
		
		
	}
			
	if(mousepoint.y-go_y>120&&abs(mousepoint.y-go_y)>abs(mousepoint.x-go_x))
	{
			mdc->SelectObject(m[3]);
	
		if(sc_y<128*50&&go_y<450)
		{

			if(thismap[go_map_y+1][go_map_x]<6)
			{
		if(go_y+50<450)
		{
			go_y=go_y+50;
		go_map_y=(go_y+sc_y)/50+1;
		}
		else
		{
			sc_y=sc_y+50;
			
		go_map_y=(go_y+sc_y)/50+1;
	
		
		}
			}
		
	dis_map_x=sc_x/50;
	dis_map_y=sc_y/50;

			
		
			if(d==3)
		{
			c=c+1;
			if(c>=9)
			c=2;
		}
		else 
			c=2;
		d=3;
		xp=96*c;
		}
	
		mdc1->SelectObject(a[3]);
		
		
	}
	
	if(go_x-mousepoint.x>40&&abs(mousepoint.y-go_y)<abs(mousepoint.x-go_x))
	{
		mdc->SelectObject(m[2]);
		if(sc_x>=0&&go_x>0)
		{

⌨️ 快捷键说明

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