diff --git a/F1-nolib/Tetris/TETRIS.bin b/F1-nolib/Tetris/TETRIS.bin index f795364..5ba973d 100755 Binary files a/F1-nolib/Tetris/TETRIS.bin and b/F1-nolib/Tetris/TETRIS.bin differ diff --git a/F1-nolib/Tetris/balls.c b/F1-nolib/Tetris/balls.c index 5e4a796..bb8e535 100644 --- a/F1-nolib/Tetris/balls.c +++ b/F1-nolib/Tetris/balls.c @@ -41,7 +41,7 @@ static int xspeed[NBALLSMAX], yspeed[NBALLSMAX]; static uint8_t colr[NBALLSMAX]; static int Nballs = 0; -static uint32_t Tlast, Taddb; +static uint32_t TlastB, Taddb; static void clear_balls(){ for(int i = 0; i < Nballs; ++i) @@ -70,21 +70,21 @@ static void add_ball(){ void balls_init(){ Nballs = 0; - setBGcolor(0); + setBGcolor(COLOR_BLACK); ClearScreen(); ScreenON(); - Tlast = Tms; + TlastB = Tms; add_ball(); } //#define abs(x) ((x<0) ? -x : x) void process_balls(){ - if(Tms == Tlast) return; + if(Tms == TlastB) return; if(Nballs < NBALLSMAX && Taddb < Tms){ add_ball(); } - int diff = (int)(Tms - Tlast); + int diff = (int)(Tms - TlastB); for(int i = 0; i < Nballs; ++i){ xnew[i] = x[i] + xspeed[i]*diff; ynew[i] = y[i] + yspeed[i]*diff; @@ -104,5 +104,5 @@ void process_balls(){ } clear_balls(); draw_balls(); - Tlast = Tms; + TlastB = Tms; } diff --git a/F1-nolib/Tetris/buttons.c b/F1-nolib/Tetris/buttons.c index bf71536..20e267b 100644 --- a/F1-nolib/Tetris/buttons.c +++ b/F1-nolib/Tetris/buttons.c @@ -22,6 +22,7 @@ #include "hardware.h" extern volatile uint32_t Tms; +uint32_t lastUnsleep = 0; // threshold in ms for press/hold #define PRESSTHRESHOLD (9) @@ -54,6 +55,7 @@ void process_keys(){ keybase *k = &allkeys[i]; keyevent e = k->event; if(PRESSED(k->port, k->pin)){ // key is in pressed state + lastUnsleep = Tms; // update activity time (any key is in pressed state) switch(e){ case EVT_NONE: // just pressed case EVT_RELEASE: diff --git a/F1-nolib/Tetris/buttons.h b/F1-nolib/Tetris/buttons.h index f5eba8b..1d3d99a 100644 --- a/F1-nolib/Tetris/buttons.h +++ b/F1-nolib/Tetris/buttons.h @@ -43,6 +43,8 @@ typedef enum{ EVT_RELEASE // released } keyevent; +extern uint32_t lastUnsleep; // last keys activity time + void process_keys(); void clear_events(); uint8_t keystate(keycode k, keyevent *evt); diff --git a/F1-nolib/Tetris/main.c b/F1-nolib/Tetris/main.c index e6edc85..1a9a559 100644 --- a/F1-nolib/Tetris/main.c +++ b/F1-nolib/Tetris/main.c @@ -25,9 +25,13 @@ #include "proto.h" #include "screen.h" #include "snake.h" +#include "tetris.h" #include "usb.h" #include "usb_lib.h" +// timeout for autosleep (30s) +#define AUTOSLEEP_TMOUT (30000) + volatile uint32_t Tms = 0; uint8_t balls = 0; @@ -35,7 +39,8 @@ enum{ STATE_MENU, STATE_SNAKE, STATE_TETRIS, - STATE_SLEEP + STATE_SLEEP, + STATE_GAMEOVER } curstate = STATE_SLEEP; /* Called when systick fires */ @@ -89,6 +94,8 @@ static void process_menu(){ break; case MENU_TETRIS: USB_send("Select 'Tetris'\n"); + tetris_init(); + curstate = STATE_TETRIS; break; default: break; @@ -112,7 +119,7 @@ int main(void){ USBPU_OFF(); adc_setup(); USB_setup(); - iwdg_setup(); + //iwdg_setup(); USBPU_ON(); keyevent evt; @@ -133,12 +140,32 @@ int main(void){ break; case STATE_MENU: process_menu(); + if(Tms - lastUnsleep > AUTOSLEEP_TMOUT){ + USB_send("Autosleep\n"); + ScreenOFF(); + curstate = STATE_SLEEP; + } break; case STATE_SNAKE: - if(!proces_snake()) gotomenu(); + if(!snake_proces()){ + show_gameover(); + curstate = STATE_GAMEOVER; + } break; case STATE_TETRIS: - ; + if(!tetris_process()){ + show_gameover(); + curstate = STATE_GAMEOVER; + } + break; + case STATE_GAMEOVER: // show gameover screen + if(keystate(KEY_M, &evt) && evt == EVT_RELEASE){ + gotomenu(); + }else if(Tms - lastUnsleep > AUTOSLEEP_TMOUT){ + USB_send("Autosleep\n"); + ScreenOFF(); + curstate = STATE_SLEEP; + } break; } diff --git a/F1-nolib/Tetris/menu.c b/F1-nolib/Tetris/menu.c index 0bc7069..ef83ab6 100644 --- a/F1-nolib/Tetris/menu.c +++ b/F1-nolib/Tetris/menu.c @@ -39,13 +39,12 @@ static const char* items[] = { [MENU_TETRIS] = " Tetris " }; - static menuitem curitem = MENU_SLEEP; // show menu static void _menu(){ choose_font(FONT14); - setBGcolor(0); + setBGcolor(COLOR_BLACK); ClearScreen(); int charH = curfont->height, charB = curfont->baseline; int curI = midNo; @@ -60,8 +59,7 @@ static void _menu(){ setBGcolor(MENUBGCOLOR); setFGcolor(MENUFGCOLOR); } - PutStringAt((SCREEN_WIDTH - strpixlen(items[i]))/2, - midY + (i-curI)*charH - charB, items[i]); + CenterStringAt(midY + (i-curI)*charH - charB, items[i]); } } @@ -89,3 +87,15 @@ menuitem menu_activated(){ clear_events(); // clear all other events return MENU_NONE; } + +// show "GAME OVER" screen +void show_gameover(){ + setBGcolor(COLOR_BLACK); + setFGcolor(COLOR_RED); + ClearScreen(); + choose_font(FONT14); + CenterStringAt(SCREEN_HEIGHT/2 - curfont->baseline - 1, "Game Over!"); + setFGcolor(COLOR_YELLOW); + uint8_t l = PutStringAt(0, SCREEN_HEIGHT - curfont->baseline, "Score: "); + PutStringAt(l, SCREEN_HEIGHT - curfont->baseline, u2str(score)); +} diff --git a/F1-nolib/Tetris/menu.h b/F1-nolib/Tetris/menu.h index 1273845..8ac68a9 100644 --- a/F1-nolib/Tetris/menu.h +++ b/F1-nolib/Tetris/menu.h @@ -19,6 +19,8 @@ #ifndef MENU_H__ #define MENU_H__ +#include + typedef enum{ MENU_SLEEP, MENU_BALLS, @@ -29,6 +31,7 @@ typedef enum{ } menuitem; void show_menu(); +void show_gameover(); menuitem menu_activated(); diff --git a/F1-nolib/Tetris/screen.c b/F1-nolib/Tetris/screen.c index abec1be..10481f2 100644 --- a/F1-nolib/Tetris/screen.c +++ b/F1-nolib/Tetris/screen.c @@ -23,15 +23,17 @@ #include "screen.h" //#include "usb.h" - // !!!FOR LITTLE-ENDIAN!!! +uint32_t score = 0; // current game's score +uint32_t StepMS, Tlast, incSpd = 0; // common for all games timer values + // X coordinate - from left to right! // Y coordinate - from top to bottom! // (0,0) is top left corner // all-screen buffer -static uint8_t screenbuf[SCREENBUF_SZ]; +uint8_t screenbuf[SCREENBUF_SZ]; extern volatile uint32_t Tms; // time for FPS count static uint32_t FPS = 0, Tfps = 0; // approx FPS uint32_t getFPS(){return FPS;} @@ -112,6 +114,17 @@ uint8_t PutStringAt(int16_t X, int16_t Y, const char *str){ return X - Xold; } +/** + * @brief CenterStringAt - draw string @ center line + * @param Y - y coordinate + * @param str - string + * @return - text width in pixels + */ +uint8_t CenterStringAt(int16_t Y, const char *str){ + int l = strpixlen(str); + return PutStringAt((SCREEN_WIDTH - l)/2, Y, str); +} + // full string length in pixels int strpixlen(const char *str){ int l = 0; @@ -122,8 +135,6 @@ int strpixlen(const char *str){ return l; } -uint8_t *getScreenBuf(){return screenbuf;} - static screen_state ScrnState = SCREEN_RELAX; screen_state getScreenState(){return ScrnState;} diff --git a/F1-nolib/Tetris/screen.h b/F1-nolib/Tetris/screen.h index 7d4c4cd..6ded8e8 100644 --- a/F1-nolib/Tetris/screen.h +++ b/F1-nolib/Tetris/screen.h @@ -43,6 +43,23 @@ typedef enum{ // screen states SCREEN_UPDATENXT // update next quarter } screen_state; +// colors +#define COLOR_BLACK (0b00000000) +#define COLOR_WHITE (0b11111111) +#define COLOR_BLUE (0b00000011) +#define COLOR_LBLUE (0b00000001) +#define COLOR_GREEN (0b00011100) +#define COLOR_LGREEN (0b00001100) +#define COLOR_RED (0b11100000) +#define COLOR_LRED (0b01100000) +#define COLOR_CYAN (0b00011111) +#define COLOR_PURPLE (0b11100011) +#define COLOR_YELLOW (0b11111100) + +extern uint32_t score; // current game score +extern uint8_t screenbuf[SCREENBUF_SZ]; +extern uint32_t StepMS, Tlast, incSpd; // common for all games timer values + screen_state getScreenState(); void ClearScreen(); void setBGcolor(uint8_t c); @@ -53,6 +70,7 @@ void XORPix(int16_t X, int16_t Y, uint8_t pix); uint8_t GetPix(int16_t X, int16_t Y); uint8_t DrawCharAt(int16_t X, int16_t Y, uint8_t Char); uint8_t PutStringAt(int16_t X, int16_t Y, const char *str); +uint8_t CenterStringAt(int16_t Y, const char *str); int strpixlen(const char *str); uint8_t *getScreenBuf(); void process_screen(); diff --git a/F1-nolib/Tetris/snake.c b/F1-nolib/Tetris/snake.c index a36514e..b98995b 100644 --- a/F1-nolib/Tetris/snake.c +++ b/F1-nolib/Tetris/snake.c @@ -29,7 +29,19 @@ #define SNAKE_BGCOLOR (0) #define SNAKE_HEADCOLOR (0b00001101) #define SNAKE_COLOR (0b00101100) +// food: +1 to size #define FOOD_COLOR (0b00011100) +// cut - -1 to size +#define CUT_COLOR (0b11100000) +// chance of CUT appears when drawing food (/1000) +#define CUT_PROMILLE (33) +// score - +10 to score +#define SCORE_COLOR (0b00000001) +// add this to score after each SCORE_COLOR eating +#define ADDSCORE (25) +// chance of SCORE appears when doing move (1/1000) +#define SCORE_PROMILLE (15) +// border #define BORDER_COLOR (0b00100000) // max and min pause between steps #define MAXSTEPMS (199) @@ -46,24 +58,28 @@ static point snake[SNAKE_MAXLEN]; static int8_t dirX, dirY; static uint8_t snakelen; static point Food; -static uint32_t StepMS, Tlast, incSpd = 0; -static uint32_t score; -static void ThrowFood(){ - int notfound = 1; - do{ - Food.x = 1 + getRand() % (SCREEN_WIDTH-2); - Food.y = 1 + getRand() % (SCREEN_HEIGHT-2); - for(int i = 0; i < snakelen; ++i){ - if(Food.x != snake[i].x && Food.y != snake[i].y){ - notfound = 0; - break; - } - } - }while(notfound); - USB_send("Food @ "); USB_send(u2str(Food.x)); - USB_send(", "); USB_send(u2str(Food.y)); USB_send("\n"); +static point RandCoords(){ + point pt = {0,0}; + for(int i = 0; i < 10000; ++i){ // not more than 10000 tries + pt.x = 1 + getRand() % (SCREEN_WIDTH-2); + pt.y = 1 + getRand() % (SCREEN_HEIGHT-2); + if(GetPix(pt.x, pt.y) == SNAKE_BGCOLOR) break; + }; + return pt; +} + +// return 0 if failed +static int ThrowFood(){ + Food = RandCoords(); + if(Food.x == 0 && Food.y == 0) return 0; DrawPix(Food.x, Food.y, FOOD_COLOR); + if(getRand() % 1000 < CUT_PROMILLE){ + point p = RandCoords(); + if(p.x == 0 && p.y == 0) return 1; + DrawPix(p.x, p.y, CUT_COLOR); + } + return 1; } /* static void draw_snake(){ @@ -101,7 +117,6 @@ void snake_init(){ dirX = 1; dirY = 0; break; case 1: - dirX = -1; dirY = 0; break; case 2: @@ -115,12 +130,14 @@ void snake_init(){ Tlast = Tms; incSpd = 0; score = 0; DrawPix(snake[0].x, snake[0].y, SNAKE_HEADCOLOR); + USB_send("Snake inited\n"); } // return 0 if failed static int move_snake(){ int last = snakelen - 1; point tail = {snake[last].x, snake[last].y}; + DrawPix(Food.x, Food.y, FOOD_COLOR); // redraw food DrawPix(snake[0].x, snake[0].y, SNAKE_COLOR); // redraw head with body color DrawPix(tail.x, tail.y, SNAKE_BGCOLOR); // delete tail for(int i = snakelen-1; i > 0; --i){ @@ -135,6 +152,11 @@ static int move_snake(){ if(nxtcolr == SNAKE_COLOR) USB_send("slum into yourself\n"); else USB_send("crush into border\n"); return 0; + }else if(nxtcolr == CUT_COLOR && snakelen > 1){ // make snake shorter + --snakelen; + DrawPix(snake[snakelen].x, snake[snakelen].y, SNAKE_BGCOLOR); + }else if(nxtcolr == SCORE_COLOR){ // add score + score += ADDSCORE; } // not border or snake -> FOOD?! if(snake[0].x == Food.x && snake[0].y == Food.y){ // increase snake len & speed & score @@ -145,7 +167,7 @@ static int move_snake(){ ++snakelen; DrawPix(tail.x, tail.y, SNAKE_COLOR); } - ThrowFood(); + if(!ThrowFood()) return 0; USB_send("Got food, score="); USB_send(u2str(score)); USB_send(", len="); USB_send(u2str(snakelen)); USB_send(", speed="); USB_send(u2str(StepMS)); @@ -163,7 +185,7 @@ static int move_snake(){ #define DECRSPD() do{incSpd /= 2;}while(0) // return 0 if game over -int proces_snake(){ +int snake_proces(){ keyevent evt; static uint8_t paused = 0; // change moving direction @@ -173,13 +195,13 @@ int proces_snake(){ if(keystate(KEY_D, &evt)){ // Down if(evt == EVT_PRESS){ if(dirY != -1) SETDIR(0, 1);} } - if(keystate(KEY_L, &evt)){ // Down + if(keystate(KEY_L, &evt)){ // Left if(evt == EVT_PRESS){ if(dirX != 1) SETDIR(-1, 0);} } - if(keystate(KEY_R, &evt) && (evt == EVT_PRESS || evt == EVT_HOLD)){ // Down + if(keystate(KEY_R, &evt) && (evt == EVT_PRESS || evt == EVT_HOLD)){ // Right if(evt == EVT_PRESS){ if(dirX != -1) SETDIR(1, 0);} } - if(keystate(KEY_M, &evt) && (evt == EVT_PRESS || evt == EVT_HOLD)){ // Pause or out + if(keystate(KEY_M, &evt) && (evt == EVT_PRESS || evt == EVT_HOLD)){ // Menu - Pause or out if(3 == ++paused){ USB_send("Exit snake\n"); clear_events(); @@ -187,7 +209,7 @@ int proces_snake(){ return 0; } } - if(keystate(KEY_S, &evt) && (evt == EVT_PRESS || evt == EVT_HOLD)){ // Pause + if(keystate(KEY_S, &evt) && (evt == EVT_PRESS || evt == EVT_HOLD)){ // Set - Pause if(paused){ USB_send("Snake resume\n"); paused = 0; @@ -226,4 +248,3 @@ int proces_snake(){ return 1; } -uint32_t snake_getscore(){return score;} diff --git a/F1-nolib/Tetris/snake.h b/F1-nolib/Tetris/snake.h index 6b9e3a3..603ab2c 100644 --- a/F1-nolib/Tetris/snake.h +++ b/F1-nolib/Tetris/snake.h @@ -19,10 +19,7 @@ #ifndef SNAKE_H__ #define SNAKE_H__ -#include - void snake_init(); -int proces_snake(); -uint32_t snake_getscore(); +int snake_proces(); #endif // SNAKE_H__ diff --git a/F1-nolib/Tetris/tetris.c b/F1-nolib/Tetris/tetris.c new file mode 100644 index 0000000..951c876 --- /dev/null +++ b/F1-nolib/Tetris/tetris.c @@ -0,0 +1,314 @@ +/* + * This file is part of the TETRIS project. + * Copyright 2021 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "adcrandom.h" +#include "buttons.h" +#include "screen.h" +#include "tetris.h" + +#include "usb.h" +#include "proto.h" + +// backround color +#define BACKGROUND_COLOR (COLOR_BLACK) +#define CUP_COLOR (COLOR_LRED) + +// height of cup +#define CUPHEIGHT (30) +#define CUPWIDTH (20) +// screen coordinate of x=0 @ cup +#define CUPX0 (1) +// screen coordinate of y=0 @ cup (Y grows upside down) +#define CUPY0 (0) +// coordinates of "NEXT figure" +#define NEXTX0 (30) +#define NEXTY0 (4) +#define xmax (CUPWIDTH-1) +#define ymax (CUPHEIGHT-1) + +// min speed +#define MAXSTEPMS (199) +// max speed / drop speed +#define MINSTEPMS (19) +// each NXTSPEEDSCORE of score the speed will be increased +#define NXTSPEEDSCORE (50) + +extern uint32_t Tms; + +// get coordinates by figure member +#define GETX(u) (((u) >> 4) - 2) +#define GETY(u) (((u) & 0xf) - 2) + +typedef struct{ + uint8_t f[4]; + uint8_t color; +} figure; + +// L: 00, 01, 02, 10 + 2 = 0x22, 0x23, 0x24, 0x32 +static const figure L = { + .f = {0x22, 0x23, 0x24, 0x32}, + .color = COLOR_LBLUE +}; +// J: 00, 01, 02, -10 + 2 = 0x22, 0x23, 0x24, 0x12 +static const figure J = { + .f = {0x22, 0x23, 0x24, 0x12}, + .color = COLOR_BLUE +}; +// O: 00, 01, 10, 11 + 2 = 0x22, 0x23, 0x32, 0x33 +static const figure O = { + .f = {0x22, 0x23, 0x32, 0x33}, + .color = COLOR_YELLOW +}; + +// I: 0-1, 00, 01, 02 + 2 = 0x21, 0x22, 0x23, 0x24 +static const figure I = { + .f = {0x21, 0x22, 0x23, 0x24}, + .color = COLOR_CYAN +}; + +// S: -10, 00, 01, 11 + 2 = 0x12, 0x22, 0x23, 0x33 +static const figure S = { + .f = {0x12, 0x22, 0x23, 0x33}, + .color = COLOR_LGREEN +}; + +// Z: -11, 01, 00, 10 + 2 = 0x13, 0x23, 0x23, 0x32 +static const figure Z = { + .f = {0x13, 0x23, 0x23, 0x32}, + .color = COLOR_GREEN +}; + +// T: -10, 01, 00, 10 + 2 = 0x12, 0x23, 0x22, 0x32 +static const figure T = { + .f = {0x12, 0x23, 0x22, 0x32}, + .color = COLOR_PURPLE +}; + +#define FIGURESNUM (7) +static const figure *figures[FIGURESNUM] = {&L, &J, &O, &I, &S, &Z, &T}; +static figure curfigure, nextfigure; + +// current figure coordinates +static int xf, yf; +// next score the speed will be encreased +static uint32_t nextSpeedScore = 0; + + +// chk if figure can be put to (x,y); @return 1 if empty +static int chkfigure(int x, int y, const figure *F){ + if(x < 0 || x > xmax) return 0; + if(y < 0 || y > ymax) return 0; + for(int i = 0; i < 4; ++i){ + int xn = x + GETX(F->f[i]), yn = y + GETY(F->f[i]); + if(yn > ymax || yn < 0) return 0; + if(xn < 0 || xn > xmax || yn < 0) return 0; // border + if(GetPix(xn ,yn) != BACKGROUND_COLOR) return 0; // occupied + } + return 1; +} + +// clear figure from old location +static void clearfigure(int x, int y){ + USB_send("Clear @ "); USB_send(u2str(x)); USB_send(", "); USB_send(u2str(y)); USB_send("\n"); + for(int i = 0; i < 4; ++i){ + int xn = x + GETX(curfigure.f[i]), yn = y + GETY(curfigure.f[i]); + if(xn < 0 || xn > xmax || yn < 0 || yn > ymax) continue; // out of cup + DrawPix(xn, yn, BACKGROUND_COLOR); + } +} + +// put figure into new location +static void putfigure(int x, int y, figure *F){ + USB_send("Put @ "); USB_send(u2str(x)); USB_send(", "); USB_send(u2str(y)); USB_send("\n"); + for(int i = 0; i < 4; ++i){ + int xn = x + GETX(F->f[i]), yn = y + GETY(F->f[i]); + if(xn < 0 || xn > xmax || yn < 0 || yn > ymax) continue; // out of cup + DrawPix(xn, yn, F->color); + } +} + +// dir=-1 - right, =1 - left +static void rotatefigure(int dir, figure *F){ + figure nF = {.color = F->color}; + for(int i = 0; i < 4; ++i){ + int x = GETX(F->f[i]), y = GETY(F->f[i]), xn, yn; + if(dir > 0){ // CW + xn = y; yn = -x; + USB_send("Rotate CW\n"); + }else if(dir < 0){ // CCW + xn = -y; yn = x; + USB_send("Rotate CCW\n"); + } + nF.f[i] = ((xn + 2) << 4) | (yn + 2); + } + *F = nF; +} + +// check cup for full lines & delete them; return number of deleted lines +static int checkandroll(){ + int upper = 0, ret = 0; + for(int y = 0; y < CUPHEIGHT; ++y){ + int N = 0; + for(int x = 0; x < CUPWIDTH; ++x) + if(GetPix(x, y) != BACKGROUND_COLOR) ++N; + if(N == 0) upper = y; + else if(N == CUPWIDTH){ // full line - roll all upper + ++ret; + for(int yy = y; yy <= upper; --yy){ + uint8_t *ptr = &screenbuf[(yy+CUPY0)*SCREEN_WIDTH + CUPX0]; + for(int x = 0; x < CUPWIDTH; ++x, ++ptr) + *ptr = ptr[-CUPWIDTH]; // copy upper row into the current + } + ++upper; + } + } + USB_send("Roll="); USB_send(u2str(ret)); USB_send("\n"); + return ret; +} + +// get random number of next figure +static int getrand(){ + static int oldnum[2] = {-1, -1}; + int r = getRand() % FIGURESNUM; + if(r == oldnum[1]) r = getRand() % FIGURESNUM; + if(r == oldnum[0]) r = getRand() % FIGURESNUM; + oldnum[0] = oldnum[1]; + oldnum[1] = r; + return r; +} + +// return 0 if cannot move +static int mvfig(int *x, int *y, int dx){ + register int xx = *x, yy = *y; + int xnew = xx+dx, ynew = yy+1, ret = 1; + clearfigure(xx, yy); + if(chkfigure(xnew, ynew, &curfigure)){ + xx = xnew; yy = ynew; + *x = xx; *y = yy; + }else ret = 0; + putfigure(xx, yy, &curfigure); + return ret; +} + +// dir == -1 - left, 1 - right +static void rotfig(int x, int y, int dir){ + if(!dir) return; + figure newF = curfigure; + rotatefigure(dir, &newF); + clearfigure(x, y); + if(chkfigure(x, y, &newF)){ // can rotate - substitute old figure with new + curfigure = newF; + } + putfigure(x, y, &curfigure); +} + +// draw current & next figures; return 0 if can't +static int drawnext(){ + curfigure = nextfigure; + clearfigure(NEXTX0, NEXTY0); // clear NEXT + nextfigure = *figures[getrand()]; + putfigure(NEXTX0, NEXTY0, &nextfigure); + xf = xmax/2; yf = ymax/2; + if(!chkfigure(xf, yf, &curfigure)) return 0; // can't draw next + putfigure(xf, yf, &curfigure); + return 1; +} + +void tetris_init(){ + setBGcolor(BACKGROUND_COLOR); + ClearScreen(); + ScreenON(); + StepMS = MAXSTEPMS; + Tlast = Tms; + incSpd = StepMS; + score = 0; + nextSpeedScore = NXTSPEEDSCORE; + // draw cup + for(int y = 0; y < CUPHEIGHT; ++y){ + DrawPix(CUPX0, CUPY0 + y, CUP_COLOR); + DrawPix(CUPX0 + CUPWIDTH, CUPY0 + y, CUP_COLOR); + } + for(int x = 0; x < CUPWIDTH; ++x) + DrawPix(CUPX0 + x, CUPY0 + CUPHEIGHT, CUP_COLOR); + nextfigure = *figures[getrand()]; + drawnext(); +} + +// process tetris game; @return 0 if game is over +int tetris_process(){ + uint8_t moveX = 0, rot = 0; + keyevent evt; + static uint8_t paused = 0; + // change moving direction + if(keystate(KEY_U, &evt) && (evt == EVT_PRESS || evt == EVT_HOLD)){ // UP - pause + if(paused){ + USB_send("Tetris resume\n"); + paused = 0; + }else{ + USB_send("Tetris paused\n"); + paused = 1; + } + } + if(keystate(KEY_D, &evt) && evt == EVT_PRESS){ // Down - drop + incSpd = MINSTEPMS; + } + if(keystate(KEY_L, &evt) && evt == EVT_PRESS){ // L - left + moveX = -1; + } + if(keystate(KEY_R, &evt) && evt == EVT_PRESS){ // Right + moveX = 1; + } + if(keystate(KEY_M, &evt) && evt == EVT_PRESS){ // Menu - rotate CCW + rot = -1; + } + if(keystate(KEY_S, &evt) && evt == EVT_PRESS){ // Set - rotate CW + rot = 1; + } + clear_events(); + if(Tms - Tlast > incSpd){ + Tlast = Tms; + if(paused) return 1; + int xnew = xf, ynew = yf; + if(rot) rotfig(xf, yf, rot); + if(!mvfig(&xnew, &ynew, moveX) && ynew == yf){ // can't move: end of moving? + int s = checkandroll(); + switch(s){ // add score + case 1: + score += 1; + break; + case 2: + score += 3; + break; + case 3: + score += 7; + break; + case 4: + score += 15; + break; + default: + break; + } + if(StepMS > MINSTEPMS && score > nextSpeedScore){ // increase speed + --StepMS; + nextSpeedScore += NXTSPEEDSCORE; + } + if(!drawnext()) return 0; + } + } + return 1; +} diff --git a/F1-nolib/Tetris/tetris.h b/F1-nolib/Tetris/tetris.h new file mode 100644 index 0000000..ca04e1b --- /dev/null +++ b/F1-nolib/Tetris/tetris.h @@ -0,0 +1,25 @@ +/* + * This file is part of the TETRIS project. + * Copyright 2021 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once +#ifndef TETRIS_H__ +#define TETRIS_H__ + +void tetris_init(); +int tetris_process(); + +#endif // TETRIS_H__