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

Arduino Game Code for OLED Display

The document contains code for an interactive game system using an Arduino with various functionalities including a display, servo control, and joystick input. It features multiple mini-games such as Snake, Star Wars, Flappy Bird, and Dino, along with emotional animations for a character named 'Nutty Bot'. The code includes setup and game logic functions, as well as routines for drawing different emotions on the display.

Uploaded by

Girish Upadhyay
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 views7 pages

Arduino Game Code for OLED Display

The document contains code for an interactive game system using an Arduino with various functionalities including a display, servo control, and joystick input. It features multiple mini-games such as Snake, Star Wars, Flappy Bird, and Dino, along with emotional animations for a character named 'Nutty Bot'. The code includes setup and game logic functions, as well as routines for drawing different emotions on the display.

Uploaded by

Girish Upadhyay
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

#include <Wire.

h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Servo.h>

#define SCREEN_WIDTH 128


#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

Servo tail;

const int MPU_addr=0x68;


int16_t AcX, AcY, AcZ;
const int touchPin = 2;

const int joyX = A0, joyY = A1, joySW = 3, speaker = 9;

int globalMode = 0;
int menuIdx = 0;

int touchMode = 0;
unsigned long touchStartTime = 0, exitTimer = 0;
bool isLongTouch = false;
int tearY = 0;

bool introPlayed = false;


unsigned long introStartTime = 0, stateTimer = 0, lastFastBlink = 0, lastBlinkTime
= 0, lookTimer = 0;
int handState = 0, blinkCount = 0, eyeLook = 0;

int shipX = 60, bulletY = -1, enemyX = 30, enemyY = 0;


float birdY = 30, velocity = 0;
int pipeX = 120, gapY = 30;

int dinoY = 45, dinoJump = 0, cactusX = 128, score = 0;

// --- SNAKE GAME VARS ---


int snakeX[20], snakeY[20], snakeLen = 3, foodX = 40, foodY = 30, dirX = 1, dirY =
0;
unsigned long snakeTimer = 0;

// --- MASTER SERVO FUNCTION ---


void moveTail(int speed, int swing, int centerPos = 90, int step = 5) {
static unsigned long lastMove = 0;
static int pos = 90;
static int direction = 1;
if (millis() - lastMove > speed) {
lastMove = millis();
pos += (step * direction);
if (pos >= (centerPos + swing) || pos >= 180 || pos <= (centerPos - swing) ||
pos <= 0) direction *= -1;
[Link](constrain(pos, 0, 180));
}
}

void setup() {
[Link]();
[Link](6);
[Link](90);
pinMode(touchPin, INPUT);
pinMode(joySW, INPUT_PULLUP);
pinMode(speaker, OUTPUT);
[Link](MPU_addr);
[Link](0x6B); [Link](0); [Link](true);
[Link](SSD1306_SWITCHCAPVCC, 0x3C);
introStartTime = millis();
}

// ======================== GAMES LOGIC ========================

void playSnake() {
moveTail(100, 10);
if(analogRead(joyY) < 100) {
if(exitTimer == 0) exitTimer = millis();
if(millis() - exitTimer > 1500) { globalMode = 1; exitTimer = 0; return; }
} else exitTimer = 0;

int xVal = analogRead(joyX), yVal = analogRead(joyY);


if(xVal < 300 && dirX == 0) { dirX = -1; dirY = 0; }
else if(xVal > 700 && dirX == 0) { dirX = 1; dirY = 0; }
else if(yVal < 300 && dirY == 0) { dirY = -1; dirX = 0; }
else if(yVal > 700 && dirY == 0) { dirY = 1; dirX = 0; }

if(millis() - snakeTimer > 150) {


snakeTimer = millis();
for(int i = snakeLen-1; i > 0; i--) { snakeX[i] = snakeX[i-1]; snakeY[i] =
snakeY[i-1]; }
snakeX[0] += dirX * 4; snakeY[0] += dirY * 4;
if(snakeX[0] < 0 || snakeX[0] >= 128 || snakeY[0] < 0 || snakeY[0] >= 64)
{ globalMode = 1; snakeLen = 3; tone(speaker, 200, 300); }
if(abs(snakeX[0]-foodX) < 4 && abs(snakeY[0]-foodY) < 4) {
snakeLen++; if(snakeLen > 19) snakeLen = 19;
foodX = random(2, 25)*4; foodY = random(2, 12)*4; tone(speaker, 1500, 20);
}
}
[Link](foodX, foodY, 4, 4, WHITE);
for(int i=0; i<snakeLen; i++) [Link](snakeX[i], snakeY[i], 4, 4,
WHITE);
}

void playStarWars() {
moveTail(100, 15);
if(analogRead(joyY) < 100) {
if(exitTimer == 0) exitTimer = millis();
if(millis() - exitTimer > 1500) { globalMode = 1; exitTimer = 0; tone(speaker,
400, 200); return; }
} else { exitTimer = 0; }
int xVal = analogRead(joyX);
if(xVal < 400) shipX -= 4; if(xVal > 600) shipX += 4;
shipX = constrain(shipX, 0, 118);
[Link](shipX, 58, 10, 4, WHITE);
[Link](enemyX, enemyY, 3, WHITE);
enemyY += 2; if(enemyY > 64) { enemyY = 0; enemyX = random(10, 110); }
if(digitalRead(joySW) == LOW && bulletY < 0) { bulletY = 55; tone(speaker, 1500,
20); }
if(bulletY >= 0) { [Link](shipX+5, bulletY, 4, WHITE); bulletY -=
5;
if(bulletY <= enemyY && abs((shipX+5)-enemyX) < 6) { enemyY = 0; enemyX =
random(10, 110); bulletY = -1; tone(speaker, 800, 30); }
}
}

void playFlappy() {
moveTail(80, 20);
if(analogRead(joyY) < 100) {
if(exitTimer == 0) exitTimer = millis();
if(millis() - exitTimer > 1500) { globalMode = 1; exitTimer = 0; tone(speaker,
400, 200); return; }
} else { exitTimer = 0; }
velocity += 0.25; birdY += velocity;
if(digitalRead(joySW) == LOW) { velocity = -2.5; tone(speaker, 900, 10); }
[Link](25, birdY, 4, WHITE);
[Link](pipeX, 0, 10, gapY, WHITE);
[Link](pipeX, gapY + 25, 10, 64, WHITE);
pipeX -= 3; if(pipeX < -10) { pipeX = 128; gapY = random(10, 35); }
if(birdY > 64 || birdY < 0 || (pipeX < 30 && pipeX > 15 && (birdY < gapY || birdY
> gapY+25))) {
globalMode = 1; birdY = 30; pipeX = 120; tone(speaker, 150, 500); delay(1000);
}
}

void playDino() {
moveTail(60, 25);
if(analogRead(joyY) < 100) {
if(exitTimer == 0) exitTimer = millis();
if(millis() - exitTimer > 1500) { globalMode = 1; exitTimer = 0; tone(speaker,
400, 200); return; }
} else { exitTimer = 0; }
[Link](0, 55, 128, WHITE);
if(analogRead(joyY) < 100 && dinoY == 45) { dinoJump = 12; tone(speaker, 1200,
10); }
dinoY -= dinoJump; dinoJump -= 2;
if(dinoY > 45) { dinoY = 45; dinoJump = 0; }
[Link](20, dinoY, 8, 10, WHITE);
[Link](cactusX, 47, 6, 8, WHITE);
cactusX -= 4; if(cactusX < -6) { cactusX = 128; score++; }
if(cactusX > 20 && cactusX < 28 && dinoY > 40) { globalMode = 1; cactusX = 128;
score = 0; tone(speaker, 200, 400); delay(1000); }
[Link](100, 0); [Link](score);
}

// ======================== EMOTIONS ========================

void drawNuttyIntro() {
[Link](); [Link](2); [Link](WHITE);
[Link](15, 20); [Link]("Nutty Bot");
long elapsed = millis() - introStartTime;
int progress = map(elapsed, 0, 3000, 0, 100); if (progress > 100) progress = 100;

if(elapsed > 500 && elapsed < 550) tone(speaker, 800, 50);
if(elapsed > 800 && elapsed < 850) tone(speaker, 1000, 50);
if(elapsed > 1100 && elapsed < 1150) tone(speaker, 1200, 50);

[Link](14, 45, 100, 8, WHITE); [Link](16, 47, progress, 4,


WHITE); [Link]();
}
void drawSquareEyes(int x_off, int y_off, bool closed, int look = 0) {
int l_off = (look == 1) ? -10 : (look == 2) ? 10 : 0;
if (closed) { [Link](35 + x_off + l_off, 30 + y_off, 22, 4, WHITE);
[Link](75 + x_off + l_off, 30 + y_off, 22, 4, WHITE); }
else { [Link](35 + x_off + l_off, 20 + y_off, 22, 25, 6, WHITE);
[Link](75 + x_off + l_off, 20 + y_off, 22, 25, 6, WHITE); }
}

void drawNormal() {
moveTail(120, 10);
if (millis() - lookTimer > 1200) {
eyeLook = random(0, 3); lookTimer = millis();
if(eyeLook > 0) { tone(speaker, 600, 30); delay(40); tone(speaker, 800, 30); }
}
bool closed = false; if (millis() - lastBlinkTime > 3000) { closed = true; if
(millis() - lastBlinkTime > 3150) lastBlinkTime = millis(); }
int m_off = (eyeLook == 1) ? -6 : (eyeLook == 2) ? 6 : 0;
drawSquareEyes(0, 0, closed, eyeLook); [Link](64 + m_off, 48, 5,
WHITE); [Link](55 + m_off, 40, 20, 8, BLACK);
}

void drawPetting() {
moveTail(5, 45, 90, 15);
if(millis() % 600 < 50) { tone(speaker, 900, 100); delay(100); tone(speaker,
1200, 150); }
int s = random(-1, 2); [Link](35+s, 25, 50+s, 32, WHITE);
[Link](50+s, 32, 35+s, 40, WHITE);
[Link](92+s, 25, 77+s, 32, WHITE); [Link](77+s, 32, 92+s, 40,
WHITE);
[Link](64+s, 48, 6, WHITE); [Link](55+s, 40, 20, 8, BLACK);
}

void drawSuperLove() {
moveTail(25, 35, 90, 8);
if(millis() % 1000 < 50) { tone(speaker, 1500, 50); delay(60); tone(speaker,
1800, 50); }
int pulse = (millis() / 400) % 2 == 0 ? 2 : 0;
[Link](38-pulse, 25, 8+pulse, WHITE); [Link](52+pulse,
25, 8+pulse, WHITE);
[Link](30-pulse, 28, 60+pulse, 28, 45, 45+pulse, WHITE);
[Link](78-pulse, 25, 8+pulse, WHITE); [Link](92+pulse,
25, 8+pulse, WHITE);
[Link](70-pulse, 28, 100+pulse, 28, 85, 45+pulse, WHITE);
[Link](60, 50, 10, 6, 3, WHITE);
}

void drawHungry() {
moveTail(35, 25, 90, 6);
if(millis() % 2500 < 50) {
tone(speaker, 300, 200); delay(250); tone(speaker, 1000, 20); delay(100);
tone(speaker, 1000, 20); delay(100); tone(speaker, 800, 300);
}
if (handState == 0) {
bool isClosed = (blinkCount % 2 != 0);
if (millis() - lastFastBlink > 500) { lastFastBlink = millis(); blinkCount++;
if (blinkCount > 6) { handState = 1; stateTimer = millis(); blinkCount = 0; } }
drawSquareEyes(0, 0, isClosed); [Link](54, 52, 20, WHITE);
int h = (millis() / 150) % 2 == 0 ? 4 : 0; [Link](110, 45 + h,
12, 8, 2, WHITE); [Link](111, 52 + h, 111, 60 + h, WHITE);
} else {
drawSquareEyes(-20, -8, false); [Link](44, 42, 7, WHITE);
if (millis() - stateTimer > 3000) { handState = 0; stateTimer = millis(); }
int move = (millis() / 200) % 2 == 0 ? 6 : 0; [Link](105 - move,
45, 10, 10, 2, WHITE); [Link](105 - move, 47, 90 - move, 47, WHITE);
}
}

void drawSleepy() {
[Link](180);
if(millis() % 4000 < 100) { tone(speaker, 200, 400); delay(500); tone(speaker,
150, 600); }
drawSquareEyes(0, 5, true); [Link](100, 20 - (millis()/200)%20);
[Link]("Zz"); [Link](54, 55, 20, WHITE);
}

void drawAngry() {
moveTail(10, 25, 150, 15);
if(millis() % 200 < 50) { tone(speaker, random(100, 150), 50); }
int s = random(-2, 3); [Link](35+s, 22+s, 22, 22, 5, WHITE);
[Link](75+s, 22+s, 22, 22, 5, WHITE);
[Link](15+s, 8+s, 50+s, 20+s, WHITE); [Link](113+s, 8+s,
78+s, 20+s, WHITE); [Link](54+s, 55+s, 20, WHITE);
}

void drawCrying() {
[Link](180);
if(millis() % 1500 < 100) { tone(speaker, 400, 100); delay(150); tone(speaker,
300, 300); }
int s = random(-2, 3); [Link](35+s, 30, 22, 4, WHITE);
[Link](75+s, 30, 22, 4, WHITE);
[Link](40+s, 42+tearY, 44+s, 42+tearY, 42+s, 47+tearY, WHITE);
[Link](80+s, 42+tearY, 84+s, 42+tearY, 82+s, 47+tearY, WHITE);
tearY += 2; if(tearY > 15) tearY = 0;
for(int i=0; i<40; i++) { int wave = sin((millis() / 50.0) + (i / 2.0)) * 3;
[Link](44+s+i, 55+wave, WHITE); }
}

void drawLaughing() {
moveTail(50, 20);
if(millis() % 500 < 100) { tone(speaker, 1000, 50); delay(70); tone(speaker,
1000, 50); }
int s = random(-2, 3); [Link](35+s, 24, 50+s, 32, WHITE);
[Link](50+s, 32, 35+s, 40, WHITE); [Link](92+s, 24, 77+s, 32,
WHITE); [Link](77+s, 32, 92+s, 40, WHITE); [Link](54+s,
48, 20, random(5, 12), 4, WHITE);
}

void drawCold() {
moveTail(200, 5);
tone(speaker, random(1000, 1200), 10);
int s = random(-3, 4); drawSquareEyes(s, 0, false); [Link](54+s,
55, 20, WHITE); for(int i=0; i<5; i++) [Link](random(0,128),
random(0,64), WHITE);
}

void drawHacker() {
moveTail(100, 10);
if(millis() % 100 < 20) tone(speaker, random(2000, 4000), 5);
[Link](1); for(int i=0; i<10; i++) { [Link](random(0,
120), random(0, 60)); [Link](random(0, 2)); }
}

void drawSpeedometer() {
moveTail(100, 10);
tone(speaker, random(50, 150), 20);
for (int i=0; i<2; i++) { int cx = (i==0) ? 45 : 85; for (int ang = 180; ang <=
360; ang += 45) { float rad = ang * 0.01745; [Link](cx + cos(rad)*14, 35
+ sin(rad)*14, cx + cos(rad)*18, 35 + sin(rad)*18, WHITE); } [Link](cx-
18, 35, cx+18, 35, WHITE); } float val = (sin(millis() / 1000.0) + 1.0) / 2.0;
float angle = 3.14 + (val * 3.14); [Link](45, 35, 45 + cos(angle)*14, 35
+ sin(angle)*14, WHITE); [Link](85, 35, 85 + cos(angle)*14, 35 +
sin(angle)*14, WHITE); [Link](58, 55); [Link]((int)(val * 180));
}

void drawHot() {
moveTail(150, 10);
if(millis() % 1000 < 200) tone(speaker, 200, 200);
drawSquareEyes(0, 0, false); [Link](115, 15, 7, WHITE); int sw =
(millis() / 100) % 40; [Link](25, 20 + sw, 2, WHITE);
[Link](54, 55, 20, WHITE);
}

void drawWink() {
moveTail(100, 15);
unsigned long c = millis() % 2000; bool wink = (c > 1000);
if(wink && c < 1050) tone(speaker, 2500, 20);
[Link](35, 20, 22, 25, 6, WHITE); if (wink) [Link](75,
30, 22, 4, WHITE); else [Link](75, 20, 22, 25, 6, WHITE);
[Link](64, 48, 5, WHITE); [Link](55, 40, 20, 8, BLACK);
}

void drawMusic() {
moveTail(50, 20);
// --- Happy Birthday to You Melody ---
int melody[] = {262, 262, 294, 262, 349, 330, 262, 262, 294, 262, 392, 349};
int durations[] = {200, 200, 400, 400, 400, 600, 200, 200, 400, 400, 400, 600};

static int noteIdx = 0;


static unsigned long lastNoteTime = 0;

if (millis() - lastNoteTime > durations[noteIdx] + 50) {


noteIdx = (noteIdx + 1) % 12;
tone(speaker, melody[noteIdx], durations[noteIdx]);
lastNoteTime = millis();
}

for(int i=0; i<8; i++) {


int h = 10 + sin((millis() / 250.0) + i) * 20;
[Link](15 + (i * 13), 60 - h, 8, h, WHITE);
}
}

// ======================== LOOP ========================

void loop() {
if (!introPlayed) { if (millis() - introStartTime < 3000) { drawNuttyIntro();
return; } else introPlayed = true; }
[Link]();
if(digitalRead(joySW) == LOW && globalMode == 0) { delay(400); globalMode = 1; }

if(globalMode == 1) {
[Link](90);
[Link](1); [Link](20, 0); [Link]("- SELECT
GAME -");
String opt[] = {"STAR WARS", "FLAPPY BIRD", "DINO RUN", "SNAKE", "EXIT"};
for(int i=0; i<5; i++) { [Link](25, 12 + (i * 10)); if(i ==
menuIdx) [Link]("> "); [Link](opt[i]); }
int yVal = analogRead(joyY);
if(yVal < 300) { menuIdx--; tone(speaker, 1000, 20); delay(200); }
if(yVal > 700) { menuIdx++; tone(speaker, 1000, 20); delay(200); }
if(menuIdx < 0) menuIdx = 4; if(menuIdx > 4) menuIdx = 0;
if(digitalRead(joySW) == LOW) {
delay(300);
if(menuIdx == 0) globalMode = 2;
else if(menuIdx == 1) globalMode = 3;
else if(menuIdx == 2) globalMode = 4;
else if(menuIdx == 3) globalMode = 5; // Snake Mode
else globalMode = 0;
}
}
else if(globalMode == 2) playStarWars();
else if(globalMode == 3) playFlappy();
else if(globalMode == 4) playDino();
else if(globalMode == 5) playSnake();
else {
[Link](MPU_addr); [Link](0x3B);
[Link](false); [Link](MPU_addr, 6, true);
AcX = [Link]()<<8|[Link](); AcY = [Link]()<<8|[Link](); AcZ =
[Link]()<<8|[Link]();
if (digitalRead(touchPin)) { if (touchStartTime == 0) touchStartTime =
millis(); if (millis() - touchStartTime > 1000) isLongTouch = true; }
else { if (touchStartTime > 0) { if (!isLongTouch) touchMode = (touchMode +
1) % 11; touchStartTime = 0; isLongTouch = false; } }
if (isLongTouch) drawPetting(); else if (AcZ < -14000) drawCrying(); else if
(abs(AcX) > 11500 || abs(AcY) > 11500) drawAngry();
else {
switch(touchMode) {
case 0: drawNormal(); break; case 1: drawSuperLove(); break; case 2:
drawSpeedometer(); break; case 3: drawHungry(); break;
case 4: drawLaughing(); break; case 5: drawCold(); break; case 6:
drawHacker(); break; case 7: drawHot(); break;
case 8: drawWink(); break; case 9: drawMusic(); break; case 10:
drawSleepy(); break;
}
}
}
[Link]();
}

You might also like