#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; } // プログラムは WinMain から始まります int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { SetGraphMode(1600, 680, 32); // ウィンドウの大きさを指定 ChangeWindowMode(TRUE); if (DxLib_Init() == -1) // DXライブラリ初期化処理 { return -1; // エラーが起きたら直ちに終了 } //キー取得用配列 //char key[256]; //1. 3x3マスの2次元配列 int pos[][3] = { {0, 0, 0 }, {0, 0, 0 }, {0, 0, 0 } }; int playerX = 0; // キャラのX座標 int playerY = 300; // キャラのY座表 //double a[2][2];//移動制限のための配列の変数 int x = 0; int y = 0; //グラフィックハンドル格納用配列 int gh[12]; LoadDivGraph("charall.png", 12, 3, 4, 49, 66, gh); DrawGraph(playerX, playerY, gh[8], FALSE);// プレイヤーの画像を描画 while (ScreenFlip() == 0 && ProcessMessage() == 0&& gpUpdateKey() == 0) { int 加算=40; int b = 40; // カーソルキーの右が押されている if (Key[KEY_INPUT_RIGHT] == 1) { for (int x = 0; x < 3; x++) { playerX = playerX +加算; if (playerX == x && playerY == y) { // プレイヤーのX座標を加算//左だけplayerX2とするとfor文ではないので、一回しか起きない、playerXだとplayerX = playerX + 加算より、加算されたあとのplayerXが再び右辺のplayerXに入り加算されるを繰り返すため∞に右に行けるのだ // 画面に出力 ScreenFlip(); // 画面をクリア ClearDrawScreen(); // プレイヤーの画像を描画 DrawGraph(playerX, playerY, gh[2], true);//DrawGraphはLoadDivGraphにより12分割された配列の一つである、gh[8]を描画できる関数である。 } } } else { while (DrawGraph(playerX, playerY, gh[8], true)); }//右を押されて加算されていく中で、もし加算されない間はキャラの描画はgh[8]にする。elseを付けることで条件を否定できる。 if (Key[KEY_INPUT_UP] == 1) { playerY = playerY - b; // プレイヤーのY座標を加算 // 画面に出力 ScreenFlip(); // 画面をクリア ClearDrawScreen(); // プレイヤーの画像を描画 DrawGraph(playerX, playerY, gh[5], true); } if (Key[KEY_INPUT_LEFT] == 1){ playerX = playerX - 加算; // プレイヤーのX座標を加算 // 画面をクリア ClearDrawScreen(); // プレイヤーの画像を描画 DrawGraph(playerX, playerY, gh[1], true); } if (Key[KEY_INPUT_DOWN] == 1) { playerY = playerY + b; // プレイヤーのY座標を加算 // 画面に出力 ScreenFlip(); // 画面をクリア ClearDrawScreen(); // プレイヤーの画像を描画 DrawGraph(playerX, playerY, gh[11], true); } } DxLib_End(); // DXライブラリ使用の終了処理 return 0; // ソフトの終了 }
以上のプログラムは合っているかわかりませんが、何を行いたいのか明確に書かせて頂きます。
int型の関数aの横移動(横に40加算する)をfor(int x=0;x<3;x++)により横には2回しか[0][0]と[1][0]と進み、その先の[2][0]に演算後a(=80)を代入したいです。
どうもfor文のxを使い加算する回数を2回で、かつ、[0]〜[2]のx座標内の範囲に出来ないため困っています。
どうか教えてください。
編集
もしかしたらint 加算=40;なしに3*3のマスのプロラグムが作れるかもしれません。
要は画像の最初の位置は[1][1]なので、if (Key[KEY_INPUT_RIGHT] == 1)より右のキーが押されたら[2][1]に進むように作りたいです。

回答1件
あなたの回答
tips
プレビュー