#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <pico/stdlib.h>
#include <hardware/spi.h>
#include <stdbool.h>
#include <math.h>
#include "ssd1306.h"
typedef struct vector {int x;int y;}vector2D;
typedef struct coordinate {int x;int y;}coord;
void vector_overwrite(struct vector*,int,int);
int main(){
// Create the buffer and fill it with 0s
int screenbuffer[30][30];
for (int y = 0; y < 30; y++ ){
for (int x = 0; x < 30; x++ ){
screenbuffer[x][y] = 0;
}
}
// Initiate the variables
coord snake_pos = {3,3};
int snake_len = 3;
vector2D snake_dir = {0,1};
coord snake_body[15];
snake_body[0] = (coord) {1,3}; snake_body[1] = (coord) {2,3}; snake_body[2] = (coord) {3,3};
int ascii_input;
// Main loop
while (true){
// Showing the content
for (int y = 0; y < 30; y++ ){
for (int x = 0; x < 30; x++ ){
printf("%d",screenbuffer[x][y]);
}
printf("\n");
}
printf("\n");
// snake_pos and snake_body operations
snake_pos.x += snake_dir.x; snake_pos.y += snake_dir.y; // move the snake_pos depending on direction
snake_body[snake_len] = snake_pos; // add snake_pos in front of the body
// Apply front-most and back-most parts on the screenbuffer
screenbuffer[snake_pos.x][snake_pos.y] = 1; // set the front-most part of the snake as "filled"
screenbuffer[snake_body[0].x][snake_body[0].y] = 0; // set the back-most part of the snake to be erased
for (int i = 0; i < snake_len;i++ ){ // shift every element backwards by 1
snake_body[i] = snake_body[i+1];
}
//? Controls
if (kbhit()){
ascii_input = getch();
switch (ascii_input){
case 119: // w
vector_overwrite(&snake_dir, 0, -1);
break;
case 97: // a
vector_overwrite(&snake_dir, -1, 0);
break;
case 115: // s
vector_overwrite(&snake_dir, 0, 1);
break;
case 100: // d
vector_overwrite(&snake_dir, 1, 0);
break;
}
}
Sleep(200);
}
return 0;
};
void vector_overwrite(struct vector* direction,int x, int y){
direction->x = x;
direction->y = y;
return;
};
uint8_t ssd1306_get_pixel(ssd1306_t *p, uint32_t x, uint32_t y){
const uint8_t p = (p->buffer[x+p->width*(y>>3)]>>(y&0x7))&1;
return p;