Re:マリオ風アクションゲームのブロックとの当たり判定
Posted: 2010年6月14日(月) 13:54
by KEYONN_
//その1です
#include"DxLib.h"
#define CHIP_SIZE 32
enum object{DOKAN};
int ScrollX;
int ScrollY;
int BGTbl[14][16]={
5,5,5,5, 5,5,5,5, 5,5,5,5, 5,5,5,5,
5,5,5,5, 5,5,5,5, 5,5,5,5, 5,5,5,5,
5,5,5,5, 5,5,5,5, 5,5,5,5, 5,5,5,5,
5,5,5,5, 5,5,5,5, 5,5,5,5, 5,5,5,5,
5,5,5,5, 5,5,5,5, 5,5,5,5, 5,5,5,5,
5,5,5,5, 5,5,5,5, 5,5,5,5, 5,5,5,5,
5,5,5,5, 5,5,5,5, 5,5,5,5, 5,5,5,5,
5,5,5,5, 5,5,5,5, 5,5,5,5, 5,5,5,5,
5,5,5,5, 5,1,1,5, 5,5,5,5, 5,5,5,5,
1,1,5,1, 1,5,5,1, 1,1,1,1, 1,2,1,1,
5,5,5,5, 5,5,5,5, 5,5,5,5, 5,5,5,5,
5,5,5,5, 5,5,5,5, 5,5,5,5, 5,5,5,5,
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0
};
typedef struct HITBG{
int x;
int y;
int width;
int height;
int kind;
}HITBG;
class CBG{
public:
//配列BGデータ
int num;
//BG番号
HITBG HBG[14][16];
int objectnum;
//オブジェクト番号
int table[16*14];
CBG()
{
}
~CBG()
{
//特になし
}
void DrawChip(int num,int x,int y)
{
DrawGraph(x,y,table[ num ],TRUE);
}
void DrawBG_Object(int x,int y,int objectnum)
{
}
void DrawBG()
{
int StartX=ScrollX/32,StartY=ScrollY/32;
int EndX=(ScrollX+512-1)/32,EndY=(ScrollY+448-1)/32;
for(int i=StartY;i<=EndY;i++)
{
for(int j=StartX;j<=EndX;j++)
{
DrawChip(BGTbl[j],j*32-ScrollX,i*32-ScrollY);
}
}
}
void HitCheck(int &x,int &y,int width,int height)
{
int i,j;
for(i=0;i<14;i++)
{
for(j=0;j<16;j++)
{
if(HBG[j].kind!=6)
{
if(y+height>=HBG[j].y &&
y+height<=HBG[j].y+32)
{
/*
if(x+width>=HBG[j].x &&
x+width<=HBG[j].x)
{
//左に接触
x=HBG[j].x+width;
}
if(x<=HBG[j].x+32 &&
x>=HBG[j].x+32)
{
//右に接触
x=HBG[j].x-32;
}
*/
if(x+width>=HBG[i][j].x &&
x+width<=HBG[i][j].x+32)
{
if(y+height>=HBG[i][j].y &&
y+height<HBG[i][j].y)
{
//下
y=HBG[i][j].y+height;
}
if(y>=HBG[i][j].y+32 &&
y<HBG[i][j].y+32)
{
//上
y=HBG[i][j].y-32;
if(HBG[i][j].kind==2)
{
}
}
}
}
}
}
}
}
};
CBG BG;
class Character{
public:
int GraphHandle;
float x,y;
int width,height;
void Show()
{
DrawGraph(x,y,GraphHandle,TRUE);
}
Character()
{
//特になし
}
Character(char str[/url])
{
GraphHandle=LoadGraph(str);
}
~Character()
{
//特になし
}
};
enum Dir{RIGHT,LEFT,UP,DOWN};
enum Kind{SMALL,BIG,FIRE};
Re:マリオ風アクションゲームのブロックとの当たり判定
Posted: 2010年6月14日(月) 13:55
by KEYONN_
//その2です。
class Mario : public Character{
public:
unsigned char kisuu;
bool muki;
int joutai;
int vx,vy;
unsigned char hosuu;
void Show()
{
hosuu=hosuu%2;
switch(joutai)
{
case SMALL:
DrawRectGraph(x,y,
hosuu*32, 0, 32, 32,
GraphHandle, TRUE, muki*1 ) ;
break;
case BIG:
DrawRectGraph(x,y,
0,32,64,64,
GraphHandle ,TRUE,muki*1);
break;
case FIRE:
DrawRectGraph(x,y,
0,64,64,64,
GraphHandle ,TRUE,muki*1);
break;
}
}
void Jump()
{
static bool jump=false;
float jump_speed = vx==2 ? -14.0: -20.0;
float jump_kasoku=1.0f;
float groundy=448-96;
float speed=0.2f;
if(!jump)
{
if(CheckHitKey(KEY_INPUT_X)){
jump=true;
vy=jump_speed;
}
}else{
vy+=jump_kasoku;
y+=vy;
if(vy>0 && y>=groundy){
jump=false;
y=groundy;
}
if(vy<0 && !CheckHitKey(KEY_INPUT_X)){
vy=0;
}
}
if(x<0) x=0;
if(x>512-1)x=512-1;
}
void SayuuMove()
{
if(CheckHitKey(KEY_INPUT_RIGHT))
{
if(CheckHitKey(KEY_INPUT_Z))
{
vx=5;
}else{
vx=2;
}
AutoMove(RIGHT,vx);
muki=RIGHT;
hosuu++;
}
if(CheckHitKey(KEY_INPUT_LEFT))
{
if(CheckHitKey(KEY_INPUT_Z))
{
vx=5;
}else{
vx=2;
}
AutoMove(LEFT,vx);
muki=LEFT;
hosuu++;
}
if(!CheckHitKey(KEY_INPUT_RIGHT) && !CheckHitKey(KEY_INPUT_LEFT))
{
hosuu=0;
}
}
void AutoMove(int muki,int speed)
{
switch(muki)
{
case RIGHT:
x+=speed;
break;
case LEFT:
x-=speed;
break;
case UP:
y-=speed;
break;
case DOWN:
y+=speed;
break;
}
}
Mario()
{
kisuu=3;
muki=RIGHT;
joutai=SMALL;
x=(512-32)/2;
y=448-96;
hosuu=0;
vx=2;
vy=0;
}
};
Mario P;
void Init()
{
P.GraphHandle=LoadGraph("./mario.png");
ScrollX=0;
ScrollY=0;
LoadDivGraph( "./BG.png" , 16*14 ,
16 , 14 ,
32 , 32 , BG.table ) ;
int i,j;
for(i=0;i<14;i++)
{
for(j=0;j<16;j++)
{
BGTbl[j]=BGTbl[j]+1;
BG.HBG[j].kind=BGTbl[j];
BG.HBG[j].x=j*32;
BG.HBG[j].y=i*32;
BG.HBG[j].width=32;
BG.HBG[j].height=32;
}
}
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hP,LPSTR lpC,int nC)
{
SetGraphMode(512,448,32);
ChangeWindowMode(TRUE) ;
SetMainWindowText("SuperMarioBrothers"); //ウィンドウのタイトル
if(DxLib_Init() == -1) return (-1); //DXライブラリ初期化
//データ読み込み中と表示する
DrawString( 0 , 0 , "データ読み込み中" , GetColor(255,255,255) );
//各種変数の初期化など
Init();
//乱数の初期化
srand((unsigned)GetTickCount());
//メインループ
while( ProcessMessage()==0 && CheckHitKey(KEY_INPUT_ESCAPE)==0){
ProcessMessage();
ClsDrawScreen(); //画面を初期化
BG.DrawBG();
P.SayuuMove();
P.Jump();
int a,b;
a=(int)P.x;
b=(int)P.y;
BG.HitCheck(a,b,32,32);
P.x=a;
P.y=b;
P.Show();
//DrawFormatString(0,40,GetColor(255,255,255),"angle=%f",angle);
//DrawFormatString(0,60,GetColor(255,255,255),"angle2=%f",angle2);
ScreenFlip(); //フリップする
}
DxLib_End(); //DXライブラリ終了処理
return (0); //終了
}