現在コードは以下のようになっています
#include "DxLib.h"
const char gStageData[] = "\
########\n\
# .. #\n\
# o o #\n\
# p #\n\
########";
const int gStageWidth = 8;
const int gStageHeight = 5;
int handle[gStageWidth*gStageHeight];
enum Object {
OBJ_SPACE,
OBJ_WALL,
OBJ_GOAL,
OBJ_BLOCK,
OBJ_BLOCK_ON_GOAL,
OBJ_MAN,
OBJ_MAN_ON_GOAL,
OBJ_UNKNOWN
};
int Key[256];
void initialize(Object* state, int width, int height, const char* stageData);
void draw(const Object* state, int width, int height);
void update(Object* s, int w, int h);
bool checkClear(const Object* s, int width, int height);
int gpUpdateKey();
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) {
ChangeWindowMode(true);
DxLib_Init();
SetDrawScreen(DX_SCREEN_BACK);
Object* state = new Object[gStageWidth*gStageHeight];
initialize(state, gStageWidth, gStageHeight, gStageData);
while (true) {
draw(state, gStageWidth, gStageHeight);
if (checkClear(state, gStageWidth, gStageHeight))
break;
update(state, gStageWidth, gStageHeight);
}
delete[] state;
state = 0;
DxLib_End();
return 0;
}
void initialize(Object* state, int width, int height, const char* stageData) {
const char* d = stageData;
int x = 0;
int y = 0;
while (*d != '\0') {
Object t;
switch (*d) {
case '#':t = OBJ_WALL; break;
case ' ':t = OBJ_SPACE; break;
case 'o':t = OBJ_BLOCK; break;
case 'O':t = OBJ_BLOCK_ON_GOAL; break;
case '.':t = OBJ_GOAL; break;
case 'p':t = OBJ_MAN; break;
case 'P':t = OBJ_MAN_ON_GOAL; break;
case '\n':
x = 0;
y++;
t = OBJ_UNKNOWN;
break;
default:t = OBJ_UNKNOWN;
}
d++;
if (t != OBJ_UNKNOWN) {
state[y*width + x] = t;
x++;
}
}
}
void draw(const Object* state, int width, int height) {
int x = 0;
int y = 0;
const Object* s = state;
ClearDrawScreen();
for (int i = 0; i < gStageHeight; i++) {
for (int j = 0; j < gStageWidth; j++) {
switch (*s) {
case OBJ_SPACE:handle[i*gStageHeight + j] = LoadGraph("objspace.png"); break;
case OBJ_WALL:handle[i*gStageHeight + j] = LoadGraph("objwall.png"); break;
case OBJ_GOAL:handle[i*gStageHeight + j] = LoadGraph("objgoal.png"); break;
case OBJ_BLOCK:handle[i*gStageHeight + j] = LoadGraph("objblock.png"); break;
case OBJ_BLOCK_ON_GOAL:handle[i*gStageHeight + j] = LoadGraph("objblockongoal.png"); break;
case OBJ_MAN:handle[i*gStageHeight + j] = LoadGraph("objman.png"); break;
case OBJ_MAN_ON_GOAL:handle[i*gStageHeight + j] = LoadGraph("objmanongoal.png"); break;
}
DrawGraph(x, y, handle[i*gStageHeight + j], true);
x = x + 50;
s++;
}
x = 0;
y = y + 50;
}
ScreenFlip();
}
void update(Object* s, int w, int h) {
int dx = 0;
int dy = 0;
while (gpUpdateKey()==0) {
if (Key[KEY_INPUT_UP] >= 1) {
dy = -1;
break;
}
if (Key[KEY_INPUT_DOWN] >= 1) {
dy = 1;
break;
}
if (Key[KEY_INPUT_LEFT] >= 1) {
dx = -1;
break;
}
if (Key[KEY_INPUT_RIGHT] >= 1) {
dx = 1;
break;
}
}
int i = -1;
for (i = 0; i < w*h; i++)
if (s[i] == OBJ_MAN || s[i] == OBJ_MAN_ON_GOAL)
break;
int x = i%w;
int y = i / w;
int tx = x + dx;
int ty = y + dy;
if (tx < 0 || ty < 0 || tx >= w || ty >= h)
return;
int p = y*w + x;
int tp = ty*w + tx;
if (s[tp] == OBJ_SPACE || s[tp] == OBJ_GOAL) {
s[tp] == (s[tp] == OBJ_GOAL) ? OBJ_MAN_ON_GOAL : OBJ_MAN;
s[p] == (s[p] == OBJ_MAN_ON_GOAL) ? OBJ_GOAL : OBJ_SPACE;
}
else if (s[tp] == OBJ_BLOCK || s[tp] == OBJ_BLOCK_ON_GOAL) {
int tx2 = tx + dx;
int ty2 = ty + dy;
if (tx2 < 0 || ty2 < 0 || tx2 >= w || ty2 >= h)
return;
int tp2 = (ty + dy)*w + (tx + dx);
if (s[tp2] == OBJ_SPACE || s[tp2] == OBJ_GOAL) {
s[tp2] = (s[tp2] == OBJ_GOAL) ? OBJ_BLOCK_ON_GOAL : OBJ_BLOCK;
s[tp] = (s[tp] == OBJ_BLOCK_ON_GOAL) ? OBJ_MAN_ON_GOAL : OBJ_MAN;
s[p] = (s[p] == OBJ_MAN_ON_GOAL) ? OBJ_GOAL : OBJ_SPACE;
}
}
}
bool checkClear(const Object* s, int width, int height) {
for (int i = 0; i < width*height; i++)
if (s[i] == OBJ_BLOCK)
return false;
return true;
}
int gpUpdateKey() {
char tmpKey[256];
GetHitKeyStateAll(tmpKey);
for (int i = 0; i < 256; i++) {
if (tmpKey[i] != 0) {
Key[i]++;
}
else
{
Key[i] = 0;
}
}
return 0;
}
となっているのですがキー入力のところでなぜか↑キーを押したときしか処理が行われず、しかもそのあとはどのキーをおしても処理が行われなくなります。どうすればうまく処理が行われるでしょうか?