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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },//13
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },//14
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },//15
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },//16
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },//17
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },//18
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },//19
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },//20
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },//21
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },//22
};
enum {
eScene_Game,
eScene_Clear
}eScene = eScene_Game;
int getMinToMaxRand( int min, int max );
int toChipSize( int n );
void Init();
void OnTimer();
void Update();
void Draw();
int mNowClock, mLastClock;
int mPX, mPY, mPMuki, mLastPX, mLastPY;
int mMoveX[ 4 ] = { 0, -1, 0, 1 }, mMoveY[ 4 ] = { -1, 0, 1, 0 };
int mFX, mFY, mFoodFlag;
int mOnKey;
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
ChangeWindowMode( TRUE );
SetDrawScreen( DX_SCREEN_BACK );
if ( DxLib_Init() == -1 ) return -1;
Init();
mLastClock = GetNowCount();
while ( ProcessMessage() == 0 && CheckHitKey( KEY_INPUT_ESCAPE ) == 0 )
{
ClearDrawScreen();
OnTimer();
Draw();
ScreenFlip();
}
DxLib_End();
return 0;
}
int getMinToMaxRand( int min, int max ) {
int r;
r = GetRand( max - min ) + min;
return r;
}
int toChipSize( int n ) {
int cs, s = CHIP_SIZE;
cs = n / s;
return cs;
}
void Init() {
mPX = 6, mPY = 10;
}
void Update() {
mField[ mPY ][ mPX ] = 0;
if ( mFoodFlag != 1 ) { //もし食べ物がないのなら
mField[ mFY ][ mFX ] = 0;
mFX = getMinToMaxRand( 1, 19 );
mFY = getMinToMaxRand( 1, 19 );
mField[ mFY ][ mFX ] = 3;
mFoodFlag = 1;
}
if ( mFX == mPX && mFY == mPY ) mFoodFlag = 0;
if ( CheckHitKey( KEY_INPUT_UP ) == 1 ) mOnKey = 0;
if ( CheckHitKey( KEY_INPUT_LEFT ) == 1 ) mOnKey = 1;
if ( CheckHitKey( KEY_INPUT_DOWN ) == 1 ) mOnKey = 2;
if ( CheckHitKey( KEY_INPUT_RIGHT ) == 1 ) mOnKey = 3;
if ( 1 < mPX && toChipSize( FIELD_SIZE - CHIP_SIZE ) < mPX ) {
mPX += mMoveX[ mOnKey ];
} else if ( 1 < mPY && toChipSize( FIELD_SIZE - CHIP_SIZE ) < mPY ) {
mPY += mMoveY[ mOnKey ];
}
mField[ mPY ][ mPX ] = 2;
}
void OnTimer() {
mNowClock = GetNowCount();
if ( mNowClock >= mLastClock + 200 ) {
mLastClock = mNowClock;
Update();
}
}
void Draw() {
for ( int y = 0; y < FIELD_SIZE + 2; y++ ) {
for ( int x = 0; x < FIELD_SIZE + 2; x++ ) {
int chip = mField[ y ][ x ];
char* chipTex[ 4 ] = { " ", "#", "●", "◯" };
DrawString( x * CHIP_SIZE, y * CHIP_SIZE, chipTex[ chip ], GetColor( 255, 255, 255 ) );
}
}
}
現在、プレイヤー(●)を自由に動かすところまでできたのですが、
壁にあたったときの処理がうまくいきません。
はじめに、
if ( 1 < mPX && toChipSize( FIELD_SIZE - CHIP_SIZE ) < mPX &&
1 < mPY && toChipSize( FIELD_SIZE - CHIP_SIZE ) < mPY ) {
mPX += mMoveX[ mOnKey ], mPY += mMoveY[ mOnKey ];
}
XとYを分けてやってみたのですが、挙動がおかしいです。
(実際には、上や下には行かず、右や左にも何回もキーを押さないと動きません。止まるときもあります)
やりたいこととしては、壁に突っ込んだら(同じ座標になったら)ゲームオーバー判定で、そうじゃないなら普通に動けるようにしたいです。
またまたよろしくおねがいします。