STM32学习笔记九:WS2812制作像素游戏屏(俄罗斯方块-下)

上章中我们把俄罗斯方块的核心功能都实现了。这回我们把它弄到像素屏上。

STM32学习笔记八:WS2812制作像素游戏屏(俄罗斯方块-上)

我设计担任和双人两种玩法。分别是Tetris和Tetris2。上章中的公共核心算法,都放在TetrisBase基类里面。TetrisBase 实现IGame,而 Tetris和Tetris2 继承 TetrisBase。

1、我们先来看单人玩法。

俄罗斯方块的标准尺寸是10*20,而我们的像素屏分辨率是64*32,横屏时怎么摆都不好看,所以单人玩法限制只能竖屏。

gameDef.cpp中应用定义:

#define IDX_Game_Tetris 4

Games_t games[] = { { IDX_Game_Picture, "Picture", H_V },
 { IDX_Game_Clock,"Clock", H_ONLY }, 
{IDX_Game_FireWork, "FireWork", H_V }, 
{ IDX_Game_Tetris, "Tetris", V_ONLY } };

IGame* Create_Game(uint8_t idx) {
	Serial_print("Create Game. idx=%d, game=%d", idx, games[idx].idx);
	switch (games[idx].idx) {
	case IDX_Game_Picture:
		return new Image();
	case IDX_Game_Clock:
		return new Clock();
	case IDX_Game_FireWork:
		return new FireWork();
	case IDX_Game_Tetris:
		return new Tetris();
	}
	return NULL;
}

游戏结束的判断方法。

uint8_t Tetris::fix() {
	uint8_t res = Tetris_RemoveLine(player);
	score += res * res;
	sprintf(str, "%05d", score);
	ws2812_stringAt(32, 0, str);

	if (player->currSharp->y == 0
			&& Tetris_Check(player->board, player->currSharp->x,
					player->currSharp->y, player->currSharp->data)) {
		Serial_print("Tetris_Width=%d\n", Tetris_Width);
		Tetris_ShowSharp(player->currSharp, player->Board_Offset_X,
				player->Board_Offset_Y);
		ws2812_fill(0, 20, 32, 10, 0, 0, 0);
		ws2812_stringAt(5, 21, "OVER");

		int rec = (record[0] << 24) | (
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

vvind

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值