作成手順として背景の表示、移動、加速、減速、ジャンプ、とやってきたのですが、マリオみたく長くボタンを押すと大きくジャンプするみたいなことができません。
色々なサイトのコードを参考にさせてもらっていて頑張ってみたのですが、そのジャンプの処理は実装することができませんでした。
C言語は少し前から授業で触った程度です。Dxライブラリも同時期に使い始めました。
どのようにしたら私が望んでいるようにできるのか教えていただけると助かります。
#include "DxLib.h"
int count=0;
char Key[256];
void wait_fanc();
void fps();
void GetKeyInput(int *KeyBuf);
struct Chara {
double x;
double y;
int g;
double spdX;
double spdY;
};
struct Chara mario;
int WINAPI WinMain(HINSTANCE hI, HINSTANCE hP, LPSTR lpC, int nC)
{
ChangeWindowMode(TRUE);
if(DxLib_Init() == -1) return(-1);
SetDrawScreen(DX_SCREEN_BACK);
mario.x = (double)0;
mario.y = (double)32;//480 - 32 * 2;
mario.g = LoadGraph("media\\test001.png");
mario.spdX = (double)0;
mario.spdY = (double)0;
while(ProcessMessage() == 0&& CheckHitKey(KEY_INPUT_ESCAPE) == 0) {
ClsDrawScreen();
int Bg_flag = 0;
int count_flag = 0;
int grav = 10;
int Buf[256];
GetKeyInput(Buf);
if(Bg_flag == 0){
DrawBox(0,0,640,480,GetColor(105,122,243),TRUE); //背景を水色に1 - 1
Bg_flag = 1;
}
fps();
count++;
//-------------------------------------------------------------------------
if(CheckHitKey(KEY_INPUT_LEFT) == 1){
if(CheckHitKey(KEY_INPUT_Z) == TRUE) {
mario.spdX -= 1.0;
}else {
mario.spdX -= 0.3;
}
mario.x += mario.spdX;
}
if(((CheckHitKey(KEY_INPUT_RIGHT)) == false) &&(CheckHitKey(KEY_INPUT_LEFT) == false) && (mario.spdX > 0)){
mario.spdX -= 0.3;
mario.x += mario.spdX;
count_flag += 1;
}
if(CheckHitKey(KEY_INPUT_RIGHT) == 1){
if(CheckHitKey(KEY_INPUT_Z) == TRUE) {
mario.spdX += 1.0;
}else {
mario.spdX += 0.3;
}
mario.x += mario.spdX;
}
if((CheckHitKey(KEY_INPUT_LEFT) == false) && ((CheckHitKey(KEY_INPUT_RIGHT)) == false) && (mario.spdX < 0)){
mario.spdX += 0.3;
mario.x += mario.spdX;
count_flag += 1;
}
mario.y -= mario.spdY;
mario.spdY -= 1;
if(mario.x < 0) mario.x = 0;
//if(mario.x > 640 - 32) mario.x = 640 - 32;
if((CheckHitKey(KEY_INPUT_Z))) {
if(mario.spdX > 8.0) mario.spdX = 8.0;
if(mario.spdX < -8.0) mario.spdX = -8.0;
}else {
if(mario.spdX > 5.0) mario.spdX = 5.0;
if(mario.spdX < -5.0) mario.spdX = -5.0;
}
if(count_flag > 1) {count_flag = 0; mario.spdX = 0;}
if(mario.y > 480 - 32) {mario.y = 480 - 32; mario.spdY = 0;}
if(mario.spdY > 30.0) mario.spdY = 30.0;
if(((CheckHitKey(KEY_INPUT_C)) == TRUE && mario.y == 480 -32) && (Buf[KEY_INPUT_C] > 0 && Buf[KEY_INPUT_C] < 10)) {
mario.spdY = 15;
}else if((CheckHitKey(KEY_INPUT_C)) == TRUE && mario.y == 480 -32 && (Buf[KEY_INPUT_C] >= 10)) {
mario.spdY = 20;
}
DrawGraph((int)mario.x, (int)mario.y, mario.g, TRUE);
//-------------------------------------------------------------------------
ScreenFlip();
wait_fanc();
}
DxLib_End();
return(0);
}
void wait_fanc(){
int term;
static int t=0;
term = GetNowCount()-t;
if(16-term>0)
Sleep(16-term);
t=GetNowCount();
return;
}
void fps(){
int i;
static int t=0,ave=0,f[60];
f[count%60]=GetNowCount()-t;
t=GetNowCount();
if(count%60==59){
ave=0;
for(i=0;i<60;i++)
ave+=f[i];
ave/=60;
}
if(ave!=0){
DrawFormatString(0, 0,GetColor(0,0,0),"%.1fFPS",1000.0/(double)ave);
DrawFormatString(0,20,GetColor(0,0,0),"%dms" ,ave);
}
return;
}
void GetKeyInput( int *KeyBuf ){
char damy[256];
GetHitKeyStateAll( damy ) ;
for( int i=0 ; i<256 ; i++ ){
if( damy[i] == 1 )
KeyBuf[i]++;
else
KeyBuf[i] = 0;
}
}