Skip to content

Instantly share code, notes, and snippets.

@mr1337357
Created July 11, 2024 18:59
Show Gist options
  • Save mr1337357/2e658655ffa15cc58de7efcada988b35 to your computer and use it in GitHub Desktop.
Save mr1337357/2e658655ffa15cc58de7efcada988b35 to your computer and use it in GitHub Desktop.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>
#define UP 9
#define DOWN 18
#define LEFT 11
#define RIGHT 7
#define i2c_Address 0x3c //initialize with the I2C addr 0x3C Typically eBay OLED's
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // QT-PY / XIAO
Adafruit_SH1106G display = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
typedef struct sneknode_s
{
int x;
int y;
}
sneknode;
sneknode body[256];
uint8_t snekstart = 0;
uint8_t snekend = 0;
sneknode food = {0,0};
int gameState = 0;
int dir = 0;
void setup() {
// put your setup code here, to run once:
delay(250); // wait for the OLED to power up
display.begin(i2c_Address, true); // Address 0x3C default
//display.setContrast (0); // dim display
display.setTextSize(1);
display.setTextColor(SH110X_WHITE);
display.setCursor(0, 0);
// Clear the buffer.
display.clearDisplay();
display.display();
delay(2000);
display.printf("Snek\n");
display.display();
pinMode(UP,INPUT_PULLUP);
pinMode(DOWN,INPUT_PULLUP);
pinMode(LEFT,INPUT_PULLUP);
pinMode(RIGHT,INPUT_PULLUP);
}
void moveFood()
{
bool valid = false;
uint8_t i;
while(valid == false)
{
food.x = random(0,30);
food.y = random(0,15);
valid = true;
for(i=snekend;i!=(snekstart+1);i++)
{
if((food.x==body[i].x)&&(food.y==body[i].y))
{
valid = false;
}
}
}
}
bool bodyHit(sneknode *head)
{
uint8_t i;
for(i=snekend;i!=(snekstart+1);i++)
{
if((head->x == body[i].x)&&(head->y == body[i].y))
{
return true;
}
}
return false;
}
void move()
{
sneknode head = body[snekstart];
switch(dir)
{
case 0:
head.y -= 1;
if(head.y < 0)
{
gameState = 0;
}
break;
case 1:
head.x += 1;
if(head.x > 30)
{
gameState = 0;
}
break;
case 2:
head.y += 1;
if(head.y > 15)
{
gameState = 0;
}
break;
case 3:
head.x -= 1;
if(head.x < 0)
{
gameState = 0;
}
break;
}
if((head.x == food.x)&&(head.y == food.y))
{
moveFood();
}
else
{
if(bodyHit(&head))
{
gameState = 0;
}
snekend++;
}
snekstart++;
body[snekstart] = head;
}
void draw()
{
uint8_t i;
display.clearDisplay();
switch(gameState)
{
case 0:
display.setTextSize(1);
display.setTextColor(SH110X_WHITE);
display.setCursor(0, 0);
display.printf("snek");
break;
case 1:
for(i=snekend;i!=(snekstart+1);i++)
{
display.drawRect(body[i].x*4, body[i].y*4, 3, 3, SH110X_WHITE);
}
display.drawRect(food.x*4+1, food.y*4+1, 2, 2, SH110X_WHITE);
break;
}
display.display();
}
int last;
void loop() {
int now;
now = millis();
if(now<last+250)
{
return;
}
last = now;
// put your main code here, to run repeatedly:
int buttons = (digitalRead(UP)<<3)|(digitalRead(DOWN)<<2)|(digitalRead(LEFT)<<1)|(digitalRead(RIGHT)<<0);
switch(buttons)
{
case 7:
dir = 0;
break;
case 11:
dir = 2;
break;
case 13:
dir = 3;
break;
case 14:
dir = 1;
break;
default:
/*
display.setCursor(0, 0);
display.clearDisplay();
display.printf("%d\n",buttons);
display.display();
*/
break;
}
switch(gameState)
{
case 0:
if(buttons != 15)
{
gameState = 1;
snekstart = 0;
snekend = 0;
body[snekstart] = {15,8};
moveFood();
}
break;
case 1:
move();
break;
}
draw();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment