CAVE だもんね。(17)


状態遷移版も同時で書いてました。
イコン画像も書いてしまったことだし一先ず今年中に公開することにしました。


本当はもうちょっとMSXユーザが驚く仕掛けも考えてはいるのですがそれは来年ということで。


元ネタは rerofumi さんの MSX-BASIC 版 CAVE です。
ただ僕があまり MSX に精通してないので
BASIC リストを完全にC言語に落とし込んでないのが残念なところと言えば残念なところです。


rerofumiのつぶやき >> 洞窟を進むゲーム for MSX-BASIC
http://www.fumi2kick.com/rrtalk/archives/780


バナーアイコン


タイトル


ケイブ


Cソースは昨日のものに追加で、
Title.h, Title.c が加わったのと、 main.c, Bg.c の内容変更があります。
MSX2J 風のフォントリソースは「CAVE だもんね。(15)」エントリのものを含んでます。


CAVE だもんね。(15)
http://d.hatena.ne.jp/dumbo001/20090322/1237762244


main.c

/*---------------------------------------------------------------------------------
	
	CAVE GAME
	
	prototype 0.01
	Dec 26, 2009
	
	By. REGEKATSU
	
---------------------------------------------------------------------------------*/

#include <nds/ndstypes.h>
#include <nds/interrupts.h>
#include <nds/arm9/input.h>

#include "Bg.h"
#include "Title.h"
#include "Cave.h"


typedef enum{
	MAIN_TITLE, 
	MAIN_CAVE
}MAIN_ACT;

typedef struct{
	u16 act;
}MainStatus;


static MainStatus st_main = {MAIN_TITLE};


void main_Init(void);


//---------------------------------------------------------------------------------
void main_Init(void){
//---------------------------------------------------------------------------------
	
	Bg_Init();
	
}



//---------------------------------------------------------------------------------
int main(void){
//---------------------------------------------------------------------------------
	
	main_Init();
	
	while(1) {
		
		swiWaitForVBlank();
		scanKeys();
		
		switch(st_main.act){
			
		case MAIN_TITLE:
			
			Title_Update();
			
			if(Title_IsExit()){
				st_main.act = MAIN_CAVE;
			}
			
			break;
			
		case MAIN_CAVE:
			
			Cave_Update();
			
			if(Cave_IsExit()){
				st_main.act = MAIN_TITLE;
			}
			
			break;
			
		default:
			break;
			
		}
		
	}
	
}

//---------------------------------------------------------------------------------


Bg.c

/*---------------------------------------------------------------------------------
	
	bg module routine
	
	version 0.01
	Dec 26, 2009
	
	By. REGEKATSU
	
---------------------------------------------------------------------------------*/

#include <stdio.h>
#include <string.h>

#include <nds/arm9/video.h>
#include <nds/arm9/background.h>
#include <nds/arm9/console.h>

#include "Bg.h"
#include "font.h"


typedef struct{
	
	PrintConsole bottom_cons;
	ConsoleFont font;
	
	u16* bottom_cons_map;
	
}BgStatus;


static BgStatus st_bg;


//---------------------------------------------------------------------------------
void Bg_Init(void){
//---------------------------------------------------------------------------------
	
	//initStatus
	memset(&st_bg, 0, sizeof(st_bg));
	
	//initConsole
	st_bg.bottom_cons = *consoleDemoInit();
	
	//initConsoleFont
	st_bg.font.gfx = (u16*)fontTiles;
	st_bg.font.bpp = 4;
	st_bg.font.asciiOffset = 0;
	st_bg.font.numChars = 256;
	st_bg.font.convertSingleColor = true;
	
	consoleSetFont(&st_bg.bottom_cons, &st_bg.font);
	
	//getMapPtr
	st_bg.bottom_cons_map = (u16*)bgGetMapPtr(st_bg.bottom_cons.bgId);
	
	//initPalette
	BG_PALETTE_SUB[0] = RGB8(0, 0, 231);
	BG_PALETTE_SUB[255] = RGB8(231, 227, 231);
	
	//DrawFunctionKeys
	printf("\x1b[23;2Hcolor auto  goto  list  run");
	
	//setConsoleWindow
	consoleSetWindow(&st_bg.bottom_cons, 0, 0, 32, 23);
	consoleSelect(&st_bg.bottom_cons);
	
}



//---------------------------------------------------------------------------------
void Bg_DrawCave(int x, int y, int size){
//---------------------------------------------------------------------------------
	
	printf("\x1b[%d;0H\n", y);
	
	printf("\x1b[%d;2H############################", y);
	printf("\x1b[%d;%dH#", y, x);
	
	
	int i;
	
	for(i = 0;i < size;i++){
		printf(" ");
	}
	
	
	printf("\x1b[%d;%dH#", y, (x + 1) + size);
	
}

//---------------------------------------------------------------------------------
void Bg_DrawScore(int x, int y, int score){
//---------------------------------------------------------------------------------
	
	printf("\x1b[%d;%dHSCORE  %d", y, x, score);
	
}

//---------------------------------------------------------------------------------
void Bg_DrawPlayer(int x, int y, bool is_crash){
//---------------------------------------------------------------------------------
	
	const static char player[3] = {'V', '*', '.'};
	
	static int old_x = 0, old_y = 0;
	
	
	printf("\x1b[%d;%dH%c", old_y - 1, old_x, player[2]);
	printf("\x1b[%d;%dH%c", y, x, player[ is_crash ]);
	
	
	if(is_crash){
		
		old_x = 0;
		old_y = 0;
		
	}else{
		
		old_x = x;
		old_y = y;
		
	}
	
}



//---------------------------------------------------------------------------------
u16 Bg_GetMapValue(u16 map_addr){
//---------------------------------------------------------------------------------
	
	return st_bg.bottom_cons_map[ map_addr ];
	
}

//---------------------------------------------------------------------------------


Title.h

/*---------------------------------------------------------------------------------
	
	title module header
	
	version 0.01
	Dec 26, 2009
	
	By. REGEKATSU
	
---------------------------------------------------------------------------------*/

#ifndef _TITLE_H_
#define _TITLE_H_


#include <nds/ndstypes.h>


#ifdef __cplusplus
extern "C" {
#endif


void Title_Update(void);
bool Title_IsExit(void);


#ifdef __cplusplus
}
#endif

#endif	// _TITLE_H_


Title.c

/*---------------------------------------------------------------------------------
	
	title module routine
	
	version 0.01
	Dec 26, 2009
	
	By. REGEKATSU
	
---------------------------------------------------------------------------------*/

#include <stdio.h>

#include <nds/ndstypes.h>
#include <nds/arm9/input.h>

#include "Title.h"


typedef enum{
	TITLE_INIT, 
	TITLE_EXEC, 
	TITLE_EXIT
}TITLE_ACT;

typedef struct{
	u16 act;
}TitleStatus;


static TitleStatus st_title = {TITLE_INIT};


void title_Init(void);
void title_Exec(void);
void title_Exit(void);


//---------------------------------------------------------------------------------
void title_Init(void){
//---------------------------------------------------------------------------------
	
	printf("\x1b[2J");
	
	printf("\x1b[7;12HC A V E");
	printf("\x1b[9;10Hpush start key");
	
	st_title.act = TITLE_EXEC;
	
}

//---------------------------------------------------------------------------------
void title_Exec(void){
//---------------------------------------------------------------------------------
	
	if(keysDown() & KEY_START){
		st_title.act = TITLE_EXIT;
	}
	
}

//---------------------------------------------------------------------------------
void title_Exit(void){
//---------------------------------------------------------------------------------
	
	st_title.act = TITLE_INIT;
	
}



//---------------------------------------------------------------------------------
void Title_Update(void){
//---------------------------------------------------------------------------------
	
	switch(st_title.act){
		
	case TITLE_INIT:
		title_Init();
		break;
		
	case TITLE_EXEC:
		title_Exec();
		break;
		
	case TITLE_EXIT:
		title_Exit();
		break;
		
		default:
		break;
	}
	
}

//---------------------------------------------------------------------------------
bool Title_IsExit(void){
//---------------------------------------------------------------------------------
	
	return (st_title.act == TITLE_INIT) ? true : false;
	
}

//---------------------------------------------------------------------------------


毎度の如くプロジェクト一式は以下の URL からダウンロードできます。
ttp://page.freett.com/ntr/title/cavep.zip