Table of Contents

Marius-Vlad COŢOFANĂ - Little Snake

Autorul poate fi contactat la adresa: Login pentru adresa

Introducere

Mi-am propus sa realizez un joc clasic, distractiv, de tip Snake.

8290946.jpg

De ce ?

Este un proiect interesant deoarece permite familiarizarea cu programarea embedded într-un mod practic și distractiv (fiind un joc).

Scopul ?

Jocul ofera utilizatorilor sansa sa retraiasca jocurile video ale copilariei, cand nu existau ecrane color si butoanele erau fizice.

Ideea ?

Ideea mi-a venit de la faimoasele Brick games care integrau o multime de jocuri, folosind doar cateva butoane si un ecran mic. Mereu m-am intrebat cu functioneaza un astfel de 'device', iar acum chiar am ocazia sa construiesc unul.

Descriere generală

qr16dy.jpg Link

Hardware Design

2mzvzi8.jpg

Software Design

Pentru compilare, pe Windows, folositi Cygwin, pentru a putea folosi Makefile-ul si compilatorul nvr-gcc, apoi incarcati programul pe placuta, folosind bootloader-ul din laborator.

Exemplu de cod (functia main):

int main(void) {

double axg = 0, ayg = 0, azg = 0;
double gxds = 0, gyds = 0, gzds = 0;

//init mpu6050
mpu6050_init();
_delay_ms(50);

LCD_init();
LCD_clear();

put_food();

// INtro
while (1) {
  // Read current parameters
  mpu6050_getConvData(&axg, &ayg, &azg, &gxds, &gyds, &gzds);

  LCD_write_string(0,0," Little Snake ");
  LCD_write_string(0,3," >> Move << ");
  if (axg > LIMIT || axg < -LIMIT ||ayg < -LIMIT || ayg > LIMIT)
    break;
}

// Init snake, both on screen and array
screen[3][5] = SNAKE;
screen[3][6] = SNAKE;
screen[3][7] = SNAKE;
snake[0].x = 3; snake[0].y = 5;
snake[1].x = 3; snake[1].y = 6;
snake[2].x = 3; snake[2].y = 7;

snake_tail = 0; snake_len = 3;
for (int z=snake_len; z<MAX_LEN; z++) snake[z].x = -1;


while(game) {

  // Read current parameters
  mpu6050_getConvData(&axg, &ayg, &azg, &gxds, &gyds, &gzds);

  // Take a decision
  if(axg > LIMIT) {if (direction != UP) direction = DOWN;}
  else if(axg < -LIMIT) {if (direction != DOWN) direction = UP;}
  if(ayg > LIMIT) {if (direction != LEFT) direction = RIGHT;}
  else if(ayg < -LIMIT) {if (direction != RIGHT) direction = LEFT;}

  // Make it happen
  change_dir();

  // Check end game
  if (!game) break;

  LCD_init();
  LCD_clear();

  // Print entire screen
  for (int x=0; x<HEIGHT; x++)
  for (int y=0; y<WIDTH; y++) {
    if (x == snake[snake_len-1].x && y == snake[snake_len-1].y) {
      LCD_write_char(HEAD);
    } else if (screen[x][y] == SNAKE) {
      LCD_write_char(SNAKE);
    } else if (screen[x][y] == FOOD) {
      LCD_write_char(FOOD);
    } else {
      LCD_write_char(SPACE);
    }
  }

  _delay_ms(250);
}
}

Intregul cod este disponibil mai jos, in sectiunea de Download.

Rezultate Obţinute

img_1031.jpg

img_1032.jpg

Concluzii

Ecranul ales de mine s-a dovedit cam mic pentru un joc de tipul acesta. In plus, folosirea bitmap-urilor pentru implementarea a fost prea dificila, asa ca am folosit caractere ASCII pentru reprezentarea sarpelui si a mancarii sale. Cu toate acestea jocul se misca excelent. :)

img_1033.jpg

Excelent proiectul, mi-ar fi placut sa lucram la el inca de la primele laboratoare si sa adaugam componente pe masura ce avansam cu materia.

Proiectul este complet si 100% functional! 8-)

Download

Codul folosit este open-source, distribuit sub o licenta MIT si disponibil oricui, pe GitHub, la acest repo.

Bibliografie/Resurse

Datasheet accelerometru

Schema accelerometru

Informatii modul LCD