このプログラムでジャンプできるようにしたいのですが・・
Posted: 2012年12月07日(金) 18:21
#include "DxLib.h"
#include <math.h>
#define g 9.8
#define y_max 0.500
typedef struct
{
int x,y,img,muki,walk_flag;
}ch_t;
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow ){
int y_prev = 0, y_temp = 0;
int time1,time2;
int flag = 0;
double t;
bool jflag = false; //ジャンプしているかを見るフラグ
int image[24];
char Key[256];
int cnt = 0;
int Green = GetColor( 0, 255, 0 );// 緑のコード
ch_t ch;
if( ChangeWindowMode(TRUE) != DX_CHANGESCREEN_OK || DxLib_Init() == -1 ) return -1; //ウィンドウ化と初期化処理
ch.x = 320;
ch.y = 320;
ch.walk_flag = 0;
ch.muki = 0;
SetDrawScreen( DX_SCREEN_BACK );//描画先を裏画面に設定
LoadDivGraph( "画像/6chara.png" , 24 , 2 , 12 , 55 , 70 , image );//画像を分割してimage配列に保存
while(!ProcessMessage() && !ClearDrawScreen() && !GetHitKeyStateAll( Key ) && !Key[KEY_INPUT_ESCAPE]){
//↑メッセージ処理 ↑画面をクリア ↑キーボード入力状態取得 ↑ESCが押されると終了
if(ch.x%32==0 && ch.y%32==0){ //座標が32で割り切れたら入力可能
//歩くフラグを立てる。
ch.walk_flag=1;
//ジャンプ処理//
if(jflag == true)
{
y_temp = ch.y;
ch.y += ( ch.y - y_prev ) + 1;
y_prev = y_temp;
if( ch.y == 320)
{
jflag = false;
}
}
if( Key[ KEY_INPUT_RETURN ] == 1 )
{ //エンターが押されたら
time1 = GetNowCount(); //time1にエンターが押された時の時間を格納
flag=1; //飛び上がりフラグを立てる。
}
if(flag==1)
{
time2 = GetNowCount() ; // 現在経過時間を得る
t = (double)(time2 - time1) / 1000.000; // ミリ秒を秒に変換して、エンターが押されてからの経過時間を計算
ch.y = (int)((sqrt ( 2.00 * g * y_max) * t - 0.500 * g * t * t ) * 320.000 / y_max);//y座標を計算
if(ch.y>=0) // 1回目に回って来たか、画面内にy座標がある時
DrawGraph( ch.x , 320-ch.y , image[8] , TRUE );//画像を描画
else
ch.walk_flag=0;
flag=0; // 画面外に来ると、飛び上がりフラグを戻す
}
if ( Key[ KEY_INPUT_RIGHT ] == 1 ) //上ボタンが押されたら
{
ch.muki=0; //右向きフラグを立てる
if ( Key[ KEY_INPUT_UP ] == 1 )
ch.muki=2;
if( Key[ KEY_INPUT_DOWN] == 1 ) //下ボタンが押されたら
ch.muki=6; //右下向きフラグを立てる
}
else if( Key[ KEY_INPUT_LEFT ] == 1 ) //左ボタンが押されたら
{
ch.muki=1; //左向きフラグを立てる
if ( Key[ KEY_INPUT_UP ] == 1 )
ch.muki=4;
if( Key[ KEY_INPUT_DOWN] == 1 ) //下ボタンが押されたら
ch.muki=7; //左下向きフラグを立てる
}
else if( Key[ KEY_INPUT_UP ] == 1 /*&& jflag == false*/ ) //上ボタンが押されたら
{
//jflag = true;
//y_prev = ch.y;
//ch.y = ch.y - 20;
ch.muki=3; //上向きフラグを立てる
}
else //何のボタンも押されてなかったら
ch.walk_flag=0; //歩かないフラグを立てる
}
if(ch.walk_flag==1){ //歩くフラグが立っていたら
if (ch.muki==0) //右向き
ch.x++;
else if(ch.muki==1) //左向き
ch.x--;
else if(ch.muki ==3 /*&& jflag == false*/) //上向き
{
if(jflag == false)
{
jflag = true;
y_prev = ch.y;
ch.y-=20;
}
}
else if(ch.muki==6) //右下向き(スライディング)
ch.x+=2;
else if(ch.muki==7) //左下向き(スライディング)
ch.x-=2;
else if(ch.muki==2) //右上向き
{
ch.x++;
ch.y--;
}
else if(ch.muki==4) //左上向き
{
ch.x--;
ch.y--;
}
}
ch.img=image[(ch.x%32+ch.y%32)/16 + ch.muki*2]; //画像をセット
DrawGraph( ch.x , ch.y , ch.img , TRUE ) ;//画像を描画
ScreenFlip();//裏画面を表画面に反映
}
DxLib_End();
return 0;
}
#include <math.h>
#define g 9.8
#define y_max 0.500
typedef struct
{
int x,y,img,muki,walk_flag;
}ch_t;
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow ){
int y_prev = 0, y_temp = 0;
int time1,time2;
int flag = 0;
double t;
bool jflag = false; //ジャンプしているかを見るフラグ
int image[24];
char Key[256];
int cnt = 0;
int Green = GetColor( 0, 255, 0 );// 緑のコード
ch_t ch;
if( ChangeWindowMode(TRUE) != DX_CHANGESCREEN_OK || DxLib_Init() == -1 ) return -1; //ウィンドウ化と初期化処理
ch.x = 320;
ch.y = 320;
ch.walk_flag = 0;
ch.muki = 0;
SetDrawScreen( DX_SCREEN_BACK );//描画先を裏画面に設定
LoadDivGraph( "画像/6chara.png" , 24 , 2 , 12 , 55 , 70 , image );//画像を分割してimage配列に保存
while(!ProcessMessage() && !ClearDrawScreen() && !GetHitKeyStateAll( Key ) && !Key[KEY_INPUT_ESCAPE]){
//↑メッセージ処理 ↑画面をクリア ↑キーボード入力状態取得 ↑ESCが押されると終了
if(ch.x%32==0 && ch.y%32==0){ //座標が32で割り切れたら入力可能
//歩くフラグを立てる。
ch.walk_flag=1;
//ジャンプ処理//
if(jflag == true)
{
y_temp = ch.y;
ch.y += ( ch.y - y_prev ) + 1;
y_prev = y_temp;
if( ch.y == 320)
{
jflag = false;
}
}
if( Key[ KEY_INPUT_RETURN ] == 1 )
{ //エンターが押されたら
time1 = GetNowCount(); //time1にエンターが押された時の時間を格納
flag=1; //飛び上がりフラグを立てる。
}
if(flag==1)
{
time2 = GetNowCount() ; // 現在経過時間を得る
t = (double)(time2 - time1) / 1000.000; // ミリ秒を秒に変換して、エンターが押されてからの経過時間を計算
ch.y = (int)((sqrt ( 2.00 * g * y_max) * t - 0.500 * g * t * t ) * 320.000 / y_max);//y座標を計算
if(ch.y>=0) // 1回目に回って来たか、画面内にy座標がある時
DrawGraph( ch.x , 320-ch.y , image[8] , TRUE );//画像を描画
else
ch.walk_flag=0;
flag=0; // 画面外に来ると、飛び上がりフラグを戻す
}
if ( Key[ KEY_INPUT_RIGHT ] == 1 ) //上ボタンが押されたら
{
ch.muki=0; //右向きフラグを立てる
if ( Key[ KEY_INPUT_UP ] == 1 )
ch.muki=2;
if( Key[ KEY_INPUT_DOWN] == 1 ) //下ボタンが押されたら
ch.muki=6; //右下向きフラグを立てる
}
else if( Key[ KEY_INPUT_LEFT ] == 1 ) //左ボタンが押されたら
{
ch.muki=1; //左向きフラグを立てる
if ( Key[ KEY_INPUT_UP ] == 1 )
ch.muki=4;
if( Key[ KEY_INPUT_DOWN] == 1 ) //下ボタンが押されたら
ch.muki=7; //左下向きフラグを立てる
}
else if( Key[ KEY_INPUT_UP ] == 1 /*&& jflag == false*/ ) //上ボタンが押されたら
{
//jflag = true;
//y_prev = ch.y;
//ch.y = ch.y - 20;
ch.muki=3; //上向きフラグを立てる
}
else //何のボタンも押されてなかったら
ch.walk_flag=0; //歩かないフラグを立てる
}
if(ch.walk_flag==1){ //歩くフラグが立っていたら
if (ch.muki==0) //右向き
ch.x++;
else if(ch.muki==1) //左向き
ch.x--;
else if(ch.muki ==3 /*&& jflag == false*/) //上向き
{
if(jflag == false)
{
jflag = true;
y_prev = ch.y;
ch.y-=20;
}
}
else if(ch.muki==6) //右下向き(スライディング)
ch.x+=2;
else if(ch.muki==7) //左下向き(スライディング)
ch.x-=2;
else if(ch.muki==2) //右上向き
{
ch.x++;
ch.y--;
}
else if(ch.muki==4) //左上向き
{
ch.x--;
ch.y--;
}
}
ch.img=image[(ch.x%32+ch.y%32)/16 + ch.muki*2]; //画像をセット
DrawGraph( ch.x , ch.y , ch.img , TRUE ) ;//画像を描画
ScreenFlip();//裏画面を表画面に反映
}
DxLib_End();
return 0;
}