Advertisement
cisco404

Kode sederhana Game Interaktif dengan Arduino dan Joypad

May 3rd, 2024
659
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.98 KB | Source Code | 0 0
  1. #include <Joystick.h>
  2.  
  3. Joystick joy; // Objek Joystick untuk membaca input dari Joypad
  4.  
  5. // -------------------------------------------
  6. // Kode sederhana Game Interaktif dengan Arduino dan Joypad
  7. // www.ardukode.blogspot.com
  8. // -------------------------------------------
  9.  
  10. int x = 0; // Nilai sumbu X Joypad
  11. int y = 0; // Nilai sumbu Y Joypad
  12. int button1 = 0; // Status tombol 1 Joypad
  13. int button2 = 0; // Status tombol 2 Joypad
  14. int button3 = 0; // Status tombol 3 Joypad
  15. int button4 = 0; // Status tombol 4 Joypad
  16.  
  17. void setup() {
  18.   joy.begin(); // Inisialisasi library Joystick
  19.   Serial.begin(9600); // Inisialisasi komunikasi serial
  20. }
  21.  
  22. void loop() {
  23.   joy.readJoystick(); // Baca input dari Joypad
  24.  
  25.   x = joy.getAxis(0); // Dapatkan nilai sumbu X
  26.   y = joy.getAxis(1); // Dapatkan nilai sumbu Y
  27.   button1 = joy.getButton(1); // Dapatkan status tombol 1
  28.   button2 = joy.getButton(2); // Dapatkan status tombol 2
  29.   button3 = joy.getButton(3); // Dapatkan status tombol 3
  30.   button4 = joy.getButton(4); // Dapatkan status tombol 4
  31.  
  32.   // Proses input dari Joypad untuk mengontrol game
  33.   if (x > 0) {
  34.     // Pemain bergerak ke kanan
  35.   } else if (x < 0) {
  36.     // Pemain bergerak ke kiri
  37.   }
  38.  
  39.   if (y > 0) {
  40.     // Pemain bergerak ke atas
  41.   } else if (y < 0) {
  42.     // Pemain bergerak ke bawah
  43.   }
  44.  
  45.   if (button1) {
  46.     // Tombol 1 ditekan
  47.   }
  48.  
  49.   if (button2) {
  50.     // Tombol 2 ditekan
  51.   }
  52.  
  53.   if (button3) {
  54.     // Tombol 3 ditekan
  55.   }
  56.  
  57.   if (button4) {
  58.     // Tombol 4 ditekan
  59.   }
  60.  
  61.   // Tampilkan informasi game di layar LCD (opsional)
  62.   if (lcd) {
  63.     lcd.clear();
  64.     lcd.print("X: ");
  65.     lcd.print(x);
  66.     lcd.print(", Y: ");
  67.     lcd.print(y);
  68.     lcd.println();
  69.     lcd.print("Button 1: ");
  70.     lcd.print(button1);
  71.     lcd.print(", Button 2: ");
  72.     lcd.print(button2);
  73.     lcd.println();
  74.     lcd.print("Button 3: ");
  75.     lcd.print(button3);
  76.     lcd.print(", Button 4: ");
  77.     lcd.println(button4);
  78.   }
  79.  
  80.   // Putar suara game di speaker (opsional)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement