#include <Arduino.h>
#include <U8g2lib.h>
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/U8X8_PIN_NONE);
// Movement pins
#define Wmove 0 // Pin for moving up (increase y)
#define Amove 4 // Pin for moving left (decrease x)
#define Smove 2 // Pin for moving down (decrease y)
#define Dmove 3 // Pin for moving right (increase x)
int x = 0; // Player's x-coordinate (row)
int y = 0; // Player's y-coordinate (column)
// Store image data in program memory (Flash) to save SRAM
static const unsigned char image_ButtonCenter_bits[] PROGMEM = {
0x1c, 0x22, 0x5d, 0x5d, 0x5d, 0x22, 0x1c };
void setup() {
u8g2.begin();
Serial.begin(9600);
// Configure input pins for movement
pinMode(Wmove, INPUT_PULLUP);
pinMode(Amove, INPUT_PULLUP);
pinMode(Smove, INPUT_PULLUP);
pinMode(Dmove, INPUT_PULLUP); }
void drawPlayerIcon(int player_x, int player_y) {
u8g2.clearBuffer();
u8g2.setFontMode(1);
u8g2.setBitmapMode(1);
// Draw the outer frame
u8g2.drawFrame(7, 1, 107, 59);
// Calculate player icon's position dynamically
int player_x_offset = 9 + (player_x * 8); // Horizontal offset
int player_y_offset = 3 + (player_y * 8); // Vertical offset
// Draw the player icon at the current position
u8g2.drawXBMP(player_x_offset, player_y_offset, 7, 7, image_ButtonCenter_bits);
u8g2.sendBuffer(); }
void Movement() {
if (digitalRead(Wmove) == LOW && y < 6) { // 13 columns, y max is 12
y++;
delay(50); // Debounce
}
if (digitalRead(Amove) == LOW && x > 0) {
x--;
delay(50); // Debounce
}
if (digitalRead(Smove) == LOW && y > 0) {
y--;
delay(50); // Debounce
}
if (digitalRead(Dmove) == LOW && x < 12) { // 7 rows, x max is 6
x++;
delay(50); }}
void loop() {
Movement();
drawPlayerIcon(x, y);
Serial.print("Player is at: (");
Serial.print(x + 1);
Serial.print(", ");
Serial.print(y + 1);
Serial.println(")");
delay(100);
}