画面上をキャラクターが歩いて、行く事の出来ない場所には行けないというようには出来ましたが
この方法だと画面が固定されてしまっていて、画面全体より大きいmap上を動く事が出来ません。
↓
#include "DxLib.h"
typedef struct{
int x,y,img,muki,walking_flag;
}ch_t;
int hantei[15][20] = {
{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0 },
{ 0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
{ 0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1 },
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0 },
};
int IsAbleToGo(int x,int y,int muki){
if(muki==0)
if(hantei[y/40-1][(x+20)/40]==1)
return 1;
if(muki==1)
if(hantei[y/40][(x+20)/40-1]==1)
return 1;
if(muki==2)
if(hantei[y/40+1][(x+20)/40]==1)
return 1;
if(muki==3)
if(hantei[y/40][(x+20)/40+1]==1)
return 1;
return 0;
}
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow ){
SetGraphMode( 800, 600 , 32) ;
int image[16],map,idouryou=2,tekisouguu_kakuritu=80;
char Key[256];
ch_t ch;
if( ChangeWindowMode(TRUE) != DX_CHANGESCREEN_OK || DxLib_Init() == -1 ) return -1;
ch.x =380;
ch.y =280;
ch.walking_flag=0;
ch.muki=3;
SetDrawScreen( DX_SCREEN_BACK ) ;
LoadDivGraph( "画像/キャラ.png" , 16 , 4 , 4 , 40 , 40 , image ) ;
map = LoadGraph( "画像/map.png" );
while(!ProcessMessage() && !ClearDrawScreen() && !GetHitKeyStateAll( Key ) && !Key[KEY_INPUT_ESCAPE])
{
if(!(Key[ KEY_INPUT_W] == 1 || Key[ KEY_INPUT_M] == 1) && (ch.x+20)%40==0 && ch.y%40==0){
ch.walking_flag=1;
if ( Key[ KEY_INPUT_UP ] == 1 )
ch.muki=0;
else if( Key[ KEY_INPUT_LEFT ] == 1 )
ch.muki=1;
else if( Key[ KEY_INPUT_DOWN ] == 1 )
ch.muki=2;
else if( Key[ KEY_INPUT_RIGHT] == 1 )
ch.muki=3;
else
ch.walking_flag=0;
if(ch.walking_flag==1)
if(IsAbleToGo(ch.x,ch.y,ch.muki)==1)
ch.walking_flag=0;
}
ch.img=image[((ch.x+20)%40+ch.y%40)/10 + ch.muki*4];
if(ch.walking_flag==1){
if(Key[ KEY_INPUT_X] == 1 && (ch.x+20)%4==0 && ch.y%4==0)idouryou=4;
else idouryou=2;
if (ch.muki==0)
ch.y=ch.y-idouryou;
else if(ch.muki==1)
ch.x=ch.x-idouryou;
else if(ch.muki==2)
ch.y=ch.y+idouryou;
else if(ch.muki==3)
ch.x=ch.x+idouryou;
}
ch.img=image[((ch.x+20)%40+ch.y%40)/10 + ch.muki*4];
DrawGraph( -20 , 0 , map , TRUE ) ;
DrawGraph( ch.x , ch.y , ch.img , TRUE ) ;
ScreenFlip();
}
DxLib_End();
return 0;
}
実際、ほとんどのRPGではそうなっていると思います。
単純にmap_x,map_yのような変数を作って、キャラが歩いた時にそれを変化させるだけだと、キャラが一歩だけ歩くという事もできなくなるし、行けない場所も行けてしまい、逆に行けるはずの場所も行けなくなってしまいます。
このコードを望み通りの動作に変えるにはどこをどういう風に変えればよいのでしょうか?教えてください。