#1
by Ouxiy » 4年前
以下のプログラムに関して、
コード:
#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 stage[4][7][2]; // 盤上のマスの格子点の座標
int pos[3][6][2]; // キャラ描画座標
void init_stage() // stage と pos を初期化する
{
for (int j = 0; j < 7; j++) {
int w = (j - 3) * 100, h = 600;
for (int i = 4; --i >= 0; ) {
stage[i][j][0] = w + 400, stage[i][j][1] = h - 200;
w = w * 9 / 10, h = h * 9 / 10;
}
}
for (int i = 0; i < 3; i++)
for (int j = 0; j < 6; j++) {
pos[i][j][0] = (stage[i][j][0] + stage[i + 1][j + 1][0]) / 2 - 25;
pos[i][j][1] = (stage[i][j][1] + stage[i + 1][j + 1][1]) / 2 - 55;
}
}
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
SetGraphMode(780, 680, 32); // ウィンドウの大きさを指定
ChangeWindowMode(TRUE); // 全画面ではなくウインドウを使用
if (DxLib_Init() == -1) return -1; // DXライブラリ初期化処理
SetDrawScreen(DX_SCREEN_BACK); // 裏画面を使用する設定
init_stage(); // stage, pos の初期化
int enemyX = 4, enemyY = 1; // 敵の位置
int playerX = 1, playerY = 1; // 俺の位置
int enemyMove = 0; // 敵の移動状態
int playerMove = 1;
int lock = 0; // ロック状態
int enemyGHandle[12]; // 敵のグラフィックハンドル格納用配列
int playerGHandle[12]; // 俺のグラフィックハンドル格納用配列
// 0-2:後ろ向き、3-5:右向き、6-8:前向き、9-11:左向き
LoadDivGraph("charall.png", 12, 3, 4, 49, 66, enemyGHandle);
LoadDivGraph("charall.png", 12, 3, 4, 49, 66, playerGHandle);
int enemyImage = enemyGHandle[11]; // 敵 左向き
int playerImage = playerGHandle[4]; // 俺 右向き
const int MOVE_INTERVAL = 1000; // 何ミリ秒ごとに移動処理をするか
int nextMoveTime = GetNowCount() + MOVE_INTERVAL; // 次回移動処理をする時刻
int stopCount = 0; // 動かないのがあと何回か
while (ProcessMessage() == 0) {
gpUpdateKey(); // キーの入力状態を取得
// 俺の移動
if (Key[KEY_INPUT_RIGHT] == 1 && playerX < 2) {
playerMove = 1; playerX++; playerImage = playerGHandle[4];
}
if (Key[KEY_INPUT_LEFT] == 1 && playerX > 0) {
playerMove = 1; playerX--; playerImage = playerGHandle[9];
}
if (Key[KEY_INPUT_UP] == 1 && playerY > 0) {
playerMove = 1; playerY--; playerImage = playerGHandle[2];
}
if (Key[KEY_INPUT_DOWN] == 1 && playerY < 2) {
playerMove = 1; playerY++; playerImage = playerGHandle[8];
}
if (Key[KEY_INPUT_R] == 1 && playerY==enemyY) {
enemyImage = enemyGHandle[6];
if (Key[KEY_INPUT_A] == 1) { // アタック
playerX = enemyX - 1;
playerX = playerX;
playerMove = 1; playerImage = playerGHandle[4];
enemyMove = 1; enemyImage = enemyGHandle[10];
}
}
else {
// 敵の移動
int t = GetNowCount();
if (t >= nextMoveTime) { // 指定の時間が経ったら(1sごとに)
nextMoveTime = t + MOVE_INTERVAL; // 次回移動処理をする時刻
if (stopCount > 0) { // 停止中のとき
stopCount--; // 止まっている残り時間(回数)を減らす
}
else { // 普通の状態のとき
if (GetRand(99) < 10) { // たまに(10%の確率で)
stopCount = GetRand(4); // 数秒間(1~5秒間)その場に止まる
}
else { // 9マス上のいずれかのパネルに移動させる
int cy = enemyY, cx = enemyX;
do {
enemyY = GetRand(2);
enemyX = GetRand(2) + 3;
} while (enemyX == cx && enemyY == cy ||
enemyX == playerX && enemyY == playerY);
enemyImage = enemyGHandle[10];
enemyMove = 1;
}
}
}
}
if (enemyMove > 0 && ++enemyMove > 20) {
enemyMove = 0; enemyImage = enemyGHandle[11];
}
if (playerMove > 0 && ++playerMove > 20) {//右を向くためのプログラム
playerImage = playerGHandle[4];//playerMoveが20より大きい21になったら[4]を描画し、次に—1して20になったら[5]を描画し、再び+1して[4]を描画してif (playerMove > 0 && ++playerMove > 20によってループを繰り返す。
--playerMove ; playerImage = playerGHandle[5];//playerMoveは20となる
if (playerMove == 20) {
++playerMove; playerImage = playerGHandle[4];//playerMoveは21となる
}
}
ClearDrawScreen(); // 裏画面をクリア
// ステージの描画
int stageColor = GetColor(160, 64, 64);
for (int i = 0; i < 4; i++)
DrawLine(stage[i][0][0], stage[i][0][1],
stage[i][6][0], stage[i][6][1], stageColor, 5);
for (int j = 0; j < 7; j++)
DrawLine(stage[0][j][0], stage[0][j][1],
stage[3][j][0], stage[3][j][1], stageColor, 5);
DrawGraph(pos[enemyY][enemyX][0], pos[enemyY][enemyX][1],
enemyImage, true); // 敵キャラの描画
DrawGraph(pos[playerY][playerX][0], pos[playerY][playerX][1],
playerImage, true); // 俺キャラの描画
if (lock) DrawFormatString(100, 200, GetColor(255, 255, 255), "LOCK");
ScreenFlip(); // 裏画面を表画面に反映
}
DxLib_End(); // DXライブラリ使用の終了処理
return 0; // ソフトの終了
}
使用した画像のサイトの画像を使って以下のように移動していないときでも、その座標上で足踏みするようにしたいです。
以下のようにプログラムを書きましたが思うように動きません。あと何が足りないのでしょうか。
出来る限りこのプログラムの原型のままで問題を解決したいです。
どうかよろしくお願いいたします。
コード:
if (playerMove > 0 && ++playerMove > 20) {//右を向くためのプログラム
playerImage = playerGHandle[4];//playerMoveが20より大きい21になったら[4]を描画し、次に—1して20になったら[5]を描画し、再び+1して[3]を描画してif (playerMove > 0 && ++playerMove > 20によってループを繰り返す。
--playerMove ; playerImage = playerGHandle[5];//playerMoveは20となる
if (playerMove == 20) {
++playerMove; playerImage = playerGHandle[3];//playerMoveは21となる
}
}
以下のプログラムに関して、
[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 stage[4][7][2]; // 盤上のマスの格子点の座標
int pos[3][6][2]; // キャラ描画座標
void init_stage() // stage と pos を初期化する
{
for (int j = 0; j < 7; j++) {
int w = (j - 3) * 100, h = 600;
for (int i = 4; --i >= 0; ) {
stage[i][j][0] = w + 400, stage[i][j][1] = h - 200;
w = w * 9 / 10, h = h * 9 / 10;
}
}
for (int i = 0; i < 3; i++)
for (int j = 0; j < 6; j++) {
pos[i][j][0] = (stage[i][j][0] + stage[i + 1][j + 1][0]) / 2 - 25;
pos[i][j][1] = (stage[i][j][1] + stage[i + 1][j + 1][1]) / 2 - 55;
}
}
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
SetGraphMode(780, 680, 32); // ウィンドウの大きさを指定
ChangeWindowMode(TRUE); // 全画面ではなくウインドウを使用
if (DxLib_Init() == -1) return -1; // DXライブラリ初期化処理
SetDrawScreen(DX_SCREEN_BACK); // 裏画面を使用する設定
init_stage(); // stage, pos の初期化
int enemyX = 4, enemyY = 1; // 敵の位置
int playerX = 1, playerY = 1; // 俺の位置
int enemyMove = 0; // 敵の移動状態
int playerMove = 1;
int lock = 0; // ロック状態
int enemyGHandle[12]; // 敵のグラフィックハンドル格納用配列
int playerGHandle[12]; // 俺のグラフィックハンドル格納用配列
// 0-2:後ろ向き、3-5:右向き、6-8:前向き、9-11:左向き
LoadDivGraph("charall.png", 12, 3, 4, 49, 66, enemyGHandle);
LoadDivGraph("charall.png", 12, 3, 4, 49, 66, playerGHandle);
int enemyImage = enemyGHandle[11]; // 敵 左向き
int playerImage = playerGHandle[4]; // 俺 右向き
const int MOVE_INTERVAL = 1000; // 何ミリ秒ごとに移動処理をするか
int nextMoveTime = GetNowCount() + MOVE_INTERVAL; // 次回移動処理をする時刻
int stopCount = 0; // 動かないのがあと何回か
while (ProcessMessage() == 0) {
gpUpdateKey(); // キーの入力状態を取得
// 俺の移動
if (Key[KEY_INPUT_RIGHT] == 1 && playerX < 2) {
playerMove = 1; playerX++; playerImage = playerGHandle[4];
}
if (Key[KEY_INPUT_LEFT] == 1 && playerX > 0) {
playerMove = 1; playerX--; playerImage = playerGHandle[9];
}
if (Key[KEY_INPUT_UP] == 1 && playerY > 0) {
playerMove = 1; playerY--; playerImage = playerGHandle[2];
}
if (Key[KEY_INPUT_DOWN] == 1 && playerY < 2) {
playerMove = 1; playerY++; playerImage = playerGHandle[8];
}
if (Key[KEY_INPUT_R] == 1 && playerY==enemyY) {
enemyImage = enemyGHandle[6];
if (Key[KEY_INPUT_A] == 1) { // アタック
playerX = enemyX - 1;
playerX = playerX;
playerMove = 1; playerImage = playerGHandle[4];
enemyMove = 1; enemyImage = enemyGHandle[10];
}
}
else {
// 敵の移動
int t = GetNowCount();
if (t >= nextMoveTime) { // 指定の時間が経ったら(1sごとに)
nextMoveTime = t + MOVE_INTERVAL; // 次回移動処理をする時刻
if (stopCount > 0) { // 停止中のとき
stopCount--; // 止まっている残り時間(回数)を減らす
}
else { // 普通の状態のとき
if (GetRand(99) < 10) { // たまに(10%の確率で)
stopCount = GetRand(4); // 数秒間(1~5秒間)その場に止まる
}
else { // 9マス上のいずれかのパネルに移動させる
int cy = enemyY, cx = enemyX;
do {
enemyY = GetRand(2);
enemyX = GetRand(2) + 3;
} while (enemyX == cx && enemyY == cy ||
enemyX == playerX && enemyY == playerY);
enemyImage = enemyGHandle[10];
enemyMove = 1;
}
}
}
}
if (enemyMove > 0 && ++enemyMove > 20) {
enemyMove = 0; enemyImage = enemyGHandle[11];
}
if (playerMove > 0 && ++playerMove > 20) {//右を向くためのプログラム
playerImage = playerGHandle[4];//playerMoveが20より大きい21になったら[4]を描画し、次に—1して20になったら[5]を描画し、再び+1して[4]を描画してif (playerMove > 0 && ++playerMove > 20によってループを繰り返す。
--playerMove ; playerImage = playerGHandle[5];//playerMoveは20となる
if (playerMove == 20) {
++playerMove; playerImage = playerGHandle[4];//playerMoveは21となる
}
}
ClearDrawScreen(); // 裏画面をクリア
// ステージの描画
int stageColor = GetColor(160, 64, 64);
for (int i = 0; i < 4; i++)
DrawLine(stage[i][0][0], stage[i][0][1],
stage[i][6][0], stage[i][6][1], stageColor, 5);
for (int j = 0; j < 7; j++)
DrawLine(stage[0][j][0], stage[0][j][1],
stage[3][j][0], stage[3][j][1], stageColor, 5);
DrawGraph(pos[enemyY][enemyX][0], pos[enemyY][enemyX][1],
enemyImage, true); // 敵キャラの描画
DrawGraph(pos[playerY][playerX][0], pos[playerY][playerX][1],
playerImage, true); // 俺キャラの描画
if (lock) DrawFormatString(100, 200, GetColor(255, 255, 255), "LOCK");
ScreenFlip(); // 裏画面を表画面に反映
}
DxLib_End(); // DXライブラリ使用の終了処理
return 0; // ソフトの終了
}[/code]
[url=https://bituse.info/game/21]使用した画像[/url]のサイトの画像を使って以下のように移動していないときでも、その座標上で足踏みするようにしたいです。
以下のようにプログラムを書きましたが思うように動きません。あと何が足りないのでしょうか。
出来る限りこのプログラムの原型のままで問題を解決したいです。
どうかよろしくお願いいたします。
[code]if (playerMove > 0 && ++playerMove > 20) {//右を向くためのプログラム
playerImage = playerGHandle[4];//playerMoveが20より大きい21になったら[4]を描画し、次に—1して20になったら[5]を描画し、再び+1して[3]を描画してif (playerMove > 0 && ++playerMove > 20によってループを繰り返す。
--playerMove ; playerImage = playerGHandle[5];//playerMoveは20となる
if (playerMove == 20) {
++playerMove; playerImage = playerGHandle[3];//playerMoveは21となる
}
}[/code]