A Spaceship Game is a type of video game where the player controls a spaceship, often navigating through space while avoiding obstacles, battling enemies, and completing missions. The main objective usually involves shooting enemies, dodging hazards, and surviving as long as possible.
1- Spaceship Control:
The player uses controls (like a joystick) to move the spaceship up, down, left, and right.
A button is often used to fire rockets or lasers at enemies.
2- Enemies and Obstacles:
The player faces enemy ships, asteroids, or other space hazards.
Enemies may move in patterns, shoot back, or become faster over time.
3- Weapons and Shooting:
The spaceship fires projectiles (like rockets or lasers) to destroy enemies.
Some games have power-ups to upgrade weapons.
4- Scoring System:
Points are earned by destroying enemies and surviving longer.
High scores motivate players to improve their performance.
5- Lives and Health:
The spaceship may have a limited number of lives or a health bar.
The game ends when all lives are lost.
6- Levels and Difficulty:
As the player progresses, levels become more difficult with faster enemies, more obstacles, and challenging bosses.
The objective of the Spaceship Game is to control a spaceship using a joystick, displayed on an LCD I2C screen, while avoiding obstacles and shooting down enemies. The game provides an interactive experience by incorporating a buzzer for sound effects and a scoring system to track the player's progress.
Control the Spaceship:
The player moves the spaceship left and right using the joystick.
The spaceship continuously moves forward on the LCD screen.
Shoot Rockets:
The player can press the joystick button to fire rockets at incoming enemy ships.
Each successful hit earns points.
Avoid Collisions:
Enemy ships and obstacles move toward the spaceship.
If the spaceship collides with an enemy or an obstacle, the player loses a life.
Scoring System:
The player gains points by destroying enemies.
A high score feature can be added to track the best performance.
Game Over Condition:
The player has a limited number of lives.
When all lives are lost, the game ends, and the final score is displayed.
Sound Effects with Buzzer:
The buzzer plays a sound when:
The spaceship fires a rocket.
An enemy is destroyed.
The spaceship collides with an enemy or obstacle.
Arduino UNO
The Arduino UNO is the main microcontroller board that controls all the game logic, processes player inputs, updates the display, and triggers sound effects.
Role in the Game:
- Reads input from the joystick.
- Controls the LCD I2C screen to display the spaceship, enemies, bullets, and score.
- Sends signals to the buzzer for sound effects.
LCD I2C Screen (16x2 or 20x4)
The LCD I2C display shows game graphics like the spaceship, enemies, bullets, and score.
Role in the Game:
- Displays the spaceship movement.
- Shows enemies approaching.
- Updates the score and game over messages.
Joystick Module
The joystick is an analog control device with two axes (X and Y) for movement and a push button for firing rockets.
Role in the Game:
- Move the spaceship left and right.
- Shoot rockets to destroy enemies.
Buzzer
A small electronic component that generates sound effects during the game.
Role in the Game:
1- Plays sounds when the player fires a rocket.
2- Collision alert when the spaceship hits an enemy.
3- Game over sound when the player loses.
Breadboard
A tool for prototyping circuits without soldering.
Role in the Game:
Connects the joystick, buzzer, and I2C display to the Arduino UNO.
Simplifies circuit management.
Jumper Wires
Flexible wires used to create temporary electrical connections on the breadboard.
Role in the Game:
Connect VCC, GND, signal pins from components to the Arduino UNO.
Ensures stable connections for reliable gameplay.
Connect VCC to 5V on Arduino.
Connect GND to GND on Arduino.
Connect X-axis to A0 and Y-axis to A1 on Arduino.
Connect the joystick button to a digital pin (e.g., D2).
Connect SDA to A4, SCL to A5, VCC to 5V, and GND to GND.
Connect the positive pin to a digital output pin (e.g., D3) on Arduino.
Connect the negative pin to GND.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
// 🚀 Spaceship Game using Arduino UNO, LCD I2C, Joystick, and Buzzer #include <LiquidCrystal_I2C.h> // LCD I2C Configuration LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 columns, 2 rows // Pin Definitions const int joystickX = A0; // Joystick X-axis const int buttonPin = 2; // Joystick button const int buzzerPin = 3; // Buzzer pin // Game Variables int spaceshipPos = 7; // Initial spaceship position (middle) int enemyPos = 0; // Initial enemy position int enemyRow = 0; // Enemy appears on the first row int score = 0; // Player score bool gameOver = false; // Game over flag void setup() { pinMode(buttonPin, INPUT_PULLUP); pinMode(buzzerPin, OUTPUT); lcd.begin(16, 2); lcd.backlight(); lcd.clear(); lcd.print("Spaceship Game!"); delay(2000); lcd.clear(); } void loop() { if (!gameOver) { lcd.clear(); // Read joystick input int xValue = analogRead(joystickX); bool buttonPressed = !digitalRead(buttonPin); // Move spaceship left or right if (xValue < 400 && spaceshipPos > 0) { spaceshipPos--; } else if (xValue > 600 && spaceshipPos < 15) { spaceshipPos++; } // Draw spaceship lcd.setCursor(spaceshipPos, 1); lcd.print("^"); // Move enemy down enemyRow++; if (enemyRow > 1) { enemyRow = 0; enemyPos = random(0, 16); } // Draw enemy lcd.setCursor(enemyPos, enemyRow); lcd.print("*"); // Check collision if (enemyRow == 1 && enemyPos == spaceshipPos) { tone(buzzerPin, 1000, 200); gameOver = true; lcd.clear(); lcd.print("Game Over!"); lcd.setCursor(0, 1); lcd.print("Score: "); lcd.print(score); delay(3000); } // Shooting mechanism if (buttonPressed) { tone(buzzerPin, 2000, 100); // Shooting sound if (enemyRow == 0 && enemyPos == spaceshipPos) { score += 10; enemyPos = random(0, 16); enemyRow = 0; } } // Display score lcd.setCursor(0, 0); lcd.print("Score:"); lcd.print(score); delay(200); } } |
This Arduino program controls a simple Spaceship Game using an Arduino UNO, LCD I2C screen, joystick, buzzer, breadboard, and jumper wires. Let’s break down the code section by section:
1. Including Libraries and Initial Setup :
1 |
#include <LiquidCrystal_I2C.h> |
LiquidCrystal_I2C.h
: Controls the LCD I2C display to show text and game graphics.
2. LCD and Pin Configuration
1 2 3 4 5 |
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 columns, 2 rows const int joystickX = A0; // Joystick X-axis const int buttonPin = 2; // Joystick button const int buzzerPin = 3; // Buzzer pin |
LCD Initialization: The LCD has 16 columns and 2 rows, with I2C address 0x27
.
Joystick & Buzzer Pins: The X-axis controls movement, and the button pin fires rockets. The buzzer is connected to pin 9.
3. Game Variables
1 2 3 4 5 |
int spaceshipPos = 7; // Spaceship starts at the center int enemyPos = 0; // Enemy's initial horizontal position int enemyRow = 0; // Enemy starts from the top row int score = 0; // Player's score starts at 0 bool gameOver = false; // Tracks if the game is over |
Spaceship Position: Starts in the middle of the screen (position 7
on the bottom row).
Enemy Position: Randomized to make the game dynamic.
Score & Game Over Flag: Tracks the player's score and stops the game when needed.
4. setup()
Function (Initial Configuration)
1 2 3 4 5 6 7 8 9 10 11 |
void setup() { pinMode(buttonPin, INPUT_PULLUP); pinMode(buzzerPin, OUTPUT); lcd.begin(16, 2); lcd.backlight(); lcd.clear(); lcd.print("Spaceship Game!"); delay(2000); lcd.clear(); } |
Pin Modes:
INPUT_PULLUP
for the joystick button to prevent floating values.
OUTPUT
for the buzzer to generate sounds.
LCD Setup: Displays “Spaceship Game!” for 2 seconds as a welcome message.
5. loop()
Function (Main Game Logic)
1 2 3 |
void loop() { if (!gameOver) { lcd.clear(); |
The game runs continuously until gameOver
becomes true.
Spaceship Movement
1 2 3 4 5 6 7 8 9 10 |
int xValue = analogRead(joystickX); bool buttonPressed = !digitalRead(buttonPin); if (xValue < 400 && spaceshipPos > 0) { spaceshipPos--; } else if (xValue > 600 && spaceshipPos < 15) { spaceshipPos++; } lcd.setCursor(spaceshipPos, 1); lcd.print("^"); |
Joystick Reading: Reads analog values from the X-axis.
Left Movement: If X < 400, the spaceship moves left. Right Movement: If X > 600, it moves right.
Spaceship Symbol: Displayed as ^
on the second row (bottom).
Enemy Spawning and Movement
1 2 3 4 5 6 7 |
enemyRow++; if (enemyRow > 1) { enemyRow = 0; enemyPos = random(0, 16); } lcd.setCursor(enemyPos, enemyRow); lcd.print("*"); |
Enemy Movement: The enemy moves downward row by row.
Random Position: When the enemy reaches the bottom, it reappears at a random horizontal position (0 to 15
).
Collision Detection
1 2 3 4 5 6 7 8 9 10 |
if (enemyRow == 1 && enemyPos == spaceshipPos) { tone(buzzerPin, 1000, 200); gameOver = true; lcd.clear(); lcd.print("Game Over!"); lcd.setCursor(0, 1); lcd.print("Score: "); lcd.print(score); delay(3000); } |
Collision Check: If the enemy's position matches the spaceship’s position on the bottom row, a collision occurs.
Game Over: Displays the message and score, along with a buzzer sound (1000 Hz
).
Shooting Mechanism
1 2 3 4 5 6 7 8 |
if (buttonPressed) { tone(buzzerPin, 2000, 100); // Shooting sound if (enemyRow == 0 && enemyPos == spaceshipPos) { score += 10; enemyPos = random(0, 16); enemyRow = 0; } } |
Button Press: When the joystick button is pressed, the spaceship fires a rocket.
Enemy Hit: If the enemy is in the same column as the spaceship when fired, the player scores 10 points.
Sound Effect: Plays a high-pitched shooting sound (2000 Hz
).
Displaying the Score
1 2 3 4 5 6 |
lcd.setCursor(0, 0); lcd.print("Score:"); lcd.print(score); delay(200); } } |
The score is continuously updated at the top-left corner of the screen.
A short delay (200 ms) slows down the game loop for better control.
6. Buzzer Sounds Summary
Shooting: tone(buzzerPin, 2000, 100)
→ High-pitched quick beep.
Collision: tone(buzzerPin, 1000, 200)
→ Lower-pitched longer beep for alerts.
Educational robotics refers to the use of robots and robotics technology to promote learning in educational settings. It involves the integration of technology, engineering, and computer science into the classroom, allowing students to engage in hands-on, project-based learning experiences.
In this context, our website represents an excellent resource for parents, teachers and children who wish to discover robotics.
Zaouiet Kontech-Jemmel-Monastir-Tunisia
+216 92 886 231
medaliprof@gmail.com
Robotic site created by MedAli-Teacher info