Snake game Update #2

In a previous post I shared a schematic how to wire two 74HC595N shift registers, PNP transistors and 8×8 LED matrix, but I did not thought everything carefully enough and did not put a resistor in between second shift register and transistor, so I manage to “burn” shift register that controls row of cathodes on matrix. Thankfully they are not so expensive and I have ordered new one on eBay.

New design after improvements should look like this:

Led matrix 8x8 74hc595n pnp transistor

…………………………………………………..

Also, I did some work on Matrix class and Snake game by itself. Game code if far from complete and what I expect to see, but some functionality is working.

In matrix class only thing I changed is that now I turn off all leds at the end of cycle, otherwise last row stays longer on that others and look little bit brighter. Otherwise no changes.

void MatrixClass::display()
{
  for (int i = 0; i < 8; i++)
  {
    byte row = B00000001 << (7 - i);
    byte col = B00000000;

    for (int j = 0; j < 8; j++)
    {
      if (leds[i][j] == 1)
      {
        col |= B00000001 << (7 - j);
      }
    }

    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, col);
    shiftOut(dataPin, clockPin, LSBFIRST, row);
    digitalWrite(latchPin, HIGH);

    delay(2);
  }

  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, LSBFIRST, B00000000);
  shiftOut(dataPin, clockPin, LSBFIRST, B00000000);
  digitalWrite(latchPin, HIGH);
}

And first tests to see how Matrix is working.

#include "Matrix.h"

enum Direction
{
  UP, RIGHT, DOWN, LEFT
};

struct Position
{
  int x;
  int y;
};

// Matrix pins
int DATA_PIN = 2;
int CLOCK_PIN = 3;
int LATCH_PIN = 4;
MatrixClass matrix;

// Joystick pins
// A1 - X axis
// A0 - Y axis

int deadZone = 32;

// Snake
int size;
Direction direction;
Position snake[64];

int frameLenght = 1000;
int frameTime = 0;
long lastUpdate = 0;

bool gameOver;

void setup()
{
  // Initialize led matrix
  matrix.init(DATA_PIN, CLOCK_PIN, LATCH_PIN);

  // Setup input
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);

  restartGame();

  lastUpdate = millis();
}

void loop()
{
  // Update input
  getInput();

  /* Update game
  I do update only every second
  */
  frameTime = frameTime + millis() - lastUpdate;
  lastUpdate = millis();
  if (frameTime > frameLenght)
  {
    // Update game
    updateSnake();
    frameTime = frameTime - frameLenght;
  }

  // Draw game
  matrix.clear();
  drawSnake();
  matrix.display();
}

void restartGame()
{
  direction = RIGHT;
  size = 3;
  snake[0].x = 4;
  snake[0].y = 4;
  snake[1].x = 3;
  snake[1].y = 4;
  snake[2].x = 2;
  snake[2].y = 4;
  gameOver = false;
}

void getInput()
{
  int x = analogRead(A1);
  int y = analogRead(A0);

  if (x < (512 - deadZone))
  {
    direction = RIGHT;
  }
  else if (x > 512 + deadZone)
  {
    direction = LEFT;
  }
  else if (y < 512 - deadZone)
  {
    direction = UP;
  }
  else if (y > 512 + deadZone)
  {
    direction = DOWN;
  }
}

void updateSnake()
{
  // Move all joints up by one pisition in array
  for (int i = size - 1; i > 0; i--)
  {
    snake[i] = snake[i - 1];
  }

  // Set a heads position depending on which side snake is facing
  if (direction == RIGHT)
  {
    snake[0].x = snake[1].x + 1;
    snake[0].y = snake[1].y;
  }
  else if (direction == DOWN)
  {
    snake[0].x = snake[1].x;
    snake[0].y = snake[1].y + 1;
  }
  else if (direction == LEFT)
  {
    snake[0].x = snake[1].x - 1;
   snake[0].y = snake[1].y;
  }
  else if (direction == UP)
  {
    snake[0].x = snake[1].x;
    snake[0].y = snake[1].y - 1;
   }
}

void drawSnake()
{
  for (int i = 0; i < size; i++)
  {
    matrix.setHigh(snake[i].y, snake[i].x);
  }
}

And of course final result

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s