早速なのですがこちらを参考に簡単なRPGを作ろうとしています。http://dixq.net/g/
さて、その「22. キャラを4方向に歩かせる。」の項目を元に、タイトル画面を加えてみたのですがキャラが動きません。
初心者なので、その原因が分からないのです。
汚いコードですが張りますね。
原因、できましたら改良点のご指摘をお願いします。
//main.h
typedef struct{
int x,y,img,muki,walking_flag;
}charadate;
int function_status=0,White,a=2,b,c;
int image[16],im_boss[16],g_map[6],i,j;
int hantei[15][20] = {
{ 0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2 },
{ 1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0 },
{ 1,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0 },
{ 1,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 1,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1 },
{ 1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1 },
{ 1,1,1,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1,1 },
{ 1,1,1,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1,1 },
{ 0,0,0,0,1,0,0,0,0,0,1,0,0,0,1,0,0,0,1,1 },
{ 0,1,1,1,1,0,0,0,1,1,1,0,0,0,1,0,0,0,1,1 },
{ 0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1 },
{ 0,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1 },
{ 0,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1 },
{ 0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 },
{ 0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 },
};
int can_or_cannot(int x,int y,int muki){//進めるかを判定する
if(muki==0)//上向きなら
if((hantei[y/32-1][x/32]==1)||(hantei[y/32-1][x/32]==2))//進めるか判定
return 1;//エラー
if(muki==1)//左向きなら
if((hantei[y/32][x/32-1]==1)||(hantei[y/32][x/32-1]==2))
return 1;
if(muki==2)//下向きなら
if((hantei[y/32+1][x/32]==1)||(hantei[y/32+1][x/32]==2))
return 1;
if(muki==3)//右向きなら
if((hantei[y/32][x/32+1]==1)||(hantei[y/32][x/32+1]==2))
return 1;
return 0;//正常
};
char KeyBuf[ 256 ];//game.cpp
#include "DxLib.h"
#include <windows.h>
#include "main.h"
void title();
void game();
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow ){
ChangeWindowMode( TRUE ) ; //ウィンドウモードに変更
if( DxLib_Init() == -1 ) return -1; // DXライブラリ初期化処理 エラーが起きたら終了
White = GetColor( 255 , 255 , 255 ) ; //色の取得
SetDrawScreen( DX_SCREEN_BACK ) ; // 描画先を裏画面に設定
while(!ProcessMessage() && !ClearDrawScreen() && !GetHitKeyStateAll( KeyBuf ) && !KeyBuf[KEY_INPUT_ESCAPE]){
switch(function_status){
case 0:
title();
break;
case 1:
game();
break;
default:
DxLib_End() ; // DXライブラリ使用の終了処理
return 0;
break;
}
ScreenFlip() ; // 裏画面データを表画面へ反映
}
DxLib_End() ; // DXライブラリ使用の終了処理
return 0 ; // ソフトの終了
}
void game(){
charadate ch;
ch.x=0;
ch.y=0;
ch.muki=2;
ch.walking_flag=0;
LoadDivGraph( "主人公.bmp" , 16 , 4 , 4 , 32 , 32 , image ) ;//画像を分割してimage配列に保存
LoadDivGraph("mapchip.bmp",6,3,2,32,32,g_map);
LoadDivGraph("boss.bmp",16,4,4,32,32,im_boss);
for(i=0;i<15;i++)
for(j=0;j<20;j++)
if(hantei[i][j]==1)
DrawGraph(j*32,i*32,g_map[2],TRUE);
else if(hantei[i][j]==0)
DrawGraph(j*32,i*32,g_map[0],TRUE);
else if(hantei[i][j]==2){
DrawGraph(j*32,i*32,g_map[0],TRUE);
DrawGraph(j*32,i*32,im_boss[8],TRUE);
}
if(ch.x%32==0 && ch.y%32==0){ //座標が32で割り切れたら入力可能
ch.walking_flag=1; //歩くフラグを立てる。
if ( KeyBuf[ KEY_INPUT_UP ] == 1 ) //上ボタンが押されたら
ch.muki=0; //上向きフラグを立てる
else if( KeyBuf[ KEY_INPUT_LEFT ] == 1 ) //左ボタンが押されたら
ch.muki=1; //左向きフラグを立てる
else if( KeyBuf[ KEY_INPUT_DOWN ] == 1 ) //下ボタンが押されたら
ch.muki=2; //下向きフラグを立てる
else if( KeyBuf[ KEY_INPUT_RIGHT] == 1 ) //右ボタンが押されたら
ch.muki=3; //右向きフラグを立てる
else //何のボタンも押されてなかったら
ch.walking_flag=0; //歩かないフラグを立てる
if(ch.walking_flag==1) //もし歩くなら
if(can_or_cannot(ch.x,ch.y,ch.muki)==1)//行き先が歩けないなら
ch.walking_flag=0; //歩かないフラグを立てる。
}
if(ch.walking_flag==1){ //歩くフラグが立っていたら
if (ch.muki==0) //上向きならch.y座標を減らす
ch.y--;
else if(ch.muki==1) //左向きならch.x座標を減らす
ch.x--;
else if(ch.muki==2) //下向きならch.y座標を増やす
ch.y++;
else if(ch.muki==3) //右向きならch.x座標を増やす
ch.x++;
}
ch.img=image[(ch.x%32+ch.y%32)/8 + ch.muki*4]; //画像をセット
DrawGraph( ch.x , ch.y , ch.img , TRUE ) ;//画像を描画
}
void title(){
DrawString(100 + b,100,"ニューゲーム",White);
DrawString(100 + c,125,"シャットダウン",White);
if(KeyBuf[KEY_INPUT_DOWN]==1||KeyBuf[KEY_INPUT_UP]==1){
a ++;
Sleep(200);
}
if(a==2){
b=0;
c=0;
}
if(a%2 == 0 && a != 2){
b = 25;
c = 0;
}
if(a%2 == 1){
b = 0;
c = 25;
}
if(KeyBuf[KEY_INPUT_RETURN]==1)
if(a%2==0 && a != 2)
function_status=1;
else
function_status=2;
}