aboutsummaryrefslogtreecommitdiff
path: root/Jetris.ino
diff options
context:
space:
mode:
Diffstat (limited to 'Jetris.ino')
-rw-r--r--Jetris.ino78
1 files changed, 66 insertions, 12 deletions
diff --git a/Jetris.ino b/Jetris.ino
index 99d71f8..bd19f4c 100644
--- a/Jetris.ino
+++ b/Jetris.ino
@@ -1,6 +1,7 @@
// THings that miss
// Functions that return something
// Make more safety
+// Malloc etc.
#include "sprites.h"
#include "MaxCommands.h"
@@ -12,6 +13,7 @@
#define leftPin 2
#define rightPin 4
#define dropPin 11
+#define rotatePin 10
#define clickTime 200
//Use the drawDot function to draw
@@ -35,6 +37,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);
@@ -54,13 +57,13 @@ void setup() {
// GAME LOGIC //
/////////////////////////////
-Sprite *block;
+struct Sprite block;
int xPos;
int yPos;
-long loopTime;
+unsigned long loopTime;
void initGame() {
initBlock();
@@ -75,7 +78,7 @@ void loop() {
//Move
if(checkCollision(0, 1)) {
- drawSprite(buttonLayer, xPos, yPos, block);
+ drawSprite(buttonLayer, xPos, yPos, &block);
renderAll();
initBlock();
}else{
@@ -90,7 +93,7 @@ void initBlock(){
xPos = 0;
yPos = 0;
- block = blocks[random(7)];
+ block = *blocks[random(7)];
}
void moveBlock(int xMove, int yMove) {
@@ -101,7 +104,7 @@ void moveBlock(int xMove, int yMove) {
drawRegion(topLayer, 0xFF, 0xFFFF, false);
//Draw ball
- drawSprite(topLayer, xPos, yPos, block);
+ drawSprite(topLayer, xPos, yPos, &block);
//Render
renderAll();
@@ -114,13 +117,13 @@ int checkCollision(int xMove, int yMove) {
int yNew = yPos + yMove;
int xNew = xPos + xMove;
- for(int i = 0; i < block->height; i++) {
- if(( block->buff[i] >> xNew) & buttonLayer[i + yNew])
+ for(int i = 0; i < block.height; i++) {
+ if(( block.buff[i + block.yOff] >> xNew - block.xOff ) & buttonLayer[i + yNew])
return 1;
}
//Check if out of bounds
- if(xNew + block->width > 8 || xNew < 0 || yNew + block->height > 16)
+ if(xNew + block.width > 8 || xNew < 0 || yNew + block.height > 16)
return 1;
return 0;
@@ -134,7 +137,46 @@ void dropBlock() {
}
- drawSprite(buttonLayer, xPos, yPos, block);
+ drawSprite(buttonLayer, xPos, yPos, &block);
+}
+
+//Rotate sprite with the clock
+void rotateSprite(struct Sprite *sprite, int count) {
+ uint8_t oldBuffer[8];
+
+ Serial.println(oldBuffer[0]);
+ Serial.println(oldBuffer[2]);
+ Serial.println(sprite->buff[0]);
+ Serial.println(sprite->buff[2]);
+
+ //VULN TODO
+ memcpy(oldBuffer, sprite->buff, sizeof(oldBuffer) );
+
+ Serial.println("Took copy");
+ Serial.println(oldBuffer[0]);
+ Serial.println(sprite->buff[0]);
+
+ for(int i = 0; i < 8; i++) {
+ sprite->buff[i] = 0;
+ }
+
+ //Cannot explain how this works in text TODO
+ for(int i = 0; i < 8; i++) {
+ uint8_t row = oldBuffer[7 - i];
+ for(int j = 0; j < 8; j++) {
+ sprite->buff[j] |= row & ( 1 << i );
+ }
+ }
+
+ Serial.println("Rotated array");
+
+ //Switch height/width
+ uint8_t temp = sprite->height;
+ sprite->height = sprite->width;
+ sprite->width = temp;
+
+ Serial.println("Changed dimensions");
+
}
void renderAll(){
@@ -158,13 +200,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) {
@@ -190,6 +233,17 @@ void handleButtons() {
dropBlock();
}
+
+ if(millis() - rotateLast > clickTime && rotateState) {
+ rotateLast = millis();
+
+ //TODO collision checks
+ rotateSprite(&block, 1);
+
+ //Redraw moveblock
+ moveBlock(0, 0);
+
+ }
}
/////////////////////////////
@@ -216,7 +270,7 @@ void initDisplay() {
//Draw a region, where mask specifies where to draw.
// xMask = 0b00111100, yMask = 0b01111110 will draw a small 4x6 box
-void drawSprite(uint8_t layer[], uint8_t x, uint8_t y, Sprite* sprite) {
+void drawSprite(uint8_t layer[], uint8_t x, uint8_t y, struct Sprite* sprite) {
//Check if in range
if (x + sprite->width - 1 > 7 || y + sprite->height - 1> 15 )
return;
@@ -224,7 +278,7 @@ void drawSprite(uint8_t layer[], uint8_t x, uint8_t y, Sprite* sprite) {
for ( int i = y; i < sprite->height + y; i++) {
//Calculate bits
uint8_t row = layer[i];
- row |= sprite->buff[i - y] >> x;
+ row |= sprite->buff[i - y + sprite->yOff] >> x - sprite->xOff;
layer[i] = row;