#1
by Ouxiy » 4年前
過去に「画像のように3D っぽい2Dの9マスA~I上をキャラにキー入力で移動させたい」というプログラムにおいて、質問したものです。
あれから
コード:
#include "DxLib.h"
int Key[256];
int gpUpdateKey()
{
char tmpKey[256];
GetHitKeyStateAll(tmpKey);
for (int i = 0; i < 256; i++)
(tmpKey[i] == 0) ? (Key[i] = 0) : Key[i]++;
return 0;
}
int idou[5][5] = {
{ 1, 1, 1, 1, 1 },
{ 1, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1 },
{ 1, 1, 1, 1, 1 },
};
int box[4][7][2], player[5][5][2];
void init_box()
{
for (int j = 0; j < 7; j++) {
int w = (j - 3) * 100, h = 600;
for (int i = 4; --i >= 0; ) {
box[i][j][0] = w + 400, box[i][j][1] = h - 200;
w = w * 9 / 10, h = h * 9 / 10;
}
}
for (int i = 1; i <= 3; i++)
for (int j = 1; j <= 3; j++) {
player[i][j][0] = (box[i - 1][j - 1][0] + box[i][j][0]) / 2 - 25;
player[i][j][1] = (box[i - 1][j - 1][1] + box[i][j][1]) / 2 - 66;
}
}
int WINAPI WinMain(HINSTANCE hi, HINSTANCE hp, LPSTR cl, int cs)
{
SetGraphMode(780, 680, 32); // ウィンドウの大きさを指定
ChangeWindowMode(TRUE); // 全画面ではなくウインドウを使用
if (DxLib_Init() == -1) return -1; // DXライブラリ初期化処理
SetDrawScreen(DX_SCREEN_BACK); // 裏画面を使用する設定
init_box();
int boxColor = GetColor(160, 64, 64);
int nx = 2, ny = 2; // プレイヤーの初期位置
int px = player[ny][nx][0], py = player[ny][nx][1]; // 表示位置
int keep = 0; // 移動不可能状態継続カウンタ
int gh[12]; //グラフィックハンドル格納用配列
// 5:正面、7:右向き、2:左向き、4:上向き、3:下向き、9:移動不可
LoadDivGraph("charall.png", 12, 3, 4, 49, 66, gh); //画像読み込み
int playerphoto = gh[5];
while (ProcessMessage() == 0) {
gpUpdateKey(); // キーの入力状態を取得
if (Key[KEY_INPUT_RIGHT] == 1) { // 右キーが押されている
if (idou[nx + 1][ny] == 0) { // 移動先が空いていれば
nx++; playerphoto = gh[7]; // 右向き
}
else {
keep = 1; playerphoto = gh[9]; // 移動不可能
}
}
if (Key[KEY_INPUT_LEFT] == 1) {
if (idou[nx - 1][ny] == 0) { // 移動先が空いていれば
nx--; playerphoto = gh[2]; // 左向き
}
else {
keep = 1; playerphoto = gh[9]; // 移動不可能
}
}
if (Key[KEY_INPUT_UP] == 1) {
if (idou[nx][ny - 1] == 0) { // 移動先が空いていれば
ny--; playerphoto = gh[4]; // 上向き
}
else {
keep = 1; playerphoto = gh[9]; // 移動不可能
}
}
if (Key[KEY_INPUT_DOWN] == 1) {
if (idou[nx][ny + 1] == 0) { // 移動先が空いていれば
ny++; playerphoto = gh[3]; // 下向き
}
else {
keep = 1; playerphoto = gh[9]; // 移動不可能
}
}
ClearDrawScreen(); // 裏画面をクリア
for (int i = 0; i < 4; i++)
DrawLine(box[i][0][0], box[i][0][1],
box[i][6][0], box[i][6][1], boxColor);
for (int j = 0; j < 7; j++)
DrawLine(box[0][j][0], box[0][j][1],
box[3][j][0], box[3][j][1], boxColor);
int x = player[ny][nx][0], y = player[ny][nx][1]; // 表示位置に変換
if (x == px && y == py) {
if (keep == 0 || ++keep == 10) {
keep = 0; playerphoto = gh[5];
}
}
else {
if (abs(x - px) < 8) px = x;
else if (x > px) px += 8;
else if (x < px) px -= 8;
if (abs(y - py) < 6) py = y;
else if (y > py) py += 6;
else if (y < py) py -= 6;
}
DrawGraph(px, py, playerphoto, FALSE); // プレイヤーを裏画面に描画
ScreenFlip(); // 裏画面を表画面に反映
}
DxLib_End(); // DXライブラリ使用の終了処理
return 0; // ソフトの終了
}
以上のプログラムを使わせて頂いているのですが、敵を登場させるために、敵のプログラムも必要になりました。そこで以下のプログラムでランダム関数と時間の関数を使い、9マス上を毎秒1s事に敵キャラを9マス上のいずれかのパネルに移動させ、たまに数秒間その場に止まるようなプログラムを作ろうとしました。
ですが、時間に関する関数が見つからず困っています。
ランダム関数は見つかったので、なんとかプログラムを作ろうとしているのですが、
毎1sごとにランダムに変数の中身を変えて、9マス上をランダムに動く敵キャラのを実現させたいです。
以下はランダム関数と敵キャラcharall.pngを表したプログラムです。
コード:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "DxLib.h"
int idou[5][5] = {//敵の動く範囲
{ 1, 1, 1, 1, 1 },
{ 1, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1 },
{ 1, 1, 1, 1, 1 },
};
int teki[5][5][2] = {
{{0}}, // どうせここには来ない
{{0, 0}, {60, 60}, {100, 60}, {140, 60}, {0, 0}},//敵の配列の範囲に座標を加えて、この上の配列内のこの座標上を動くようにする。
{{0, 0}, {50, 100}, {100, 100}, {150, 100}, {0, 0}},
{{0, 0}, {40, 160}, {100, 160}, {160, 160}, {0, 0}},
{{0}} // どうせここには来ない
};
int WINAPI WinMain(HINSTANCE hi, HINSTANCE hp, LPSTR cl, int cs)
{
SetGraphMode(780, 680, 32); // ウィンドウの大きさを指定
ChangeWindowMode(TRUE); // 全画面ではなくウインドウを使用
if (DxLib_Init() == -1) return -1; // DXライブラリ初期化処理
SetDrawScreen(DX_SCREEN_BACK); // 裏画面を使用する設定
//int boxColor = GetColor(160, 64, 64);//ステージの色
int nx = 2, ny = 2; // 敵の初期位置
int px = teki[ny][nx][0], py = teki[ny][nx][1]; // 表示位置
int gh[12]; //グラフィックハンドル格納用配列
// 5:正面、7:右向き、2:左向き、4:上向き、3:下向き、9:移動不可
LoadDivGraph("charall.png", 12, 3, 4, 49, 66, gh); //画像読み込み
int playerphoto = gh[5];
while (ProcessMessage() == 0) {
ClearDrawScreen(); // 裏画面をクリア
DrawGraph(teki[ny][nx][0], teki[ny][nx][1], playerphoto, FALSE);
ScreenFlip(); // 裏画面を表画面に反映
}
int s;
/* 乱数の種を初期化 */
srand(time(NULL));
/* 1 〜 9 のランダムな数値 */
s = rand() % 9 + 1;
/* 出力 */
printf("%d\n", s);
DxLib_End(); // DXライブラリ使用の終了処理
return 0; // ソフトの終了
}
過去に「画像のように3D っぽい2Dの9マスA~I上をキャラにキー入力で移動させたい」というプログラムにおいて、質問したものです。
あれから
[code]#include "DxLib.h"
int Key[256];
int gpUpdateKey()
{
char tmpKey[256];
GetHitKeyStateAll(tmpKey);
for (int i = 0; i < 256; i++)
(tmpKey[i] == 0) ? (Key[i] = 0) : Key[i]++;
return 0;
}
int idou[5][5] = {
{ 1, 1, 1, 1, 1 },
{ 1, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1 },
{ 1, 1, 1, 1, 1 },
};
int box[4][7][2], player[5][5][2];
void init_box()
{
for (int j = 0; j < 7; j++) {
int w = (j - 3) * 100, h = 600;
for (int i = 4; --i >= 0; ) {
box[i][j][0] = w + 400, box[i][j][1] = h - 200;
w = w * 9 / 10, h = h * 9 / 10;
}
}
for (int i = 1; i <= 3; i++)
for (int j = 1; j <= 3; j++) {
player[i][j][0] = (box[i - 1][j - 1][0] + box[i][j][0]) / 2 - 25;
player[i][j][1] = (box[i - 1][j - 1][1] + box[i][j][1]) / 2 - 66;
}
}
int WINAPI WinMain(HINSTANCE hi, HINSTANCE hp, LPSTR cl, int cs)
{
SetGraphMode(780, 680, 32); // ウィンドウの大きさを指定
ChangeWindowMode(TRUE); // 全画面ではなくウインドウを使用
if (DxLib_Init() == -1) return -1; // DXライブラリ初期化処理
SetDrawScreen(DX_SCREEN_BACK); // 裏画面を使用する設定
init_box();
int boxColor = GetColor(160, 64, 64);
int nx = 2, ny = 2; // プレイヤーの初期位置
int px = player[ny][nx][0], py = player[ny][nx][1]; // 表示位置
int keep = 0; // 移動不可能状態継続カウンタ
int gh[12]; //グラフィックハンドル格納用配列
// 5:正面、7:右向き、2:左向き、4:上向き、3:下向き、9:移動不可
LoadDivGraph("charall.png", 12, 3, 4, 49, 66, gh); //画像読み込み
int playerphoto = gh[5];
while (ProcessMessage() == 0) {
gpUpdateKey(); // キーの入力状態を取得
if (Key[KEY_INPUT_RIGHT] == 1) { // 右キーが押されている
if (idou[nx + 1][ny] == 0) { // 移動先が空いていれば
nx++; playerphoto = gh[7]; // 右向き
}
else {
keep = 1; playerphoto = gh[9]; // 移動不可能
}
}
if (Key[KEY_INPUT_LEFT] == 1) {
if (idou[nx - 1][ny] == 0) { // 移動先が空いていれば
nx--; playerphoto = gh[2]; // 左向き
}
else {
keep = 1; playerphoto = gh[9]; // 移動不可能
}
}
if (Key[KEY_INPUT_UP] == 1) {
if (idou[nx][ny - 1] == 0) { // 移動先が空いていれば
ny--; playerphoto = gh[4]; // 上向き
}
else {
keep = 1; playerphoto = gh[9]; // 移動不可能
}
}
if (Key[KEY_INPUT_DOWN] == 1) {
if (idou[nx][ny + 1] == 0) { // 移動先が空いていれば
ny++; playerphoto = gh[3]; // 下向き
}
else {
keep = 1; playerphoto = gh[9]; // 移動不可能
}
}
ClearDrawScreen(); // 裏画面をクリア
for (int i = 0; i < 4; i++)
DrawLine(box[i][0][0], box[i][0][1],
box[i][6][0], box[i][6][1], boxColor);
for (int j = 0; j < 7; j++)
DrawLine(box[0][j][0], box[0][j][1],
box[3][j][0], box[3][j][1], boxColor);
int x = player[ny][nx][0], y = player[ny][nx][1]; // 表示位置に変換
if (x == px && y == py) {
if (keep == 0 || ++keep == 10) {
keep = 0; playerphoto = gh[5];
}
}
else {
if (abs(x - px) < 8) px = x;
else if (x > px) px += 8;
else if (x < px) px -= 8;
if (abs(y - py) < 6) py = y;
else if (y > py) py += 6;
else if (y < py) py -= 6;
}
DrawGraph(px, py, playerphoto, FALSE); // プレイヤーを裏画面に描画
ScreenFlip(); // 裏画面を表画面に反映
}
DxLib_End(); // DXライブラリ使用の終了処理
return 0; // ソフトの終了
}
[/code]
以上のプログラムを使わせて頂いているのですが、敵を登場させるために、敵のプログラムも必要になりました。そこで以下のプログラムでランダム関数と時間の関数を使い、9マス上を毎秒1s事に敵キャラを9マス上のいずれかのパネルに移動させ、たまに数秒間その場に止まるようなプログラムを作ろうとしました。
ですが、時間に関する関数が見つからず困っています。
ランダム関数は見つかったので、なんとかプログラムを作ろうとしているのですが、[b]毎1sごとにランダムに変数の中身を変えて、9マス上をランダムに動く敵キャラのを実現させたい[/b]です。
以下はランダム関数と敵キャラcharall.pngを表したプログラムです。
[code]#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "DxLib.h"
int idou[5][5] = {//敵の動く範囲
{ 1, 1, 1, 1, 1 },
{ 1, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1 },
{ 1, 1, 1, 1, 1 },
};
int teki[5][5][2] = {
{{0}}, // どうせここには来ない
{{0, 0}, {60, 60}, {100, 60}, {140, 60}, {0, 0}},//敵の配列の範囲に座標を加えて、この上の配列内のこの座標上を動くようにする。
{{0, 0}, {50, 100}, {100, 100}, {150, 100}, {0, 0}},
{{0, 0}, {40, 160}, {100, 160}, {160, 160}, {0, 0}},
{{0}} // どうせここには来ない
};
int WINAPI WinMain(HINSTANCE hi, HINSTANCE hp, LPSTR cl, int cs)
{
SetGraphMode(780, 680, 32); // ウィンドウの大きさを指定
ChangeWindowMode(TRUE); // 全画面ではなくウインドウを使用
if (DxLib_Init() == -1) return -1; // DXライブラリ初期化処理
SetDrawScreen(DX_SCREEN_BACK); // 裏画面を使用する設定
//int boxColor = GetColor(160, 64, 64);//ステージの色
int nx = 2, ny = 2; // 敵の初期位置
int px = teki[ny][nx][0], py = teki[ny][nx][1]; // 表示位置
int gh[12]; //グラフィックハンドル格納用配列
// 5:正面、7:右向き、2:左向き、4:上向き、3:下向き、9:移動不可
LoadDivGraph("charall.png", 12, 3, 4, 49, 66, gh); //画像読み込み
int playerphoto = gh[5];
while (ProcessMessage() == 0) {
ClearDrawScreen(); // 裏画面をクリア
DrawGraph(teki[ny][nx][0], teki[ny][nx][1], playerphoto, FALSE);
ScreenFlip(); // 裏画面を表画面に反映
}
int s;
/* 乱数の種を初期化 */
srand(time(NULL));
/* 1 〜 9 のランダムな数値 */
s = rand() % 9 + 1;
/* 出力 */
printf("%d\n", s);
DxLib_End(); // DXライブラリ使用の終了処理
return 0; // ソフトの終了
}[/code]