Arduino LCD game not displaying


i getting crazy on snake lcd game. use arduino mega , i'd use lcd screen , pmodkypd snake game. problem not display anything, no matter key press. here code:
code: [select]
#include <liquidcrystal.h>
#include "keypad.h"

byte mysnake[8][8] =
{
....
};
 // keypad type definition
const byte rows = 4; //four rows
const byte cols = 4; //three columns
char keys[rows][cols] =
 {{'1','2','3','a'},
  {'4','5','6','b'},
  {'7','8','9','c'},
  {'0','f','e','d'}};

byte rowpins[rows] = {
  37, 36, 35, 34}; //connect row pinouts of keypad
byte colpins[cols] = {
  33,32, 31, 30}; // connect column pinouts of keypad
keypad keypad = keypad( makekeymap(keys), rowpins, colpins, rows, cols );

boolean levelz[5][2][16] = {
{{false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false},
{false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false}},

{{true,false,false,false,false,false,false,false,false,false,false,false,false,false,false,true},
{true,false,false,false,false,false,false,false,false,false,false,false,false,false,false,true}},

{{true,false,false,false,true,false,false,false,false,false,false,true,false,false,false,true},
{true,false,false,false,false,false,false,false,true,false,false,false,false,false,false,true}},

{{true,false,true,false,false,false,false,false,false,true,false,false,false,true,false,false},
{false,false,false,false,true,false,false,true,false,false,false,true,false,false,false,true}}
};

liquidcrystal lcd(7, 6, 5, 4, 3, 2);
unsigned long time, timenow;
int gamespeed;
boolean skip, gameover, gamestarted;
int olddir;
int selectedlevel,levels;

int num_keys = 5;
int adc_key_in;
int key=-1;
char key_read;
int oldkey=-1;

boolean x[16][80];
byte mychar[8];
byte nullchar[8] = { 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0 };
boolean special;

struct partdef
{
  int row,column,dir; //0 - up, 1 - down, 2 - right, 3 - left
  struct partdef *next;
};
typedef partdef part;

part *head, *tail;
int i,j,collected;
long pc,pr;

void drawmatrix()
{
  int cc=0;
  if (!gameover)
  {
  x[pr][pc] = true;
  //for (i=0;i<8;i++) lcd.createchar(i, nullchar);
  for(int r=0;r<2;r++)
  {
    for(int c=0;c<16;c++)
    {
      special = false;
      for(int i=0;i<8;i++)
      {
        byte b=b00000;
        if (x[r*8+i][c*5+0]) {b+=b10000; special = true;}
        if (x[r*8+i][c*5+1]) {b+=b01000; special = true;}
        if (x[r*8+i][c*5+2]) {b+=b00100; special = true;}
        if (x[r*8+i][c*5+3]) {b+=b00010; special = true;}
        if (x[r*8+i][c*5+4]) {b+=b00001; special = true;}
        mychar[i] = b;
      }
      if (special)
      {
        lcd.createchar(cc, mychar);
        lcd.setcursor(c,r);
        lcd.write(byte(cc));
        cc++;
      }
      else
      {
        lcd.setcursor(c,r);
        if (levelz[selectedlevel][r][c]) lcd.write(255);
        else lcd.write(254);
      }
    }
  }
  }
}

void freelist()
{
  part *p,*q;
  p = tail;
  while (p!=null)
  {
    q = p;
    p = p->next;
    free(q);
  }
  head = tail = null;
}

void gameoverfunction()
{
  delay(1000);
  lcd.clear();
  freelist();
  lcd.setcursor(3,0);
  lcd.print("game over!");
  lcd.setcursor(4,1);
  lcd.print("score: ");
  lcd.print(collected);
  delay(1000);
}

void growsnake()
{
  part *p;
  p = (part*)malloc(sizeof(part));
  p->row = tail->row;
  p->column = tail->column;
  p->dir = tail->dir;
  p->next = tail;
  tail = p;
}

void newpoint()
{

  part *p;
  p = tail;
  boolean newp = true;
  while (newp)
  {
    pr = random(16);
    pc = random(80);
    newp = false;
    if (levelz[selectedlevel][pr / 8][pc / 5]) newp=true;
    while (p->next != null && !newp)
    {
      if (p->row == pr && p->column == pc) newp = true;
      p = p->next;
    }
  }

  if (collected < 13 && gamestarted) growsnake();
}

void movehead()
{
  switch(head->dir) // 1 step in direction
  {
    case 0: head->row--; break;
    case 1: head->row++; break;
    case 2: head->column++; break;
    case 3: head->column--; break;
    default : break;
  }
  if (head->column >= 80) head->column = 0;
  if (head->column < 0) head->column = 79;
  if (head->row >= 16) head->row = 0;
  if (head->row < 0) head->row = 15;

  if (levelz[selectedlevel][head->row / 8][head->column / 5]) gameover = true; // wall collision check

  part *p;
  p = tail;
  while (p != head && !gameover) // self collision
  {
    if (p->row == head->row && p->column == head->column) gameover = true;
    p = p->next;
  }
  if (gameover)
    gameoverfunction();
  else
  {
  x[head->row][head->column] = true;

  if (head->row == pr && head->column == pc) // point pickup check
  {
    collected++;
    if (gamespeed < 25) gamespeed+=3;
    newpoint();
  }
  }
}

void moveall()
{
  part *p;
  p = tail;
  x[p->row][p->column] = false;
  while (p->next != null)
  {
    p->row = p->next->row;
    p->column = p->next->column;
    p->dir = p->next->dir;
    p = p->next;
  }
  movehead();
}

void createsnake(int n) // n = size of snake
{
  for (i=0;i<16;i++)
    for (j=0;j<80;j++)
      x[i][j] = false;

  part *p, *q;
  tail = (part*)malloc(sizeof(part));
  tail->row = 7;
  tail->column = 39 + n/2;
  tail->dir = 3;
  q = tail;
  x[tail->row][tail->column] = true;
  for (i = 0; < n-1; i++) // build snake tail head
  {
    p = (part*)malloc(sizeof(part));
    p->row = q->row;
    p->column = q->column - 1; //initial snake id placed horizoltally
    x[p->row][p->column] = true;
    p->dir = q->dir;
    q->next = p;
    q = p;
  }
  if (n>1)
  {
    p->next = null;
    head  = p;
  }
  else
  {
    tail->next = null;
    head = tail;
  }
}

void startf()
{
  gameover = false;
  gamestarted = false;
  selectedlevel = 1;

  lcd.clear();
  lcd.setcursor(0,0);
  lcd.print("select level: 1");
  for(i=0;i<8;i++)
  {
    lcd.createchar(i,mysnake[i]);
    lcd.setcursor(i+4,1);
    lcd.write(byte(i));
  }
  collected = 0;
  gamespeed = 8;
  createsnake(3);
  time = 0;
}
void setup()
{
  levels = 5; //number of lvls
  lcd.begin(16, 2);
  startf();
}

void loop()
{
  if (!gameover && !gamestarted)
  {
   key_read = keypad.getkey();  // key press
   key = key_read - '0';
   if (key != oldkey)   // if keypress detected
   {
      delay(50);  // wait debounce time
      key_read = keypad.getkey();  // key press
      key = key_read - '0';
     if (key != oldkey)    
     {  
       oldkey = key;
       if (key >=0)
       {
         olddir = head->dir;
         if (key==1 && selectedlevel<levels) selectedlevel++;
         if (key==2 && selectedlevel>1) selectedlevel--;
         if (key==4)
         {
           lcd.clear();
           selectedlevel--;
           newpoint();
           gamestarted = true;
         }
         else
         {
           lcd.setcursor(14,0);
           lcd.print(selectedlevel);
         }
       }
     }
   }
  }
  if (!gameover && gamestarted)
  {
   skip = false; //skip second moveall() function call if first made

   key_read = keypad.getkey();  // key press
   key = key_read - '0';
   if (key != oldkey)   // if keypress detected
   {
     delay(50);  // wait debounce time
      key_read = keypad.getkey();  // key press
      key = key_read - '0';
     if (key != oldkey)    
     {  
       oldkey = key;
       if (key >=0)
       {
         olddir = head->dir;
         if (key==0 && head->dir!=3) head->dir = 2;
         if (key==1 && head->dir!=1) head->dir = 0;
         if (key==2 && head->dir!=0) head->dir = 1;
         if (key==3 && head->dir!=2) head->dir = 3;

         if (olddir != head->dir)
         {
           skip = true;
           delay(1000/gamespeed);
           moveall();
           drawmatrix();
         }
       }
     }
   }
   if (!skip)
   {
     timenow = millis();
     if (timenow - time > 1000 / gamespeed)
     {
       moveall();
       drawmatrix();
       time = millis();
     }
   }
  }
  if(gameover)
  {

    key_read = keypad.getkey();  // key press
    key = key_read - '0';
   if (key != oldkey)   // if keypress detected
   {
     delay(50);  // wait debounce time
      key_read = keypad.getkey();  // key press
      key = key_read - '0';
     if (key != oldkey)    
     {  
       oldkey = key;
       if (key >=0)
       {
          startf();
       }
     }
   }

  }
}


does know issue?



Arduino Forum > Using Arduino > Displays > Arduino LCD game not displaying


arduino

Comments

Popular posts from this blog

DHT11 Time out error using v0.4.1library

Sketch upload fails with Java error (___REMOVE___/bin/avrdude)!

Arduino Uno + KTY81/210 temperature sensor