⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 blockdata.cpp

📁 用VC在BREW平台上开发的手机游戏Superball的源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include "blockdata.h"
#include "Sprite2D.h"
#include "superball_res.h"
#include "blockarbiter.h"
#include "GeneralView.h"

static const uint16 BG08_12_01[] =
{
	 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0,
	 0, 0, 0, 0, 0, 0, 0, 0,
};

static const uint8 BA1[8][8] = 
{
	{0, 0, 0, 1, 1, 0, 0, 0},
	{0, 0, 1, 2, 2, 1, 0, 0},
	{0, 1, 2, 2, 2, 2, 1, 0},
	{1, 2, 2, 3, 3, 2, 2, 1},
	{1, 2, 2, 3, 3, 2, 2, 1},
	{0, 1, 2, 2, 2, 2, 1, 0},
	{0, 0, 1, 2, 2, 1, 0, 0},
	{0, 0, 0, 1, 1, 0, 0, 0}
};

static const uint8 BA2[8][8] = 
{
	{1, 1, 1, 1, 1, 1, 1, 1},
	{1, 2, 2, 1, 1, 3, 3, 1},
	{1, 2, 2, 1, 1, 3, 3, 1},
	{1, 1, 1, 1, 1, 1, 1, 1},
	{1, 1, 1, 1, 1, 1, 1, 1},
	{1, 4, 4, 1, 1, 5, 5, 1},
	{1, 4, 4, 1, 1, 5, 5, 1},
	{1, 1, 1, 1, 1, 1, 1, 1}
};

static const uint8 BA3[8][8] = 
{
	{2, 2, 0, 0, 0, 0, 3,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	{4, 4, 4, 4, 4, 4, 4, 4},
	{4, 4, 6, 6, 6, 6, 4, 4},
	{4, 4, 4, 4, 4, 4, 4, 4}
};

static const uint8 BA8[8][8] = 
{
	{4, 4, 4, 4, 4, 4, 4, 4},
	{4, 6, 1, 1, 1, 1, 6, 4},
	{4, 2, 6, 1, 1, 6, 2, 4},
	{4, 2, 2, 6, 6, 2, 2, 4},
	{4, 2, 2, 6, 6, 2, 2, 4},
	{4, 2, 6, 1, 1, 6, 2, 4},
	{4, 6, 1, 1, 1, 1, 6, 4},
	{4, 4, 4, 4, 4, 4, 4, 4}
};

static const uint8 BA9[8][8] = 
{
	{6, 4, 4, 4, 4, 4, 4, 6},
	{6, 5, 6, 1, 2, 6, 5, 6},
	{6, 5, 6, 2, 1, 6, 5, 6},
	{6, 5, 6, 1, 2, 6, 5, 6},
	{6, 5, 6, 2, 1, 6, 5, 6},
	{6, 5, 6, 1, 2, 6, 5, 6},
	{6, 5, 6, 2, 1, 6, 5, 6},
	{6, 4, 4, 4, 4, 4, 4, 6}
};


static const uint8 BA10[8][8] = 
{
	{0, 0, 0, 0, 0, 0, 0, 0},
	{0, 6, 0, 0, 0, 0, 0, 0},
	{0, 6, 1, 0, 0, 0, 0, 0},
	{0, 6, 2, 4, 0, 0, 0, 0},
	{0, 6, 3, 5, 2, 0, 0, 0},
	{0, 6, 4, 3, 4, 3, 0, 0},
	{0, 6, 5, 2, 1, 3, 5, 0},
	{0, 6, 6, 6, 6, 6, 6, 1}
};

static const uint8 BA11[8][8] = 
{
	{5, 4, 5, 4, 5, 4, 5, 4},
	{4, 5, 4, 5, 4, 5, 4, 5},
	{5, 4, 5, 4, 5, 4, 5, 4},
	{1, 1, 1, 1, 1, 1, 1, 1},
	{2, 2, 2, 2, 2, 2, 2, 2},
	{0, 6, 6, 6, 6, 6, 6, 6},
	{0, 0, 0, 0, 0, 0, 0, 0},
	{6, 6, 6, 6, 6, 6, 6, 0}
};

static const uint8 BA12[8][8] = 
{
	{5, 3, 3, 3, 3, 3, 3, 4},
	{5, 6, 1, 2, 1, 2, 6, 4},
	{5, 6, 2, 1, 2, 1, 6, 4},
	{5, 6, 1, 2, 1, 2, 6, 4},
	{5, 6, 2, 1, 2, 1, 6, 4},
	{5, 6, 1, 2, 1, 2, 6, 4},
	{5, 6, 2, 1, 2, 1, 6, 4},
	{5, 6, 6, 6, 6, 6, 6, 4}
};

BlockData::BlockData(BlockApp *app) : AppData((AEEApplet *)app)
{	
	AEEDeviceInfo di; 
	di.wStructSize = sizeof(AEEDeviceInfo);
	ISHELL_GetDeviceInfo(m_app->m_pIShell, &di);
	scrWidth  = di.cxScreen;
	scrHeight = di.cyScreen;

	////init graphic interface
	ISHELL_CreateInstance(GetApp()->m_pIShell, AEECLSID_GRAPHICS, (void**)&pg);
	IGRAPHICS_SetFillMode(pg, true);
}

BlockData::~BlockData()
{
	delete mgr;
	if(pg)
		IGRAPHICS_Release(pg);
}

void BlockData::initialize()
{
	int i; 
	int j;

	leftedge = scrWidth/2 - 60;
	rightedge = scrWidth/2 + 60;
	blockcount = 0;

	////set block array
	for(i = 0; i < ARRAY_ROW; i++)
		for(j = 0; j < ARRAY_COL; j++)
		{
			block_array[i][j] = BA1[i][j];    ////****
			if(block_array[i][j] >= 1 && block_array[i][j] <= 5)
				blockcount++;
		}

	////initialize sprites and tiles
	mgr = new SpriteMgr((AEEApplet*)m_app);
	mgr->RegisterTile(1);
	if(scrWidth == 120)
		mgr->RegisterSprite(ARRAY_ROW*ARRAY_COL+3+5+3+8);
	else
		mgr->RegisterSprite(ARRAY_ROW*ARRAY_COL+3+5+3+8+24);
	////2 ball, 1 board, 5 score, 3 life, 8 letter, 24 edge
	mgr->LoadScene(SPRITE_SIZE_16X16, IDI_TILE16, SUPERBALL_RES_FILE, TILE);
	mgr->LoadScene(SPRITE_SIZE_8X8,   IDI_BALL,   SUPERBALL_RES_FILE, SPRITE);
	mgr->LoadScene(SPRITE_SIZE_16X16, IDI_BLOCKS, SUPERBALL_RES_FILE, SPRITE);
	mgr->LoadScene(SPRITE_SIZE_32X32, IDI_BOARD,  SUPERBALL_RES_FILE, SPRITE);
	map.SetManager(mgr);
    map.RegisterProperty((uint16*)BG08_12_01, 0, 0, MAP_SIZE_8, MAP_SIZE_16, TILE_SIZE_16X16);
	for(i = 0; i < 5; i++)    ////show score	
	{
		scoresm[i].SetManager(mgr);
		scoresm[i].RegisterProperty(8+i*8, 1, 6, 3, SPRITE_SIZE_16X16);
	}
	for(i = 0; i < 3; i++)
	{
		lifesm[i].SetManager(mgr);
		lifesm[i].RegisterProperty(scrWidth-21-17*i, 8, 0, 3, SPRITE_SIZE_32X32);
	}
	for(i = 0; i < 8; i++)    ////letters	
	{
		lettersm[i].SetManager(mgr);
		lettersm[i].RegisterProperty(0, 0, 16, 3, SPRITE_SIZE_16X16);
		lettersm[i].Hide();
	}
	if(scrWidth != 120)
		for(i = 0; i < 24; i++)
		{
			edgesm[i].SetManager(mgr);
			edgesm[i].RegisterProperty((i%2)*124, i/2*16, 72+i%2, 0, SPRITE_SIZE_16X16);
			//edgesm[i].ModifyShape(180, 1);
		}
	////initialize ball, board and blocks
	ball1.cx = scrWidth/2;
	ball1.cy = scrHeight - 15;	////****
	ball1.specify = 3;			
	ball1.speed = 2;		
	ball1.dir = NORTHEAST;
	ball1.picindex = -1;
	ball1.status = 0;
	ball1.sm.SetManager(mgr);
	ball1.sm.RegisterProperty(ball1.cx-4, ball1.cy-4, 0, 3, SPRITE_SIZE_8X8);
	ball2.specify = 3;
	ball2.picindex = -1;
	ball2.sm.SetManager(mgr);
	ball2.sm.RegisterProperty(0, 0, 0, 3, SPRITE_SIZE_8X8);
	ball2.sm.Hide();
	board.cx = scrWidth/2;
	board.cy = scrHeight-10;	////****
	board.specify = 12;			
	board.speed = 4;
	board.dir = 0;				
	board.picindex = 1;			////****
	board.status = 0;
	board.sm.SetManager(mgr);
	board.sm.RegisterProperty(board.cx-16, board.cy-2, 1, 3, SPRITE_SIZE_32X32);
	for(i = 0; i < ARRAY_ROW; i++)
		for(j = 0; j < ARRAY_COL; j++)
		{
			blocks[i][j].sm.SetManager(mgr);
			blocks[i][j].sm.RegisterProperty(leftedge+j*15, i*8+8, block_array[i][j]%6, 1, SPRITE_SIZE_16X16);////****
			if(block_array[i][j] != 0)
			{
				blocks[i][j].cx = j*15+7;
				blocks[i][j].cy = i*8+20;       
				blocks[i][j].picindex = block_array[i][j] % 6;    
				blocks[i][j].status = 0;
			}
			else
				blocks[i][j].sm.Hide();
		}	

	////initialize game parameters
	ballcount = 0;
	score = 0;
	life = 3;
	level = 1;
	upgrading = false;
	newlevel = false;
	newlife = false;
	newuser = false;
	highscore = false;
}

void BlockData::sendball()
{
	if(ballcount >0)
		return;
	if(ballcount == 0 && life > 0)
	{
		ball1.picindex = 0;
		ballcount++;	
	}
}

void BlockData::moveboard(boolean PorR, boolean RorL)
{
	////dir of board: 0--stop, 1--moving right, 2--moving left   
	if(PorR)
		board.dir = RorL ? 1 : 2;
	else
	{
		board.dir = 0;
		board.cx = RorL ? (board.cx+board.speed) : (board.cx-board.speed);
	}		
}

void BlockData::moveall()
{
	int i;
	int j;

	////timer of number of ball
	if(ballcount == 2)
		if(++ballnumbertime == 200)
		{
			ballcount--;
			if(ball1.picindex == 0)
			{
				ball1.picindex = -1;
				ball1.sm.Hide();
			}
			else
			{
				ball2.picindex = -1;
				ball2.sm.Hide();
			}	
		}
	////timer of speed of ball
	if(ball1.speed == 3)
		if(++ballspeedtime == 200)
		{
			ball1.speed = 2;
			ball2.speed = 2;
		}
	////timer of super ball
	if(ball1.picindex == 1 || ball2.picindex == 1)
		if(++superballtime == 200)
		{
			if(ball1.picindex != -1)
			{
				ball1.picindex = 0;
				ball1.sm.ModifyStripIndex(ball1.picindex);
			}
			if(ball2.picindex != -1)
			{
				ball2.picindex = 0;
				ball2.sm.ModifyStripIndex(ball2.picindex);
			}
		}
	////timer of size of board
	if(board.status != 0)
		if(++boardsizetime == 200)
		{
			board.status = 0;
			board.picindex = 1;
			board.specify = 12;
			board.sm.ModifyStripIndex(board.picindex);
		}
		
	////blocks
	for(i = 0; i < ARRAY_ROW; i++)
		for(j = 0; j < ARRAY_COL; j++)
			if(blocks[i][j].status == 1)	////in dropping
			{
				blocks[i][j].cy += blocks[i][j].speed;
				if((blocks[i][j].cy >= board.cy) && (blocks[i][j].cy <= board.cy+6) && 
					(blocks[i][j].cx >= board.cx-board.specify) && 
					(blocks[i][j].cx <= board.cx+board.specify))
				{
					catchblock(blocks[i][j].specify);
					blocks[i][j].status = 2;
					blocks[i][j].sm.Hide();
				}
				if(blocks[i][j].cy >= scrHeight+4 || ballcount == 0)
				{
					blocks[i][j].status = 2;
					blocks[i][j].sm.Hide();
				}
				blocks[i][j].sm.MoveToPos(blocks[i][j].cx-7, blocks[i][j].cy-12);
			}
	
	////board
	if(board.dir != 0)
		board.cx = (board.dir == 1) ? (board.cx+board.speed) : (board.cx-board.speed);
	if(board.cx < leftedge+board.specify)
		board.cx = leftedge+board.specify;
	if(board.cx > rightedge-board.specify)
		board.cx = rightedge-board.specify;
	board.sm.MoveToPos(board.cx-board.specify, board.cy-2);
	if(ballcount == 0)
	{
		ball1.cx = board.cx;
		ball1.cy = board.cy-5;
		ball1.sm.MoveToPos(ball1.cx-4, ball1.cy-4);
	}
	/////score
	if(score > 99999)
	{
		upgrading = true;
		upgradetime = 0;
		for(i = 0; i < 3; i++)
			lifesm[i].Hide();
		for(i = 0; i < 4; i++)    ////游戏结束
		{
			lettersm[i].ModifyStripIndex(87+i);
			lettersm[i].MoveToPos(-16, -16);
			lettersm[i].Reveal(3);
		}
	};
	scoresm[0].ModifyStripIndex(score/10000+6);
	scoresm[1].ModifyStripIndex((score%10000)/1000+6);
	scoresm[2].ModifyStripIndex((score%1000)/100+6);
	scoresm[3].ModifyStripIndex((score%100)/10+6);
	scoresm[4].ModifyStripIndex(score%10+6);

}

void BlockData::moveball(item* ballx)
{
	int dx, dy;
	if(ballx->status == 0)
	{
		dx = 0;
		dy = 0;
	}
	else if(ballx->status == 1)
	{
		dx = -1;
		dy = 1;
	}
	else
	{
		dx = 1;
		dy = -1;
	}

	switch(ballx->dir)	////change position of ball
	{
	case NORTHEAST: 
		ballx->cx += ballx->speed + dx;
		ballx->cy -= ballx->speed + dy;
		break;
	case NORTHWEST:
		ballx->cx -= ballx->speed + dx;
		ballx->cy -= ballx->speed + dy;
		break;
	case SOUTHEAST: 
		ballx->cx += ballx->speed + dx;
		ballx->cy += ballx->speed + dy;
		break;
	case SOUTHWEST:
		ballx->cx -= ballx->speed + dx;
		ballx->cy += ballx->speed + dy;
		break;
	default:
		break;		
	}
	ballx->sm.MoveToPos(ballx->cx-ballx->specify, ballx->cy-ballx->specify);
}

void BlockData::check()
{
	if(ball1.picindex != -1)
		checkball(&ball1);
	if(ball2.picindex != -1)
	    checkball(&ball2);
}

void BlockData::checkball(item* ballx)
{
	boolean ballshine;
	
	ballx->row = (ballx->cy-16)/8;
	ballx->col = (ballx->cx-leftedge)/15;
	ballshine = false;
	ballx->sm.ModifyStripIndex(ballx->picindex);

	if(ballx->row >= 0 && ballx->row < ARRAY_ROW && block_array[ballx->row][ballx->col] != 0)
	{	////make sure not entered a block
		if(ballx->picindex == 0)
			switch(ballx->dir)
			{
			case NORTHEAST:    ballx->dir = SOUTHWEST;    break;
			case NORTHWEST:    ballx->dir = SOUTHEAST;    break;
			case SOUTHEAST:    ballx->dir = NORTHWEST;    break;
			case SOUTHWEST:    ballx->dir = NORTHEAST;    break;
			default:    break;
			}
		if(block_array[ballx->row][ballx->col] <= 5)
			hitblock(ballx->row, ballx->col);
		else if(ballx->picindex == 1)
		{
			block_array[ballx->row][ballx->col] = 0;
			blocks[ballx->row][ballx->col].sm.Hide();
		}
		ballshine = true;
	}

	if(ballx->status != 0 && ballx->speed == 3)
		ballx->specify = 4;
	else
		ballx->specify = 3;
	switch(ballx->dir)	//// decide hitting 
	{
		case NORTHEAST:
			////up
			if((ballx->row == -2) && (ballx->cy <= ballx->specify))   ////hit upedge
				ballx->dir = SOUTHEAST;
			else if ((ballx->row > 0) && (ballx->row <= ARRAY_ROW) && (block_array[ballx->row-1][ballx->col] != 0) 
				&& (ballx->cy <= 16+ballx->row*8+ballx->specify))		  ////hit up block
			{
				if(ballx->picindex == 0)
					ballx->dir = SOUTHEAST;
				if(block_array[ballx->row-1][ballx->col] <= 5)
					hitblock(ballx->row-1, ballx->col);
				ballshine = true;
			}
			////right
			if((ballx->col == ARRAY_COL-1) && (ballx->cx >= rightedge-ballx->specify))  ////hit right edge
				ballx->dir = (ballx->dir == NORTHEAST) ? NORTHWEST : SOUTHWEST;
			else if ((ballx->row >= 0) && (ballx->row < ARRAY_ROW) && (ballx->col != ARRAY_COL-1) &&   ////hit right block 
				(block_array[ballx->row][ballx->col+1] != 0) && 
				(ballx->cx >= leftedge+ballx->col*15+15-ballx->specify))
			{
				if(ballx->picindex == 0)
					ballx->dir = (ballx->dir == NORTHEAST) ? NORTHWEST : SOUTHWEST;
				if(block_array[ballx->row][ballx->col+1] <= 5)
					hitblock(ballx->row, ballx->col+1);
				ballshine = true;
			}
			break;
		case NORTHWEST:
			////up
			if((ballx->row == -2) && (ballx->cy <= ballx->specify))    ////hit upedge  
				ballx->dir = SOUTHWEST;
			else if ((ballx->row > 0) && (ballx->row <= ARRAY_ROW) &&	(block_array[ballx->row-1][ballx->col] != 0) 
				&& (ballx->cy <= 16+ballx->row*8+ballx->specify))    ////hit up block	
			{
				if(ballx->picindex == 0)
					ballx->dir = SOUTHWEST;
				if(block_array[ballx->row-1][ballx->col] <= 5)
					hitblock(ballx->row-1, ballx->col);
				ballshine = true;
			}
			////left
			if((ballx->col == 0) && (ballx->cx <= leftedge+ballx->specify))   ////hit leftedge	
				ballx->dir = (ballx->dir == NORTHWEST) ? NORTHEAST : SOUTHEAST;
			else if ((ballx->row >= 0) && (ballx->row < ARRAY_ROW) && (ballx->col != 0) &&     ////hit left block 
			    (block_array[ballx->row][ballx->col-1] != 0) && 
			    (ballx->cx <= leftedge+ballx->col*15+ballx->specify))
			{
				if(ballx->picindex == 0)
					ballx->dir = (ballx->dir == NORTHWEST) ? NORTHEAST : SOUTHEAST;
				if(block_array[ballx->row][ballx->col-1] <= 5)
					hitblock(ballx->row, ballx->col-1);
				ballshine = true;
			}
			break;

		case SOUTHEAST:
			////down
			if(loseball(ballx))
				return;
			if((ballx->cy >= board.cy-5) && (ballx->cy <= board.cy) && 
				(ballx->cx >= board.cx-board.specify) && (ballx->cx <= board.cx+board.specify)) 
			{
			    ballx->dir = NORTHEAST;          ////hit the board
				if(ballx->cx-board.cx<=board.specify/3 && board.cx-ballx->cx<=board.specify/3)
					ballx->status = 1;
				else if(ballx->cx-board.cx>=board.specify*2/3 || board.cx-ballx->cx>=board.specify*2/3)
					ballx->status = 2;
				else
					ballx->status = 0;
				if(ballx->status != 0 && ballx->speed == 3)
					ballx->specify = 4;
				else
					ballx->specify = 3;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -