📄 gamemap.cpp
字号:
#include "stdafx.h"
#include "gamemap.h"
#include "stdio.h"
#include "stdlib.h"
#include "ctype.h"
#include "math.h"
#include "filereport.h"
#include "bitmaptool.h"
extern MYBITMAP bBombAni;
//FILEREPORT fgame("detect.txt");
#define ISTEXT(x) ((x)>=32 && (x)<=126)
#define DISTANCE(x,y,x2,y2) (((x)-(x2))*((x)-(x2))+((y)-(y2))*((y)-(y2)))
int CmpDistance(int x,int y,int x2,int y2,int l)
{
if(DISTANCE(x,y,x2,y2)<=l*l)
{
return 1;
}
return 0;
}
int FGetLine(char *pchar,FILE *fp)
{
int ch=-1;
int i=0;
//过滤前面的非法字符
while(!ISTEXT(ch) && !feof(fp))
{
ch=fgetc(fp);
}
if(!ISTEXT(ch))
{
pchar[0]=0;
return 0;
}
while(ISTEXT(ch) && !feof(fp))
{
pchar[i]=ch;
i++;
ch=fgetc(fp);
}
pchar[i]=0;
return 1;
}
int GAMEMAP::LoadMap()
{
char temp[50]={0};
int find=0;
int i;
memset(objectarray,0,sizeof(objectarray));
fp=fopen("ini\\match.txt","r");
if(!fp)
{
return 0;
}
while(!find && !feof(fp))
{
FGetLine(temp,fp);
if(temp[0]=='*' && temp[1]=='0'+iGameMatch)
{
find=1;
}
}
if(!find)
{
return 0;
}
//找到了某一关的地图数据
FGetLine(temp,fp);
iMoneyDest=atoi(temp);
i=0;
FGetLine(temp,fp);
while(temp[0]!='#' && !feof(fp))
{
sscanf(temp,"%d %d %d",&objectarray[i].id,&objectarray[i].x,&objectarray[i].y);
i++;
FGetLine(temp,fp);
}
iObjectNum=i;
fclose(fp);
return 1;
}
GAMEMAP::GAMEMAP()
{
//各个物体的检测半径,检测圆心坐标(偏移量)
GAMEOBJECT temp[]={
{14,14,14},
{25,25,25},
{30,30,30},
{23,23,23},
{14,14,14}
};
int tempspeed[]={6,4,2,4,6};
int tempvalue[]={50,150,500,15,600};
iMoneyDest=0;
iObjectNum=0;
iCatchId=-1;
//物体的碰撞检测
memset(objecthit,0,sizeof(objecthit));
memcpy(objecthit,temp,sizeof(temp));
memset(object_speed,0,sizeof(object_speed));
memcpy(object_speed,tempspeed,sizeof(tempspeed));
memset(object_value,0,sizeof(object_value));
memcpy(object_value,tempvalue,sizeof(tempvalue));
//控制过关动画
AniEnd.active=0;
AniEnd.i=0;
iMatchNum=0;
iGameMatch=0;
iFireNum=0;
}
GAMEMAP::~GAMEMAP()
{
}
int GAMEMAP::DetectCatch(int x,int y,int iangle)
{
int i;
int j;
int x0,y0; //中点
int tempx,tempy;
int xoff,yoff;
//将爪子边上的直线5等分,依次取点比较
int point[]={0,2,4,-2,-4};
if(iCatchId!=-1)
{
return 0;//当前已经获得了物品
}
xoff=8*cos(10*iangle*1.57/90);
yoff=8*sin(10*iangle*1.57/90);
x0=x+8*xoff;//中点坐标
y0=y+8*yoff;
for(i=0;i<iObjectNum;i++)
{
//如果当前物品已经取走,继续判断下一个
if(0==objectarray[i].id)
continue;
for(j=0;j<5;j++)
{
tempx=x0+point[j]*yoff;
tempy=y0-point[j]*xoff;
if(CmpDistance(objectarray[i].x+objecthit[objectarray[i].id-1].x,
objectarray[i].y+objecthit[objectarray[i].id-1].y,
tempx,tempy,
objecthit[objectarray[i].id-1].id))
{
iCatchId=i;
return 1;
}
}
}
return 0;
}
//入参:叉子的坐标和角度
void GAMEMAP::MoveObject(int x,int y,int iangle)
{
if(-1==iCatchId)
return;
objectarray[iCatchId].x=x+20*cos(iangle*10*1.59/90);
objectarray[iCatchId].y=y+20*sin(iangle*10*1.59/90);
if(y<=58)
{
//增加钱币
iMoneyNow+=object_value[objectarray[iCatchId].id-1];
//一个物品拉到目的地
objectarray[iCatchId].id=0;
iCatchId=-1;
}
}
void GAMEMAP::DrawFrame()
{
int i;
for(i=0;i<iObjectNum;i++)
{
if(objectarray[i].id<=0)
continue;
Ellipse(hdc,objectarray[i].x,objectarray[i].y,
objectarray[i].x+2*objecthit[objectarray[i].id-1].x,
objectarray[i].y+2*objecthit[objectarray[i].id-1].y);
}
}
void GAMEMAP::SetSpeedBack(GAMECATCH &c)
{
c.iSpeedBack=object_speed[objectarray[iCatchId].id-1];
}
void GAMEMAP::ShowMoneyNow()
{
char temp[50]={0};
SelectObject(hdc,hf[0]);
SetBkColor(hdc,GetPixel(hdc,19,19));
SetTextColor(hdc,RGB(0,0,0));
sprintf(temp,"money: %d ",iMoneyNow);
TextOut(hdc,20,20,temp,strlen(temp));
sprintf(temp,"goal: %d ",iMoneyDest);
TextOut(hdc,20,45,temp,strlen(temp));
}
void GAMEMAP::GameEnd()
{
char *temp[]={"恭禧进入下一关","游戏失败","恭禧全部过关"};
HPEN hpen;
HBRUSH hbrush;
SIZE size;
//屏幕动画
if(!AniEnd.active)
{
AniEnd.active=1;
AniEnd.i=0;
}
else
{
AniEnd.i++;
}
hpen=(HPEN)GetStockObject(WHITE_PEN);
hbrush=(HBRUSH)GetStockObject(WHITE_BRUSH);
SelectObject(hdc,hpen);
SelectObject(hdc,hbrush);
Rectangle(hdc,AniEnd.i*25,0,AniEnd.i*25+25,GAMEHEIGHT);
SelectObject(hdc,hf[4]);
SetBkColor(hdc,RGB(255,255,255));
SetTextColor(hdc,RGB(255,128,64));
if(iMoneyNow>=iMoneyDest)
{
if((iGameMatch+1)>=iMatchNum)
{
//全部过关
GetTextExtentPoint32(hdc,temp[2],strlen(temp[2]),&size);
TextOut(hdc,(GAMEWIDTH-size.cx)/2,(GAMEHEIGHT-size.cy)/2-35,
temp[2],strlen(temp[2]));
}
else
{
GetTextExtentPoint32(hdc,temp[0],strlen(temp[0]),&size);
TextOut(hdc,(GAMEWIDTH-size.cx)/2,(GAMEHEIGHT-size.cy)/2-35,
temp[0],strlen(temp[0]));
}
}
else
{
//游戏失败
GetTextExtentPoint32(hdc,temp[1],strlen(temp[1]),&size);
TextOut(hdc,(GAMEWIDTH-size.cx)/2,(GAMEHEIGHT-size.cy)/2-35,
temp[1],strlen(temp[1]));
}
}
void GAMEMAP::GameEndAniOver()
{
AniEnd.active=0;
AniEnd.i=0;
}
void GAMEMAP::SetFont(HFONT *p,int count)
{
memset(hf,0,sizeof(hf));
memcpy(hf,p,count);
}
void GAMEMAP::GameNext()
{
char *temp[]={"你可以选择道具"};
HPEN hpen;
HBRUSH hbrush;
//清屏
hpen=(HPEN)GetStockObject(WHITE_PEN);
hbrush=(HBRUSH)GetStockObject(WHITE_BRUSH);
SelectObject(hdc,hpen);
SelectObject(hdc,hbrush);
Rectangle(hdc,0,0,GAMEWIDTH,GAMEHEIGHT);
SelectObject(hdc,hf[1]);
SetBkColor(hdc,RGB(255,255,255));
SetTextColor(hdc,RGB(255,128,64));
TextOut(hdc,50,100,temp[0],strlen(temp[0]));
}
int GAMEMAP::CanNextMatch()
{
if((iGameMatch+1)>=iMatchNum)
return 0;
if(iMoneyNow>=iMoneyDest)
{
iGameMatch++;
return 1;
}
return 0;
}
void GAMEMAP::GetMatchNum()
{
iMatchNum=0;
fp=fopen("ini\\match.txt","r");
if(!fp)
{
return;
}
while( !feof(fp))
{
if(fgetc(fp)=='*')
{
iMatchNum++;
}
}
fclose(fp);
}
int GAMEMAP::GetMatchNow()
{
return iGameMatch;
}
void GAMEMAP::UseEquip(int money,int iequip,GAMECATCH &c)
{
iMoneyNow-=money;
if(iMoneyNow<0)
{
iMoneyNow=0;
}
switch(iequip)
{
case 0:
//增加炮的数量
iFireNum++;
break;
case 1:
//增加拉动速度
c.iSpeedMore=2;
break;
}
}
void GAMEMAP::ClearMoney()
{
iMoneyNow=0;
}
void GAMEMAP::LoadFirstMap()
{
iGameMatch=0;
LoadMap();
}
void GAMEMAP::KeyProc(int iKey)
{
switch(iKey)
{
case VK_UP:
if(iFireNum>0 && iCatchId>=0)
{
iFireNum--;
bBombAni.SetAni(objectarray[iCatchId].x,objectarray[iCatchId].y);
//一个物销毁
objectarray[iCatchId].id=0;
iCatchId=-1;
}
break;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -