diff options
-rw-r--r-- | Jetris.ino | 86 | ||||
-rw-r--r-- | drawing.ino | 8 | ||||
-rw-r--r-- | sprites.h | 10 |
3 files changed, 85 insertions, 19 deletions
@@ -5,13 +5,18 @@ #define RIGHT_PIN 4 #define DROP_PIN 11 #define ROTATE_PIN 10 + #define CLICK_TIME 200 +#define FALL_TIME 300 +#define DEBUG_TO_SERIAL 1 #define RENDER_TO_SERIAL 0 void setup() { +#if DEBUG_TO_SERIAL == 1 or RENDER_TO_SERIAL == 1 Serial.begin(115200); Serial.println("Starting \n\n"); +#endif /* Prepare the random generator */ randomSeed( analogRead(0) ); @@ -31,6 +36,7 @@ void setup() { initSprites(); initGame(); + } void loop() { @@ -44,7 +50,7 @@ void loop() { struct Sprite *cur_block; -int x_pos, y_pos; +int x_pos, y_pos, score; void initGame() { initBlock(); @@ -56,17 +62,28 @@ void gameLoop() { handleButtons(); - if( millis() - loop_time < 500) + if( millis() - loop_time < FALL_TIME) return; /* Move the cur_block */ if( checkCollision(0, 1) ){ drawSprite(buttonLayer, x_pos, y_pos, cur_block); + /* Debug information */ +#if DEBUG_TO_SERIAL == 1 + Serial.print("Writing block to "); + Serial.print(x_pos); + Serial.print(", "); + Serial.println(y_pos); +#endif + /* Handle the rows, from the button */ handleFullRows(0); - initBlock(); + /* If full end the game */ + if( initBlock() ) + endGame(); + renderAll(); }else { /* Free to move (one down)*/ @@ -76,10 +93,14 @@ void gameLoop() { loop_time = millis(); } -void initBlock() { - x_pos = y_pos = 0; - +/* Makes a new block and returns 1 if full */ +int initBlock() { cur_block = blocks[ random(7) ]; + + y_pos = 0; + x_pos = 4 - cur_block->width/2; + + return checkCollision(0, 0); } void moveBlock(int x_move, int y_move) { @@ -106,7 +127,7 @@ int checkCollision(int xMove, int yMove) { } /* Check if border is hit */ - if( xNew + cur_block->width > 8 || xNew < 0 || yNew + cur_block->height > 16 ) + if( xNew + cur_block->width > 8 || xNew < 0 || yNew + cur_block->height > 16 || yNew < 0 ) return 1; return 0; @@ -130,9 +151,6 @@ void dropBlock() { /* Add delay to make falling effect */ delay(5); } - - /* Move to cur_block to the backlayer */ - drawSprite(buttonLayer, x_pos, y_pos, cur_block); } /* Check and delete full rows(starting at `start`) */ @@ -145,17 +163,63 @@ void handleFullRows(int start) { for( int j = i; j < 16; j++ ){ buttonLayer[15 - j] = buttonLayer[14 - j]; } + + /* Increase score */ + score++; } else { i++; } } } +void endGame() { + /* Slowly clear screen */ + for( int i = 0; i < 16; i++ ) { + buttonLayer[15 - i] = topLayer[15 - i] = 0; + renderAll(); + delay(100); + } + + /* Fill button of screen with score */ + int xFill = score % 8; + int yFill = score / 8; + int y = 0; +#if DEBUG_TO_SERIAL == 1 + Serial.print("Finished with score "); + Serial.println(score); +#endif + + for( y = 0; y < yFill; y++ ){ + buttonLayer[15 - y] = 0xFF; + } + for( int x = 0; x < xFill; x++ ){ + buttonLayer[15 - y] = buttonLayer[15 - y] | ( 1 << x ); + } + + /* Replicate the DVD gliding logo thing */ + + cur_block = &smiley; + /* Velocity */ + int xV = 1, yV = 1; + y_pos = 3; + + while(1){ + if( checkCollision(xV, 0) ) { + xV *= -1; + } + if( checkCollision(0, yV) ) { + yV *= -1; + } + + moveBlock(xV, yV); + delay(100); + } +} + /* Draw first 8 bytes on first desplay and second 8 on last */ void renderAll() { #if RENDER_TO_SERIAL == 1 renderToSerial(); - return; #endif render(0, 0); diff --git a/drawing.ino b/drawing.ino index 7269f08..4bfbb30 100644 --- a/drawing.ino +++ b/drawing.ino @@ -84,14 +84,6 @@ void renderToSerial() { /* Write a register to specified display */ static void writeCommand(int display, uint8_t addr, uint8_t data) { - Serial.print("Writing byte "); - Serial.print(data); - Serial.print(" to addr "); - Serial.print(addr); - Serial.print(" on display "); - Serial.println(display); - - /* Chip select low */ digitalWrite(CS, LOW); @@ -189,5 +189,15 @@ struct Sprite lBlockL = { struct Sprite *blocks[] = { &iBlock, &oBlock, &tBlock, &sBlock, &zBlock, &jBlock, &lBlock }; +/* End animation */ +struct Sprite smiley = { + { + 0b10100000, + 0b00000000, + 0b11100000, + 0b00100000, + }, 3, 4, NULL +}; + #endif |