This website uses cookies to ensure you get the best experience on our website.
Crafting a Galactic Adventure: Coding an OLED Display Game with Arduino
Create an OLED Display Game with Arduino: An Intergalactic Adventure

Crafting a Galactic Adventure: Coding an OLED Display Game with Arduino

In the vast expanse of the coding universe, where creativity meets logic, there's a particular kind of satisfaction that comes from bringing a simple idea to life through code. Today, we'll embark on a journey through the stars, not by rocket, but by coding an exciting mini-game on an OLED display using Arduino. This adventure blends technology with imagination, turning a few lines of code into a galactic battle between good and evil.

The Birth of an Idea

Imagine a time long ago in a galaxy far, far away, where a brave X-Wing fighter faces off against the menacing Death Star. This battle isn't just a scene from a movie; it's the very essence of the game we're about to create. Our battlefield is a tiny 128x64 pixel OLED display, and our weapons of choice are Arduino, the Adafruit GFX library, and the SSD1306 driver.

The inspiration behind this game is simple yet thrilling: create an engaging, fast-paced game that fits in the palm of your hand. It's a story of survival, where the player must dodge enemy fire, launch attacks, and ultimately triumph over the dark forces of the universe.

Assembling the Cast

Every great story has its characters, and in our game, they're represented by a few crucial components. The OLED display, driven by the SSD1306 controller, is the stage where the drama unfolds. The Adafruit GFX library provides the graphical tools needed to draw, animate, and bring our characters to life.

Our code begins by initializing the display and setting up the stage:

#define OLED_RESET 4
#define SCREEN_WIDTH 128 
#define SCREEN_HEIGHT 64

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

This small block of code is like the opening crawl of a Star Wars movie—it's the setup that prepares us for the adventure ahead.

The Heroes and Villains

In any epic tale, there are always heroes and villains. Our hero is a pixelated X-Wing fighter, bravely dodging enemy fire while launching counterattacks. The villain? A menacing Death Star, represented by a series of rapidly descending bullets that the player must avoid.

The game’s variables are the heartbeat of this interstellar conflict:

int projectileX = 0;
int projectileY = 0;
int projectileActive = 0;
int enemyPosition = 8;
int enemyDirection = 0;
int gameRunning = 0;
int score = 0;
int lives = 5;

These variables track everything from the player's position and actions to the enemy's movement, creating a dynamic and challenging gameplay experience.

The Battle Begins

With the scene set and the characters ready, the game kicks off in the setup function. This is where our X-Wing first appears on the screen, ready to take on the Death Star:

display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  
display.clearDisplay();
display.drawBitmap(6, 11, storm, 48, 48, 1);
display.display();

This initial setup displays an iconic image of the X-Wing and Death Star, setting the mood for the impending battle. But this isn’t just a static image; it’s the prelude to an action-packed game where every second counts.

Surviving the Onslaught

As the game progresses, the player must dodge enemy fire, represented by small circles that fall from the top of the screen. Using the OLED’s drawing capabilities, we bring this battle to life:

if (shotFired > 0) { display.drawCircle(enemyX1, enemyY1, 2, 1); enemyX1 -= enemySpeed; }

The player can move left or right using the buttons connected to the Arduino. A well-timed button press could mean the difference between life and death. The player's X-Wing moves, shoots, and dodges in a dance of survival:

if (digitalRead(12) == LOW && playerPosition >= 2) { playerPosition -= 2; }
if (digitalRead(11) == LOW && playerPosition <= 46) { playerPosition += 2; }

But the player isn't just defending—they're attacking too. By pressing a button, the X-Wing fires a projectile that hurtles toward the Death Star, aiming to bring it down:

if (digitalRead(3) == LOW && projectileActive == 0) {
  projectileActive = 1;
  projectileX = 6;
  projectileY = playerPosition + 8;
  beep(1200, 20);
}

This is where the true fun of the game shines through. Each successful hit brings the player closer to victory, while each miss could spell disaster.

The Climax: Game Over?

As in any great story, there’s always a moment of tension, a point where everything hangs in the balance. In our game, it’s the inevitable showdown between the X-Wing and the descending bullets. If the player fails to dodge, they lose a life. If all lives are lost, the game ends:

if (lives == 0) {
  gameRunning = 1;
  display.clearDisplay();
  display.setCursor(7, 10);
  display.println("GAME OVER!");
  display.display();
}

But defeat isn’t the end; it’s a chance to start again, to play better, and to aim for a higher score.

The Conclusion

With this simple yet thrilling game, we’ve turned a handful of lines of code into an intergalactic adventure. The X-Wing’s battle against the Death Star is more than just a game; it’s a testament to the power of coding, where creativity and technology collide to create something truly engaging.

Whether you’re a seasoned coder or a newcomer to the Arduino world, this project shows that with a little imagination, even the smallest screen can hold the biggest adventures. So why not give it a try? Dive into the code, tweak the game, and perhaps even create your own version of this galactic saga.

After all, the galaxy is a vast place, and there are always more adventures waiting to be coded.


Leave a Reply

Your email address will not be published. Required fields are marked *


Math Captcha
− 4 = 1