शुरुआत: 🕹️ सरल Arduino एलसीडी गेम: "कूदो और आगे बढ़ो"
अद्यतित Arduino स्केच
हम एलसीडी के दाईं ओर दिखाई देने वाली यादृच्छिक बाधाओं को पेश करेंगे और बाईं ओर बढ़ेंगे। टकराव से बचने के लिए खिलाड़ी को उन पर कूदना होगा।
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <stdlib.h> // Include the standard library for random number functions
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int buttonLeftPin = 2;
const int buttonJumpPin = 3;
const int buttonRightPin = 4;
int fly = 0;
int positionX = 7; // Start in the middle of the screen
int obstaclePosition = 15; // Start obstacle off-screen
bool inAir = false; // Is the character in the air?
void setup() {
pinMode(buttonLeftPin, INPUT_PULLUP);
pinMode(buttonJumpPin, INPUT_PULLUP);
pinMode(buttonRightPin, INPUT_PULLUP);
lcd.init();
lcd.backlight();
lcd.setCursor(positionX, 1);
lcd.print("*"); // Character representation
randomSeed(analogRead(0)); // Initialize random seed
}
void loop() {
if (digitalRead(buttonLeftPin) == LOW) {
moveLeft();
}
if (digitalRead(buttonRightPin) == LOW) {
moveRight();
}
if (digitalRead(buttonJumpPin) == LOW && !inAir) {
jump();
}
if (millis() % 20 == 0) { // Every 20 milliseconds, update the obstacle
moveObstacle();
}
checkCollision();
}
void moveLeft() {
if (positionX > 0) {
lcd.clear();
lcd.setCursor(--positionX, 1);
lcd.print("*");
delay(200);
}
}
void moveRight() {
if (positionX < 15) {
lcd.clear();
lcd.setCursor(++positionX, 1);
lcd.print("*");
delay(200);
}
}
void jump() {
lcd.clear();
inAir = true;
lcd.setCursor(positionX, 0);
lcd.print("*");
delay(200);
fly = obstaclePosition - 3;
}
void moveObstacle() {
delay(200);
lcd.setCursor(obstaclePosition, 1);
lcd.print(" "); // Clear the old position
if (obstaclePosition > 0) {
lcd.setCursor(--obstaclePosition, 1);
lcd.print("#"); // Display the new position
if (fly == obstaclePosition){
lcd.clear();
lcd.setCursor(positionX, 1);
lcd.print("*");
inAir = false; // Return to ground
}
} else {
obstaclePosition = 15; // Reset the obstacle to the start
}
}
void checkCollision() {
if (positionX == obstaclePosition && !inAir) {
// Handle collision - game over or reduce life
lcd.clear();
lcd.setCursor(positionX, 1);
lcd.print("0");
delay(1000);
lcd.clear();
lcd.setCursor(positionX - 1, 1);
lcd.print(">0<");
delay(1000);
lcd.clear();
lcd.print("Game Over!");
delay(2000);
lcd.clear();
lcd.print("Restarting...");
delay(2000);
lcd.clear();
positionX = 7; // Reset position
obstaclePosition = 15; // Reset obstacle
lcd.setCursor(positionX, 1);
lcd.print("*");
}
}
खेल यांत्रिकी
- बाधाएं: बाधाएं (
#) अनियमित रूप से प्रकट होते हैं और स्क्रीन के दाईं ओर से बाईं ओर चलते हैं। - दिवसping: खिलाड़ी को टकराव से बचने के लिए बाधाओं पर कूदना होगा।
- टक्कर की पहचान हुई है: यदि खिलाड़ी हवा में नहीं है और किसी बाधा से टकरा जाता है, तो खेल "खेल खत्म!" प्रदर्शित करता है और फिर पुनः शुरू होता है।
यह अपडेट बुनियादी बाधा यांत्रिकी पेश करता है और खेल की जटिलता को बढ़ाता है। आप कई प्रकार की बाधाओं, अलग-अलग गति, या यहां तक कि कितनी बाधाओं को सफलतापूर्वक टाला गया है, इसके आधार पर स्कोर जोड़कर इसे और बढ़ा सकते हैं।