#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Game constants and variables
const int WORLD_WIDTH = 16; // Width of the world in blocks
const int WORLD_HEIGHT = 8; // Height of the world in blocks
byte world[WORLD_HEIGHT][WORLD_WIDTH]; // The world array
int playerX = 0; // Player's X position
int playerY = 0; // Player's Y position
void setup() {
// Initialize the display
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
// Initialize the world to empty
for (int y = 0; y < WORLD_HEIGHT; y++) {
for (int x = 0; x < WORLD_WIDTH; x++) {
world[y][x] = 0; // 0 represents an empty space
}
}
// Set up the buttons as inputs
pinMode(2, INPUT_PULLUP); // Up
pinMode(3, INPUT_PULLUP); // Down
pinMode(4, INPUT_PULLUP); // Left
pinMode(5, INPUT_PULLUP); // Right
pinMode(6, INPUT_PULLUP); // A button
pinMode(7, INPUT_PULLUP); // B button
}
void loop() {
// Read the button states
bool up = !digitalRead(2);
bool down = !digitalRead(3);
bool left = !digitalRead(4);
bool right = !digitalRead(5);
bool place = !digitalRead(6);
bool breakBlock = !digitalRead(7);
// Update the player's position based on button presses
if (up) playerY--;
if (down) playerY++;
if (left) playerX--;
if (right) playerX++;
// Place a block
if (place && world[playerY][playerX] == 0) {
world[playerY][playerX] = 1; // 1 represents a block
}
// Break a block
if (breakBlock && world[playerY][playerX] == 1) {
world[playerY][playerX] = 0;
}
// Update the display
display.clearDisplay();
drawWorld();
display.display();
}
void drawWorld() {
for (int y = 0; y < WORLD_HEIGHT; y++) {
for (int x = 0; x < WORLD_WIDTH; x++) {
if (world[y][x] == 1) {
// Draw a block
display.fillRect(x * 8, y * 8, 8, 8, WHITE);
}
}
}
// Draw the player
display.drawBitmap(playerX * 8, playerY * 8, playerBitmap, 8, 8, WHITE);
}