キー入力で画像を変える方法

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
dfordcus
記事: 1
登録日時: 6年前

キー入力で画像を変える方法

#1

投稿記事 by dfordcus » 6年前

キー入力をして画像を変更する方法が知りたいです。

アクションゲームのように、
右へ移動するボタンを押したら右を向いた画像が表示されて、
左へ移動するボタンを押したら左を向いた画像が表示されるようにしたいです。

コードです↓

コード:

#include "DxLib.h"

int APlayerX, APlayerY;
double AJumpPower;

int APlayerUGraph;
int APlayerRGraph;
int APlayerLGraph;

int Key[256]; 

int gpUpdateKey() {
	char tmpKey[256];
	GetHitKeyStateAll(tmpKey);
	for (int i = 0; i < 256; i++) {
		if (tmpKey[i] != 0) {
			Key[i]++;   
		}
		else {              
			Key[i] = 0;   // 0にする
		}
	}
	return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
{
SetGraphMode(640, 480, 16);

if (DxLib_Init() == -1)	
	{
		return -1;				
	}

SetDrawScreen(DX_SCREEN_BACK);

APlayerX = 0;
APlayerY = 0;
AJumpPower = 0;

while(ProcessMessage()==0&&CheckHitKey(KEY_INPUT_ESCAPE) == 0)
	{
		APlayerUGraph = LoadGraph("APU.bmp"); //上向き
		APlayerRGraph = LoadGraph("APR.bmp"); //右向き
		APlayerLGraph = LoadGraph("APL.bmp"); //左向き
		
		if (CheckHitKey(KEY_INPUT_D) >= 1) {
			APlayerX += 3;
			DrawGraph(APlayerX, APlayerY, APlayerRGraph, TRUE);
		}

		if (CheckHitKey(KEY_INPUT_A) >= 1) {
			APlayerX -= 3;
			DrawGraph(APlayerX, APlayerY, APlayerLGraph, TRUE);
		}
		
APlayerY -= AJumpPower;
AJumpPower -= 0.7;

		if (APlayerY > 400)
		{
			APlayerY = 400;
			AJumpPower = 0;
		}
		
		if ((CheckHitKey(KEY_INPUT_W) != 0) && APlayerY == 400) {
			AJumpPower = 20;
			DrawGraph(APlayerX, APlayerY, APlayerUGraph, FALSE);
		}
		
		ClearDrawScreen();

		ScreenFlip();
	}		
	DxLib_End();	
	return 0;			
}


これでは表示されませんでした。
初心者なので出来るだけ詳しくお願いします...

かずま

Re: キー入力で画像を変える方法

#2

投稿記事 by かずま » 6年前

せっかく DrawGraph で裏画面に描いたものを ClearDrawScreen で消して、
その消えた画面を ScreenFlip で表画面にしても、何も表示はされません。

LoadGraph をループの中で何度も実行してはいけません。
ループに入る前に一度だけ実行しておきましょう。

コード:

#include "DxLib.h"

int WINAPI WinMain(HINSTANCE hi, HINSTANCE hp, LPSTR cl, int cs)
{
	SetGraphMode(640, 480, 16); // ChangeWindowMode(TRUE);
	if (DxLib_Init() == -1) return -1;
	SetDrawScreen(DX_SCREEN_BACK);

	int APlayerX = 0, APlayerY = 0;
	double AJumpPower = 0;

	int APlayerUGraph = LoadGraph("APU.bmp");	//上向き
	int APlayerRGraph = LoadGraph("APR.bmp");	//右向き
	int APlayerLGraph = LoadGraph("APL.bmp");	//左向き

	while (!ProcessMessage() && !CheckHitKey(KEY_INPUT_ESCAPE)) {
		ClearDrawScreen();

		APlayerY -= AJumpPower;
		AJumpPower -= 0.7;
		if (APlayerY > 400) { APlayerY = 400; AJumpPower = 0; }

		DrawGraph(APlayerX, APlayerY, APlayerUGraph, FALSE);
		if (CheckHitKey(KEY_INPUT_D)) {
			APlayerX += 3;
			DrawGraph(APlayerX, APlayerY, APlayerRGraph, FALSE);
		}
		if (CheckHitKey(KEY_INPUT_A)) {
			APlayerX -= 3;
			DrawGraph(APlayerX, APlayerY, APlayerLGraph, FALSE);
		}
		if (CheckHitKey(KEY_INPUT_W) && APlayerY == 400)
			AJumpPower = 20;

		ScreenFlip();
	}
	DxLib_End();
	return 0;
}
グローバル変数はできるだけ使わないようにしましょう。

CheckHitKey の代わりに GetHitKeyStateAll を使いたければ、

コード:

#include "DxLib.h"

int gpUpdateKey(int *Key)
{
	char tmpKey[256];
	GetHitKeyStateAll(tmpKey);
	for (int i = 0; i < 256; i++)
		if (tmpKey[i] != 0) Key[i]++;   
		else Key[i] = 0;
	return 0;
}

int WINAPI WinMain(HINSTANCE hi, HINSTANCE hp, LPSTR cl, int cs)
{
	SetGraphMode(640, 480, 16); // ChangeWindowMode(TRUE);
	if (DxLib_Init() == -1) return -1;
	SetDrawScreen(DX_SCREEN_BACK);

	int Key[256] = { 0 };
	int APlayerX = 0, APlayerY = 0;
	double AJumpPower = 0;
	int APlayerUGraph = LoadGraph("APU.bmp");	//上向き
	int APlayerRGraph = LoadGraph("APR.bmp");	//右向き
	int APlayerLGraph = LoadGraph("APL.bmp");	//左向き

	while (!ProcessMessage()) {
		ClearDrawScreen();
		gpUpdateKey(Key);
		if (Key[KEY_INPUT_ESCAPE]) break;

		APlayerY -= AJumpPower;
		AJumpPower -= 0.7;
		if (APlayerY > 400) { APlayerY = 400; AJumpPower = 0; }

		DrawGraph(APlayerX, APlayerY, APlayerUGraph, FALSE);
		if (Key[KEY_INPUT_D]) {
			APlayerX += 3;
			DrawGraph(APlayerX, APlayerY, APlayerRGraph, FALSE);
		}
		if (Key[KEY_INPUT_A]) {
			APlayerX -= 3;
			DrawGraph(APlayerX, APlayerY, APlayerLGraph, FALSE);
		}
		if (Key[KEY_INPUT_W] && APlayerY == 400)
			AJumpPower = 20;

		ScreenFlip();
	}
	DxLib_End();
	return 0;
}

かずま

Re: キー入力で画像を変える方法

#3

投稿記事 by かずま » 6年前

かずま さんが書きました:
6年前
LoadGraph をループの中で何度も実行してはいけません。
ループに入る前に一度だけ実行しておきましょう。
LoadGraph は、新たにメモリを確保して、ファイルから
そこに画像データを読み込みます。何度も実行すると、
大量にメモリを確保し、そのうち動かなくなります。
使い終わったら、DeleteGraph でメモリを解放すればよい
のですが、LoadGraph は、ファイルをオープンしてそこから
画像データを読み込むという処理で、時間がかかります。
やはり、実行はループ前の 1回だけにするべきです。
かずま さんが書きました:
6年前

コード:

		DrawGraph(APlayerX, APlayerY, APlayerUGraph, FALSE);
		if (CheckHitKey(KEY_INPUT_D)) {
			APlayerX += 3;
			DrawGraph(APlayerX, APlayerY, APlayerRGraph, FALSE);
		}
DrawGraph を重ねて行うのは、最後の引数を TRUE にすると
変なことになるので、次のように修正します。

コード:

#include "DxLib.h"

int WINAPI WinMain(HINSTANCE hi, HINSTANCE hp, LPSTR cl, int cs)
{
	SetGraphMode(640, 480, 16); // ChangeWindowMode(TRUE);
	if (DxLib_Init() == -1) return -1;
	SetDrawScreen(DX_SCREEN_BACK);
	SetBackgroundColor(0, 128, 0);

	int APlayerX = 0, APlayerY = 0;
	double AJumpPower = 0;

	int APlayerUGraph = LoadGraph("APU.bmp");	//上向き
	int APlayerRGraph = LoadGraph("APR.bmp");	//右向き
	int APlayerLGraph = LoadGraph("APL.bmp");	//左向き

	while (!ProcessMessage() && !CheckHitKey(KEY_INPUT_ESCAPE)) {
		ClearDrawScreen();
		APlayerY -= AJumpPower;
		AJumpPower -= 0.7;
		if (APlayerY > 400) APlayerY = 400, AJumpPower = 0;
		int gh = APlayerUGraph;
		if (CheckHitKey(KEY_INPUT_D)) APlayerX += 3, gh = APlayerRGraph;
		if (CheckHitKey(KEY_INPUT_A)) APlayerX -= 3, gh = APlayerLGraph;
		if (CheckHitKey(KEY_INPUT_W) && APlayerY == 400) AJumpPower = 20;
		DrawGraph(APlayerX, APlayerY, gh, FALSE);
		ScreenFlip();
	}
	DxLib_End();
	return 0;
}

かずま

Re: キー入力で画像を変える方法

#4

投稿記事 by かずま » 6年前

かずま さんが書きました:
6年前

コード:

		DrawGraph(APlayerX, APlayerY, gh, FALSE);
FALSE を TRUE に訂正します。

コード:

		DrawGraph(APlayerX, APlayerY, gh, TRUE);

Math

Re: キー入力で画像を変える方法

#5

投稿記事 by Math » 6年前

大まかには グローバル変数宣言→初期化Init()→ループ→クリア
      操作Update()→Draw()→フリップ
とすればよいから
Debug用のX,Y座標も表示して

コード:

#include "DxLib.h"
////////////////////////////////////////////////////////////////////////////////
// グローバル変数宣言

int APlayerX, APlayerY;
double AJumpPower;

int APlayerUGraph;
int APlayerRGraph;
int APlayerLGraph;
int Draw_Graph; // 追加

int Key[256]; 
////////////////////////////////////////////////////////////////////////////////
int gpUpdateKey() {
	char tmpKey[256];
	GetHitKeyStateAll(tmpKey);
	for (int i = 0; i < 256; i++) {
		if (tmpKey[i] != 0) {
			Key[i]++;   
		}
		else {              
			Key[i] = 0;   // 0にする
		}
	}
	return 0;
}
////////////////////////////////////////////////////////////////////////////////
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
{
////////////////////////////////////////////////////////////////////////////////
// 初期化  Init();
	
//ウィンドウモード変更
ChangeWindowMode(TRUE);

SetGraphMode(640, 480, 16);

if (DxLib_Init() == -1)	
	{
		return -1;				
	}

SetDrawScreen(DX_SCREEN_BACK);

APlayerX = 0;
APlayerY = 0;
AJumpPower = 0;

APlayerUGraph = LoadGraph("画像/image003.png"); //上向き
APlayerRGraph = LoadGraph("画像/image002.png"); //右向き
APlayerLGraph = LoadGraph("画像/image001.png"); //左向き
///////////////////////////////////////////////////////////////////////////////

while(ProcessMessage()==0 && CheckHitKey(KEY_INPUT_ESCAPE) == 0)
	{
		ClearDrawScreen();
// 変数チェック
DrawFormatString( 0, 0, GetColor(255,255,255), "APlayerX=%d,APlayerY=%d",APlayerX,APlayerY) ;

		// APlayerUGraph = LoadGraph("画像/image003.png"); //上向き
		// APlayerRGraph = LoadGraph("画像/image002.png"); //右向き
		// APlayerLGraph = LoadGraph("画像/image001.png"); //左向き
		
		///////////////////////////////////////////////////////////////
		// Update();
		if (CheckHitKey(KEY_INPUT_D) >= 1) {
			APlayerX += 3;
			// DrawGraph(APlayerX, APlayerY, APlayerRGraph, TRUE);
			Draw_Graph=APlayerRGraph;
		}

		if (CheckHitKey(KEY_INPUT_A) >= 1) {
			APlayerX -= 3;
			// DrawGraph(APlayerX, APlayerY, APlayerLGraph, TRUE);
			Draw_Graph=APlayerLGraph;
		}
		
APlayerY -= AJumpPower;
AJumpPower -= 0.7;

		if (APlayerY > 400)
		{
			APlayerY = 400;
			AJumpPower = 0;
		}
		
		if ((CheckHitKey(KEY_INPUT_W) != 0) && APlayerY == 400) {
			AJumpPower = 20;
			// DrawGraph(APlayerX, APlayerY, APlayerUGraph, FALSE);
			Draw_Graph=APlayerUGraph;
		}
		
		// ClearDrawScreen();
		////////////////////////////////////////////////////////////////
		// Draw()
		DrawGraph(APlayerX, APlayerY, Draw_Graph, FALSE);
		
		ScreenFlip();
	}		
	DxLib_End();	
	return 0;			
}
VisualStudio2019Communityで実行してみました。
http://www2.koyoen.birdview.co.jp/~abcx ... -20-x-.PNG

http://www2.koyoen.birdview.co.jp/~abcxyz/draw.gif

Math

Re: キー入力で画像を変える方法

#6

投稿記事 by Math » 6年前

DXライブラリーも今年4月updateのVer3.20e にしました。

返信

“C言語何でも質問掲示板” へ戻る