はいいんだけどそっからわからなすぎる!!
それぞれのオブジェクトに衝突したとき、方向を変えろという命令をFieldオブジェクトから下々のオブジェクトへと伝えるわけですが...
わからーん! もう何が何だか分からなくなって嫌になってくる...
今回の場合あたり判定は
ball --> barであり ball --> blocksです。
それらの判定はFieldのUpdate関数で行っているわけですが...ballはbarによっても変更されるしblocksによっても変更されるし...うわあああああああ!!! わかんなさすぎる。
投げそうです...
というわけでとりあえず動けばいいや精神でとりあえずFieldクラスのUpdateメンバに全部ぶん投げました、設計も絶対変だし...
っていってもどう設計すればいいのか分からん...
早くももう質問掲示板の力を借りるときが来たのかもしれません。
とりあえずVC++でDxLibが動く環境をお持ちの方であれば、ソースコードのコピペで動くようにしました。
突っ込みどころしかないコードですのでどんどん突っ込んでくださいお願いします(切実
► スポイラーを表示
#include "DxLib.h"
/******************全体で使う値*******************/
const int WINDOW_X = 640;
const int WINDOW_Y = 480;
/**********************クラス宣言***********************/
class Bar_t
{
private:
int x; //座標
int y;
int rect_x; //サイズ
int rect_y;
int color;
bool flag; //何かに使うかもしれない
public:
Bar_t();
~Bar_t(){}
int get_x(){ return x; }
int get_y(){ return y; }
int get_rect_x(){ return rect_x; }
int get_rect_y(){ return rect_y; }
void Init();
void Update();
void Draw();
};
class Ball_t
{
private:
//初期位置と現在位置用に二つ
float x[2]; //座標
float y[2];
float v_x[2]; //軸の速度
float v_y[2];
int r[2];
int color[2];
bool flag[2]; //何かに使うかもしれない
public:
Ball_t();
~Ball_t();
//ボールの位置とか半径を返す
float get_x(){ return x[0]; }
float get_y(){ return y[0]; }
int get_r(){ return r[0]; }
void Init(float arg_x, float arg_y, float arg_v_x, float arg_v_y, int arg_r);
void Reset();
//当たっているかどうか伝えるためのフラグ
void Update(bool hit);
void Draw();
};
class Blocks_t
{
private:
typedef struct Block
{
int x;
int y;
int rect_x;
int rect_y;
int color;
bool flag;
}Block;
const int block_x = 16;
const int block_y = 16;
const int blocksize_x = (int)580 / block_x;
const int blocksize_y = (int)256 / block_y;
const int space = (int)64 / block_x;
Block block[16][16];
public:
Blocks_t();
~Blocks_t();
//x,y番目のブロックの座標とそれぞれのサイズを返す
int get_x(int num_x, int num_y){ return block[num_x][num_y].x; }
int get_y(int num_x, int num_y){ return block[num_x][num_y].y; }
int get_rect_x(){ return blocksize_x; }
int get_rect_y(){ return blocksize_y; }
bool get_flag(int num_x, int num_y){ return block[num_x][num_y].flag; }
void Init();
void Reset();
void Update(bool hit[16][16]);//当たっているかどうか伝えるためのフラグ
void Draw();
};
class Field_t
{
private:
//とりあえずフィールド上のオブジェクトたち
Ball_t *ball;
Blocks_t *blocks;
Bar_t *bar;
//フィールドの状態
int state;
const int FIELD_X = 640;
const int FIELD_Y = 480;
public:
Field_t();
//あたり判定の関数、実装は現時点では適当(円 対 矩形)
bool HitTest(int arg_cx, int arg_cy,int arg_cr, int arg_bx,int arg_by,int arg_bvx,int arg_bvy);
void Init();
void Update();
void Draw();
void Reset();
};
/*************クラスの中身*************/
Bar_t::Bar_t()
{
x = WINDOW_X / 2;
y = WINDOW_Y - 40;
rect_x = 96;
rect_y = 16;
color = GetColor(64, 255, 255);
}
void Bar_t::Init()
{
}
void Bar_t::Update()
{
GetMousePoint(&x, NULL);
}
void Bar_t::Draw()
{
DrawBox(x, y, x + rect_x, y + rect_y, color, true);
}
Ball_t::Ball_t()
{
x[0] = 0.0;
y[0] = 0.0;
v_x[0] = 0.0;
v_y[0] = 0.0;
r[0] = 0;
color[0] = GetColor(255, 64, 32);
}
void Ball_t::Init(float arg_x,float arg_y,float arg_v_x,float arg_v_y,int arg_r)
{
x[0] = arg_x;
y[0] = arg_y;
v_x[0] = arg_v_x;
v_y[0] = arg_v_y;
r[0] = arg_r;
//初期位置に代入
x[1] = x[0];
y[1] = y[0];
v_x[1] = v_x[0];
v_y[1] = v_y[0];
r[1] = r[0];
}
void Ball_t::Reset()
{
//現在位置を初期位置に
x[0] = x[1];
y[0] = y[1];
v_x[0] = v_x[1];
v_y[0] = v_y[1];
r[0] = r[1];
}
void Ball_t::Update(bool hit)
{
//壁際では跳ね返るよね
if (x[0] Init(320, 400, 4.0, 4.0, 8);
}
void Field_t::Reset()
{
ball->Reset();
blocks->Reset();
}
void Field_t::Update()
{
//あたり判定を適当にする
//ボールのためにあたり判定の変数
bool Hit_forBall = false;
//ボール対バーに関して
bool ball_bar = HitTest(ball->get_x(), ball->get_y(), ball->get_r(), bar->get_x(), bar->get_y(), bar->get_rect_x(), bar->get_rect_y());
//ボール対ブロックに関して
bool ball_block[16][16];
for (int i = 0; i get_flag(i, k))
{
ball_block[i][k] = HitTest(ball->get_x(), ball->get_y(), ball->get_r(), blocks->get_x(i, k), blocks->get_y(i, k), blocks->get_rect_x(), blocks->get_rect_y());
}
}
for (int i = 0; i Update(Hit_forBall);
blocks->Update(ball_block);
bar->Update();
}
void Field_t::Draw()
{
bar->Draw();
blocks->Draw();
ball->Draw();
}
bool SystemUpdate()
{
if (
ScreenFlip() == 0 &&
ProcessMessage() == 0 &&
ClearDrawScreen() == 0 &&
CheckHitKey(KEY_INPUT_ESCAPE) == 0
) return true;
return false;
}
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
ChangeWindowMode(TRUE);
DxLib_Init();
SetDrawScreen(DX_SCREEN_BACK);
Field_t gamemain;
gamemain.Init();
while (SystemUpdate())
{
gamemain.Update();
gamemain.Draw();
}
DxLib_End();
return 0;
}