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

📄 mygame.cpp

📁 本人初学C和directx时编的RPG游戏源码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
		return LEFT;
	case 2:
		return UP;
	case 3:
		return RIGHT;
	}
	return DOWN;
}

//读取地图;
BOOL LoadMaps()
{
	char *Name[]={"Maps\\Home.map","Maps\\Village.map","Maps\\weaponshop.map","Maps\\Drugstore.map",
		          "Maps\\Warehouse.map","Maps\\Authorhome.map","Maps\\Forest.map","Maps\\Hummock.map"};
	FILE *fp;
	int i,j,k;
	for (k=0; k<MAPS; k++)
	{
		fp=fopen(Name[k],"rb");
	    if (fp == NULL)	return FALSE;
	    fseek (fp, 32, 0);               //跳过文件头;  
	    fread (&map[k].width, 4, 1, fp);  //地图宽度;
	    fread (&map[k].height, 4, 1, fp);  //地图高度;
	    fseek (fp, 88, 1);                //跳过一些数据;
	    map[k].Surface = (unsigned short *)malloc(map[k].width * map[k].height*2 );
	    map[k].Object = (unsigned short *)malloc(map[k].width * map[k].height*2); 
	    map[k].Obstacle = (unsigned short *)malloc(map[k].width * map[k].height*2 );
	    map[k].Outlet = (unsigned short *)malloc(map[k].width * map[k].height*2 );
	    map[k].Item = (unsigned short *)malloc(map[k].width * map[k].height*2 );
	    for (i=0; i<map[k].height; i++)
		{
			for(j=0;j<map[k].width;j++)
			{
				fread(&map[k].Surface[i*map[k].width+j], 2, 1, fp);
			    fread(&map[k].Object[i*map[k].width+j], 2, 1, fp); 
			    fread(&map[k].Obstacle[i*map[k].width+j], 2, 1, fp);
			    fread(&map[k].Outlet[i*map[k].width+j], 2, 1, fp);
			    fread(&map[k].Item[i*map[k].width+j], 2, 1, fp); 
			}
		}
	    fclose(fp);
	}

	return TRUE;
}

//读取对话数据
void LoadTalkText()
{
	int fh;
	int nbytes;
	fh = _open("Datas\\talk.txt", _O_RDONLY);
	nbytes=_filelength(fh);
	Talk=(char *)malloc(nbytes);
	_read(fh, Talk, nbytes);
	_close(fh);
}

void InitGameData()
{
	g_game_status = GAME_PLAY;
	handfp = 0;
	nCursel = 0;
	SX = 6;
	SY = 0;
	OLDSX = 6;
	OLDSY = 0;
	DX = 0;
	OLDDX = 0;
	DY = 0;
	OLDDY = 0;
	Scene = 0;
	KILLBOSS1 = FALSE;
	role.ID = 999;
    strcpy (role.Name, "无名");
	role.exist = 1;
	role.active = 1;
	role.IsEnemy = 0;
	role.bitmap = 999;
	role.x = 128;
	role.y = 200;
	role.oldx = 280;
	role.oldy = 150;
	role.talknum = 0;
	role.nLevel = 1;
	role.nExp = 0;
	role.nMoney = 100; 
	role.nMaxLife = 80;
	role.nCurLife = 80;
	role.nPower = 0;
	role.nOriAtt = 12;
	role.nAttack = 12;
	role.nOriDef = 8;
	role.nDefence = 8;
	role.nOriSpeed = 6;
	role.nSpeed = 6;
	role.Weapon = 0;
	role.Armor = 0;
	for (int i=0; i<18; ++i)
		role.Item[i] = ' ';
	role.speed = 8;
	role.width = 32;
	role.height = 48;
	role.picnum = 0;
	role.dir = DOWN;
	role.Surface = lpDDSPlayer;
	role.Battle = 0;
}

void DestroyWorld()//释放游戏资源;
{
	free(Talk);    //释放所有对话数据;
	for(int i=0;i<MAPS;i++)
	{
		free(map[i].Surface);
		free(map[i].Object);
		free(map[i].Obstacle);
		free(map[i].Outlet);
		free(map[i].Item);
	}
}

//文字显示
void PrintText(LPDIRECTDRAWSURFACE Surf,int x,int y,char *Text,DWORD color=RGB(0,0,0), DWORD BK=TRANSPARENT, DWORD BkColor=RGB(255,255,255),int WIDTH=60)
{
	HDC hdc;
	int ChrLen;
	char Tmp[120];

	if (Text == "") return;

	Surf->GetDC(&hdc);
	SetBkMode(hdc, BK);
	SetBkColor(hdc, BkColor);
	SetTextColor(hdc, color); 

	ChrLen = strlen (Text) + 1;
	for (int i=0; i<=ChrLen/WIDTH; i++ )
	{
		memset (Tmp,0,120);
		for (int j=0; j<((i==ChrLen/WIDTH)?ChrLen%WIDTH:WIDTH); j++) 
		{
			Tmp[j] = *Text;
			Text++;
		}
		TextOut(hdc, x, y, Tmp, strlen(Tmp));
		y += 20;
	}

	Surf->ReleaseDC(hdc);
}

void ProtagonistMove()
{
	//检测方向键
	if(KEYDOWN(DIK_UP))            //上;
	{
		role.dir=UP;
	    role.picnum++;
	    if (role.picnum > 2) role.picnum=0;
	    if (SY > 0 && role.y <= 264)
		{
			DY += role.speed;
		    if (DY >= 0) { DY=-32; SY--; }
		}
	    else if (SY == 0 && DY != 0)
		{
			DY += role.speed;
		}
		else role.y -= role.speed;
	}
	if (KEYDOWN(DIK_DOWN))          //下;
	{
		role.dir = DOWN; 
	    role.picnum++;
        if (role.picnum > 2) role.picnum=0;
	    if (SY < map[Scene].height - 15 && role.y >= 264)
		{
			DY -= role.speed;
		    if (DY<=-32) { DY=0; SY++; }
		}
		else role.y += role.speed;
	}
    if (KEYDOWN(DIK_LEFT))          //左; 
	{
		role.dir=LEFT; 
	    role.picnum++;
        if (role.picnum > 2) role.picnum=0;
	    if (SX>0 && role.x <= 304)
		{
			DX += role.speed;
		    if ( DX>=0) { DX=-32; SX--; }
		}
		else if (SX==0 && DX!=0)
		{
			DX += role.speed;
		}
		else role.x -= role.speed;
	}
	if (KEYDOWN(DIK_RIGHT))         //右;
	{
		role.dir=RIGHT; 
	    role.picnum++;
        if (role.picnum > 2) role.picnum=0;
	    if (SX < map[Scene].width - 20 && role.x >= 304)
		{
			DX -= role.speed;
		    if (DX <= -32) { DX=0; SX++; }
		}
		else role.x += role.speed;
	}
    //边界检测;
	if (role.x < 0) role.x=0;
    if (role.x > ScreenX - role.width) role.x = ScreenX - role.width;
    if (role.y < role.height - 16) role.y = role.height - 16;
    if (role.y > ScreenY) role.y = ScreenY;
			
    if (SX < 0) SX = 0;
    if (SX > map[Scene].width - 20 ) SX = map[Scene].width - 20;
    if (SY < 0) SY = 0;
    if (SY > map[Scene].height - 15) SY = map[Scene].height - 15;
    //障碍物边界检测;
    int n0 = ((role.y-20-DY)/32 + SY) * map[Scene].width + ((role.x+12-DX)/32 + SX);
    int n1 = ((role.y-20-DY)/32 + SY) * map[Scene].width + ((role.x+20-DX)/32 + SX);
    int n2 = ((role.y-12-DY)/32 + SY) * map[Scene].width + ((role.x+12-DX)/32 + SX);
    int n3 = ((role.y-12-DY)/32 + SY) * map[Scene].width + ((role.x+20-DX)/32 + SX);
    if (map[Scene].Obstacle[n0]||map[Scene].Obstacle[n1] || map[Scene].Obstacle[n2] || map[Scene].Obstacle[n3])
	{
		role.x = role.oldx;
	    role.y = role.oldy;
	    SX = OLDSX; SY = OLDSY;
	    DX = OLDDX; DY = OLDDY;
	}
	//检测是否与Npc碰撞;
    for (int i=0; i<map[Scene].Npcnum; i++)
	{
		if (map[Scene].Npc[i].exist == 1)
		{
			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)
			{
				role.x = role.oldx;
		        role.y = role.oldy;
		        SX = OLDSX; SY = OLDSY;
		        DX = OLDDX; DY = OLDDY;
				//?敌人;
				if (map[Scene].Npc[i].IsEnemy == 1 &&\
					map[Scene].Npc[i].ID != 60009 &&\
					map[Scene].Npc[i].ID != 70009 &&\
					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();
						}
					}
				}
			}
		}
	}
       //刷新旧值; 
    role.oldx = role.x;
    role.oldy = role.y;
    OLDSX = SX; OLDSY = SY;
    OLDDX = DX; OLDDY = DY;
	int n = ((role.y-16-DY)/32 + SY) * map[Scene].width + ((role.x+16-DX)/32 + SX);
    if (map[Scene].Outlet[n])
		SceneSwitch( map[Scene].Outlet[n] );
    RECT rect = {role.picnum*role.width, role.dir*role.height, role.picnum*role.width+role.width, role.dir*role.height+role.height};
    Blt(lpDDSBack, role.x, role.y - role.height, lpDDSPlayer, rect, TRUE);
}

void SceneSwitch(int msg)
{
	switch(msg)
	{
	case 1:
		Scene=0;
		SX=OLDSX=0;
		SY=OLDSY=0;
		DX=OLDDX=0;
		DY=OLDDY=0;
		role.x=role.oldx=420;
		role.y=role.oldy=432;
		role.dir=UP;
		break;
	case 2:
		Scene=1;
		SX=OLDSX=6;
		SY=OLDSY=0;
		DX=OLDDX=0;
		DY=OLDDY=0;
		role.x=role.oldx=280;
		role.y=role.oldy=150;
		role.dir=DOWN;
		break;
	case 3:
		Scene=2;
		SX=OLDSX=0;
		SY=OLDSY=0;
		DX=OLDDX=0;
		DY=OLDDY=0;
		role.x=role.oldx=420;
		role.y=role.oldy=450;
		role.dir=UP;
		break;
	case 4:
		Scene=1;
		SX=OLDSX=12;
		SY=OLDSY=7;
		DX=OLDDX=0;
		DY=OLDDY=0;
		role.x=role.oldx=450;
		role.y=role.oldy=260;
		role.dir=DOWN;
		break;
	case 5:
		Scene=3;
		SX=OLDSX=0;
		SY=OLDSY=5;
		DX=OLDDX=0;
		DY=OLDDY=0;
		role.x=role.oldx=172;
		role.y=role.oldy=432;
		role.dir=UP;
		break;
	case 6:
		Scene=1;
		SX=OLDSX=2;
		SY=OLDSY=20;
		DX=OLDDX=0;
		DY=OLDDY=0;
		role.x=role.oldx=320;
		role.y=role.oldy=260;
		role.dir=DOWN;
     	break;
	case 7:
		Scene=4;
		SX=OLDSX=0;
		SY=OLDSY=0;
		DX=OLDDX=0;
		DY=OLDDY=0;
		role.x=role.oldx=300;
		role.y=role.oldy=432;
		role.dir=UP;
     	break;
	case 8:
		Scene=1;
		SX=OLDSX=0;
		SY=OLDSY=2;
		DX=OLDDX=0;
		DY=OLDDY=0;
		role.x=role.oldx=100;
		role.y=role.oldy=320;
		role.dir=DOWN;
     	break;
	case 9:
		Scene=5;
		SX=OLDSX=0;
		SY=OLDSY=0;
		DX=OLDDX=0;
		DY=OLDDY=0;
		role.x=role.oldx=146;
		role.y=role.oldy=432;
		role.dir=UP;
     	break;
	case 10:
		Scene=1;
		SX=OLDSX=12;
		SY=OLDSY=14;
		DX=OLDDX=0;
		DY=OLDDY=0;
		role.x=role.oldx=540;
		role.y=role.oldy=364;
		role.dir=DOWN;
     	break;
	case 11:
		Scene=6;
		SX=OLDSX=7;
		SY=OLDSY=0;
		DX=OLDDX=0;
		DY=OLDDY=0;
		role.x=role.oldx=284;
		role.y=role.oldy=48;
		role.dir=DOWN;
     	break;
	case 12:
		Scene=1;
		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;
	case 13:
		if (KILLBOSS1 == FALSE)
		{
			role.y=role.oldy=432;
			DialogShow(0, 90001, 1);
		}
		else
		{
			Scene=7;
		    SX=OLDSX=7;
		    SY=OLDSY=0;
		    DX=OLDDX=0;
		    DY=OLDDY=0;

⌨️ 快捷键说明

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