📄 mygame.cpp
字号:
role.x=role.oldx=284;
role.y=role.oldy=48;
role.dir=DOWN;
}
break;
case 14:
Scene=6;
SX=OLDSX=7;
SY=OLDSY=21;
DX=OLDDX=0;
DY=OLDDY=0;
role.x=role.oldx=284;
role.y=role.oldy=432;
role.dir=UP;
break;
}
}
//画背景;
void MakeBackGround()
{
unsigned short n;
for (int i=SY; i<SY+16 ; i++)
for (int j=SX; j<SX+21 ; j++)
{
//画地表;
n = map[Scene].Surface[i * map[Scene].width + j];
RECT rect = {n%20*32, n/20*32, n%20*32+32, n/20*32+32};
Blt(lpDDSBack, DX+(j-SX)*32, DY+(i-SY)*32, lpDDSSurface, rect, FALSE);
//画物体;
n = map[Scene].Object[i * map[Scene].width + j];
rect = GetRect(n%20*32, n/20 * 32, n%20*32 + 32, n/20 * 32 + 32);
Blt(lpDDSBack, DX+(j-SX)*32, DY+(i-SY)*32, lpDDSObject, rect, TRUE);
}
}
//读取NPC数据
BOOL LoadNpcData()
{
char *Npc[]={"Maps\\Home.npc","Maps\\Village.npc","Maps\\weaponshop.npc","Maps\\Drugstore.npc",
"Maps\\Warehouse.npc","Maps\\Authorhome.npc","Maps\\Forest.npc","Maps\\Hummock.npc"};
char *data=0;
int fh;
int nbytes;
int lpFile=0;
for (int i=0; i<MAPS; i++)
{
fh = _open(Npc[i], _O_RDONLY);
nbytes = _filelength(fh);
free (data);
data = (char *)malloc(nbytes);
_read (fh, data, nbytes);
_close (fh);
lpFile = 0;
map[i].Npcnum = ReadLine(data,lpFile,0);
//申请内存空间
map[i].Npc = (ROLE_STRUCT *)calloc(sizeof(ROLE_STRUCT),map[i].Npcnum);
for (int j=0; j<map[i].Npcnum; j++)
{
map[i].Npc[j].ID = ReadLine(data,lpFile,0);
ReadLine( data, lpFile, map[i].Npc[j].Name);
map[i].Npc[j].exist = ReadLine(data,lpFile,0);
map[i].Npc[j].active = ReadLine(data,lpFile,0);
map[i].Npc[j].IsEnemy = ReadLine(data,lpFile,0);
map[i].Npc[j].bitmap = ReadLine(data,lpFile,0);
map[i].Npc[j].lboundary = ReadLine(data,lpFile,0);
map[i].Npc[j].rboundary = ReadLine(data,lpFile,0);
map[i].Npc[j].uboundary = ReadLine(data,lpFile,0);
map[i].Npc[j].dboundary = ReadLine(data,lpFile,0);
map[i].Npc[j].x = ReadLine(data,lpFile,0);
map[i].Npc[j].y = ReadLine(data,lpFile,0);
map[i].Npc[j].oldx = ReadLine(data,lpFile,0);
map[i].Npc[j].oldy = ReadLine(data,lpFile,0);
map[i].Npc[j].talknum = ReadLine(data,lpFile,0);
map[i].Npc[j].nLevel = ReadLine(data, lpFile, 0);
map[i].Npc[j].nExp = ReadLine(data, lpFile, 0);
map[i].Npc[j].nMoney = ReadLine(data,lpFile,0);
map[i].Npc[j].nMaxLife = ReadLine(data,lpFile,0);
map[i].Npc[j].nCurLife = ReadLine(data,lpFile,0);
map[i].Npc[j].nPower = ReadLine(data,lpFile,0);
map[i].Npc[j].nOriAtt = ReadLine(data,lpFile,0);
map[i].Npc[j].nAttack = ReadLine(data,lpFile,0);
map[i].Npc[j].nOriDef = ReadLine(data,lpFile,0);
map[i].Npc[j].nDefence = ReadLine(data,lpFile,0);
map[i].Npc[j].nOriSpeed = ReadLine(data,lpFile,0);
map[i].Npc[j].nSpeed = ReadLine(data,lpFile,0);
map[i].Npc[j].Weapon = ReadLine(data,lpFile,0);
map[i].Npc[j].Armor = ReadLine(data,lpFile,0);
ReadLine( data, lpFile, (char *)map[i].Npc[j].Item );
map[i].Npc[j].speed = ReadLine(data,lpFile,0);
map[i].Npc[j].width = ReadLine(data,lpFile,0);
map[i].Npc[j].height = ReadLine(data,lpFile,0);
map[i].Npc[j].picnum = ReadLine(data,lpFile,0);
map[i].Npc[j].dir = Int2Dir(ReadLine(data,lpFile,0));
map[i].Npc[j].Surface = lpDDSNpc[map[i].Npc[j].bitmap];
map[i].Npc[j].Battle = ReadLine(data,lpFile,0);
ReadLine(data,lpFile,0);
}
}
return TRUE;
}
//读取物品数据
void LoadItemData()
{
char * data;
int fh;
int nbytes;
int lpFile;
fh = _open( "Datas/Items.dat", _O_RDONLY );
nbytes=_filelength(fh);
data=(char *)malloc(nbytes);
_read( fh, data, nbytes );
_close(fh);
lpFile=0;
int Nums=ReadLine(data,lpFile,0);
for( int j=2; j<Nums+2; j++)
{
Items[j].ID = ReadLine(data,lpFile,0);
Items[j].type = ReadLine(data,lpFile,0);
ReadLine( data, lpFile, Items[j].Name);
ReadLine( data, lpFile, Items[j].About);
Items[j].Money = ReadLine(data,lpFile,0);
Items[j].Life = ReadLine(data,lpFile,0);
Items[j].Power = ReadLine(data,lpFile,0);
Items[j].Attack = ReadLine(data,lpFile,0);
Items[j].Defence = ReadLine(data,lpFile,0);
Items[j].Speed = ReadLine(data,lpFile,0);
ReadLine(data,lpFile,0);
}
}
//读行
int ReadLine(char *datas,int &n,char *Chs,int Seek)
{
char chr[1024] = "";
char tmp;
int m = 0;
n += Seek;
memset (chr,0,1024);
while( 1 )
{
tmp = datas[n];
n++;
if (tmp==';')
{
while (1)
{
tmp=datas[n];
n++;
if (tmp=='\n') break;
}
break;
}
chr[m] = tmp;
m++;
}
if (Chs == 0) return atoi(chr);
else strcpy (Chs, chr);
return 1;
}
//找寻数据位置
char *GetTalk(int ID, int Talk_Num)
{
char Chars[1024] = "", buffer[20];
char IDS[10] = "[";
char *RetChars;
int i = 0, n = 0, Long = strlen(Talk);
memset (IDS, 0, 10);
IDS[0] = '[';
strcat (IDS, _itoa(ID,buffer,10));
IDS[6] = ']';
RetChars = (char *)malloc(1024);
while (n < Long)
{
if (Talk[n]==' ' || Talk[n]=='\n' || Talk[n]==';')
{
if (strcmp(IDS, Chars) == 0) //找到数据
{
for (int Line=0; Line<=Talk_Num; Line++)
while (1) { n++; if (Talk[n] == '\n') { n++; break;} } //提行
ReadLine(Talk, n, RetChars, 0);
return RetChars;
}
n++;
i = 0;
memset (Chars, 0, 1024);
}
else
{
Chars[i] = Talk[n];
i++;
n++;
}
}
return "…… ……";
}
void NpcMove()
{
for (int i=0; i<map[Scene].Npcnum; i++)
{
if (map[Scene].Npc[i].exist == 1)//如果Npc存在;
{
if (map[Scene].Npc[i].active == 1)//并且处于活动状态;
{
if (random(100) > 95)//改方向;
map[Scene].Npc[i].dir = Int2Dir(random(4));
map[Scene].Npc[i].picnum++;
if (map[Scene].Npc[i].picnum>=3) map[Scene].Npc[i].picnum=0;
//Npcs移动
switch (map[Scene].Npc[i].dir)
{
case 0: map[Scene].Npc[i].y+=map[Scene].Npc[i].speed;
break;
case 1: map[Scene].Npc[i].x-=map[Scene].Npc[i].speed;
break;
case 2: map[Scene].Npc[i].y-=map[Scene].Npc[i].speed;
break;
case 3: map[Scene].Npc[i].x+=map[Scene].Npc[i].speed;
break;
}
//边界检测
if (map[Scene].Npc[i].x <= map[Scene].Npc[i].lboundary)
{
map[Scene].Npc[i].x = map[Scene].Npc[i].oldx;
map[Scene].Npc[i].dir = Int2Dir(map[Scene].Npc[i].dir+2);
}
if (map[Scene].Npc[i].x >= map[Scene].Npc[i].rboundary)
{
map[Scene].Npc[i].x = map[Scene].Npc[i].oldx;
map[Scene].Npc[i].dir = Int2Dir(map[Scene].Npc[i].dir+2);
}
if (map[Scene].Npc[i].y <= map[Scene].Npc[i].uboundary)
{
map[Scene].Npc[i].y = map[Scene].Npc[i].oldy;
map[Scene].Npc[i].dir = Int2Dir(map[Scene].Npc[i].dir+2);
}
if (map[Scene].Npc[i].y >= map[Scene].Npc[i].dboundary)
{
map[Scene].Npc[i].y = map[Scene].Npc[i].oldy;
map[Scene].Npc[i].dir = Int2Dir(map[Scene].Npc[i].dir+2);
}
//与主角碰撞检测;
if (abs(role.x+SX*32-DX-map[Scene].Npc[i].x)<=24 &&\
abs(role.y+SY*32-DY-map[Scene].Npc[i].y)<=42)
{
map[Scene].Npc[i].x = map[Scene].Npc[i].oldx;
map[Scene].Npc[i].y = map[Scene].Npc[i].oldy;
//Npc是敌人进入战斗,BOSS不移动所以不考虑;
if (map[Scene].Npc[i].IsEnemy==1 && GetTickCount()-dwnoenemy>1500)
{
if (Scene == 6)
{
int whowin = Battle(map[Scene].Npc[i], 0, 0);
if (whowin == 1)
{
Prize(map[Scene].Npc[i]);
map[Scene].Npc[i].exist = 0;
}
else if (whowin == 2)
{
char Temp[100];
PopMsgWindow();
sprintf (Temp, "%s", "战斗失败!");
PrintText(lpDDSBack, (640-strlen(Temp)*8)/2, 230, Temp);
MsgDelay(1500);
g_game_status = GAME_OVER;
}
else if (whowin == 0)
{
dwnoenemy = GetTickCount();
}
}
else if (Scene == 7)
{
int whowin = Battle(map[Scene].Npc[i], 1, 0);
if (whowin == 1)
{
Prize(map[Scene].Npc[i]);
map[Scene].Npc[i].exist = 0;
}
else if (whowin == 2)
{
char Temp[100];
PopMsgWindow();
sprintf (Temp, "%s", "战斗失败!");
PrintText(lpDDSBack, (640-strlen(Temp)*8)/2, 230, Temp);
MsgDelay(1500);
g_game_status = GAME_OVER;
}
else if (whowin == 0)
{
dwnoenemy = GetTickCount();
}
}
}
}
//与其他Npc碰撞检测;
for (int j=0; j<map[Scene].Npcnum; j++)
{
if (j!=i && map[Scene].Npc[j].exist==1)
{
if (abs(map[Scene].Npc[i].x-map[Scene].Npc[j].x)<=24 &&\
abs(map[Scene].Npc[i].y-map[Scene].Npc[j].y)<=42)
{
map[Scene].Npc[i].x = map[Scene].Npc[i].oldx;
map[Scene].Npc[i].y = map[Scene].Npc[i].oldy;
}
}
}
//障碍物检测
int n0 = ((map[Scene].Npc[i].y-24)/32 ) * map[Scene].width + ((map[Scene].Npc[i].x+8)/32 );
int n1 = ((map[Scene].Npc[i].y-24)/32 ) * map[Scene].width + ((map[Scene].Npc[i].x+24)/32 );
int n2 = ((map[Scene].Npc[i].y-8)/32 ) * map[Scene].width + ((map[Scene].Npc[i].x+8)/32 );
int n3 = ((map[Scene].Npc[i].y-8)/32 ) * map[Scene].width + ((map[Scene].Npc[i].x+24)/32 );
if (map[Scene].Obstacle[n0] || map[Scene].Obstacle[n1] || map[Scene].Obstacle[n2] || map[Scene].Obstacle[n3])
{
map[Scene].Npc[i].x = map[Scene].Npc[i].oldx;
map[Scene].Npc[i].y = map[Scene].Npc[i].oldy;
}
else
{
map[Scene].Npc[i].oldx = map[Scene].Npc[i].x;
map[Scene].Npc[i].oldy = map[Scene].Npc[i].y;
}
}
//生成该图Npc
RECT rect = {map[Scene].Npc[i].picnum* map[Scene].Npc[i].width, map[Scene].Npc[i].dir*map[Scene].Npc[i].height, map[Scene].Npc[i].picnum*map[Scene].Npc[i].width+map[Scene].Npc[i].width, map[Scene].Npc[i].dir* map[Scene].Npc[i].height+map[Scene].Npc[i].height};
Blt(lpDDSBack, map[Scene].Npc[i].x-(SX*32)+DX, map[Scene].Npc[i].y-(map[Scene].Npc[i].height)-(SY*32)+DY, map[Scene].Npc[i].Surface, rect, TRUE);
}
}
}
char *ReadWord(char *Temp, int &n1)
{
char *Txt;
int i = 0;
n1++;
Txt = (char *)malloc(1024);
memset (Txt, 0, 1024);
while (1)
{
if (Temp[n1]=='{' || Temp[n1]=='}')
{
talktoken = Temp[n1];
endtoken = Temp[n1+1];
return Txt;
}
else
{
Txt[i] = Temp[n1];
i++;
n1++;
}
}
return "error";
}
//添加物品;
BOOL AddItem(int nItem)
{
for (int i=1; i<16; ++i)
{
if (role.Item[i] == ' ') break;
}
if (role.Item[i] == ' ')
{
role.Item[i] = nItem + 0x2f;
return TRUE;
}
else
{
return FALSE;
}
}
//画菜单背景;
void DrawMenuback()
{
int nTileW = 96;
int nTileH = 48;
int nCountW = 640/nTileW;
int nCountH = 480/nTileH;
// 画中间部分
int w,h;
RECT rect = {96, 48, 192, 96};
for (h=1; h<nCountH; h++)
{
for (w=1; w<nCountW; w++)
{
Blt(lpDDSBack, w * nTileW, h * nTileH, lpDDSMenuback, rect, FALSE);
}
}
// 左边
rect.left = 0 ;
rect.right = nTileW;
rect.top = nTileH;
rect.bottom = rect.top + nTileH;
for (h=1; h<nCountH; h++)
{
Blt(lpDDSBack, 0, h*nTileH, lpDDSMenuback, rect, FALSE);
}
// 右边
rect.left = 288-nTileW;
rect.right = 288;
rect.top = nTileH;
rect.bottom = rect.top + nTileH;
for (h=1; h<nCountH; h++)
{
Blt(lpDDSBack, 640 - nTileW, h * nTileH, lpDDSMenuback, rect, FALSE);
}
// 上边
rect.left = nTileW;
rect.right = rect.left + nTileW;
rect.top = 0;
rect.bottom = nTileH;
for (w=1; w<nCountW; w++)
{
Blt(lpDDSBack, w * nTileW, 0, lpDDSMenuback, rect, FALSE);
}
// 下边
rect.left = nTileW;
rect.right = rect.left + nTileW;
rect.top = 144 - nTileH;
rect.bottom = 144;
for (w=1; w<nCountW; w++)
{
Blt(lpDDSBack, w * nTileW, 480 - nTileH, lpDDSMenuback, rect, FALSE);
}
// 左上
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -