上章中我们把俄罗斯方块的核心功能都实现了。这回我们把它弄到像素屏上。
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) | (