#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;
}
せっかく DrawGraph で裏画面に描いたものを ClearDrawScreen で消して、
その消えた画面を ScreenFlip で表画面にしても、何も表示はされません。
LoadGraph をループの中で何度も実行してはいけません。
ループに入る前に一度だけ実行しておきましょう。
[code]
#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;
}
[/code]
グローバル変数はできるだけ使わないようにしましょう。
CheckHitKey の代わりに GetHitKeyStateAll を使いたければ、
[code]
#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;
}
[/code]