CAVE だもんね。(16)


息抜きプログラムです。
プログラム書くことが息抜きと呼べるくらいになってきました(小規模なものに限りますが。)。
何か作ってないと落ち着かないので逆に何もしないことの方が息苦しかったりします。


今年3月で飽きてしまって一時凍結してた CAVE ゲームを書き直しました。
何となく再開です( id:DK_alpha さんの受けが良かったのがきっかけと言えばきっかけ。)。


ゲーム本体のみでタイトルなどの状態は含んでません。
当り判定を VRAM 読んで取ってるところが見所でしょうか。(マテ


CAVE


main.c

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

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

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


void main_Init(void);


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



//---------------------------------------------------------------------------------
int main(void){
//---------------------------------------------------------------------------------
	
	main_Init();
	
	while(1) {
		
		swiWaitForVBlank();
		scanKeys();
		
		Cave_Update();
		
		if(Cave_IsExit()){
			return 0;
		}
		
	}
	
}

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


Bg.h

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

#ifndef _BG_H_
#define _BG_H_


#include <nds/ndstypes.h>


#ifdef __cplusplus
extern "C" {
#endif


void Bg_Init(void);

void Bg_DrawCave(int x, int y, int size);
void Bg_DrawScore(int x, int y, int score);
void Bg_DrawPlayer(int x, int y, bool is_crash);

u16 Bg_GetMapValue(u16 map_addr);


#ifdef __cplusplus
}
#endif

#endif	// _BG_H_


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"


typedef struct{
	
	PrintConsole bottom_cons;
	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();
	
	//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);
	
	//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 ];
	
}

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


Cave.h

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

#ifndef _GAME_H_
#define _GAME_H_


#ifdef __cplusplus
extern "C" {
#endif


void Cave_Update(void);
bool Cave_IsExit(void);


#ifdef __cplusplus
}
#endif

#endif


Cave.c

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

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include <nds.h>

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


#define WAIT_RATE 12

#define SCORE_POS_X 4
#define SCORE_POS_Y 0

#define PLAYER_POS_X 16
#define PLAYER_POS_Y 3

#define CAVE_POS_X 10
#define CAVE_POS_Y 22

#define CAVE_SIZE_MIN 2
#define CAVE_SIZE_MAX 12

#define CAVE_MOVE_MIN 2
#define CAVE_MOVE_MAX 29

#define CAVE_MOVE_L (-1)
#define CAVE_MOVE_R  1


typedef enum{
	CAVE_INIT, 
	CAVE_PLAY, 
	CAVE_MISS, 
	CAVE_END, 
	CAVE_EXIT
}CAVE_ACT;

typedef struct{
	
	u16 act;
	u32 wait;
	
	int score;
	
	int px;
	int py;
	
	int cx;
	int cy;
	int size;
	
}CaveStatus;


static CaveStatus st_cave;


#define CHR_PAL_NUM(x) ((x) << 12)

void cave_UpdateSize(void);
void cave_UpdateCave(void);
void cave_UpdateScore(void);
void cave_UpdatePlayer(void);

bool cave_IsCrash(void);

void cave_Init(void);
void cave_Play(void);
void cave_Miss(void);
void cave_End(void);
void cave_Exit(void);


//---------------------------------------------------------------------------------
void cave_UpdateSize(void){
//---------------------------------------------------------------------------------
	
	if(!st_cave.score){
		return;
	}
	
	if( (st_cave.score % 30) == 0){
		
		if(st_cave.size >= CAVE_SIZE_MIN){
			st_cave.size--;
		}
		
	}
	
}

//---------------------------------------------------------------------------------
void cave_UpdateCave(void){
//---------------------------------------------------------------------------------
	
	int rnd = rand() % 2;
	
	
	if(rnd){
		
		if(st_cave.cx > CAVE_MOVE_MIN){
			st_cave.cx += CAVE_MOVE_L;
		}
		
	}else{
		
		if( ( (st_cave.cx + 1) + st_cave.size ) < CAVE_MOVE_MAX ){
			st_cave.cx += CAVE_MOVE_R;
		}
		
	}
	
	
	Bg_DrawCave(st_cave.cx, st_cave.cy, st_cave.size);
	
}



//---------------------------------------------------------------------------------
void cave_UpdateScore(void){
//---------------------------------------------------------------------------------
	
	st_cave.score++;
	Bg_DrawScore(SCORE_POS_X, SCORE_POS_Y, st_cave.score);
	
}

//---------------------------------------------------------------------------------
void cave_UpdatePlayer(void){
//---------------------------------------------------------------------------------
	
	if(keysHeld() & KEY_LEFT){
		
		if(st_cave.px > CAVE_MOVE_MIN){
			st_cave.px--;
		}
		
	}else if(keysHeld() & KEY_RIGHT){
		
		if(st_cave.px < CAVE_MOVE_MAX){
			st_cave.px++;
		}
		
	}
	
	
	if(cave_IsCrash()){
		st_cave.act = CAVE_MISS;
	}
	
	
	Bg_DrawPlayer(st_cave.px, st_cave.py, cave_IsCrash());
	
}

//---------------------------------------------------------------------------------
bool cave_IsCrash(void){
//---------------------------------------------------------------------------------
	
	return ( Bg_GetMapValue( st_cave.px + (st_cave.py * 32) ) == (CHR_PAL_NUM(15) | '#') ) ? true:false;
	
}



//---------------------------------------------------------------------------------
void cave_Init(void){
//---------------------------------------------------------------------------------
	
	printf("\x1b[2J");
	
	srand(time(NULL));
	
	st_cave.wait = WAIT_RATE;
	
	st_cave.score = 0;
	st_cave.px = PLAYER_POS_X;
	st_cave.py = PLAYER_POS_Y;
	st_cave.cx = CAVE_POS_X;
	st_cave.cy = CAVE_POS_Y;
	st_cave.size = CAVE_SIZE_MAX;
	
	st_cave.act = CAVE_PLAY;
	
}

//---------------------------------------------------------------------------------
void cave_Play(void){
//---------------------------------------------------------------------------------
	
	st_cave.wait--;
	
	if(st_cave.wait){
		return;
	}
	
	st_cave.wait = WAIT_RATE;
	
	cave_UpdateSize();
	cave_UpdateCave();
	cave_UpdateScore();
	cave_UpdatePlayer();
	
}

//---------------------------------------------------------------------------------
void cave_Miss(void){
//---------------------------------------------------------------------------------
	
	printf("\x1b[7;12HGAME OVER");
	st_cave.act = CAVE_END;
	
}

//---------------------------------------------------------------------------------
void cave_End(void){
//---------------------------------------------------------------------------------
	
	if(keysDown() & KEY_START){
		st_cave.act = CAVE_EXIT;
	}
	
}

//---------------------------------------------------------------------------------
void cave_Exit(void){
//---------------------------------------------------------------------------------
	
	st_cave.act = CAVE_INIT;
	
}



//---------------------------------------------------------------------------------
void Cave_Update(void){
//---------------------------------------------------------------------------------
	
	switch(st_cave.act){
		
	case CAVE_INIT:
		cave_Init();
		break;
		
	case CAVE_PLAY:
		cave_Play();
		break;
		
	case CAVE_MISS:
		cave_Miss();
		break;
		
	case CAVE_END:
		cave_End();
		break;
		
	case CAVE_EXIT:
		cave_Exit();
		break;
		
	default:
		break;
		
	}
	
}

//---------------------------------------------------------------------------------
bool Cave_IsExit(void){
//---------------------------------------------------------------------------------
	
	return (st_cave.act == CAVE_INIT) ? true : false;
	
}

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


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