mirror of
https://github.com/eddyem/stm32samples.git
synced 2025-12-09 12:15:21 +03:00
add pre-pre-alpha of tetris
This commit is contained in:
parent
7b02c43bf7
commit
097895127d
Binary file not shown.
@ -41,7 +41,7 @@ static int
|
|||||||
xspeed[NBALLSMAX], yspeed[NBALLSMAX];
|
xspeed[NBALLSMAX], yspeed[NBALLSMAX];
|
||||||
static uint8_t colr[NBALLSMAX];
|
static uint8_t colr[NBALLSMAX];
|
||||||
static int Nballs = 0;
|
static int Nballs = 0;
|
||||||
static uint32_t Tlast, Taddb;
|
static uint32_t TlastB, Taddb;
|
||||||
|
|
||||||
static void clear_balls(){
|
static void clear_balls(){
|
||||||
for(int i = 0; i < Nballs; ++i)
|
for(int i = 0; i < Nballs; ++i)
|
||||||
@ -70,21 +70,21 @@ static void add_ball(){
|
|||||||
|
|
||||||
void balls_init(){
|
void balls_init(){
|
||||||
Nballs = 0;
|
Nballs = 0;
|
||||||
setBGcolor(0);
|
setBGcolor(COLOR_BLACK);
|
||||||
ClearScreen();
|
ClearScreen();
|
||||||
ScreenON();
|
ScreenON();
|
||||||
Tlast = Tms;
|
TlastB = Tms;
|
||||||
add_ball();
|
add_ball();
|
||||||
}
|
}
|
||||||
|
|
||||||
//#define abs(x) ((x<0) ? -x : x)
|
//#define abs(x) ((x<0) ? -x : x)
|
||||||
|
|
||||||
void process_balls(){
|
void process_balls(){
|
||||||
if(Tms == Tlast) return;
|
if(Tms == TlastB) return;
|
||||||
if(Nballs < NBALLSMAX && Taddb < Tms){
|
if(Nballs < NBALLSMAX && Taddb < Tms){
|
||||||
add_ball();
|
add_ball();
|
||||||
}
|
}
|
||||||
int diff = (int)(Tms - Tlast);
|
int diff = (int)(Tms - TlastB);
|
||||||
for(int i = 0; i < Nballs; ++i){
|
for(int i = 0; i < Nballs; ++i){
|
||||||
xnew[i] = x[i] + xspeed[i]*diff;
|
xnew[i] = x[i] + xspeed[i]*diff;
|
||||||
ynew[i] = y[i] + yspeed[i]*diff;
|
ynew[i] = y[i] + yspeed[i]*diff;
|
||||||
@ -104,5 +104,5 @@ void process_balls(){
|
|||||||
}
|
}
|
||||||
clear_balls();
|
clear_balls();
|
||||||
draw_balls();
|
draw_balls();
|
||||||
Tlast = Tms;
|
TlastB = Tms;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,6 +22,7 @@
|
|||||||
#include "hardware.h"
|
#include "hardware.h"
|
||||||
|
|
||||||
extern volatile uint32_t Tms;
|
extern volatile uint32_t Tms;
|
||||||
|
uint32_t lastUnsleep = 0;
|
||||||
|
|
||||||
// threshold in ms for press/hold
|
// threshold in ms for press/hold
|
||||||
#define PRESSTHRESHOLD (9)
|
#define PRESSTHRESHOLD (9)
|
||||||
@ -54,6 +55,7 @@ void process_keys(){
|
|||||||
keybase *k = &allkeys[i];
|
keybase *k = &allkeys[i];
|
||||||
keyevent e = k->event;
|
keyevent e = k->event;
|
||||||
if(PRESSED(k->port, k->pin)){ // key is in pressed state
|
if(PRESSED(k->port, k->pin)){ // key is in pressed state
|
||||||
|
lastUnsleep = Tms; // update activity time (any key is in pressed state)
|
||||||
switch(e){
|
switch(e){
|
||||||
case EVT_NONE: // just pressed
|
case EVT_NONE: // just pressed
|
||||||
case EVT_RELEASE:
|
case EVT_RELEASE:
|
||||||
|
|||||||
@ -43,6 +43,8 @@ typedef enum{
|
|||||||
EVT_RELEASE // released
|
EVT_RELEASE // released
|
||||||
} keyevent;
|
} keyevent;
|
||||||
|
|
||||||
|
extern uint32_t lastUnsleep; // last keys activity time
|
||||||
|
|
||||||
void process_keys();
|
void process_keys();
|
||||||
void clear_events();
|
void clear_events();
|
||||||
uint8_t keystate(keycode k, keyevent *evt);
|
uint8_t keystate(keycode k, keyevent *evt);
|
||||||
|
|||||||
@ -25,9 +25,13 @@
|
|||||||
#include "proto.h"
|
#include "proto.h"
|
||||||
#include "screen.h"
|
#include "screen.h"
|
||||||
#include "snake.h"
|
#include "snake.h"
|
||||||
|
#include "tetris.h"
|
||||||
#include "usb.h"
|
#include "usb.h"
|
||||||
#include "usb_lib.h"
|
#include "usb_lib.h"
|
||||||
|
|
||||||
|
// timeout for autosleep (30s)
|
||||||
|
#define AUTOSLEEP_TMOUT (30000)
|
||||||
|
|
||||||
volatile uint32_t Tms = 0;
|
volatile uint32_t Tms = 0;
|
||||||
uint8_t balls = 0;
|
uint8_t balls = 0;
|
||||||
|
|
||||||
@ -35,7 +39,8 @@ enum{
|
|||||||
STATE_MENU,
|
STATE_MENU,
|
||||||
STATE_SNAKE,
|
STATE_SNAKE,
|
||||||
STATE_TETRIS,
|
STATE_TETRIS,
|
||||||
STATE_SLEEP
|
STATE_SLEEP,
|
||||||
|
STATE_GAMEOVER
|
||||||
} curstate = STATE_SLEEP;
|
} curstate = STATE_SLEEP;
|
||||||
|
|
||||||
/* Called when systick fires */
|
/* Called when systick fires */
|
||||||
@ -89,6 +94,8 @@ static void process_menu(){
|
|||||||
break;
|
break;
|
||||||
case MENU_TETRIS:
|
case MENU_TETRIS:
|
||||||
USB_send("Select 'Tetris'\n");
|
USB_send("Select 'Tetris'\n");
|
||||||
|
tetris_init();
|
||||||
|
curstate = STATE_TETRIS;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
@ -112,7 +119,7 @@ int main(void){
|
|||||||
USBPU_OFF();
|
USBPU_OFF();
|
||||||
adc_setup();
|
adc_setup();
|
||||||
USB_setup();
|
USB_setup();
|
||||||
iwdg_setup();
|
//iwdg_setup();
|
||||||
USBPU_ON();
|
USBPU_ON();
|
||||||
|
|
||||||
keyevent evt;
|
keyevent evt;
|
||||||
@ -133,12 +140,32 @@ int main(void){
|
|||||||
break;
|
break;
|
||||||
case STATE_MENU:
|
case STATE_MENU:
|
||||||
process_menu();
|
process_menu();
|
||||||
|
if(Tms - lastUnsleep > AUTOSLEEP_TMOUT){
|
||||||
|
USB_send("Autosleep\n");
|
||||||
|
ScreenOFF();
|
||||||
|
curstate = STATE_SLEEP;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case STATE_SNAKE:
|
case STATE_SNAKE:
|
||||||
if(!proces_snake()) gotomenu();
|
if(!snake_proces()){
|
||||||
|
show_gameover();
|
||||||
|
curstate = STATE_GAMEOVER;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case STATE_TETRIS:
|
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;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -39,13 +39,12 @@ static const char* items[] = {
|
|||||||
[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(FONT14);
|
||||||
setBGcolor(0);
|
setBGcolor(COLOR_BLACK);
|
||||||
ClearScreen();
|
ClearScreen();
|
||||||
int charH = curfont->height, charB = curfont->baseline;
|
int charH = curfont->height, charB = curfont->baseline;
|
||||||
int curI = midNo;
|
int curI = midNo;
|
||||||
@ -60,8 +59,7 @@ static void _menu(){
|
|||||||
setBGcolor(MENUBGCOLOR);
|
setBGcolor(MENUBGCOLOR);
|
||||||
setFGcolor(MENUFGCOLOR);
|
setFGcolor(MENUFGCOLOR);
|
||||||
}
|
}
|
||||||
PutStringAt((SCREEN_WIDTH - strpixlen(items[i]))/2,
|
CenterStringAt(midY + (i-curI)*charH - charB, items[i]);
|
||||||
midY + (i-curI)*charH - charB, items[i]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -89,3 +87,15 @@ menuitem menu_activated(){
|
|||||||
clear_events(); // clear all other events
|
clear_events(); // clear all other events
|
||||||
return MENU_NONE;
|
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));
|
||||||
|
}
|
||||||
|
|||||||
@ -19,6 +19,8 @@
|
|||||||
#ifndef MENU_H__
|
#ifndef MENU_H__
|
||||||
#define MENU_H__
|
#define MENU_H__
|
||||||
|
|
||||||
|
#include <stm32f1.h>
|
||||||
|
|
||||||
typedef enum{
|
typedef enum{
|
||||||
MENU_SLEEP,
|
MENU_SLEEP,
|
||||||
MENU_BALLS,
|
MENU_BALLS,
|
||||||
@ -29,6 +31,7 @@ typedef enum{
|
|||||||
} menuitem;
|
} menuitem;
|
||||||
|
|
||||||
void show_menu();
|
void show_menu();
|
||||||
|
void show_gameover();
|
||||||
menuitem menu_activated();
|
menuitem menu_activated();
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -23,15 +23,17 @@
|
|||||||
#include "screen.h"
|
#include "screen.h"
|
||||||
//#include "usb.h"
|
//#include "usb.h"
|
||||||
|
|
||||||
|
|
||||||
// !!!FOR LITTLE-ENDIAN!!!
|
// !!!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!
|
// X coordinate - from left to right!
|
||||||
// Y coordinate - from top to bottom!
|
// Y coordinate - from top to bottom!
|
||||||
// (0,0) is top left corner
|
// (0,0) is top left corner
|
||||||
|
|
||||||
// all-screen buffer
|
// all-screen buffer
|
||||||
static uint8_t screenbuf[SCREENBUF_SZ];
|
uint8_t screenbuf[SCREENBUF_SZ];
|
||||||
extern volatile uint32_t Tms; // time for FPS count
|
extern volatile uint32_t Tms; // time for FPS count
|
||||||
static uint32_t FPS = 0, Tfps = 0; // approx FPS
|
static uint32_t FPS = 0, Tfps = 0; // approx FPS
|
||||||
uint32_t getFPS(){return 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;
|
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
|
// full string length in pixels
|
||||||
int strpixlen(const char *str){
|
int strpixlen(const char *str){
|
||||||
int l = 0;
|
int l = 0;
|
||||||
@ -122,8 +135,6 @@ int strpixlen(const char *str){
|
|||||||
return l;
|
return l;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t *getScreenBuf(){return screenbuf;}
|
|
||||||
|
|
||||||
static screen_state ScrnState = SCREEN_RELAX;
|
static screen_state ScrnState = SCREEN_RELAX;
|
||||||
screen_state getScreenState(){return ScrnState;}
|
screen_state getScreenState(){return ScrnState;}
|
||||||
|
|
||||||
|
|||||||
@ -43,6 +43,23 @@ typedef enum{ // screen states
|
|||||||
SCREEN_UPDATENXT // update next quarter
|
SCREEN_UPDATENXT // update next quarter
|
||||||
} screen_state;
|
} 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();
|
screen_state getScreenState();
|
||||||
void ClearScreen();
|
void ClearScreen();
|
||||||
void setBGcolor(uint8_t c);
|
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 GetPix(int16_t X, int16_t Y);
|
||||||
uint8_t DrawCharAt(int16_t X, int16_t Y, uint8_t Char);
|
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 PutStringAt(int16_t X, int16_t Y, const char *str);
|
||||||
|
uint8_t CenterStringAt(int16_t Y, const char *str);
|
||||||
int strpixlen(const char *str);
|
int strpixlen(const char *str);
|
||||||
uint8_t *getScreenBuf();
|
uint8_t *getScreenBuf();
|
||||||
void process_screen();
|
void process_screen();
|
||||||
|
|||||||
@ -29,7 +29,19 @@
|
|||||||
#define SNAKE_BGCOLOR (0)
|
#define SNAKE_BGCOLOR (0)
|
||||||
#define SNAKE_HEADCOLOR (0b00001101)
|
#define SNAKE_HEADCOLOR (0b00001101)
|
||||||
#define SNAKE_COLOR (0b00101100)
|
#define SNAKE_COLOR (0b00101100)
|
||||||
|
// food: +1 to size
|
||||||
#define FOOD_COLOR (0b00011100)
|
#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)
|
#define BORDER_COLOR (0b00100000)
|
||||||
// max and min pause between steps
|
// max and min pause between steps
|
||||||
#define MAXSTEPMS (199)
|
#define MAXSTEPMS (199)
|
||||||
@ -46,24 +58,28 @@ static point snake[SNAKE_MAXLEN];
|
|||||||
static int8_t dirX, dirY;
|
static int8_t dirX, dirY;
|
||||||
static uint8_t snakelen;
|
static uint8_t snakelen;
|
||||||
static point Food;
|
static point Food;
|
||||||
static uint32_t StepMS, Tlast, incSpd = 0;
|
|
||||||
static uint32_t score;
|
|
||||||
|
|
||||||
static void ThrowFood(){
|
static point RandCoords(){
|
||||||
int notfound = 1;
|
point pt = {0,0};
|
||||||
do{
|
for(int i = 0; i < 10000; ++i){ // not more than 10000 tries
|
||||||
Food.x = 1 + getRand() % (SCREEN_WIDTH-2);
|
pt.x = 1 + getRand() % (SCREEN_WIDTH-2);
|
||||||
Food.y = 1 + getRand() % (SCREEN_HEIGHT-2);
|
pt.y = 1 + getRand() % (SCREEN_HEIGHT-2);
|
||||||
for(int i = 0; i < snakelen; ++i){
|
if(GetPix(pt.x, pt.y) == SNAKE_BGCOLOR) break;
|
||||||
if(Food.x != snake[i].x && Food.y != snake[i].y){
|
};
|
||||||
notfound = 0;
|
return pt;
|
||||||
break;
|
}
|
||||||
}
|
|
||||||
}
|
// return 0 if failed
|
||||||
}while(notfound);
|
static int ThrowFood(){
|
||||||
USB_send("Food @ "); USB_send(u2str(Food.x));
|
Food = RandCoords();
|
||||||
USB_send(", "); USB_send(u2str(Food.y)); USB_send("\n");
|
if(Food.x == 0 && Food.y == 0) return 0;
|
||||||
DrawPix(Food.x, Food.y, FOOD_COLOR);
|
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(){
|
static void draw_snake(){
|
||||||
@ -101,7 +117,6 @@ void snake_init(){
|
|||||||
dirX = 1; dirY = 0;
|
dirX = 1; dirY = 0;
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
|
|
||||||
dirX = -1; dirY = 0;
|
dirX = -1; dirY = 0;
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
@ -115,12 +130,14 @@ void snake_init(){
|
|||||||
Tlast = Tms; incSpd = 0;
|
Tlast = Tms; incSpd = 0;
|
||||||
score = 0;
|
score = 0;
|
||||||
DrawPix(snake[0].x, snake[0].y, SNAKE_HEADCOLOR);
|
DrawPix(snake[0].x, snake[0].y, SNAKE_HEADCOLOR);
|
||||||
|
USB_send("Snake inited\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
// return 0 if failed
|
// return 0 if failed
|
||||||
static int move_snake(){
|
static int move_snake(){
|
||||||
int last = snakelen - 1;
|
int last = snakelen - 1;
|
||||||
point tail = {snake[last].x, snake[last].y};
|
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(snake[0].x, snake[0].y, SNAKE_COLOR); // redraw head with body color
|
||||||
DrawPix(tail.x, tail.y, SNAKE_BGCOLOR); // delete tail
|
DrawPix(tail.x, tail.y, SNAKE_BGCOLOR); // delete tail
|
||||||
for(int i = snakelen-1; i > 0; --i){
|
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");
|
if(nxtcolr == SNAKE_COLOR) USB_send("slum into yourself\n");
|
||||||
else USB_send("crush into border\n");
|
else USB_send("crush into border\n");
|
||||||
return 0;
|
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?!
|
// not border or snake -> FOOD?!
|
||||||
if(snake[0].x == Food.x && snake[0].y == Food.y){ // increase snake len & speed & score
|
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;
|
++snakelen;
|
||||||
DrawPix(tail.x, tail.y, SNAKE_COLOR);
|
DrawPix(tail.x, tail.y, SNAKE_COLOR);
|
||||||
}
|
}
|
||||||
ThrowFood();
|
if(!ThrowFood()) return 0;
|
||||||
USB_send("Got food, score="); USB_send(u2str(score));
|
USB_send("Got food, score="); USB_send(u2str(score));
|
||||||
USB_send(", len="); USB_send(u2str(snakelen));
|
USB_send(", len="); USB_send(u2str(snakelen));
|
||||||
USB_send(", speed="); USB_send(u2str(StepMS));
|
USB_send(", speed="); USB_send(u2str(StepMS));
|
||||||
@ -163,7 +185,7 @@ static int move_snake(){
|
|||||||
#define DECRSPD() do{incSpd /= 2;}while(0)
|
#define DECRSPD() do{incSpd /= 2;}while(0)
|
||||||
|
|
||||||
// return 0 if game over
|
// return 0 if game over
|
||||||
int proces_snake(){
|
int snake_proces(){
|
||||||
keyevent evt;
|
keyevent evt;
|
||||||
static uint8_t paused = 0;
|
static uint8_t paused = 0;
|
||||||
// change moving direction
|
// change moving direction
|
||||||
@ -173,13 +195,13 @@ int proces_snake(){
|
|||||||
if(keystate(KEY_D, &evt)){ // Down
|
if(keystate(KEY_D, &evt)){ // Down
|
||||||
if(evt == EVT_PRESS){ if(dirY != -1) SETDIR(0, 1);}
|
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(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(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){
|
if(3 == ++paused){
|
||||||
USB_send("Exit snake\n");
|
USB_send("Exit snake\n");
|
||||||
clear_events();
|
clear_events();
|
||||||
@ -187,7 +209,7 @@ int proces_snake(){
|
|||||||
return 0;
|
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){
|
if(paused){
|
||||||
USB_send("Snake resume\n");
|
USB_send("Snake resume\n");
|
||||||
paused = 0;
|
paused = 0;
|
||||||
@ -226,4 +248,3 @@ int proces_snake(){
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t snake_getscore(){return score;}
|
|
||||||
|
|||||||
@ -19,10 +19,7 @@
|
|||||||
#ifndef SNAKE_H__
|
#ifndef SNAKE_H__
|
||||||
#define SNAKE_H__
|
#define SNAKE_H__
|
||||||
|
|
||||||
#include <stm32f1.h>
|
|
||||||
|
|
||||||
void snake_init();
|
void snake_init();
|
||||||
int proces_snake();
|
int snake_proces();
|
||||||
uint32_t snake_getscore();
|
|
||||||
|
|
||||||
#endif // SNAKE_H__
|
#endif // SNAKE_H__
|
||||||
|
|||||||
314
F1-nolib/Tetris/tetris.c
Normal file
314
F1-nolib/Tetris/tetris.c
Normal file
@ -0,0 +1,314 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the TETRIS project.
|
||||||
|
* Copyright 2021 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#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;
|
||||||
|
}
|
||||||
25
F1-nolib/Tetris/tetris.h
Normal file
25
F1-nolib/Tetris/tetris.h
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the TETRIS project.
|
||||||
|
* Copyright 2021 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
#pragma once
|
||||||
|
#ifndef TETRIS_H__
|
||||||
|
#define TETRIS_H__
|
||||||
|
|
||||||
|
void tetris_init();
|
||||||
|
int tetris_process();
|
||||||
|
|
||||||
|
#endif // TETRIS_H__
|
||||||
Loading…
x
Reference in New Issue
Block a user