rjk79

Arduino Game

Feb 1st, 2013
42
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <LiquidCrystal.h>
  2. #include <Wire.h>
  3. #include <ArduinoNunchuk.h>
  4.  
  5. ArduinoNunchuk nunchuk = ArduinoNunchuk();
  6. LiquidCrystal lcd(2,3,4,5,6,7);
  7.  
  8. int posX = 90;
  9. int x = 0;
  10. int x2 = 0;
  11. int prevx = 0;
  12. int prevx2 = 0;
  13. int prevPosX = 0;
  14. int zState = 0;
  15. unsigned long previousMillis = 0;
  16. long interval = 500;
  17. int i = 0;
  18. int j = 0;
  19. int score = 0;
  20.  
  21. byte enemy[8] = {
  22. B01110,
  23. B10001,
  24. B10101,
  25. B10001,
  26. B10101,
  27. B11011,
  28. B10101,
  29. };
  30.  
  31. byte gun[8] = {
  32. B00000,
  33. B00000,
  34. B00100,
  35. B01110,
  36. B01010,
  37. B01110,
  38. B11111,
  39. };
  40.  
  41. byte full[8] = {
  42. B11111,
  43. B11111,
  44. B11111,
  45. B11111,
  46. B11111,
  47. B11111,
  48. B11111,
  49. };
  50.  
  51. byte empty[8] = {
  52. B00000,
  53. B00000,
  54. B00000,
  55. B00000,
  56. B00000,
  57. B00000,
  58. B00000,
  59. };
  60.  
  61. void setup(){
  62. Serial.begin(9600);
  63. lcd.begin(16,2);
  64. nunchuk.init();
  65. lcd.createChar(5, enemy);
  66. lcd.createChar(1, gun);
  67. lcd.createChar(2, full);
  68. lcd.createChar(3, empty);
  69. }
  70.  
  71. void loop(){
  72. nunchuk.update();
  73. x = constrain(x, 0, 14);
  74. x2 = constrain(x2, 0, 14);
  75.  
  76. lcd.setCursor(x,0);
  77. lcd.write(5);
  78. lcd.setCursor(x2,1);
  79. lcd.write(1);
  80. lcd.setCursor(15, 0);
  81. lcd.print(score);
  82.  
  83. zState = nunchuk.zButton;
  84.  
  85. posX = nunchuk.analogX;
  86.  
  87. if(zState == HIGH && x == x2){
  88. lcd.setCursor(x,0);
  89. lcd.write(2);
  90. delay(333);
  91. score++;
  92. x = random(0,15);
  93. }
  94.  
  95. if(posX < 60 && prevPosX > 60){
  96. x2--;
  97. }
  98. if(posX > 200 && prevPosX < 200){
  99. x2++;
  100. }
  101.  
  102. unsigned long currentMillis = millis();
  103.  
  104. if(currentMillis - previousMillis > interval){
  105. i = random(0,2);
  106. if(i == 0){
  107. x--;
  108. }
  109. if(i == 1){
  110. x++;
  111. }
  112. previousMillis = currentMillis;
  113. }
  114.  
  115. if(zState == HIGH){
  116. lcd.setCursor(15,1);
  117. lcd.write(2);
  118. }
  119.  
  120. if(zState == LOW){
  121. lcd.setCursor(15,1);
  122. lcd.write(3);
  123. }
  124.  
  125. if(score == 9){
  126. lcd.setCursor(0,0);
  127. lcd.print(" GAME OVER ");
  128. lcd.setCursor(2,1);
  129. lcd.print(currentMillis / 1000);
  130. lcd.print(" Seconds");
  131. lcd.setCursor(15,1);
  132. lcd.write(3);
  133. delay(5000);
  134. score = 0;
  135. }
  136.  
  137. if(x != prevx || x2 != prevx2){
  138. lcd.clear();
  139. }
  140.  
  141. prevx = x;
  142. prevPosX = posX;
  143. prevx2 = x2;
  144. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×