The Spaceship Game is an interactive and fast-paced arcade game where players control a spaceship to navigate through a field of obstacles, dodge enemies, and earn points by surviving as long as possible or destroying targets. The game combines simple mechanics, immersive sound effects, and a compact setup to deliver an engaging experience.
1. Objective:
The primary goal is to steer the spaceship safely, avoiding collisions while collecting points. Players aim to survive for as long as possible while increasing their score.
2. Controls:
The spaceship is controlled using a joystick.
Players can move up, down, left, or right and use the joystick button for additional actions, such as firing projectiles.
3. Visuals:
The game is displayed on an LCD screen, showcasing the spaceship, obstacles, and current score in a simplified graphical style.
4. Audio Feedback:
A buzzer provides sound effects for key events such as firing, collisions, or level-ups, enhancing the gameplay experience.
5. Gameplay Mechanics:
Obstacles or enemies appear randomly and move toward the spaceship.
Difficulty increases as the game progresses, with faster or more frequent obstacles.
The Spaceship Game is an arcade-style game designed for an ESP32 microcontroller platform, utilizing a joystick for control, an LCD I2C display for visualization, and a buzzer for sound effects. It provides an engaging gameplay experience by combining simple mechanics with interactive feedback.
The game revolves around controlling a spaceship to navigate a field of moving obstacles while attempting to survive as long as possible and achieve a high score. The spaceship can dodge obstacles and shoot projectiles at targets. The LCD screen displays the game environment, while the buzzer adds sound effects for various actions like shooting, collisions, and level progressions.
1. Spaceship Movement:
Controlled using the joystick, allowing movement in two directions (left, right).
Boundaries prevent the spaceship from moving off-screen.
2. Shooting Mechanic:
Players can shoot projectiles by pressing the joystick button.
Successfully hitting an enemy and clears the enemy.
3. Sound Effects:
A quick beep sound plays when :
- the ship is moving
- Shooting: the joystick button is pressed
ESP32 board
Executes game logic, handles inputs from the joystick, and manages output to the LCD and buzzer.
Joystick Module
X-axis and Y-axis: Controls the movement of the spaceship.
Button: Triggers specific actions, such as firing projectiles.
LCD Screen (16x2 or 20x4)
Displays the game environment, including the spaceship and enemies.
Jumper Wires
Jumper wires will be used to make connections between the components.
Breadboard:
A breadboard can be used to create a temporary circuit for testing and prototyping.
1- Joystick to ESP32 :
GND → GND
VCC → 3V3
X-axis (VRx pin ) → GPIO34
Y-axis (VRy pin) → GPIO35
Button (SW pin) → GPIO33
LCD Screen (I2C-based) to ESP32 :
SDA → GPIO21 (I2C Data).
SCL → GPIO22 (I2C Clock).
VCC → 5V
GND → GND
1- Ensure MicroPython is installed on your ESP32
2- Flash your ESP32 with MicroPython using this file esp32-20210902-v1.17.bin
3- Import this two libraries : i2c_lcd and lcd_api for I2C LCD screen
4- Initialize peripherals (LCD, joystick) and game variables (ship position, etc.).
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 |
i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=10000) #initializing the I2C method for ESP32 lcd_snake = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns) lcd_food = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns) xAxis = ADC(Pin(34, Pin.IN)) # créer un objet ADC agissant sur une broche xAxis.atten(xAxis.ATTN_11DB) yAxis = ADC(Pin(35, Pin.IN)) # créer un objet ADC agissant sur une broche yAxis.atten(yAxis.ATTN_11DB) button = Pin(33, Pin.IN, Pin.PULL_UP) ship='^' x=12 rocket='|' enemy1="o" x1=5 enemy2="o" x2=7 enemy3="o" x3=9 enemy4="o" x4=11 enemy5="o" x5=13 enemy6="o" x6=15 |
5- Continuously read joystick input and Update the ship’s position.
1 2 3 4 |
xValue = xAxis.read() # read an analog value yValue = yAxis.read() # read an analog value iUpdate the snake’s position. |
6- If we press the joystick, we launch the rocket
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
f button.value()==0 : # If we press the joystick... # We launch the rocket lcd_rocket1.move_to(x,2) lcd_rocket1.putstr(rocket) lcd_rocket2.move_to(x,1) lcd_rocket2.putstr(rocket) sleep(0.5) # If the rocket hits an enemy, it destroys it. if x==x1 : enemy1='' if x==x2 : enemy2='' if x==x3 : enemy3='' if x==x4 : enemy4='' if x==x5 : enemy5='' if x==x6 : enemy6='' |
7- Refresh the display with updated graphics.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# Show spaceship lcd_ship.move_to(x,3) lcd_ship.putstr(ship) # Show enemies lcd_ship.move_to(x1,0) lcd_ship.putstr(enemy1) lcd_ship.move_to(x2,0) lcd_ship.putstr(enemy2) lcd_ship.move_to(x3,0) lcd_ship.putstr(enemy3) lcd_ship.move_to(x4,0) lcd_ship.putstr(enemy4) lcd_ship.move_to(x5,0) lcd_ship.putstr(enemy5) lcd_ship.move_to(x6,0) lcd_ship.putstr(enemy6) sleep(0.5) lcd_ship.clear() |
Here is the full program :
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 92 93 94 95 96 97 98 99 100 101 102 103 |
import machine from machine import Pin, SoftI2C,ADC from lcd_api import LcdApi from i2c_lcd import I2cLcd from time import sleep I2C_ADDR = 0x27 totalRows = 4 totalColumns = 20 #initializing the I2C method for ESP32 i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=10000) lcd_ship = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns) lcd_enemy1 = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns) lcd_enemy2 = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns) lcd_enemy3 = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns) lcd_enemy4 = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns) lcd_enemy5 = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns) lcd_enemy6 = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns) lcd_rocket1 = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns) lcd_rocket2 = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns) # Connect the joystick pins to the ESP32 board xAxis = ADC(Pin(34, Pin.IN)) xAxis.atten(xAxis.ATTN_11DB) yAxis = ADC(Pin(35, Pin.IN)) yAxis.atten(yAxis.ATTN_11DB) button = Pin(33, Pin.IN, Pin.PULL_UP) ship='^' x=12 rocket='|' enemy1="o" x1=5 enemy2="o" x2=7 enemy3="o" x3=9 enemy4="o" x4=11 enemy5="o" x5=13 enemy6="o" x6=15 while True: # Read the analog values returned by the Joystick xValue = xAxis.read() yValue = yAxis.read() # If we move the joystick to the right if yValue<1000 : # the ship moves to the right x=x+1 # If we move the joystick to the left if yValue==4095 : # the ship moves to the right x=x-1 if button.value()==0 : # If we press the joystick... # We launch the rocket lcd_rocket1.move_to(x,2) lcd_rocket1.putstr(rocket) lcd_rocket2.move_to(x,1) lcd_rocket2.putstr(rocket) sleep(0.5) # If the rocket hits an enemy, it destroys it. if x==x1 : enemy1='' if x==x2 : enemy2='' if x==x3 : enemy3='' if x==x4 : enemy4='' if x==x5 : enemy5='' if x==x6 : enemy6='' # Show spaceship lcd_ship.move_to(x,3) lcd_ship.putstr(ship) # Show enemies lcd_ship.move_to(x1,0) lcd_ship.putstr(enemy1) lcd_ship.move_to(x2,0) lcd_ship.putstr(enemy2) lcd_ship.move_to(x3,0) lcd_ship.putstr(enemy3) lcd_ship.move_to(x4,0) lcd_ship.putstr(enemy4) lcd_ship.move_to(x5,0) lcd_ship.putstr(enemy5) lcd_ship.move_to(x6,0) lcd_ship.putstr(enemy6) sleep(0.5) lcd_ship.clear() |
The Spaceship Game is not just a fun and challenging arcade game. it’s also an excellent project for learning MicroPython, embedded systems, and game design!
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