Advertisement
Inksaver

Arduino DIY Gamer invaders

Nov 13th, 2014
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.42 KB | None | 0 0
  1. #include <Gamer.h>
  2. Gamer gamer;
  3.  
  4. const int laserY = 7;
  5. int laserX = 0;
  6. const int shipY = 0;
  7. int shipX = 7;
  8. int bomb1 = 0;
  9. int bomb2 = 0;
  10. int bomb1Y = 1;
  11. int bomb2Y = 1;
  12. int gunX = 0;
  13. int gunY = 0;
  14. boolean newShip = true;
  15.  
  16. //setup device
  17. void setup()
  18. {
  19.   gamer.begin();
  20. }
  21. void setBombs()
  22. {
  23.   bomb1 = random(8);
  24.   bomb2 = random(8);
  25.   while (bomb2 == bomb1) bomb1 = random(8);
  26. }
  27.  
  28.  
  29. void drawShip()
  30. {
  31.   if (shipX > -1 && shipX < 8) gamer.display[shipX][shipY] = 1;
  32.   if (shipX - 1 > -1 && shipX -1 < 8) gamer.display[shipX - 1][shipY] = 1;
  33.   if (shipX - 2 > -1 && shipX -2 < 8)gamer.display[shipX - 2][shipY] = 1;
  34. }
  35.  
  36. void drawBombs()
  37. {
  38.   gamer.display[bomb1][bomb1Y] = 1;
  39.   gamer.display[bomb2][bomb2Y] = 1;
  40. }
  41.  
  42. void drawLaser()
  43. {
  44.   gamer.display[laserX][laserY] = 1;
  45. }
  46.  
  47. void readSwitches()
  48. {
  49.   if (gamer.isHeld(LEFT)) laserX--;
  50.   if (laserX < 0) laserX = 0;
  51.   if (gamer.isHeld(RIGHT)) laserX++;
  52.   if (laserX > 7) laserX = 7;
  53.   if (gamer.isPressed(START)) drawLaser();
  54. }
  55.  
  56. void loop()
  57. {
  58.   gamer.clear();
  59.   if (newShip)
  60.   {
  61.     shipX = 7;
  62.     newShip = false;
  63.     setBombs();
  64.   }
  65.   else
  66.   {
  67.     shipX--;
  68.       if (shipX < 0) newShip = true;
  69.   }
  70.  
  71.   if (shipX == bomb1)
  72.   {
  73.     bomb1Y++;
  74.       if (bomb1Y < 0) bomb1Y = 7;
  75.     bomb2Y++;
  76.       if (bomb2Y < 0) bomb2Y = 7;
  77.   }
  78.  
  79.   readSwitches();
  80.   drawShip();
  81.   drawBombs();
  82.   drawLaser();
  83.   gamer.updateDisplay();
  84.   delay(300);
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement