กลับไปหน้ารวมไฟล์
arduino-1d-pong-ws2812b-game.md

ปิงปองแนวเส้นตรง: เกม Pong 1 มิติ Arduino

เกม Pong ทั่วไปเป็นแบบ 2 มิติ และต้องใช้หน้าจอ แต่ เกม Pong 1 มิติ นี้ตัดแกน Y ออกไปจากสมการโดยสิ้นเชิง! นี่คือวิดีโอเกมที่ดั้งเดิมที่สุดเท่าที่จะจินตนาการได้: ผู้เล่นสองคนนั่งตรงข้ามกันอย่างสมบูรณ์ กด Arcade Button เพียงปุ่มเดียวอย่างรวดเร็ว ในขณะที่ "Pixel" ของแสง NeoPixel ที่เคลื่อนที่เร็วสะท้อนไปมาอย่างรุนแรงที่ปลายสุดของ LED strip ที่มีความยาว 1 เมตร

button_led_basic_interaction_1772681969235.png

การจัดการ 1D Array (FastLED)

“ลูกบอล” เป็นเพียงจำนวนเต็มที่เพิ่มขึ้นเรื่อย ๆ ใน array

  1. WS2812B Strip มี LED 60 ดวง int ballPos = 30;
  2. หลักการทางฟิสิกส์: ballPos += ballDirection; (โดยที่ direction คือ +1 หรือ -1)
  3. ถ้าลูกบอลเคลื่อนที่ไปทางซ้าย (Player 1) มันจะพุ่งเข้าหา ballPos = 0
  4. ฟังก์ชัน loop() ของ C++ จะอัปเดต array อย่างรวดเร็วด้วย FastLED.show()
  5. การคำนวณ Hit-Box: Player 1 ต้องกด Arcade Button เท่านั้น เมื่อ ballPos อยู่ระหว่าง 0 ถึง 3 ("Hit Zone")
if (digitalRead(P1_Btn) == LOW && ballPos <= 3) {
   ballDirection = 1; // Violent Deflection! Send it back to Player 2!
   gameDelay -= 5;    // Terrifyingly increase the ball speed!
} else if (digitalRead(P1_Btn) == LOW && ballPos > 3) {
   // Player 1 swung too early and missed! Game Over!
}

การเพิ่มตัวบ่งชี้สี RGB

เนื่องจากกราฟิกจำกัดอยู่ที่เส้นเดียว สีจึงเข้ามาแทนที่ Geometry

  • ลูกบอลมักจะแสดงด้วยสี CRGB::White หรือ CRGB::Blue
  • Hit Zones (LEDs 0-3 และ LEDs 56-59) จะถูกระบายสีด้วย CRGB::Red อย่างถาวร
  • เมื่อผู้เล่นทำคะแนน Ace ได้อย่างเด็ดขาด แถบ 60-pixel ทั้งหมดจะระเบิดเป็น Rainbow_Stripe_Animation() ที่สว่างจ้า พร้อมกับเล่นเพลงฉลองใหญ่ผ่าน Piezo Buzzer!

อุปกรณ์ที่จำเป็นสำหรับ Tabletop Pong

  • Arduino Uno/Nano (ความเร็วในการประมวลผลมาตรฐาน)
  • WS2812B NeoPixel LED Strip (โดยทั่วไป 60 ถึง 144 LEDs ต่อเมตร เพื่อให้ครอบคลุมความยาวของโต๊ะทั้งหมด!)
  • Arcade Tactics Push Buttons ขนาด 60 มม. แบบสปริงขนาดใหญ่สองปุ่ม
  • 5V 3A Power Brick (สำคัญมาก: NeoPixels 144 ดวง ที่สว่างด้วยแสงสีขาวเมื่อชนะ จะดึงกระแส 5 แอมป์ ซึ่งจะทำให้ laptop USB port ไหม้ได้ทันที!)

ข้อมูล Frontmatter ดั้งเดิม

title: "DIY addictive Arduino 1D Pong game"
description: "Minimalist reflex engine! Program an extraordinarily fast, single-pixel physics array using pure C++ loops to simulate a high-speed ball bouncing violently back and forth on a singular line of WS2812B LEDs."
category: "Gaming & Entertainment"
difficulty: "Intermediate"