0% found this document useful (0 votes)
6 views4 pages

Pre Code

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Pre Code

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <SPI.

h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>

#define i2c_Address 0x3C // I2C address of the OLED display


#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define MAX_LENGTH 32 // Maximum length of the snake

bool moveLeftFlag = false; // Flag for left movement state


bool moveRightFlag = false; // Flag for right movement state
bool moveUpFlag = false; // Flag for up movement state
bool moveDownFlag = false; // Flag for down movement state

Adafruit_SH1106G display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

class Player {
private:
int x; // X position of the player pixel
int y; // Y position of the player pixel
int tailX[MAX_LENGTH]; // X positions of the snake's body segments
int tailY[MAX_LENGTH]; // Y positions of the snake's body segments
int tailLength; // Current length of the snake

public:
Player(int initialX, int initialY) {
x = initialX;
y = initialY;
tailLength = 0;
}

void moveLeft() {
clearDisplay();
if (x > 0) {
x--; // Move left
}
drawPlayer();
}

void moveRight() {
clearDisplay();
if (x < SCREEN_WIDTH - 1) {
x++; // Move right
}
drawPlayer();
}

void moveUp() {
clearDisplay();
if (y > 0) {
y--; // Move up
}
drawPlayer();
}

void moveDown() {
clearDisplay();
if (y < SCREEN_HEIGHT - 1) {
y++; // Move down
}
drawPlayer();
}

void clearDisplay() {
display.clearDisplay();
}

void drawPlayer() {
// Draw the head of the snake
display.drawPixel(x, y, SH110X_WHITE);

// Draw the body of the snake


for (int i = 0; i < tailLength; i++) {
display.drawPixel(tailX[i], tailY[i], SH110X_WHITE);
}
}

int getX() {
return x;
}

int getY() {
return y;
}

void updateTail() {
// Move the body segments of the snake
for (int i = tailLength - 1; i > 0; i--) {
tailX[i] = tailX[i - 1];
tailY[i] = tailY[i - 1];
}

// Update the first body segment to the previous position of the head
if (tailLength > 0) {
tailX[0] = x;
tailY[0] = y;
}
}

void increaseLength() {
if (tailLength < MAX_LENGTH) {
tailX[tailLength] = x; // Add current head position to tailX array
tailY[tailLength] = y; // Add current head position to tailY array
tailLength++; // Increase the tail length
}
}
};

class Apple {
private:
int x; // X position of the apple
int y; // Y position of the apple

public:
Apple() {
spawn(); // Spawn the apple at a random position
}
void spawn() {
x = random(SCREEN_WIDTH);
y = random(SCREEN_HEIGHT);
}

int getX() {
return x;
}

int getY() {
return y;
}

void draw() {
// Draw the apple
display.drawPixel(x, y, SH110X_WHITE);
}
};

Player player(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2); // Create player at the center


of the screen
Apple apple; // Create an instance of the
Apple class

void setup() {
Serial.begin(115200);
display.begin(i2c_Address, true);
display.clearDisplay();
player.drawPlayer();
randomSeed(analogRead(A2));
apple.spawn();
display.drawPixel(apple.getX(), apple.getY(), SH110X_WHITE);
display.display();
}

void loop() {
if (player.getX() == apple.getX() && player.getY() == apple.getY()) {
// Respawn the apple at a new random position
apple.spawn();
player.increaseLength();
}

if (Serial.available()) {
unsigned char input = Serial.read();
if (input == 'a') {
movementFlags(true, false, false, false); // Set left movement flag to true
} else if (input == 'd') {
movementFlags(false, true, false, false); // Set right movement flag to true
} else if (input == 'w') {
movementFlags(false, false, true, false); // Set up movement flag to true
} else if (input == 's') {
movementFlags(false, false, false, true); // Set down movement flag to true
}
}

// Move the player based on the movement flags


if (moveLeftFlag) {
player.moveLeft();
} else if (moveRightFlag) {
player.moveRight();
} else if (moveUpFlag) {
player.moveUp();
} else if (moveDownFlag) {
player.moveDown();
}

player.updateTail();
apple.draw();
display.display();
}

void movementFlags(bool left, bool right, bool up, bool down) {


moveLeftFlag = left;
moveRightFlag = right;
moveUpFlag = up;
moveDownFlag = down;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy