Devices & Components
Stereo Enclosed Speaker - 3W 8Ω
Box 180 mixed buttons
40 colored male-male jumper wires
Arduino Uno Rev3
Box 525 1% precision resistors - 17 values
16x2 LCD display with I²C interface
Software & Tools
Arduino IDE
Project description
Code
Space Racer code
csharp
asteroids wont despawn if hit +player glides right
1#include <LiquidCrystal.h> 2 3// Constants 4const int R_BUTTON = 3; 5const int L_BUTTON = 2; 6const int SPEAKER = 4; 7 8const int RS = 8; 9const int E = 9; 10const int DB4 = 10; 11const int DB5 = 11; 12const int DB6 = 12; 13const int DB7 = 13; 14 15LiquidCrystal lcd(RS, E, DB4, DB5, DB6, DB7); 16 17// Custom characters 18byte ship[8] = { 19 B00010, 20 B00110, 21 B01110, 22 B11111, 23 B01110, 24 B00110, 25 B00010, 26 B00000 27}; 28 29byte asteroid[8] = { 30 B00110, 31 B01111, 32 B11101, 33 B11011, 34 B11111, 35 B01110, 36 B00100, 37 B00000 38}; 39 40byte flameA[8] = { 41 B00000, 42 B00001, 43 B00011, 44 B00111, 45 B00011, 46 B00001, 47 B00000, 48 B00000 49}; 50 51byte flameB[8] = { 52 B00000, 53 B11000, 54 B11100, 55 B11110, 56 B11100, 57 B11000, 58 B00000, 59 B00000 60}; 61 62// Ship row: 0 = left, 1 = right 63int shipRow = 1; 64int lastShipRow = 1; 65 66// Asteroid 67int asteroidCol = 0; 68int asteroidRow = 0; 69 70void setup() 71{ 72 Serial.begin(9400); 73 lcd.begin(16,2); 74 75 pinMode(L_BUTTON, INPUT_PULLUP); 76 pinMode(R_BUTTON, INPUT_PULLUP); 77 78 lcd.createChar(0, ship); 79 lcd.createChar(1, asteroid); 80 lcd.createChar(2, flameA); 81 lcd.createChar(3, flameB); 82 83 lcd.clear(); 84 85 randomSeed(analogRead(A0)); 86} 87 88void loop() 89{ 90 // --- BUTTON INPUT --- 91 int rState = digitalRead(R_BUTTON); 92 int lState = digitalRead(L_BUTTON); 93 94 if (rState == LOW) shipRow = 1; 95 if (lState == LOW) shipRow = 0; 96 97 // --- ASTEROID LOGIC (LEFT → RIGHT) --- 98 99 // Respawn asteroid when it reaches ship area 100 if (asteroidCol >= 13) { 101 asteroidCol = 0; 102 asteroidRow = random(0, 2); 103 } 104 105 // Clear previous asteroid position 106 if (asteroidCol > 0) { 107 lcd.setCursor(asteroidCol - 1, asteroidRow); 108 lcd.write(' '); 109 } 110 111 // Draw asteroid 112 lcd.setCursor(asteroidCol, asteroidRow); 113 lcd.write(byte(1)); 114 115 // Move asteroid RIGHT 116 asteroidCol++; 117 118 // Collision detection 119 if (asteroidCol == 13 && asteroidRow == shipRow) { 120 tone(SPEAKER, 400, 200); 121 122 123 } 124 125 126 // --- SHIP DRAWING --- 127 128 // Clear previous ship position (no wrap) 129 lcd.setCursor(12, lastShipRow); 130 lcd.write(' '); 131 lcd.write(' '); 132 lcd.write(' '); 133 lcd.write(' '); 134 135 136 137 // Draw ship 138 lcd.setCursor(13, shipRow); 139 lcd.write(byte(0)); 140 141 // Draw flame 142 lcd.setCursor(14, shipRow); 143 lcd.write(byte(2)); 144 lcd.setCursor(15, shipRow); 145 lcd.write(byte(3)); 146 147 lastShipRow = shipRow; 148 149 delay(80); 150}
Documentation
image_2026-05-23_012500373
image_2026-05-23_012500373.png

image_2026-05-23_012525648
image_2026-05-23_012525648.png

image_2026-05-23_012557325
image_2026-05-23_012557325.png

Comments
Only logged in users can leave comments