自分としてはキャラを背景マップの床に重なり、壁に隣接するように動くように描画したいと考えています。
ソースは以下のように記述してます。
#include "DxLib.h"
int Key[256]; // キーが押されているフレーム数を格納する
// キーの入力状態を更新する
int gpUpdateKey(){
char tmpKey[256]; // 現在のキーの入力状態を格納する
GetHitKeyStateAll(tmpKey); // 全てのキーの入力状態を得る
for (int i = 0; i<256; i++){
if (tmpKey[i] != 0){ // i番のキーコードに対応するキーが押されていたら
Key[i]++; // 加算
}
else { // 押されていなければ
Key[i] = 0; // 0にする
}
}
return 0;
}
int player,chip[8];
int Handle01;//キャラクター描画差分01
int Handle02;//キャラクター描画差分02
int Handle03;//キャラクター描画差分03
int Handle04;//キャラクター描画差分04
int px =10, py = 10;//キャラクター初期位置
int aitem = 0;
//ステージマップ
int map[15][20] = {
{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 6, 6, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
{ 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 2 },
{ 2, 4, 4, 5, 3, 4, 4, 3, 3, 4, 4, 3, 3, 4, 4, 3, 5, 4, 4, 2 },
{ 2, 4, 3, 3, 1, 4, 4, 3, 4, 4, 4, 4, 3, 4, 4, 1, 3, 3, 4, 2 },
{ 2, 4, 3, 4, 4, 4, 4, 3, 4, 4, 4, 4, 3, 4, 4, 4, 4, 3, 4, 2 },
{ 2, 4, 3, 4, 4, 3, 3, 3, 5, 4, 4, 5, 3, 3, 3, 4, 4, 3, 4, 2 },
{ 2, 4, 3, 4, 4, 1, 1, 1, 3, 3, 3, 3, 1, 1, 1, 4, 4, 3, 4, 2 },
{ 2, 4, 1, 4, 4, 4, 4, 4, 1, 1, 1, 1, 4, 4, 4, 4, 4, 1, 4, 2 },
{ 2, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 4, 4, 4, 2 },
{ 2, 4, 4, 4, 3, 4, 4, 2, 4, 4, 4, 4, 2, 4, 4, 3, 4, 4, 4, 2 },
{ 2, 4, 4, 4, 3, 4, 4, 2, 5, 4, 4, 5, 2, 4, 4, 3, 4, 4, 4, 2 },
{ 2, 4, 3, 4, 1, 4, 4, 4, 3, 3, 3, 3, 4, 4, 4, 1, 4, 3, 4, 2 },
{ 2, 4, 3, 4, 4, 4, 4, 4, 1, 1, 1, 1, 4, 4, 4, 4, 4, 3, 4, 2 },
{ 2, 5, 3, 4, 4, 3, 3, 4, 5, 4, 4, 5, 4, 3, 3, 4, 4, 3, 5, 2 },
{ 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0 },
};
// キーの入力移動
void gpCalc(){
int kx =px, ky=py;
//キーの入力操作
if (Key[KEY_INPUT_RIGHT] >= 1){ // 右キーが押されていたら
player = Handle01;
kx++; // 右へ移動
}
if (Key[KEY_INPUT_DOWN] >= 1){ // 下キーが押されていたら
player = Handle02;
ky++; // 下へ移動
}
if (Key[KEY_INPUT_LEFT] >= 1){ // 左キーが押されていたら
player = Handle03;
kx--; // 左へ移動
}
if (Key[KEY_INPUT_UP] >= 1){ // 上キーが押されていたら
player = Handle04;
ky--; // 上へ移動
}
if (map[ky][kx] != 1 && map[ky][kx] != 2 && map[ky][kx] != 3 && map[ky][kx] != 6) {
px = kx;
py = ky;
}
//
if (map[py][px] == 5) {
map[py][px] = 4;
aitem += 1;
}
}
//キャラ描画
void gpDraw(){
DrawRotaGraph(px*32, py*32, 1.0, 0.0, player, TRUE);
DrawFormatString(0, 0, GetColor(255, 255, 255), "MEMORY:%d", memory);
}
//マップ表示
void gMap(){
for (int y = 0; y < 15; y++) {
for (int x = 0; x < 20; x++) {
DrawGraph(x * 32, y * 32, chip[map[y][x]], FALSE);
//ステージクリア処理
if (memory >= 10)
{
//アイテム一定数入手で扉解放
map[0][9] = 7;
map[0][10] = 7;
}
}
}
}
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) {
ChangeWindowMode(TRUE);
DxLib_Init();
SetDrawScreen(DX_SCREEN_BACK);
SetGraphMode(640, 480, 32);
Handle01 = LoadGraph("Graphics/主人公_右.png"); // 画像をロード
Handle02 = LoadGraph("Graphics/主人公_前.png"); // 画像をロード
Handle03 = LoadGraph("Graphics/主人公_左.png"); // 画像をロード
Handle04 = LoadGraph("Graphics/主人公_後.png"); // 画像をロード
LoadDivGraph("Graphics/45_libraly-map.png", 8, 4, 2, 32, 32, chip);
player = Handle02;//表示キャラ
while (ScreenFlip() == 0 && ProcessMessage() == 0 && ClearDrawScreen() == 0 && gpUpdateKey() == 0){
gpCalc();
gMap();
gpDraw();
ScreenFlip();
WaitTimer(100);
}
DxLib_End();
return 0;
}