マウスに向かってプレイヤー移動
実装イメージ
・クリックすることでプレイヤーをマウスに向かって移動させます。
・マウスに近づくにつれて移動スピードが遅くなっていきます。
サンプルプログラム
#include "DxLib.h"
#include <math.h>
// プレイヤー画像ハンドル
int PlayerGraph;
// プレイヤー座標
float PlayerX;
float PlayerY;
// ターゲット座標
float TargetX;
float TargetY;
// ==============================
// *** 初期化処理 ***
// ==============================
void Game_Init()
{
// プレイヤー画像の読み込み
PlayerGraph = LoadGraph( "プレイヤー.png" );
PlayerX = TargetX = 100.0f;
PlayerY = TargetY = 100.0f;
}
// ==============================
// *** 更新処理 ***
// ==============================
void Game_Update()
{
// マウス左ボタンを押しているとき
if( GetMouseInput() & MOUSE_INPUT_LEFT ){
int MouseX, MouseY;
// マウスの位置を取得
GetMousePoint( &MouseX, &MouseY );
// マウス座標をターゲット座標に設定
TargetX = MouseX;
TargetY = MouseY;
}
// ターゲット座標までの差を計算
float MoveX = TargetX - PlayerX;
float MoveY = TargetY - PlayerY;
// その差を小さくする
MoveX *= 0.1f;
MoveY *= 0.1f;
// 座標を進める
PlayerX += MoveX;
PlayerY += MoveY;
}
// ==============================
// *** 描画処理 ***
// ==============================
void Game_Draw()
{
// プレイヤーの描画
DrawRotaGraphF( PlayerX, PlayerY, 1.0f, 0.0f, PlayerGraph, TRUE );
}
// ==============================
// *** 終了処理 ***
// ==============================
void Game_End()
{
DeleteGraph( PlayerGraph );
}
// ******************************
// メイン関数
// ******************************
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
ChangeWindowMode( TRUE );
SetGraphMode( 500, 200, 16 );
if( DxLib_Init() == -1 ){
return -1;
}
SetDrawScreen( DX_SCREEN_BACK );
Game_Init(); // *** 初期化処理 ***
while( ProcessMessage() == 0 && CheckHitKey( KEY_INPUT_ESCAPE ) == 0 ){
Game_Update(); // *** 更新処理 ***
ClearDrawScreen();
Game_Draw(); // *** 描画処理 ***
ScreenFlip();
}
Game_End(); // *** 終了処理 ***
DxLib_End();
return 0;
}