c言語を学び始めて半年くらいです。今は授業でdxlibでゲームを作っています。
今わかる範囲でプログラムを書いてみたのですが
#include "DxLib.h"
#include "SceneMgr.h"
#define PI 3.141592654
int CheckHit(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2)
{
int L1 = x1; // 左
int R1 = x1 + w1; // 右(左+横幅)
int L2 = x2; // 左
int R2 = x2 + w2; // 右(左+横幅)
if (R1 < L2) return 0;
if (R2 < L1) return 0;
int U1 = y1; // 上
int D1 = y1 + w1; // 下(上+縦幅)
int U2 = y2; // 上
int D2 = y2 + w2; // 下(上+縦幅)
if (D1 < U2) return 0;
if (D2 < U1) return 0;
// それ以外の場合は当たっている
return 1;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
//======================================================
// ウインドウ設定とDXライブラリの初期化
//======================================================
const int SCREEN_WIDTH = 640; //< スクリーン横の大きさ
const int SCREEN_HEIGHT = 640; //< スクリーン縦の大きさ
// Windowsモードにする
ChangeWindowMode(TRUE);
// 画面サイズの設定
SetGraphMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32);
// DXライブラリ初期化
if (DxLib_Init() == -1)
{
//初期化に失敗
return -1;
}
// ダブルバッファモード
SetDrawScreen(DX_SCREEN_BACK);
//======================================================
// ゲーム部分
//======================================================
//------------------------------------------------------
// ゲーム初期化部分
char Key[256];
const int CHARA_WIDTH = 30;
const int CHARA_HEIGHT = 25;
int x = 1;
int y = 5;
int chara_act[16];
LoadDivGraph("画像/人間.png", 16, 3, 4, 32, 32, chara_act);
int act_motion[] = { 0, 1, 2, 1, };
int act_index = 0;
int MAX_MOTION_INDEX = 4; //< 0,1,2,1 の4種類 [0~3]までにするために用意
const int ACT_SPEED = 20;
int act_wait = ACT_SPEED;
const int ACT_DIR_SLIDE = 3; //< 横のキャラ分スライドさせれば次の列を選択する
int act_dir = 0;
int ENEMY_WIDTH = 31;
int ENEMY_HEIGHT = 31;
int ex = 150;
int ey = 150;
int enemy_act[16];
LoadDivGraph("画像/ゲイザー.png", 12, 3, 4, 48, 50, enemy_act);
int enemy_act_motion[] = { 0,1,2,1, };
int enemy_act_index = 0;
int ENEMY_MAX_MOTION_INDEX = 4;
const int ENEMY_ACT_SPEED = 20;
int enemy_act_wait = ENEMY_ACT_SPEED;
const int ENEMY_DIR_SLIDE = 3;
int enemy_dir = 0;
const int AOMY_WIDTH = 29;
const int AOMY_HEIGHT = 29;
int ax = 285;
int ay = 285;
int aomy_act[12];
LoadDivGraph("画像/青ゲイザー.png", 12, 3, 4, 48, 50, aomy_act);
int aomy_act_motion[] = { 0,1,2,1, };
int aomy_act_index = 0;
int AOMY_MAX_MOTION_INDEX = 4;
const int AOMY_ACT_SPEED = 20;
int aomy_act_wait = AOMY_ACT_SPEED;
const int AOMY_DIR_SLIDE = 3;
int aomy_dir = 0;
const int MIDOMY_WIDTH = 30;
const int MIDOMY_HEIGHT = 30;
int mx = 85;
int my = 320;
int midomy_act[12];
LoadDivGraph("画像/緑ゲイザー改.png", 12, 3, 4, 48, 50, midomy_act);
int midomy_act_motion[] = { 0,1,2,1, };
int midomy_act_index = 0;
int MIDOMY_MAX_MOTION_INDEX = 4;
const int MIDOMY__ACT_SPEED = 20;
int midomy_act_wait = MIDOMY__ACT_SPEED;
const int MIDOMY_DIR_SLIDE = 3;
int midomy_dir = 0;
const int KIIMY_WIDTH = 30;
const int KIIMY_HEIGHT = 30;
int kx = 440;
int ky = 560;
int kiimy_act[12];
LoadDivGraph("画像/黄ゲイザー改.png", 12, 3, 4, 48, 50, kiimy_act);
int kiimy_act_motion[] = { 0,1,2,1 , };
int kiimy_act_index = 0;
int KIIMY_MAX_MOTION_INDEX = 4;
const int KIIMY_ACT_SPEED = 20;
int kiimy_act_wait = KIIMY_ACT_SPEED;
const int KIIMY_DIR_SLIDE = 3;
int kiimy_dir = 0;
int Handle; // 画像格納用ハンドル
const int GOAL_WIDTH = 30;
const int GOAL_HEIGHT = 30;
Handle = LoadGraph("画像/ゴール.png"); // 画像のロード
// マップチップロード
const int SIZE_MAP_X = 32; //< マップチップXサイズ
const int SIZE_MAP_Y = 32; //< マップチップYサイズ
const int NUM_CHIP_X = 10; //< 横10種類
const int NUM_CHIP_Y = 4; //< 縦4種類
const int NUM_CHIP_ALL = NUM_CHIP_X * NUM_CHIP_Y; //< 40個
int chip[NUM_CHIP_ALL];
LoadDivGraph("画像/マップチップ.png", NUM_CHIP_ALL,
NUM_CHIP_X, NUM_CHIP_Y,
SIZE_MAP_X, SIZE_MAP_Y,
chip);
// マップチップを使った背景
const int NUM_MAP_X = 20; // 32x20=640
const int NUM_MAP_Y = 20; // 32x15=480
int data[] = {
// 横20x15
0, 0, 17, 0, 17, 0, 0, 0 ,0 ,17 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,17 ,0,
0, 17, 0, 0, 0, 17, 17, 17 ,0 ,17 ,0 ,17 ,17 ,0 ,17 ,17 ,17 ,0 ,17 ,0,
0, 0, 17, 17, 0, 0, 0, 17 ,0 ,0 ,0 ,17 ,0 ,0 ,0 ,17 ,0 ,0 ,0 ,0,
17, 0, 17, 0, 17, 17, 0, 0 ,0 ,17 ,17 ,17 ,17 ,17 ,0 ,0 ,17 ,0 ,17 ,0,
0, 0, 0, 0, 0, 17, 17, 17 ,0 ,17 ,0 ,0 ,0 ,0 ,17 ,17 ,0 ,17 ,17 ,0,
0, 17, 17, 17, 0, 0, 0, 0 ,0 ,17 ,0 ,17 ,17 ,17 ,0 ,17 ,0 ,0 ,0 ,0,
0, 17, 17, 17, 0, 17, 17, 17 ,17 ,17 ,0 ,17 ,0 ,0 ,0 ,0 ,17 ,17 ,17 ,0,
0, 0, 0, 17, 0, 0, 0, 0 ,17 ,0 ,0 ,17 ,0 ,17 ,17 ,0 ,0 ,0 ,17 ,0,
17, 17, 0, 17, 17, 17, 17, 17 ,17 ,0 ,17 ,17 ,0 ,0 ,17 ,17 ,17 ,0 ,17 ,17,
17, 0, 0, 0, 0, 0, 17, 17 ,0 ,0 ,0 ,0 ,17 ,0 ,17 ,17 ,0 ,0 ,17 ,0,
17, 0, 17, 17, 17, 0, 0, 0 ,17 ,0 ,17 ,0 ,0 ,0 ,0 ,0 ,17 ,17 ,0 ,0,
17, 0, 17, 0, 17, 17, 17, 0 ,17 ,0 ,17 ,17 ,0 ,17 ,17 ,0 ,0 ,0 ,0 ,17,
0, 0, 17, 0, 0, 17, 0, 0 ,17 ,0 ,0 ,17 ,0 ,17 ,0 ,17 ,17 ,17 ,0 ,0,
0, 1, 17, 17, 0, 17, 0, 0 ,0 ,0 ,17 ,17 ,0 ,17 ,0 ,17 ,17 ,0 ,17 ,0,
0, 0, 0, 17, 0, 0, 0, 0 ,17 ,0 ,17 ,17 ,0 ,17 ,0 ,0 ,0 ,0 ,0 ,0,
17, 17, 0, 17, 0, 17, 0, 17 ,17 ,0 ,0 ,17 ,0 ,17 ,17 ,0 ,17 ,17 ,17 ,0,
0, 0, 0, 17, 0, 17, 17, 17 ,17 ,17 ,0 ,17 ,0 ,17 ,17 ,0 ,0 ,0 ,17 ,0,
0, 17, 17, 17, 0, 17, 0, 0 ,0 ,0 ,0 ,17 ,0 ,0 ,0 ,17 ,17 ,0 ,17 ,17,
0, 17, 0, 0, 0, 17, 0, 17 ,17 ,17 ,0 ,0 ,17 ,17 ,0 ,17 ,0 ,0 ,0 ,0,
0, 0, 0, 17, 0, 0, 0, 17 ,17 ,17 ,17 ,0 ,0 ,0 ,0 ,17 ,17 ,17 ,17 ,0 };
//------------------------------------------------------
// メインループ部分
//------------------------------------------------------
while (ProcessMessage() == 0 && ClearDrawScreen() == 0 && GetHitKeyStateAll(Key) == 0 && Key[KEY_INPUT_ESCAPE] == 0)
{
SceneMgr_Update(); //更新
SceneMgr_Draw(); //描画
// 背景
for (int j = 0; j < NUM_MAP_Y; j++)
{
for (int i = 0; i < NUM_MAP_X; i++)
{
int no = data[i + j * NUM_MAP_X];
DrawGraph(i * SIZE_MAP_X, j * SIZE_MAP_Y, chip[no], FALSE);
}
}
// 前回の位置を覚えておく
int old_x = x;
int old_y = y;
int old_ex = ex;
int old_ey = ey;
int old_ax = ax;
int old_ay = ay;
int old_mx = mx;
int old_my = my;
int old_kx = kx;
int old_ky = ky;
// キー情報を取得する
int input = GetJoypadInputState(DX_INPUT_KEY_PAD1);
// キーで移動させる
if (input & PAD_INPUT_RIGHT)
{
x++;
act_dir = 2; //< 2列目を選ぶ(0スタート)
}
if (input & PAD_INPUT_LEFT)
{
x--;
act_dir = 1; //< 1列目を選ぶ(0スタート)
}
if (input & PAD_INPUT_UP)
{
y--;
act_dir = 3; //< 3列目を選ぶ(0スタート)
}
if (input & PAD_INPUT_DOWN)
{
y++;
act_dir = 0; //< 0列目を選ぶ(0スタート)
}
// ブロックの当たり判定
for (int j = 0; j < NUM_MAP_Y; j++)
{
for (int i = 0; i < NUM_MAP_X; i++) {
int no = data[i + j * NUM_MAP_X]; if (no >= 10 && no < 20)
{
if (CheckHit(i * SIZE_MAP_X, j * SIZE_MAP_Y, SIZE_MAP_X, SIZE_MAP_Y,
x, y, CHARA_WIDTH, CHARA_HEIGHT))
{
// 前回の位置に戻してみる
x = old_x;
y = old_y;
}
}
}
}
if (ex > x) {
ex--;
enemy_dir = 1;
for (int j = 0; j < NUM_MAP_Y; j++)
{
for (int i = 0; i < NUM_MAP_X; i++) {
int no = data[i + j * NUM_MAP_X]; if (no >= 100&& no < 200)
{
if (CheckHit(i * SIZE_MAP_X, j * SIZE_MAP_Y, SIZE_MAP_X, SIZE_MAP_Y,
kx, ky, KIIMY_WIDTH, KIIMY_HEIGHT))
{
// 前回の位置に戻してみる
kx = old_kx;
ky = old_ky;
}
}
}
}
}
if (ex < x) {
ex++;
enemy_dir = 2;
for (int j = 0; j < NUM_MAP_Y; j++)
{
for (int i = 0; i < NUM_MAP_X; i++) {
int no = data[i + j * NUM_MAP_X]; if (no >= 10 && no < 20)
{
if (CheckHit(i * SIZE_MAP_X, j * SIZE_MAP_Y, SIZE_MAP_X, SIZE_MAP_Y,
kx, ky, KIIMY_WIDTH, KIIMY_HEIGHT))
{
// 前回の位置に戻してみる
kx = old_kx;
ky = old_ky;
}
}
}
}
}
if (ey > y) {
ey--;
enemy_dir = 3;
for (int j = 0; j < NUM_MAP_Y; j++)
{
for (int i = 0; i < NUM_MAP_X; i++) {
int no = data[i + j * NUM_MAP_X]; if (no >= 10 && no < 20)
{
if (CheckHit(i * SIZE_MAP_X, j * SIZE_MAP_Y, SIZE_MAP_X, SIZE_MAP_Y,
x, y, ENEMY_WIDTH, ENEMY_HEIGHT))
{
// 前回の位置に戻してみる
ex = old_ex;
ey = old_ey;
}
}
}
}
}
if (ey < y) {
ey++;
enemy_dir = 0;
for (int j = 0; j < NUM_MAP_Y; j++)
{
for (int i = 0; i < NUM_MAP_X; i++) {
int no = data[i + j * NUM_MAP_X]; if (no >= 10 && no < 20)
{
if (CheckHit(i * SIZE_MAP_X, j * SIZE_MAP_Y, SIZE_MAP_X, SIZE_MAP_Y,
x, y, ENEMY_WIDTH, ENEMY_HEIGHT))
{
// 前回の位置に戻してみる
ex = old_ex;
ey = old_ey;
}
}
}
}
}
if (ax > x) {
ax--;
aomy_dir = 1;
for (int j = 0; j < NUM_MAP_Y; j++)
{
for (int i = 0; i < NUM_MAP_X; i++) {
int no = data[i + j * NUM_MAP_X]; if (no >= 10 && no < 20)
{
if (CheckHit(i * SIZE_MAP_X, j * SIZE_MAP_Y, SIZE_MAP_X, SIZE_MAP_Y,
ax, ay, AOMY_WIDTH, AOMY_HEIGHT))
{
// 前回の位置に戻してみる
ax = old_ax;
ay = old_ay;
}
}
}
}
}
if (ax < x) {
ax++;
aomy_dir = 2;
for (int j = 0; j < NUM_MAP_Y; j++)
{
for (int i = 0; i < NUM_MAP_X; i++) {
int no = data[i + j * NUM_MAP_X]; if (no >= 10 && no < 20)
{
if (CheckHit(i * SIZE_MAP_X, j * SIZE_MAP_Y, SIZE_MAP_X, SIZE_MAP_Y,
ax, ay, AOMY_WIDTH, AOMY_HEIGHT))
{
// 前回の位置に戻してみる
ax = old_ax;
ay = old_ay;
}
}
}
}
}
if (ay > y) {
ay--;
aomy_dir = 3;
for (int j = 0; j < NUM_MAP_Y; j++)
{
for (int i = 0; i < NUM_MAP_X; i++) {
int no = data[i + j * NUM_MAP_X]; if (no >= 10 && no < 20)
{
if (CheckHit(i * SIZE_MAP_X, j * SIZE_MAP_Y, SIZE_MAP_X, SIZE_MAP_Y,
ax, ay, AOMY_WIDTH, AOMY_HEIGHT))
{
// 前回の位置に戻してみる
ax = old_ax;
ay = old_ay;
}
}
}
}
}
if (ay < y) {
ay++;
aomy_dir = 0;
for (int j = 0; j < NUM_MAP_Y; j++)
{
for (int i = 0; i < NUM_MAP_X; i++) {
int no = data[i + j * NUM_MAP_X]; if (no >= 10 && no < 20)
{
if (CheckHit(i * SIZE_MAP_X, j * SIZE_MAP_Y, SIZE_MAP_X, SIZE_MAP_Y,
ax, ay, AOMY_WIDTH, AOMY_HEIGHT))
{
// 前回の位置に戻してみる
ax = old_ax;
ay = old_ay;
}
}
}
}
}
if (mx > x) {
mx--;
midomy_dir = 1;
for (int j = 0; j < NUM_MAP_Y; j++)
{
for (int i = 0; i < NUM_MAP_X; i++) {
int no = data[i + j * NUM_MAP_X]; if (no >= 10 && no < 20)
{
if (CheckHit(i * SIZE_MAP_X, j * SIZE_MAP_Y, SIZE_MAP_X, SIZE_MAP_Y,
mx, my, MIDOMY_WIDTH, MIDOMY_HEIGHT))
{
// 前回の位置に戻してみる
mx = old_mx;
my = old_my;
}
}
}
}
}
if (mx < x) {
mx++;
midomy_dir = 2;
for (int j = 0; j < NUM_MAP_Y; j++)
{
for (int i = 0; i < NUM_MAP_X; i++) {
int no = data[i + j * NUM_MAP_X]; if (no >= 10 && no < 20)
{
if (CheckHit(i * SIZE_MAP_X, j * SIZE_MAP_Y, SIZE_MAP_X, SIZE_MAP_Y,
mx, my, MIDOMY_WIDTH, MIDOMY_HEIGHT))
{
// 前回の位置に戻してみる
mx = old_mx;
my = old_my;
}
}
}
}
}
if (my > y) {
my--;
midomy_dir = 3;
for (int j = 0; j < NUM_MAP_Y; j++)
{
for (int i = 0; i < NUM_MAP_X; i++) {
int no = data[i + j * NUM_MAP_X]; if (no >= 10 && no < 20)
{
if (CheckHit(i * SIZE_MAP_X, j * SIZE_MAP_Y, SIZE_MAP_X, SIZE_MAP_Y,
mx, my, MIDOMY_WIDTH, MIDOMY_HEIGHT))
{
// 前回の位置に戻してみる
mx = old_mx;
my = old_my;
}
}
}
}
}
if (my < y) {
my++;
midomy_dir = 0;
}for (int j = 0; j < NUM_MAP_Y; j++)
{
for (int i = 0; i < NUM_MAP_X; i++) {
int no = data[i + j * NUM_MAP_X]; if (no >= 10 && no < 20)
{
if (CheckHit(i * SIZE_MAP_X, j * SIZE_MAP_Y, SIZE_MAP_X, SIZE_MAP_Y,
mx, my, MIDOMY_WIDTH, MIDOMY_HEIGHT))
{
// 前回の位置に戻してみる
mx = old_mx;
my = old_my;
}
}
}
}
if (kx > x) {
kx--;
kiimy_dir = 1;
for (int j = 0; j < NUM_MAP_Y; j++)
{
for (int i = 0; i < NUM_MAP_X; i++) {
int no = data[i + j * NUM_MAP_X]; if (no >= 10 && no < 20)
{
if (CheckHit(i * SIZE_MAP_X, j * SIZE_MAP_Y, SIZE_MAP_X, SIZE_MAP_Y,
kx, ky, KIIMY_WIDTH, KIIMY_HEIGHT))
{
// 前回の位置に戻してみる
kx = old_kx;
ky = old_ky;
}
}
}
}
}
if (kx < x) {
kx++;
kiimy_dir = 2;
for (int j = 0; j < NUM_MAP_Y; j++)
{
for (int i = 0; i < NUM_MAP_X; i++) {
int no = data[i + j * NUM_MAP_X]; if (no >= 10 && no < 20)
{
if (CheckHit(i * SIZE_MAP_X, j * SIZE_MAP_Y, SIZE_MAP_X, SIZE_MAP_Y,
kx, ky, KIIMY_WIDTH, KIIMY_HEIGHT))
{
// 前回の位置に戻してみる
kx = old_kx;
ky = old_ky;
}
}
}
}
}
if (ky > y) {
ky--;
kiimy_dir = 3;
for (int j = 0; j < NUM_MAP_Y; j++)
{
for (int i = 0; i < NUM_MAP_X; i++) {
int no = data[i + j * NUM_MAP_X]; if (no >= 10 && no < 20)
{
if (CheckHit(i * SIZE_MAP_X, j * SIZE_MAP_Y, SIZE_MAP_X, SIZE_MAP_Y,
kx, ky, KIIMY_WIDTH, KIIMY_HEIGHT))
{
// 前回の位置に戻してみる
kx = old_kx;
ky = old_ky;
}
}
}
}
}
if (ky < y) {
ky++;
kiimy_dir = 0;
for (int j = 0; j < NUM_MAP_Y; j++)
{
for (int i = 0; i < NUM_MAP_X; i++) {
int no = data[i + j * NUM_MAP_X]; if (no >= 10 && no < 20)
{
if (CheckHit(i * SIZE_MAP_X, j * SIZE_MAP_Y, SIZE_MAP_X, SIZE_MAP_Y,
kx, ky, KIIMY_WIDTH, KIIMY_HEIGHT))
{
// 前回の位置に戻してみる
kx = old_kx;
ky = old_ky;
}
}
}
}
}
if (--act_wait <= 0)
{
act_index++;
act_wait = ACT_SPEED;
act_index %= MAX_MOTION_INDEX;
}
if (--enemy_act_wait <= 0)
{
enemy_act_index++;
enemy_act_wait = ENEMY_ACT_SPEED;
enemy_act_index %= ENEMY_MAX_MOTION_INDEX;
}
if (--aomy_act_wait <= 0)
{
aomy_act_index++;
aomy_act_wait = AOMY_ACT_SPEED;
aomy_act_index %= AOMY_MAX_MOTION_INDEX;
}
if (--midomy_act_wait <= 0)
{
midomy_act_index++;
midomy_act_wait = MIDOMY__ACT_SPEED;
midomy_act_index %= MIDOMY_MAX_MOTION_INDEX;
}
if (--kiimy_act_wait <= 0)
{
kiimy_act_index++;
kiimy_act_wait = KIIMY_ACT_SPEED;
kiimy_act_index %= KIIMY_MAX_MOTION_INDEX;
}
int motion_index = act_motion[act_index];
int enemy_motion_index = enemy_act_motion[enemy_act_index];
int aomy_motion_index = aomy_act_motion[aomy_act_index];
int mido_motion_index = midomy_act_motion[midomy_act_index];
int kiimy_motion_index = kiimy_act_motion[kiimy_act_index];
// 自分のキャラを表示する
DrawRotaGraph(625, 630, 0.1, 0.0, Handle, TRUE);
DrawGraph(x, y, chara_act[motion_index + act_dir * ACT_DIR_SLIDE], TRUE);
DrawGraph(ex, ey, enemy_act[motion_index + enemy_dir * ENEMY_DIR_SLIDE], TRUE);
DrawGraph(ax, ay, aomy_act[motion_index + aomy_dir * AOMY_DIR_SLIDE], TRUE);
DrawGraph(mx, my, midomy_act[motion_index + midomy_dir * MIDOMY_DIR_SLIDE], TRUE);
DrawGraph(kx, ky, kiimy_act[motion_index + kiimy_dir * KIIMY_DIR_SLIDE], TRUE);
if (x > 640) x = 640;
if (x < 0) x = 0;
if (y > 640) y = 640;
if (y < 0) y = 0;
//裏画面を表画面を入れ替える
ScreenFlip();
}
DxLib_End();
return 0;
}
よろしければ敵キャラクターの当たり判定のコードを教えてください。
よろしくお願いします。