#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // LCD pins: RS, E, D4, D5, D6, D7
const int buttonPin1 = 8;
const int buttonPin2 = 9;
const int buttonPin3 = 10;
int playerpos = 0;
int playerY = 0; // Y position for the player
int bulletX = 0; // Initial bullet X position
int bulletY = 1; // Y position for the bullet
int spiderX = 15; // Initial spider X position
int spiderY = 0; // Y position for the spider
int spider2X = 18; // Initial spider X position
int spider2Y = 1; // Y position for the spider
bool GameOver=false;
int Score=0;
const int buzzerPin = 7;
int melody=0;
int runningMelody[] = {300, 0, 200, 0, 250, 0, 180, 0};
int Contrast=75;
byte playerChar[] = {
// Player character
B00100,
B01110,
B10101,
B00100,
B01010,
B10001,
B00000,
B00000
};
byte bulletChar[] = {
// Bullet character
B00000,
B00000,
B00000,
B01110,
B01110,
B00000,
B00000,
B00000
};
byte spiderChar[] = {
// Spider character
B00100,
B11111,
B01110,
B11111,
B11011,
B11111,
B01010,
B10101
};
void setup() {
analogWrite(6,Contrast);
playOpeningSound();
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("Redcontroller");
lcd.setCursor(0, 1);
lcd.print("Interactive");
delay(1500);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Spider");
lcd.setCursor(0, 1);
lcd.print("Escape");
delay(1500);
lcd.clear();
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin3, INPUT_PULLUP);
pinMode(buzzerPin, OUTPUT);
lcd.createChar(0, playerChar);
lcd.createChar(1, bulletChar);
lcd.createChar(2, spiderChar);
lcd.setCursor(bulletX, bulletY);
}
void loop() {
int toneFrequency = runningMelody[melody];
tone(buzzerPin, toneFrequency);
melody++;
if(melody==7){
melody=0;
}
// Clear the current positions of the player, bullet, and spider
lcd.setCursor(0, playerY);
lcd.print(" ");
lcd.setCursor(bulletX, bulletY);
lcd.print(" ");
lcd.setCursor(spiderX, spiderY);
lcd.print(" ");
lcd.setCursor(spider2X, spider2Y);
lcd.print(" ");
int buttonState1 = digitalRead(buttonPin1);
int buttonState2 = digitalRead(buttonPin2);
int buttonState3 = digitalRead(buttonPin3);
if (buttonState1 == LOW) {
playerY = 0;
} else if (buttonState2 == LOW) {
playerY = 1;
}
// Move the spider from right to left
if (spiderX > 0) {
spiderX--;
} else {
// Respawn the spider at the right side of the screen
spiderX = random(15, 25 + 1);
if((spiderX>spider2X+2)||(spiderX<spider2X-2)){
}else{
spiderX=-1;
}
Score++;
}
if (spider2X > 0) {
spider2X--;
} else {
// Respawn the spider at the right side of the screen
spider2X = random(15, 20 + 1);
if((spider2X>spiderX+2)||(spider2X<spiderX-2)){
}else{
spider2X=-1;
}
Score++;
}
// Display the updated player, bullet, and spider positions
lcd.setCursor(0, playerY);
lcd.write(byte(0)); // Display the player character
lcd.setCursor(spiderX, spiderY);
lcd.write(byte(2)); // Display the spider character
lcd.setCursor(spider2X, spider2Y);
lcd.write(byte(2));
lcd.setCursor(13, 0);
lcd.print(Score);
//Check for collision between the player and the spider
if (playerY == spiderY && spiderX==0 ) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Game Over");
lcd.setCursor(0, 1);
lcd.print("Score:");
lcd.print(Score);
playGameOverSound();
delay(2000);
lcd.clear();
Score=0;
}
if (playerY == spider2Y && spider2X==0 ) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Game Over");
lcd.setCursor(0, 1);
lcd.print("Score:");
lcd.print(Score);
playGameOverSound();
delay(2000);
lcd.clear();
Score=0;
}
delay(100); // Adjust the delay as needed
}
void playGameOverSound() {
// Define the frequencies for the game over sound
int gameOverMelody[] = {300, 200, 150, 100, 80, 50, 30, 20, 10, 0};
// Define the durations for each note
int noteDuration = 250; // in milliseconds
// Play the game over sound
for (int i = 0; i < sizeof(gameOverMelody) / sizeof(gameOverMelody[0]); i++) {
int toneFrequency = gameOverMelody[i];
// Check if the tone is valid (greater than zero)
if (toneFrequency > 0) {
tone(buzzerPin, toneFrequency);
delay(noteDuration);
} else {
noTone(buzzerPin); // Stop the tone if frequency is 0
}
}
}
void playOpeningSound() {
// Define the frequencies for the opening sound
int openingMelody[] = {200, 300, 400, 500, 600,0};
// Define the durations for each note
int noteDuration = 200; // in milliseconds
// Play the opening sound
for (int i = 0; i < sizeof(openingMelody) / sizeof(openingMelody[0]); i++) {
int toneFrequency = openingMelody[i];
// Check if the tone is valid (greater than zero)
if (toneFrequency > 0) {
tone(buzzerPin, toneFrequency);
delay(noteDuration);
} else {
noTone(buzzerPin); // Stop the tone if frequency is 0
}
}
}