Arduino Spaceship game

Arduino UNO 13-02-25
57 0

Tutorial plan

1- Presentation of the Spaceship game

2- Objective of our game

3- Required Components

4- Component Wiring

5- Arduino program

 

 

Presentation of the Spaceship game

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.

 

Objective of our game

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.

 

Required Components

Arduino UNO

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)

Pinouts of 1602 LCD display with I2C

 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

plaque d'essai

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

Fils de connexion

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.

 

Component Wiring

Joystick Module:

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).

LCD Screen:

Connect SDA to A4, SCL to A5, VCC to 5V, and GND to GND.

Buzzer:

Connect the positive pin to a digital output pin (e.g., D3) on Arduino.

Connect the negative pin to GND.

 

Arduino program

 

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 :

LiquidCrystal_I2C.h: Controls the LCD I2C display to show text and game graphics.

2. LCD and Pin Configuration

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

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)

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)

The game runs continuously until gameOver becomes true.

Spaceship Movement

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

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

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

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

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.

0 comment

Leave a comment

Veuillez noter s'il vous plaît*

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Purpose of this website

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.

Contact details

Zaouiet Kontech-Jemmel-Monastir-Tunisia

+216 92 886 231

medaliprof@gmail.com

Pictures of the articles

Robotic site created by MedAli-Teacher info