キャラクターは十字キーで移動で、スペースキーで弾を発射します。
どうすれば、このバグが直るのでしょうか?
開発環境はVisual C++ 2008 Express Editionです。
ソースコードを下に示します
#include "DxLib.h" #include <math.h> typedef struct{ double x, y; }ch_t; struct shot { //弾発射 double x, y; int flag; }; int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow ){ int image[16]; double rad = 0.0; // キャラクターの角度(rad) char Key[256]; ch_t ch; int n_flag = 1; //0:ななめ移動 1:十字移動 struct shot tama[10]; //弾10個 int i, counter = 0; if( ChangeWindowMode(TRUE) != DX_CHANGESCREEN_OK || DxLib_Init() == -1 ) return -1; //ウィンドウ化と初期化処理 ch.x =320; ch.y =160; for(i = 0; i < 10; i++) { //弾発射の準備 tama.x = ch.x - 16; tama.y = ch.y - 16; tama.flag = 0; } SetDrawScreen( DX_SCREEN_BACK ); //描画先を裏画面に設定 LoadDivGraph( "char.png" , 16 , 4 , 4 , 32 , 32 , image ) ;//画像を分割してimage配列に保存 while(!ProcessMessage() && !ClearDrawScreen() && !GetHitKeyStateAll( Key ) && !Key[KEY_INPUT_ESCAPE]){ if(counter < 5) counter++; else if(Key[KEY_INPUT_SPACE] == 1) { counter = 0; for(i = 0; i < 10; i++) { if(tama.flag == 0) { tama.flag = 1; break; } } } for(i = 0; i < 10; i++) { if(tama.flag == 1) { tama.y -= 8; DrawGraph(tama.x, tama.y, image[1], TRUE); if(tama.y < -32) { tama[i].x = ch.x - 16; tama[i].y = ch.y - 16; tama[i].flag = 0; } } } if(n_flag == 1){ //ななめフラグなしのとき if( Key[ KEY_INPUT_UP ] == 1 ) ch.y--; if( Key[ KEY_INPUT_LEFT ] == 1 ) ch.x--; if( Key[ KEY_INPUT_DOWN ] == 1 ) ch.y++; if( Key[ KEY_INPUT_RIGHT] == 1 ) ch.x++; } else{ //ななめフラグ立ってるとき if(Key[KEY_INPUT_UP] == 1 && Key[KEY_INPUT_RIGHT] == 1) { ch.y -= 1 / sqrt(2.0); ch.x += 1 / sqrt(2.0); } if(Key[KEY_INPUT_UP] == 1 && Key[KEY_INPUT_LEFT] == 1) { n_flag = 0; ch.y -= 1 / sqrt(2.0); ch.x -= 1 / sqrt(2.0); } if(Key[KEY_INPUT_DOWN] == 1 && Key[KEY_INPUT_RIGHT] == 1) { n_flag = 0; ch.y += 1 / sqrt(2.0); ch.x += 1 / sqrt(2.0); } if(Key[KEY_INPUT_DOWN] == 1 && Key[KEY_INPUT_LEFT] == 1) { n_flag = 0; ch.y += 1 / sqrt(2.0); ch.x -= 1 / sqrt(2.0); } } if(Key[KEY_INPUT_Z] == 1) //左回転 rad -= 0.05; else if(Key[KEY_INPUT_X] == 1) //右回転 rad += 0.05; DrawRotaGraph( ch.x, ch.y , 1.0 , rad , image[12] , TRUE ); ScreenFlip(); } DxLib_End(); return 0; }