![見出し画像](https://assets.st-note.com/production/uploads/images/96334290/rectangle_large_type_2_9c97a2d9630ad42af7205ec203b342f9.jpeg?width=1200)
ランダムな座標:サンプルプログラム
DXライブラリを使ったサンプルプログラムになります。
![](https://assets.st-note.com/production/uploads/images/96863486/picture_pc_dcfe0c5f84f4a041eaad276863aafcf6.gif)
・一定時間経過すると画像の表示座標をランダムに変更しています。
#include "DxLib.h"
// グラフィックハンドル
int GHandle;
// プレイヤー座標
int PlayerX, PlayerY;
// 時間を数えるカウンター
int Count;
// ==============================
// *** 初期化処理 ***
// ==============================
void Game_Init()
{
// プレイヤー.png 画像をメモリへの読みこんでグラフィックハンドルを GHandle へ保存
GHandle = LoadGraph( "プレイヤー.png" );
// プレイヤーの最初の座標
PlayerX = 100;
PlayerY = 100;
// 0から開始
Count = 0;
}
// ==============================
// *** 更新処理 ***
// ==============================
void Game_Update()
{
// カウントを進める
Count++;
// 100カウント数えたら
if( Count == 100 ){
// また0に戻す
Count = 0;
// プレイヤー座標をランダムに設定
PlayerX = GetRand( 450 );
PlayerY = GetRand( 150 );
}
}
// ==============================
// *** 描画処理 ***
// ==============================
void Game_Draw()
{
// 画面( PlayerX, PlayerY )の場所にグラフィックハンドルに保存された画像を描画します
DrawGraph( PlayerX, PlayerY, GHandle, TRUE );
}
// ==============================
// *** 終了処理 ***
// ==============================
void Game_End()
{
// 読み込んだ画像のグラフィックハンドルを削除
DeleteGraph( GHandle );
}
// ******************************
// メイン関数
// ******************************
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;
}