#include "pico/stdlib.h"
#include <stdio.h>
#define BUTTON 16
#define BUTTON2 19
#define BUTTON3 17
#define LED 13
#define LED2 9
#define UP 72
#define DOWN 80
#define LEFT 75
#define RIGHT 77
// #define WIDTH 5
// #define HEIGHT 5
// debouncer variables
static const uint8_t DEBOUNCE = 500; // debouncer delay
static bool pressed = false;
static int32_t alarm_id = 0;
int length;
int bend_no;
int len;
int life;
int move; //move = 1 up, move = 2 down, move = 3 left
struct cord {
int x;
int y;
int dir;
};
typedef struct cord cordinate;
cordinate head, bend[500], food, body[30];
//draw board
void draw(int *dim)
{
for (int i = 0; i < *dim; i++) {
for (int j = 0; j < *dim; j++) {
if (i == 0 || i == *dim - 1
|| j == 0
|| j == *dim - 1) {
printf("#");
}
else {
printf(" ");
}
}
printf("\n");
}
}
//display refresh
void cls() {
int n;
for (n = 0; n < 50; n++) {
// clear enter screen
printf("\n");
}
//locate at 1,1
printf("\e[%d;%df", 1, 1);
}
// button functions
int64_t enable_button(alarm_id_t alarm_id, void *user_data) {
pressed = false;
return 0;
}
//button callback function
void button_callback(uint gpio, uint32_t events) {
if (pressed) {
cancel_alarm(alarm_id);
}
else {
pressed = true;
if (gpio == BUTTON) {
move = 1; //move up
}
else if (gpio == BUTTON2) {
move = 2; //move down
}
else if (gpio == BUTTON3) {
move = 3; //move left
}
alarm_id = add_alarm_in_ms(DEBOUNCE, enable_button, NULL, false);
}
}
int main() {
stdio_init_all(); //intialize
printf("LOADING GAME.........");
sleep_ms(50); //delay
//gpio initialization
gpio_init(LED); // initialize LEDs
gpio_init(LED2);
gpio_init(BUTTON); // initialize button
gpio_init(BUTTON2);
gpio_set_dir(BUTTON, false); // set direction to input
gpio_set_dir(BUTTON2, false);
gpio_set_dir(BUTTON2, false);
gpio_set_dir(LED, true); //set direction to output
gpio_set_dir(LED2, true);
gpio_pull_down(BUTTON); // pull down resistor
gpio_pull_down(BUTTON2);
gpio_pull_down(BUTTON3);
gpio_set_irq_enabled_with_callback(
BUTTON, 0x04, 1, button_callback); // attach interrupt to button
gpio_set_irq_enabled(BUTTON2, GPIO_IRQ_EDGE_FALL,
true); // attach intr to button 2
//intial characterstics of the snake
length = 5;
head.x = 25;
head.y = 20;
int dim = 30;
//initialize array
for (int i = 0; i < 30; i++)
{
body[i].x = 0;
body[i].y = 0;
if (i == length)
break;
}
//draw board
draw(&dim);
while (true) {
draw(&dim);
sleep_ms(500);
cls();
}
return 0;
}