#include <LiquidCrystal.h>
#define JoyX A0 // Analog pin for joystick X-axis
#define JoyY A1 // Analog pin for joystick Y-axis
#define JoySW 2 // Digital pin for joystick button (SW)
#define PIN_AUTOPLAY 1
#define PIN_READWRITE 10
#define PIN_CONTRAST 12
#define SPRITE_RUN1 1
#define SPRITE_RUN2 2
#define SPRITE_JUMP 3
#define SPRITE_JUMP_UPPER '.'
#define SPRITE_JUMP_LOWER 4
#define SPRITE_TERRAIN_EMPTY ' '
#define SPRITE_TERRAIN_SOLID 5
#define SPRITE_TERRAIN_SOLID_RIGHT 6
#define SPRITE_TERRAIN_SOLID_LEFT 7
#define TERRAIN_WIDTH 16
#define TERRAIN_EMPTY 0
#define TERRAIN_LOWER_BLOCK 1
#define TERRAIN_UPPER_BLOCK 2
// Horizontal position of the boy is now variable
int boyX = TERRAIN_WIDTH / 2;
LiquidCrystal lcd(11, 9, 6, 5, 4, 3);
static char terrainUpper[TERRAIN_WIDTH + 1];
static char terrainLower[TERRAIN_WIDTH + 1];
static bool buttonPushed = false;
const char* greenTips[] = {
"Recycle Today!",
"Save Water!",
"Plant a Tree!",
"Use Less Plastic",
"Protect Nature!"
};
const int tipCount = sizeof(greenTips) / sizeof(greenTips[0]);
unsigned long lastTipTime = 0;
const unsigned long tipInterval = 15000;
void displayGreenTip() {
static int tipIndex = 0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Go Green:");
lcd.setCursor(0, 1);
lcd.print(greenTips[tipIndex]);
tipIndex = (tipIndex + 1) % tipCount;
delay(2000);
lcd.clear();
}
void initializeGraphics() {
static byte graphics[] = {
B01100,B01100,B00000,B01110,B11100,B01100,B11010,B10011,
B01100,B01100,B00000,B01100,B01100,B01100,B01100,B01110,
B01100,B01100,B00000,B11110,B01101,B11111,B10000,B00000,
B11110,B01101,B11111,B10000,B00000,B00000,B00000,B00000,
B11111,B11111,B11111,B11111,B11111,B11111,B11111,B11111,
B00011,B00011,B00011,B00011,B00011,B00011,B00011,B00011,
B11000,B11000,B11000,B11000,B11000,B11000,B11000,B11000
};
for (int i = 0; i < 7; ++i) {
lcd.createChar(i + 1, &graphics[i * 8]);
}
for (int i = 0; i < TERRAIN_WIDTH; ++i) {
terrainUpper[i] = SPRITE_TERRAIN_EMPTY;
terrainLower[i] = SPRITE_TERRAIN_EMPTY;
}
}
void advanceTerrain(char* terrain, byte newTerrain) {
for (int i = 0; i < TERRAIN_WIDTH; ++i) {
char current = terrain[i];
char next = (i == TERRAIN_WIDTH - 1) ? newTerrain : terrain[i + 1];
switch (current) {
case SPRITE_TERRAIN_EMPTY:
terrain[i] = (next == SPRITE_TERRAIN_SOLID) ? SPRITE_TERRAIN_SOLID_RIGHT : SPRITE_TERRAIN_EMPTY;
break;
case SPRITE_TERRAIN_SOLID:
terrain[i] = (next == SPRITE_TERRAIN_EMPTY) ? SPRITE_TERRAIN_SOLID_LEFT : SPRITE_TERRAIN_SOLID;
break;
case SPRITE_TERRAIN_SOLID_RIGHT:
terrain[i] = SPRITE_TERRAIN_SOLID;
break;
case SPRITE_TERRAIN_SOLID_LEFT:
terrain[i] = SPRITE_TERRAIN_EMPTY;
break;
}
}
}
// Modified drawBoy to take dynamic horizontal position
bool drawBoy(byte position,
char* terrainUpper,
char* terrainLower,
unsigned int score,
byte boyColumn)
{
bool collide = false;
char upperSave = terrainUpper[boyColumn];
char lowerSave = terrainLower[boyColumn];
byte upper, lower;
switch (position) {
case 0: upper = lower = SPRITE_TERRAIN_EMPTY; break;
case 1: upper = SPRITE_TERRAIN_EMPTY; lower = SPRITE_RUN1; break;
case 2: upper = SPRITE_TERRAIN_EMPTY; lower = SPRITE_RUN2; break;
case 3:
case 10: upper = SPRITE_TERRAIN_EMPTY; lower = SPRITE_JUMP; break;
case 4:
case 9: upper = SPRITE_JUMP_UPPER; lower = SPRITE_JUMP_LOWER; break;
case 5:
case 6:
case 7:
case 8: upper = SPRITE_JUMP; lower = SPRITE_TERRAIN_EMPTY; break;
case 11: upper = SPRITE_RUN1; lower = SPRITE_TERRAIN_EMPTY; break;
case 12: upper = SPRITE_RUN2; lower = SPRITE_TERRAIN_EMPTY; break;
default: upper = lower = SPRITE_TERRAIN_EMPTY; break;
}
if (upper != SPRITE_TERRAIN_EMPTY) {
terrainUpper[boyColumn] = upper;
collide = (upperSave != SPRITE_TERRAIN_EMPTY);
}
if (lower != SPRITE_TERRAIN_EMPTY) {
terrainLower[boyColumn] = lower;
collide |= (lowerSave != SPRITE_TERRAIN_EMPTY);
}
// Print terrain and score
terrainUpper[TERRAIN_WIDTH] = '\0';
terrainLower[TERRAIN_WIDTH] = '\0';
byte digits = (score > 9999) ? 5 : (score > 999) ? 4 : (score > 99) ? 3 : (score > 9) ? 2 : 1;
char temp = terrainUpper[TERRAIN_WIDTH - digits];
terrainUpper[TERRAIN_WIDTH - digits] = '\0';
lcd.setCursor(0, 0);
lcd.print(terrainUpper);
terrainUpper[TERRAIN_WIDTH - digits] = temp;
lcd.setCursor(0, 1);
lcd.print(terrainLower);
lcd.setCursor(TERRAIN_WIDTH - digits, 0);
lcd.print(score);
// Restore original cells
terrainUpper[boyColumn] = upperSave;
terrainLower[boyColumn] = lowerSave;
return collide;
}
void buttonPush() {
buttonPushed = true;
}
void checkJoystickInput() {
int yValue = analogRead(JoyY);
if (yValue < 400) {
buttonPushed = true;
}
}
void setup() {
pinMode(PIN_READWRITE, OUTPUT);
digitalWrite(PIN_READWRITE, LOW);
pinMode(PIN_CONTRAST, OUTPUT);
digitalWrite(PIN_CONTRAST, LOW);
pinMode(JoySW, INPUT_PULLUP);
pinMode(PIN_AUTOPLAY, OUTPUT);
digitalWrite(PIN_AUTOPLAY, HIGH);
attachInterrupt(digitalPinToInterrupt(JoySW), buttonPush, FALLING);
lcd.begin(16, 2);
initializeGraphics();
}
void loop() {
static byte boyPos = 1; // BOY_POSITION_RUN_LOWER_1
static byte newTerrainType = TERRAIN_EMPTY;
static byte newTerrainDuration = 1;
static bool playing = false;
static bool blink = false;
static unsigned int distance = 0;
// Show green tips every interval
if (millis() - lastTipTime >= tipInterval) {
displayGreenTip();
lastTipTime = millis();
}
// Read joystick button
checkJoystickInput();
// Read joystick X for horizontal movement
int xVal = analogRead(JoyX); // 0…1023
if (xVal < 400 && boyX > 0) {
boyX--;
} else if (xVal > 600 && boyX < TERRAIN_WIDTH - 1) {
boyX++;
}
if (!playing) {
drawBoy(boyPos, terrainUpper, terrainLower, distance >> 3, (byte)boyX);
if (blink) {
lcd.setCursor(0, 0);
lcd.print("Press Start");
}
delay(250);
blink = !blink;
if (buttonPushed) {
initializeGraphics();
boyPos = 1;
playing = true;
buttonPushed = false;
distance = 0;
}
return;
}
// Advance terrain
advanceTerrain(terrainLower, newTerrainType == TERRAIN_LOWER_BLOCK ? SPRITE_TERRAIN_SOLID : SPRITE_TERRAIN_EMPTY);
advanceTerrain(terrainUpper, newTerrainType == TERRAIN_UPPER_BLOCK ? SPRITE_TERRAIN_SOLID : SPRITE_TERRAIN_EMPTY);
if (--newTerrainDuration == 0) {
if (newTerrainType == TERRAIN_EMPTY) {
newTerrainType = (random(3) == 0) ? TERRAIN_UPPER_BLOCK : TERRAIN_LOWER_BLOCK;
newTerrainDuration = 2 + random(10);
} else {
newTerrainType = TERRAIN_EMPTY;
newTerrainDuration = 10 + random(10);
}
}
// Handle jump input
if (buttonPushed) {
if (boyPos <= 2) boyPos = 3; // BOY_POSITION_JUMP_1
buttonPushed = false;
}
// Draw and detect collision
if (drawBoy(boyPos, terrainUpper, terrainLower, distance >> 3, (byte)boyX)) {
playing = false;
} else {
// Update boy animation
if (boyPos == 2 || boyPos == 10) {
boyPos = 1;
} else if ((boyPos >= 5 && boyPos <= 7) && terrainLower[boyX] != SPRITE_TERRAIN_EMPTY) {
boyPos = 11;
} else if (boyPos >= 11 && terrainLower[boyX] == SPRITE_TERRAIN_EMPTY) {
boyPos = 7;
} else if (boyPos == 12) {
boyPos = 11;
} else {
++boyPos;
}
++distance;
// Autoplay control
digitalWrite(PIN_AUTOPLAY,
terrainLower[boyX + 2 < TERRAIN_WIDTH ? boyX + 2 : boyX] == SPRITE_TERRAIN_EMPTY
? HIGH
: LOW
);
}
delay(100);
}