aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian <j@teule.nl>2018-11-09 18:30:47 +0100
committerJulian <j@teule.nl>2018-11-09 18:30:47 +0100
commit0baa7ebefb4b504da4d125aab55e96e7f2c5a87e (patch)
treee4abdc87838ca8907d11d49d658adf6bbdf503d8
parent2ad2c5239e5c06a628559183a21d0e82c256a3ac (diff)
Testris works very well
-rw-r--r--Jetris.ino62
-rw-r--r--sprites.h186
-rw-r--r--sprites.ino29
-rw-r--r--tags29
4 files changed, 220 insertions, 86 deletions
diff --git a/Jetris.ino b/Jetris.ino
index 99d71f8..6c9990b 100644
--- a/Jetris.ino
+++ b/Jetris.ino
@@ -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();
}
}
diff --git a/sprites.h b/sprites.h
index 3b53d47..41dd785 100644
--- a/sprites.h
+++ b/sprites.h
@@ -1,156 +1,192 @@
#ifndef sprites
#define sprites
+#include <stdint.h>
+#include <stddef.h>
+
struct Sprite {
- uint8_t buff[8];
+ uint8_t buff[4];
uint8_t width;
uint8_t height;
+ struct Sprite *rotateNext;
};
-const Sprite smiley = {
- {
- 0b11001100,
- 0b11001100,
- 0b00000000,
- 0b00110000,
- 0b10000000,
- 0b11111100,
- 0b00000000,
- 0b00000000
- }, 6, 6
-};
-const Sprite checker = {
- {
- 0b10101010,
- 0b01010101,
- 0b10101010,
- 0b01010101,
- 0b10101010,
- 0b01010101,
- 0b10101010,
- 0b01010101
- }, 8, 8
-};
-const Sprite ball = {
+struct Sprite ball = {
{
0b11100000,
0b10100000,
0b11100000,
0b00000000,
- 0b00000000,
- 0b00000000,
- 0b00000000,
- 0b00000000
- }, 3, 3
+ }, 3, 3, NULL
};
-const Sprite miniSmiley = {
+
+struct Sprite iBlock = {
{
- 0b01010000,
- 0b10000000,
0b11110000,
0b00000000,
0b00000000,
0b00000000,
- 0b00000000,
- 0b00000000
- }, 4, 3
+ }, 4, 1, NULL
};
-
-
-const Sprite iBlock = {
+struct Sprite iBlockR = {
{
- 0b11110000,
- 0b00000000,
- 0b00000000,
- 0b00000000,
- 0b00000000,
- 0b00000000,
- 0b00000000,
- 0b00000000
- }, 4, 1
+ 0b10000000,
+ 0b10000000,
+ 0b10000000,
+ 0b10000000,
+ }, 1, 4, NULL
};
-const Sprite oBlock = {
+
+struct Sprite oBlock = {
{
0b11000000,
0b11000000,
0b00000000,
0b00000000,
- 0b00000000,
- 0b00000000,
- 0b00000000,
- 0b00000000
- }, 2, 2
+ }, 2, 2, NULL
};
-const Sprite tBlock = {
+struct Sprite tBlock = {
{
0b01000000,
0b11100000,
0b00000000,
0b00000000,
+ }, 3, 2, NULL
+};
+struct Sprite tBlockR = {
+ {
+ 0b10000000,
+ 0b11000000,
+ 0b10000000,
0b00000000,
+ }, 2, 3, NULL
+};
+struct Sprite tBlockD = {
+ {
+ 0b11100000,
+ 0b01000000,
0b00000000,
0b00000000,
- 0b00000000
- }, 3, 2
+ }, 3, 2, NULL
};
+struct Sprite tBlockL = {
+ {
+ 0b01000000,
+ 0b11000000,
+ 0b01000000,
+ 0b00000000,
+ }, 2, 3, NULL
+};
+
-const Sprite sBlock = {
+struct Sprite sBlock = {
{
0b01100000,
0b11000000,
0b00000000,
0b00000000,
+ }, 3, 2, NULL
+};
+struct Sprite sBlockR = {
+ {
+ 0b10000000,
+ 0b11000000,
+ 0b01000000,
0b00000000,
- 0b00000000,
- 0b00000000,
- 0b00000000
- }, 3, 2
+ }, 2, 3, NULL
};
-const Sprite zBlock = {
+struct Sprite zBlock = {
{
0b11000000,
0b01100000,
0b00000000,
0b00000000,
+ }, 3, 2, NULL
+};
+struct Sprite zBlockR = {
+ {
+ 0b01000000,
+ 0b11000000,
+ 0b10000000,
0b00000000,
- 0b00000000,
- 0b00000000,
- 0b00000000
- }, 3, 2
+ }, 2, 3, NULL
};
-const Sprite jBlock = {
+
+// JBLOCk
+struct Sprite jBlock = {
{
0b10000000,
0b11100000,
0b00000000,
0b00000000,
+ }, 3, 2, NULL
+};
+struct Sprite jBlockR = {
+ {
+ 0b11000000,
+ 0b10000000,
+ 0b10000000,
0b00000000,
+ }, 2, 3, NULL
+};
+struct Sprite jBlockD = {
+ {
+ 0b11100000,
+ 0b00100000,
0b00000000,
0b00000000,
- 0b00000000
- }, 3, 2
+ }, 3, 2, NULL
+};
+struct Sprite jBlockL = {
+ {
+ 0b01000000,
+ 0b01000000,
+ 0b11000000,
+ 0b00000000,
+ }, 2, 3, NULL
};
-const Sprite lBlock = {
+//Lblock
+struct Sprite lBlock = {
{
0b00100000,
0b11100000,
0b00000000,
0b00000000,
+ }, 3, 2, NULL
+};
+struct Sprite lBlockR = {
+ {
+ 0b10000000,
+ 0b10000000,
+ 0b11000000,
+ 0b00000000,
+ }, 2, 3, NULL
+};
+struct Sprite lBlockD = {
+ {
+ 0b11100000,
+ 0b10000000,
0b00000000,
0b00000000,
+ }, 3, 2, NULL
+};
+struct Sprite lBlockL = {
+ {
+ 0b11000000,
+ 0b01000000,
+ 0b01000000,
0b00000000,
- 0b00000000
- }, 3, 2
+ }, 3, 2, NULL
};
-const Sprite *blocks[] = { &iBlock, &oBlock, &tBlock, &sBlock, &zBlock, &jBlock, &lBlock };
+struct Sprite *blocks[] = { &iBlock, &oBlock, &tBlock, &sBlock, &zBlock, &jBlock, &lBlock };
#endif
diff --git a/sprites.ino b/sprites.ino
new file mode 100644
index 0000000..d519095
--- /dev/null
+++ b/sprites.ino
@@ -0,0 +1,29 @@
+#include "sprites.h"
+
+void initSprites() {
+ iBlock.rotateNext = &iBlockR;
+ iBlockR.rotateNext = &iBlock;
+
+ oBlock.rotateNext = &oBlock;
+
+ tBlock.rotateNext = &tBlockR;
+ tBlockR.rotateNext = &tBlockD;
+ tBlockD.rotateNext = &tBlockL;
+ tBlockL.rotateNext = &tBlock;
+
+ sBlock.rotateNext = &sBlockR;
+ sBlockR.rotateNext = &sBlock;
+
+ zBlock.rotateNext = &zBlockR;
+ zBlockR.rotateNext = &zBlock;
+
+ jBlock.rotateNext = &jBlockR;
+ jBlockR.rotateNext = &jBlockD;
+ jBlockD.rotateNext = &jBlockL;
+ jBlockL.rotateNext = &jBlock;
+
+ lBlock.rotateNext = &lBlockR;
+ lBlockR.rotateNext = &lBlockD;
+ lBlockD.rotateNext = &lBlockL;
+ lBlockL.rotateNext = &lBlock;
+}
diff --git a/tags b/tags
index c45c520..c8ffe2a 100644
--- a/tags
+++ b/tags
@@ -6,10 +6,21 @@
!_TAG_PROGRAM_VERSION 5.8 //
MaxCommands MaxCommands.h 2;" d
Sprite sprites.h /^struct Sprite {$/;" s
-ball sprites.h /^const Sprite ball = {$/;" v
-buff sprites.h /^ uint8_t buff[8];$/;" m struct:Sprite
-checker sprites.h /^const Sprite checker = {$/;" v
+ball sprites.h /^struct Sprite ball = {$/;" v typeref:struct:Sprite
+blocks sprites.h /^struct Sprite *blocks[] = { &iBlock, &oBlock, &tBlock, &sBlock, &zBlock, &jBlock, &lBlock };$/;" v typeref:struct:Sprite
+buff sprites.h /^ uint8_t buff[4];$/;" m struct:Sprite
height sprites.h /^ uint8_t height;$/;" m struct:Sprite
+iBlock sprites.h /^struct Sprite iBlock = {$/;" v typeref:struct:Sprite
+iBlockR sprites.h /^struct Sprite iBlockR = {$/;" v typeref:struct:Sprite
+initSprites sprites.c /^void initSprites() {$/;" f
+jBlock sprites.h /^struct Sprite jBlock = {$/;" v typeref:struct:Sprite
+jBlockD sprites.h /^struct Sprite jBlockD = {$/;" v typeref:struct:Sprite
+jBlockL sprites.h /^struct Sprite jBlockL = {$/;" v typeref:struct:Sprite
+jBlockR sprites.h /^struct Sprite jBlockR = {$/;" v typeref:struct:Sprite
+lBlock sprites.h /^struct Sprite lBlock = {$/;" v typeref:struct:Sprite
+lBlockD sprites.h /^struct Sprite lBlockD = {$/;" v typeref:struct:Sprite
+lBlockL sprites.h /^struct Sprite lBlockL = {$/;" v typeref:struct:Sprite
+lBlockR sprites.h /^struct Sprite lBlockR = {$/;" v typeref:struct:Sprite
maxDECODE_MODE MaxCommands.h 15;" d
maxDIGIT_0 MaxCommands.h 7;" d
maxDIGIT_1 MaxCommands.h 8;" d
@@ -23,7 +34,15 @@ maxINTENSITY MaxCommands.h 6;" d
maxSCAN_LIMIT MaxCommands.h 16;" d
maxSHUTDOWN_INV MaxCommands.h 4;" d
maxTEST MaxCommands.h 5;" d
-miniSmiley sprites.h /^const Sprite miniSmiley = {$/;" v
-smiley sprites.h /^const Sprite smiley = { $/;" v
+oBlock sprites.h /^struct Sprite oBlock = {$/;" v typeref:struct:Sprite
+rotateNext sprites.h /^ struct Sprite *rotateNext;$/;" m struct:Sprite typeref:struct:Sprite::Sprite
+sBlock sprites.h /^struct Sprite sBlock = {$/;" v typeref:struct:Sprite
+sBlockR sprites.h /^struct Sprite sBlockR = {$/;" v typeref:struct:Sprite
sprites sprites.h 2;" d
+tBlock sprites.h /^struct Sprite tBlock = {$/;" v typeref:struct:Sprite
+tBlockD sprites.h /^struct Sprite tBlockD = {$/;" v typeref:struct:Sprite
+tBlockL sprites.h /^struct Sprite tBlockL = {$/;" v typeref:struct:Sprite
+tBlockR sprites.h /^struct Sprite tBlockR = {$/;" v typeref:struct:Sprite
width sprites.h /^ uint8_t width;$/;" m struct:Sprite
+zBlock sprites.h /^struct Sprite zBlock = {$/;" v typeref:struct:Sprite
+zBlockR sprites.h /^struct Sprite zBlockR = {$/;" v typeref:struct:Sprite