0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

6.ランダムエンカウント実装

Posted at

6.ランダムエンカウント実装

マップを2種類作成し、ランダムエンカウントにて切り替える手順をまとめていきます。

・目次
1.ランダムエンカウント実装





1.ランダムエンカウント実装

概要
以下関数実装 (今回関係ない部分は省略しています。)
 ・checkEncounter
 ・rand

main.c
void main(){

	while (TRUE) {
		joy = joypad();
		
		if (joy & J_UP)         checkEncounter();
		else if (joy & J_DOWN)  checkEncounter();
		if (joy & J_LEFT)       checkEncounter();
		else if (joy & J_RIGHT) checkEncounter();
}
	

void checkEncounter() {
	int encounterChance = 80;
	int randomValue = rand();

	if (randomValue % encounterChance == 0) {
		SCX_REG = 0;
		SCY_REG = 0;
	
		set_bkg_data(0, 1, vstile); 
		set_bkg_tiles(160, 160, 40, 50, vsmap);
		
		while(TRUE){
			joy = joypad();
			
			if (joy & J_UP || joy & J_DOWN || joy & J_LEFT || joy & J_RIGHT){
			} else if (joy & J_A)  {
				set_bkg_data(0, 241u, AllTaile);
				set_bkg_submap(map_pos_x, map_pos_y, 20, 18, AllMap, AllMapWidth);
				return;
			}
		}
    }
}


int rand() {
    seed = (seed * 1103515245 + 12345) & 0x7FFFFFFF;
    return seed;
}


◇解説

if (joy & J_UP) checkEncounter();
 else if (joy & J_DOWN) checkEncounter();
 if (joy & J_LEFT) checkEncounter();
 else if (joy & J_RIGHT) checkEncounter();
→checkEncounterを呼び出し

void checkEncounter() {
→checkEncounter関数を定義

int encounterChance = 80;
→エンカウントの確率を設定

int randomValue = rand();
→rand関数を呼び出し、ランダム値を格納

if (randomValue % encounterChance == 0) {
→エンカウント率とランダム値を比較し、エンカウント時の処理に入る

SCX_REG = 0;
 SCY_REG = 0;
→スクロール状態初期化

set_bkg_data(0, 1, vstile);
 set_bkg_tiles(160, 160, 40, 50, vsmap);
→エンカウント時の背景を設定

while(TRUE){
→エンカウント時のキー操作するまでループ

joy = joypad();
 if (joy & J_UP || joy & J_DOWN || joy & J_LEFT || joy & J_RIGHT){
→方向キー入力を無効化

} else if (joy & J_A) {
→エンカウント終了条件として一旦Aボタン押下時の条件追加

set_bkg_data(0, 241u, AllTaile);
 set_bkg_submap(map_pos_x, map_pos_y, 20, 18, AllMap, AllMapWidth);
→通常マップの背景を設定し、エンカウント前の位置に戻る

return;
→戦闘終了

int rand() {
→rand関数定義

seed = (seed * 1103515245 + 12345) & 0x7FFFFFFF;
 return seed;
→ランダムアルゴリズムにてランダム値生成し、値を返す

コンパイル結果
ダウンロード.gif




以上!
次回はランダムエンカウント時のバトルシーンでの操作を作成しよう!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?