Working Snake & Tetris

This commit is contained in:
Edward Emelianov 2021-04-29 22:02:16 +03:00
parent d70a67cc9a
commit 02f0a4b6af
8 changed files with 885 additions and 205 deletions

Binary file not shown.

View File

@ -19,171 +19,10 @@
// this file should be included JUST ONCE! // this file should be included JUST ONCE!
// only in fonts.c // only in fonts.c
#define FONTNUMB8BYTES 8 #define FONTNUMB8BYTES 11
#define FONTNUMB8HEIGHT 8 #define FONTNUMB8HEIGHT 11
#define FONTNUMB8BASELINE 0 #define FONTNUMB8BASELINE 2
// this array shows position of every koi8-r symbol in font10_table
const uint8_t fontNumb8_encoding[256] = {
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..31
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 47
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 0, 0, 0, 0, 0, // 63
0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, // 79
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 95
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 111
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 127
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 143
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 159
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 175
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 191
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 207
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 223
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 239
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 255
};
const uint8_t fontNumb8_table[] = {
// 0 0x00 - empty
0,
________,
________,
________,
________,
________,
________,
________,
________,
// 1 0x20 - ' '
4,
________,
________,
________,
________,
________,
________,
________,
________,
// 2 0x30 - '0'
6,
_XXX____,
XX_XX___,
X___X___,
X___X___,
X___X___,
X___X___,
XX_XX___,
_XXX____,
// 3 0x31 - '1'
6,
_XX_____,
X_X_____,
__X_____,
__X_____,
__X_____,
__X_____,
__X_____,
XXXXX___,
// 4 0x32 - '2'
6,
_XXX____,
X___X___,
____X___,
____X___,
___X____,
__X_____,
_X______,
XXXXX___,
// 5 0x33 - '3'
6,
XXXX____,
____X___,
____X___,
_XXX____,
____X___,
____X___,
____X___,
XXXX____,
// 6 0x34 - '4'
7,
___XX___,
___XX___,
__X_X___,
_X__X___,
_X__X___,
XXXXXX__,
____X___,
____X___,
// 7 0x35 - '5'
5,
XXXX____,
X_______,
X_______,
XXX_____,
___X____,
___X____,
___X____,
XXX_____,
// 8 0x36 - '6'
6,
_XXX____,
X___X___,
X_______,
XXXX____,
X___X___,
X___X___,
X___X___,
_XXX____,
// 9 0x37 - '7'
5,
XXXX____,
___X____,
__X_____,
__X_____,
__X_____,
_X______,
_X______,
_X______,
// 10 0x38 - '8'
6,
_XXX____,
X___X___,
X___X___,
_XXX____,
X___X___,
X___X___,
X___X___,
_XXX____,
// 11 0x39 - '9'
6,
_XXX____,
X___X___,
X___X___,
X___X___,
_XXXX___,
____X___,
X___X___,
_XXX____,
// 12 0x41 - 'A'
6,
__X_____,
_X_X____,
_X_X____,
_X_X____,
X___X___,
XXXXX___,
X___X___,
X___X___,
// 13 0x4f - 'O'
7,
__XX____,
_X__X___,
X____X__,
X____X__,
X____X__,
X____X__,
_X__X___,
__XX____,
};
/* /*
32 (0x20) - 64 (0x40) - @ 96 (0x60) - ` 128 (0x80) - 160 (0xa0) -   192 (0xc0) - À 224 (0xe0) - à 32 (0x20) - 64 (0x40) - @ 96 (0x60) - ` 128 (0x80) - 160 (0xa0) -   192 (0xc0) - À 224 (0xe0) - à
@ -219,3 +58,823 @@ const uint8_t fontNumb8_table[] = {
62 (0x3e) - > 94 (0x5e) - ^ 126 (0x7e) - ~ 158 (0x9e) - ž 190 (0xbe) - ¾ 222 (0xde) - Þ 254 (0xfe) - þ 62 (0x3e) - > 94 (0x5e) - ^ 126 (0x7e) - ~ 158 (0x9e) - ž 190 (0xbe) - ¾ 222 (0xde) - Þ 254 (0xfe) - þ
63 (0x3f) - ? 95 (0x5f) - _ 127 (0x7f) - 159 (0x9f) - Ÿ 191 (0xbf) - ¿ 223 (0xdf) - ß 255 (0xff) - ÿ 63 (0x3f) - ? 95 (0x5f) - _ 127 (0x7f) - 159 (0x9f) - Ÿ 191 (0xbf) - ¿ 223 (0xdf) - ß 255 (0xff) - ÿ
*/ */
// this array shows position of every koi8-r symbol in font10_table
const uint8_t fontNumb8_encoding[256] = {
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..31
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
/*20*/1, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, // 47
/*30*/2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 29, 30, 31, 32, 33, 34, // 63
/*40*/35, 12, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 13, // 79
/*50*/49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 0, 0, 0, 0, 0, 0, // 95
/*60*/0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 111
/*70*/0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 127
/*80*/0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 143
/*90*/0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 159
/*A0*/0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 175
/*B0*/0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 191
/*C0*/0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 207
/*D0*/0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 223
/*E0*/0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 239
/*F0*/0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 255
};
const uint8_t fontNumb8_table[] = {
// 0 0x00 - empty
0,
________,
________,
________,
________,
________,
________,
________,
________,
________,
________,
________,
// 1 0x20 - ' '
4,
________,
________,
________,
________,
________,
________,
________,
________,
________,
________,
________,
// 2 0x30 - '0'
6,
________,
_XXX____,
XX_XX___,
X___X___,
X___X___,
X___X___,
X___X___,
XX_XX___,
_XXX____,
________,
________,
// 3 0x31 - '1'
6,
________,
_XX_____,
X_X_____,
__X_____,
__X_____,
__X_____,
__X_____,
__X_____,
XXXXX___,
________,
________,
// 4 0x32 - '2'
6,
________,
_XXX____,
X___X___,
____X___,
____X___,
___X____,
__X_____,
_X______,
XXXXX___,
________,
________,
// 5 0x33 - '3'
6,
________,
XXXX____,
____X___,
____X___,
_XXX____,
____X___,
____X___,
____X___,
XXXX____,
________,
________,
// 6 0x34 - '4'
7,
________,
___XX___,
___XX___,
__X_X___,
_X__X___,
_X__X___,
XXXXXX__,
____X___,
____X___,
________,
________,
// 7 0x35 - '5'
5,
________,
XXXX____,
X_______,
X_______,
XXX_____,
___X____,
___X____,
___X____,
XXX_____,
________,
________,
// 8 0x36 - '6'
6,
________,
_XXX____,
X___X___,
X_______,
XXXX____,
X___X___,
X___X___,
X___X___,
_XXX____,
________,
________,
// 9 0x37 - '7'
5,
________,
XXXX____,
___X____,
__X_____,
__X_____,
__X_____,
_X______,
_X______,
_X______,
________,
________,
// 10 0x38 - '8'
6,
________,
_XXX____,
X___X___,
X___X___,
_XXX____,
X___X___,
X___X___,
X___X___,
_XXX____,
________,
________,
// 11 0x39 - '9'
6,
________,
_XXX____,
X___X___,
X___X___,
X___X___,
_XXXX___,
____X___,
X___X___,
_XXX____,
________,
________,
// 12 0x41 - 'A'
6,
________,
__X_____,
_X_X____,
_X_X____,
_X_X____,
X___X___,
XXXXX___,
X___X___,
X___X___,
________,
________,
// 13 0x4f - 'O'
7,
________,
__XX____,
_X__X___,
X____X__,
X____X__,
X____X__,
X____X__,
_X__X___,
__XX____,
________,
________,
// 14 0x21 - '!'
2,
________,
X_______,
X_______,
X_______,
X_______,
X_______,
X_______,
________,
X_______,
________,
________,
// 15 0x22 - '"'
4,
________,
________,
X_X_____,
X_X_____,
X_X_____,
________,
________,
________,
________,
________,
________,
// 16 0x23 - '#'
7,
________,
________,
____X_X_,
___X_X__,
XXXXXXX_,
__X_X___,
XXXXXXX_,
_X_X____,
X_X_____,
________,
________,
// 17 0x24 - '$'
6,
________,
__X_____,
_XXX____,
X_X_X___,
X_X_____,
_XXX____,
__X_X___,
X_X_X___,
XXXX____,
__X_____,
________,
// 18 0x25 - '%'
8,
________,
________,
_XX___X_,
X__X_X__,
_XX_X___,
___X____,
__X_XX__,
_X_X__X_,
X___XX__,
________,
________,
// 19 0x26 - '&'
7,
________,
________,
_X______,
X_X_____,
X_X_____,
_X__XX__,
X_XXX___,
X__X____,
_XX_XX__,
________,
________,
// 20 0x27 - '''
2,
________,
________,
X_______,
X_______,
X_______,
________,
________,
________,
________,
________,
________,
// 21 0x28 - '('
4,
________,
__XX____,
_X______,
X_______,
X_______,
X_______,
X_______,
X_______,
_X______,
__XX____,
________,
// 22 0x29 - ')'
4,
________,
XX______,
__X_____,
___X____,
___X____,
___X____,
___X____,
___X____,
__X_____,
XX______,
________,
// 23 0x2A - '*'
6,
________,
________,
________,
__X_____,
XXXXX___,
_XXX____,
X___X___,
________,
________,
________,
________,
// 24 0x2B - '+'
6,
________,
________,
________,
__X_____,
__X_____,
XXXXX___,
__X_____,
__X_____,
________,
________,
________,
// 25 0x2C - ','
3,
________,
________,
________,
________,
________,
________,
________,
XX______,
_X______,
X_______,
________,
// 26 0x2D - '-'
6,
________,
________,
________,
________,
________,
XXXXX___,
________,
________,
________,
________,
________,
// 27 0x2E - '.'
3,
________,
________,
________,
________,
________,
________,
________,
XX______,
XX______,
________,
________,
// 28 0x2F - '/'
6,
________,
____X___,
___X____,
___X____,
__X_____,
__X_____,
_X______,
_X______,
_X______,
X_______,
________,
// 29 0x3A - ':'
3,
________,
________,
________,
XX______,
XX______,
________,
XX______,
XX______,
________,
________,
________,
// 30 0x3B - ';'
3,
________,
________,
________,
________,
XX______,
XX______,
________,
XX______,
_X______,
X_______,
________,
// 31 0x3C - '<'
4,
________,
________,
________,
__X_____,
_X______,
X_______,
_X______,
__X_____,
________,
________,
________,
// 32 0x3D - '='
5,
________,
________,
________,
________,
XXXX____,
________,
XXXX____,
________,
________,
________,
________,
// 33 0x3E - '>'
4,
________,
________,
________,
X_______,
_X______,
__X_____,
_X______,
X_______,
________,
________,
________,
// 34 0x3F - '?'
5,
________,
_XX_____,
X__X____,
___X____,
__X_____,
_X______,
_X______,
________,
_X______,
________,
________,
// 35 0x40 - '@'
8,
________,
___XXX__,
__X__X__,
_X_XXXX_,
X__X_XX_,
X_X_X_X_,
X_X_X_X_,
X__X_X__,
_X______,
__XXXX__,
________,
// 36 0x42 - 'B'
6,
________,
XXX_____,
X__X____,
X_X_____,
XXXX____,
X___X___,
X___X___,
X___X___,
XXXX____,
________,
________,
// 37 0x43 - 'C'
7,
________,
__XXX___,
_X___X__,
X_______,
X_______,
X_______,
X_______,
_X___X__,
__XXX___,
________,
________,
// 38 0x44 - 'D'
6,
________,
XXX_____,
X__X____,
X___X___,
X___X___,
X___X___,
X___X___,
X__X____,
XXX_____,
________,
________,
// 39 0x45 - 'E'
6,
________,
XXXXX___,
X_______,
X_______,
XXX_____,
X_______,
X_______,
X___X___,
XXXXX___,
________,
________,
// 40 0x46 - 'F'
6,
________,
XXXXX___,
X___X___,
X_______,
X_______,
XXX_____,
X_______,
X_______,
X_______,
________,
________,
// 41 0x47 - 'G'
7,
________,
__XXX___,
_X___X__,
X_______,
X_______,
X_______,
X__XXX__,
_X___X__,
__XXX___,
________,
________,
// 42 0x48 - 'H'
6,
________,
X___X___,
X___X___,
X___X___,
XXXXX___,
X___X___,
X___X___,
X___X___,
X___X___,
________,
________,
// 43 0x49 - 'I'
4,
________,
XXX_____,
_X______,
_X______,
_X______,
_X______,
_X______,
_X______,
XXX_____,
________,
________,
// 44 0x4A - 'J'
5,
________,
XXXX____,
X__X____,
___X____,
___X____,
___X____,
___X____,
X_XX____,
_X______,
________,
________,
// 45 0x4B - 'K'
6,
________,
X___X___,
X__X____,
X_X_____,
XXX_____,
X__X____,
X__X____,
X___X___,
X___X___,
________,
________,
// 46 0x4C - 'L'
6,
________,
X_______,
X_______,
X_______,
X_______,
X_______,
X_______,
X___X___,
XXXXX___,
________,
________,
// 47 0x4D - 'M'
8,
________,
X_____X_,
XX___XX_,
X_X_X_X_,
X_X_X_X_,
X__X__X_,
X_____X_,
X_____X_,
X_____X_,
________,
________,
// 48 0x4E - 'N'
6,
________,
X___X___,
XX__X___,
XX__X___,
X_X_X___,
X_X_X___,
X__XX___,
X__XX___,
X___X___,
________,
________,
// 49 0x50 - 'P'
6,
________,
XXXX____,
X__XX___,
X___X___,
X__XX___,
XXXX____,
X_______,
X_______,
X_______,
________,
________,
// 50 0x51 - 'Q'
7,
________,
__XX____,
_X__X___,
X____X__,
X____X__,
X____X__,
X____X__,
_X_XX___,
__XX____,
___XXX__,
________,
// 51 0x52 - 'R'
6,
________,
XXXX____,
X__XX___,
X___X___,
X__XX___,
XXXX____,
X_X_____,
X__X____,
X__XX___,
________,
________,
// 52 0x53 - 'S'
6,
________,
_XX_____,
X__X____,
X_______,
_XX_____,
__XX____,
____X___,
XX_XX___,
_XXX____,
________,
________,
// 53 0x54 - 'T'
6,
________,
XXXXX___,
__X_____,
__X_____,
__X_____,
__X_____,
__X_____,
__X_____,
__X_____,
________,
________,
// 54 0x55 - 'U'
7,
________,
X____X__,
X____X__,
X____X__,
X____X__,
X____X__,
X____X__,
_X__X___,
__XX____,
________,
________,
// 55 0x56 - 'V'
6,
________,
X___X___,
X___X___,
X___X___,
X___X___,
X___X___,
_X_X____,
_X_X____,
__X_____,
________,
________,
// 56 0x57 - 'W'
8,
________,
X_____X_,
X_____X_,
X_____X_,
X__X__X_,
X__X__X_,
_X_X_X__,
_X_X_X__,
__X_X___,
________,
________,
// 57 0x58 - 'X'
6,
________,
X___X___,
X___X___,
_X_X____,
_X_X____,
__X_____,
_X_X____,
X___X___,
X___X___,
________,
________,
// 58 0x59 - 'Y'
6,
________,
X___X___,
X___X___,
X___X___,
_X_X____,
_XXX____,
__X_____,
__X_____,
__X_____,
________,
________,
// 59 0x5A - 'Z'
6,
________,
XXXXX___,
____X___,
___X____,
__X_____,
__X_____,
_X______,
X_______,
XXXXX___,
________,
________,
};
#if 0
// 14 0x21 - '!'
0,
________,
________,
________,
________,
________,
________,
________,
________,
________,
________,
________,
#endif

View File

@ -75,7 +75,7 @@ static inline void tim_setup(){
GPIOA->CRL |= CRL(6, CNF_AFPP|MODE_FAST); GPIOA->CRL |= CRL(6, CNF_AFPP|MODE_FAST);
RCC->APB1ENR |= RCC_APB1ENR_TIM3EN; RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;
// PCS=1 don't work; // PCS=1 don't work;
TIM3->PSC = 9; // 7.2MHz TIM3->PSC = 4; // 7.2MHz
TIM3->ARR = 7; TIM3->ARR = 7;
TIM3->CCMR1 = TIM_CCMR1_OC1M; // PWM mode 2 (inactive->active) TIM3->CCMR1 = TIM_CCMR1_OC1M; // PWM mode 2 (inactive->active)
//TIM3->CCMR1 = TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1M_1; // PWM mode 1 (active->inactive) //TIM3->CCMR1 = TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1M_1; // PWM mode 1 (active->inactive)

View File

@ -33,17 +33,17 @@
#define midY (SCREEN_HH - 1) #define midY (SCREEN_HH - 1)
static const char* items[] = { static const char* items[] = {
[MENU_SLEEP] = " Sleep ", [MENU_SLEEP] = " SLEEP ",
[MENU_BALLS] = " Balls ", [MENU_BALLS] = " BALLS ",
[MENU_SNAKE] = " Snake ", [MENU_SNAKE] = " SNAKE ",
[MENU_TETRIS] = " Tetris " [MENU_TETRIS] = " TETRIS "
}; };
static menuitem curitem = MENU_SLEEP; static menuitem curitem = MENU_SLEEP;
// show menu // show menu
static void _menu(){ static void _menu(){
choose_font(FONT14); choose_font(FONTN8);
setBGcolor(COLOR_BLACK); setBGcolor(COLOR_BLACK);
ClearScreen(); ClearScreen();
int charH = curfont->height, charB = curfont->baseline; int charH = curfont->height, charB = curfont->baseline;
@ -93,9 +93,8 @@ void show_gameover(){
setBGcolor(COLOR_BLACK); setBGcolor(COLOR_BLACK);
setFGcolor(COLOR_RED); setFGcolor(COLOR_RED);
ClearScreen(); ClearScreen();
choose_font(FONT14); choose_font(FONTN8);
CenterStringAt(SCREEN_HEIGHT/2 - curfont->baseline - 1, "Game Over!"); CenterStringAt(SCREEN_HEIGHT/2 - curfont->baseline - 1, "GAME OVER!");
setFGcolor(COLOR_YELLOW); setFGcolor(COLOR_LYELLOW);
uint8_t l = PutStringAt(0, SCREEN_HEIGHT - curfont->baseline, "Score: "); CenterStringAt(SCREEN_HEIGHT - curfont->baseline - 2, u2str(score));
PutStringAt(l, SCREEN_HEIGHT - curfont->baseline, u2str(score));
} }

View File

@ -107,7 +107,6 @@ uint8_t DrawCharAt(int16_t X, int16_t Y, uint8_t Char){
* @return - text width in pixels * @return - text width in pixels
*/ */
uint8_t PutStringAt(int16_t X, int16_t Y, const char *str){ uint8_t PutStringAt(int16_t X, int16_t Y, const char *str){
DBG("PutStringAt("); DBGU(X); DBG(", "); DBGU(Y); DBG(", \""); DBG(str); DBG("\"\n");
if(!str) return 0; if(!str) return 0;
int16_t Xold = X; int16_t Xold = X;
while(*str){ while(*str){
@ -158,7 +157,7 @@ void process_screen(){
ConvertScreenBuf(screenbuf, currentB, Ntick); // convert data ConvertScreenBuf(screenbuf, currentB, Ntick); // convert data
TIM_DMA_transfer(currentB); // start transfer TIM_DMA_transfer(currentB); // start transfer
ScrnState = SCREEN_ACTIVE; ScrnState = SCREEN_ACTIVE;
if(++Ntick >= 7){ if(++Ntick >= 7 + NBLACK_FRAMES){
Ntick = 0; Ntick = 0;
if(++currentB >= NBLOCKS){ if(++currentB >= NBLOCKS){
currentB = 0; // start again currentB = 0; // start again

View File

@ -22,6 +22,9 @@
#include <stdint.h> #include <stdint.h>
// additional black frames for display won't be so bright
#define NBLACK_FRAMES (7)
// display size in px // display size in px
// PANEL_WIDTH is width of one panel // PANEL_WIDTH is width of one panel
//#define PANEL_WIDTH (64) //#define PANEL_WIDTH (64)
@ -53,8 +56,10 @@ typedef enum{ // screen states
#define COLOR_RED (0b11100000) #define COLOR_RED (0b11100000)
#define COLOR_LRED (0b01100000) #define COLOR_LRED (0b01100000)
#define COLOR_CYAN (0b00011111) #define COLOR_CYAN (0b00011111)
#define COLOR_LCYAN (0b00000101)
#define COLOR_PURPLE (0b11100011) #define COLOR_PURPLE (0b11100011)
#define COLOR_YELLOW (0b11111100) #define COLOR_YELLOW (0b11111100)
#define COLOR_LYELLOW (0b00100100)
extern uint32_t score; // current game score extern uint32_t score; // current game score
extern uint8_t screenbuf[SCREENBUF_SZ]; extern uint8_t screenbuf[SCREENBUF_SZ];

View File

@ -27,16 +27,16 @@
#define SNAKE_MAXLEN (128) #define SNAKE_MAXLEN (128)
// colors RRRGGGBB // colors RRRGGGBB
#define SNAKE_BGCOLOR (0) #define SNAKE_BGCOLOR (0)
#define SNAKE_HEADCOLOR (0b00001101) #define SNAKE_HEADCOLOR (0b00001100)
#define SNAKE_COLOR (0b00101100) #define SNAKE_COLOR (0b00000100)
// food: +1 to size // food: +1 to size
#define FOOD_COLOR (0b00011100) #define FOOD_COLOR (0b00001101)
// cut - -1 to size // cut - -1 to size
#define CUT_COLOR (0b11100000) #define CUT_COLOR (0b00011100)
// chance of CUT appears when drawing food (/1000) // chance of CUT appears when drawing food (/1000)
#define CUT_PROMILLE (80) #define CUT_PROMILLE (80)
// score - +10 to score // score - +10 to score
#define SCORE_COLOR (0b00000001) #define SCORE_COLOR (0b00100100)
// add this to score after each SCORE_COLOR eating // add this to score after each SCORE_COLOR eating
#define ADDSCORE (25) #define ADDSCORE (25)
// chance of SCORE appears when doing move (1/1000) // chance of SCORE appears when doing move (1/1000)

View File

@ -25,12 +25,12 @@
// backround color // backround color
#define BACKGROUND_COLOR (COLOR_BLACK) #define BACKGROUND_COLOR (COLOR_BLACK)
#define FOREGROUND_COLOR (COLOR_YELLOW) #define FOREGROUND_COLOR (COLOR_LYELLOW)
#define CUP_COLOR (COLOR_CYAN) #define CUP_COLOR (COLOR_LRED)
// height of cup // height of cup
#define CUPHEIGHT (30) #define CUPHEIGHT (31)
#define CUPWIDTH (20) #define CUPWIDTH (14)
// screen coordinate of x=0 @ cup // screen coordinate of x=0 @ cup
#define CUPX0 (7) #define CUPX0 (7)
// screen coordinate of y=0 @ cup (Y grows upside down) // screen coordinate of y=0 @ cup (Y grows upside down)
@ -67,7 +67,7 @@ static const figure L = {
// J: 00, 01, 02, -10 + 2 = 0x22, 0x23, 0x24, 0x12 // J: 00, 01, 02, -10 + 2 = 0x22, 0x23, 0x24, 0x12
static const figure J = { static const figure J = {
.f = {0x22, 0x23, 0x24, 0x12}, .f = {0x22, 0x23, 0x24, 0x12},
.color = 0b00000100 .color = 0b00000001
}; };
// O: 00, 01, 10, 11 + 2 = 0x22, 0x23, 0x32, 0x33 // O: 00, 01, 10, 11 + 2 = 0x22, 0x23, 0x32, 0x33
static const figure O = { static const figure O = {
@ -84,13 +84,13 @@ static const figure I = {
// S: -10, 00, 01, 11 + 2 = 0x12, 0x22, 0x23, 0x33 // S: -10, 00, 01, 11 + 2 = 0x12, 0x22, 0x23, 0x33
static const figure S = { static const figure S = {
.f = {0x12, 0x22, 0x23, 0x33}, .f = {0x12, 0x22, 0x23, 0x33},
.color = 0b00100001 .color = 0b00000100
}; };
// Z: -11, 01, 00, 10 + 2 = 0x13, 0x23, 0x22, 0x32 // Z: -11, 01, 00, 10 + 2 = 0x13, 0x23, 0x22, 0x32
static const figure Z = { static const figure Z = {
.f = {0x13, 0x23, 0x22, 0x32}, .f = {0x13, 0x23, 0x22, 0x32},
.color = 0b00100100 .color = 0b00000100
}; };
// T: -10, 01, 00, 10 + 2 = 0x12, 0x23, 0x22, 0x32 // T: -10, 01, 00, 10 + 2 = 0x12, 0x23, 0x22, 0x32
@ -210,7 +210,7 @@ static int getrand(){
return r; return r;
} }
// return 0 if cannot move // return 0 if cannot move by y
static int mvfig(int *x, int *y, int dx){ static int mvfig(int *x, int *y, int dx){
register int xx = *x, yy = *y; register int xx = *x, yy = *y;
DBG("MVFIG: x="); DBGU(*x); DBG(", y="); DBGU(*y); DBG(", dx="); DBGU(dx); NL(); DBG("MVFIG: x="); DBGU(*x); DBG(", y="); DBGU(*y); DBG(", dx="); DBGU(dx); NL();
@ -218,9 +218,14 @@ static int mvfig(int *x, int *y, int dx){
clearfigure(xx, yy); clearfigure(xx, yy);
// check dx: // check dx:
if(dx){ if(dx){
if(chkfigure(xx+dx, yy, &curfigure)){ int step = 1;
xx = xx + dx; *x = xx; if(dx < 0){step = -1; dx = -dx;}
}else ret = 0; while(dx){
if(chkfigure(xx + step, yy, &curfigure)){
xx = xx + step; *x = xx;
}else break;
--dx;
}
} }
if(chkfigure(xx, yy+1, &curfigure)){ if(chkfigure(xx, yy+1, &curfigure)){
++yy; *y = yy; ++yy; *y = yy;
@ -260,7 +265,7 @@ static int drawnext(){
void tetris_init(){ void tetris_init(){
setBGcolor(BACKGROUND_COLOR); setBGcolor(BACKGROUND_COLOR);
setFGcolor(FOREGROUND_COLOR); setFGcolor(FOREGROUND_COLOR);
choose_font(FONT14); choose_font(FONTN8);
ClearScreen(); ClearScreen();
ScreenON(); ScreenON();
score = 0; score = 0;
@ -274,7 +279,8 @@ void tetris_init(){
for(int x = 0; x < CUPWIDTH + 2; ++x) for(int x = 0; x < CUPWIDTH + 2; ++x)
DrawPix(CUPX0 - 1 + x, CUPY0 + CUPHEIGHT, CUP_COLOR); DrawPix(CUPX0 - 1 + x, CUPY0 + CUPHEIGHT, CUP_COLOR);
nextfigure = *figures[getrand()]; nextfigure = *figures[getrand()];
PutStringAt(CUPX0 + CUPWIDTH + 5, CUPY0 + 5 + curfont->height, "0 "); PutStringAt(CUPX0 + CUPWIDTH + 3, CUPY0 + 3 + curfont->height, "SCORE:");
PutStringAt(CUPX0 + CUPWIDTH + 3, CUPY0 + 3 + 2*curfont->height, "0 ");
drawnext(); drawnext();
} }
@ -284,7 +290,8 @@ int tetris_process(){
static uint8_t paused = 0; static uint8_t paused = 0;
keyevent evt; keyevent evt;
if(Tms == Tlast) return 1; if(Tms == Tlast) return 1;
#define EVENT(x) ((keystate(x, &evt) && evt == EVT_PRESS) || keyevt(x) == EVT_HOLD) #define PRESS(x) (keystate(x, &evt) && evt == EVT_PRESS)
#define HOLD(x) (keyevt(x) == EVT_HOLD)
// change moving direction // change moving direction
if(keystate(KEY_U, &evt) && (evt == EVT_PRESS || evt == EVT_HOLD)){ // UP - pause if(keystate(KEY_U, &evt) && (evt == EVT_PRESS || evt == EVT_HOLD)){ // UP - pause
if(paused){ if(paused){
@ -299,20 +306,23 @@ int tetris_process(){
if(keystate(KEY_D, &evt) && evt == EVT_PRESS){ // Down - drop if(keystate(KEY_D, &evt) && evt == EVT_PRESS){ // Down - drop
incSpd = MINSTEPMS; incSpd = MINSTEPMS;
} }
if(EVENT(KEY_L)){ // L - left if(PRESS(KEY_L)) // L - left
moveX = -1; moveX = -1;
} else if(HOLD(KEY_L))
if(EVENT(KEY_R)){ // Right moveX = -2;
if(PRESS(KEY_R)) // Right
moveX = 1; moveX = 1;
} else if(HOLD(KEY_R))
if(EVENT(KEY_M)){ // Menu - rotate CCW moveX = 2;
if(PRESS(KEY_M)){ // Menu - rotate CCW
rot = 1; rot = 1;
} }
if(EVENT(KEY_S)){ // Set - rotate CW if(PRESS(KEY_S)){ // Set - rotate CW
rot = -1; rot = -1;
} }
} }
#undef EVENT #undef PRESS
#undef HOLD
clear_events(); clear_events();
if(Tms - Tlast > incSpd){ if(Tms - Tlast > incSpd){
Tlast = Tms; Tlast = Tms;
@ -323,7 +333,7 @@ int tetris_process(){
rot = 0; rot = 0;
} }
DBG("Move down 1px\n"); DBG("Move down 1px\n");
if(!mvfig(&xnew, &ynew, moveX) && ynew == yf){ // can't move: end of moving? if(!mvfig(&xnew, &ynew, moveX)){ // can't move: end of moving?
moveX = 0; moveX = 0;
int s = checkandroll(); int s = checkandroll();
switch(s){ // add score switch(s){ // add score
@ -346,10 +356,18 @@ int tetris_process(){
default: default:
break; break;
} }
PutStringAt(CUPX0 + CUPWIDTH + 5, CUPY0 + 5 + curfont->height, u2str(score)); DBG("Score: "); DBG(u2str(score)); DBG("\n");
if(s){
setBGcolor(BACKGROUND_COLOR);
setFGcolor(FOREGROUND_COLOR);
choose_font(FONTN8);
uint8_t l = PutStringAt(CUPX0 + CUPWIDTH + 3, CUPY0 + 3 + 2*curfont->height, u2str(score));
PutStringAt(l+CUPX0 + CUPWIDTH + 3, CUPY0 + 3 + 2*curfont->height, " ");
}
if(StepMS > MINSTEPMS && score > nextSpeedScore){ // increase speed if(StepMS > MINSTEPMS && score > nextSpeedScore){ // increase speed
--StepMS; --StepMS;
nextSpeedScore += NXTSPEEDSCORE; nextSpeedScore += NXTSPEEDSCORE;
DBG("Increase speed, StepMS="); DBG(u2str(StepMS)); DBG("\n");
} }
if(!drawnext()) return 0; if(!drawnext()) return 0;
}else{ }else{