はじめまして kameoというものです。
C言語は大学の講義で半年程度やっていましたが、今は独自でゲーム作りに挑戦しています。
質問ですが、以下のようなプログラムを作成しました。
#include "DxLib.h"
typedef struct{
int x,y,dx,dy,img;
}ch_t;
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow ){
int image[4];
char Key[256];
ch_t ch;
if( ChangeWindowMode(TRUE) != DX_CHANGESCREEN_OK || DxLib_Init() == -1 ) return -1; //ウィンドウ化と初期化処理
ch.x =320;
ch.y =240;
ch.dx=4; //移動量
ch.dy=4;
SetDrawScreen( DX_SCREEN_BACK ) ; //描画先を裏画面に設定
LoadDivGraph( "jiji.png" , 4 , 2 , 2 , 32 , 32 , image ) ;//画像を分割してimage配列に保存
while(!ProcessMessage() && !ClearDrawScreen() && !GetHitKeyStateAll( Key ) && !Key[KEY_INPUT_ESCAPE]){
//↑メッセージ処理 ↑画面をクリア ↑キーボード入力状態取得 ↑ESCが押されると終了
ch.img=image[0];
if( Key[ KEY_INPUT_RIGHT ] == 1 ) {
ch.x+=ch.dx ;
ch.img=image[3];
}
else if( Key[ KEY_INPUT_LEFT ] == 1 ) {
ch.x-=ch.dx ;
ch.img=image[2];
}
else if( Key[ KEY_INPUT_UP ] == 1 ) {
ch.y-=ch.dy ;
ch.img=image[0];
}
else if( Key[ KEY_INPUT_DOWN ] == 1 ) {
ch.y+=ch.dx ;
ch.img=image[1];
}
//斜め移動
else if( Key[ KEY_INPUT_RIGHT ] == 1&&Key[ KEY_INPUT_UP ] == 1 ) {
ch.x+=ch.dx ;
ch.y-=ch.dy;
}
else if( Key[ KEY_INPUT_RIGHT ] == 1&&Key[ KEY_INPUT_DOWN ] == 1 ) {
ch.x+=ch.dx ;
ch.y+=ch.dy;
}
else if( Key[ KEY_INPUT_LEFT] == 1&&Key[ KEY_INPUT_UP ] == 1 ) {
ch.x-=ch.dx ;
ch.y-=ch.dy;
}
else if( Key[ KEY_INPUT_LEFT] == 1&&Key[ KEY_INPUT_DOWN ] == 1 ) {
ch.x-=ch.dx ;
ch.y+=ch.dy;
}
DrawGraph( ch.x , ch.y , ch.img , TRUE ) ;
ScreenFlip();//裏画面を表画面に反映
}
DxLib_End();
return 0;
}
このプログラムで斜め移動を行おうと思いましたが斜めに進んでくれません。
どのようにプログラムすればいいでしょうか?
よろしくお願いします。(画像ファイルを添付しておきます)