diff options
author | Julian <j@teule.nl> | 2018-11-09 18:30:47 +0100 |
---|---|---|
committer | Julian <j@teule.nl> | 2018-11-09 18:30:47 +0100 |
commit | 0baa7ebefb4b504da4d125aab55e96e7f2c5a87e (patch) | |
tree | e4abdc87838ca8907d11d49d658adf6bbdf503d8 /Jetris.ino | |
parent | 2ad2c5239e5c06a628559183a21d0e82c256a3ac (diff) |
Testris works very well
Diffstat (limited to 'Jetris.ino')
-rw-r--r-- | Jetris.ino | 62 |
1 files changed, 56 insertions, 6 deletions
@@ -12,6 +12,7 @@ #define leftPin 2 #define rightPin 4 #define dropPin 11 +#define rotatePin 10 #define clickTime 200 //Use the drawDot function to draw @@ -35,6 +36,7 @@ void setup() { pinMode(leftPin, INPUT_PULLUP); pinMode(rightPin, INPUT_PULLUP); pinMode(dropPin, INPUT_PULLUP); + pinMode(rotatePin, INPUT_PULLUP); //ButtonInterrupt //attachInterrupt(digitalPinToInterrupt(left), buttonHandle, FALLING); @@ -45,6 +47,8 @@ void setup() { setDisplay(1); initDisplay(); + + initSprites(); initGame(); @@ -76,6 +80,7 @@ void loop() { //Move if(checkCollision(0, 1)) { drawSprite(buttonLayer, xPos, yPos, block); + handleFullRows(0); renderAll(); initBlock(); }else{ @@ -126,12 +131,28 @@ int checkCollision(int xMove, int yMove) { return 0; } +void rotateBlock() { + Serial.println("Rotate"); + + //If next block exist, switch to that + if( block->rotateNext ) { + Serial.println((uint16_t)block); + Serial.println((uint16_t)block->rotateNext); + + block = block->rotateNext; + Serial.println((uint16_t)block); + } + + //And render + moveBlock(0, 0); +} + void dropBlock() { //Move down until it hits something int count = 0; while( !checkCollision(0, 1) ) { moveBlock(0, 1); - + delay(10); } drawSprite(buttonLayer, xPos, yPos, block); @@ -144,6 +165,18 @@ void renderAll(){ render(8); } +void handleFullRows(int start) { + for(int i = start; i < 16; i++ ) { + if(buttonLayer[15 - i] == 0xFF) { + for(int j = i; j < 16; j++) { + buttonLayer[15 - j] = buttonLayer[14 - j]; + } + handleFullRows(i); + return; + } + } +} + void renderToSerial() { Serial.write(0x33); Serial.println("[2J"); for(int i = 0; i < 16; i++) { @@ -158,13 +191,14 @@ void renderToSerial() { // Control Handling // ///////////////////////////// -int leftLast, rightLast, dropLast; +unsigned long leftLast, rightLast, dropLast, rotateLast; void handleButtons() { //Read from switch int leftState = !digitalRead(leftPin); int rightState = !digitalRead(rightPin); int dropState = !digitalRead(dropPin); + int rotateState = !digitalRead(rotatePin); //Check if last click was over 100 ms ago if(millis() - leftLast > clickTime && leftState) { @@ -188,7 +222,12 @@ void handleButtons() { dropLast = millis(); dropBlock(); + } + + if(millis() - rotateLast > clickTime+100 && rotateState) { + rotateLast = millis(); + rotateBlock(); } } @@ -251,15 +290,26 @@ void drawRegion(uint8_t layer[], uint8_t xMask, uint16_t yMask, bool state) { } } +unsigned long reRenderLast; + void render(int where) { + static uint8_t onScreen[16]; + + bool reRender = (millis() - reRenderLast ) > 1000; + for(int i = where; i < (where + 8); i++ ) { + uint8_t toWrite = buttonLayer[i] | topLayer[i]; + if(onScreen[i] == toWrite && !reRender) + continue; + writeCommand(maxDIGIT_0 + i - where, buttonLayer[i] | topLayer[i]); + + onScreen[i] = toWrite; } - - //Shift everything throught - for( int i = 0; i < curDisplay; i++) { - writeCommand(0, 0); + + if(reRender) { + reRenderLast = millis(); } } |