Advertisement
LeventeDaradici

Im-Pong-Sibble-Arduino by Andrei Daradici

Dec 26th, 2021
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.41 KB | None | 0 0
  1. #include <GyverOLED.h>
  2.  
  3. GyverOLED<SSH1106_128x64> oled;
  4.  
  5. float x = 0.2f;
  6. float y = 0.2f;
  7.  
  8. int VRx = A0;
  9. int VRy = A1;
  10. int SW = 2;
  11.  
  12. int mapX = 0;
  13. int mapY = 0;
  14. int SW_state = 0;
  15.  
  16. int freq = 1000;
  17.  
  18. int SelectedButton = 1;
  19. int Scene = 4;
  20. int titlePos = 140;
  21.  
  22. int enemyYPos = 32;
  23. int playerYPos = 32;
  24.  
  25. int XDir = 64;
  26. int YDir = 32;
  27.  
  28. bool isGoingRight = true;
  29. bool isGoingUp = false;
  30.  
  31. void setup() {
  32.  
  33.   pinMode(VRx, INPUT);
  34.   pinMode(VRy, INPUT);
  35.   pinMode(3, OUTPUT);
  36.   pinMode(SW, INPUT_PULLUP);
  37.  
  38.   Serial.begin(9600);
  39.   oled.init();
  40.   oled.clear();
  41.   oled.update();
  42. }
  43.  
  44. void loop() {
  45.   oled.home();
  46.   //Scenes
  47.   while(Scene == 0){
  48.     Scene0();
  49.   }
  50.  
  51.   while(Scene == 1)  {
  52.     Scene1();
  53.   }
  54.  
  55.   while(Scene == 2)  {
  56.     Scene2();
  57.   }
  58.  
  59.   while(Scene == 3)  {
  60.     Scene3();
  61.   }
  62.   while(Scene == 4)
  63.   {
  64.     Scene4();
  65.   }
  66. }
  67.  
  68. void EnemyPaddle(){
  69.   oled.line(127, enemyYPos, 127, enemyYPos - 10);
  70.   oled.line(127, enemyYPos, 127, enemyYPos + 10);
  71.   oled.line(126, enemyYPos, 126, enemyYPos - 10);
  72.   oled.line(126, enemyYPos, 126, enemyYPos + 10);
  73.   oled.line(125, enemyYPos, 125, enemyYPos - 10);
  74.   oled.line(125, enemyYPos, 125, enemyYPos + 10);
  75. }
  76.  
  77. void PlayerPaddle(){
  78.   oled.line(0, playerYPos, 0, playerYPos - 10);
  79.   oled.line(0, playerYPos, 0, playerYPos + 10);
  80.   oled.line(1, playerYPos, 1, playerYPos - 10);
  81.   oled.line(1, playerYPos, 1, playerYPos + 10);
  82.   oled.line(2, playerYPos, 2, playerYPos - 10);
  83.   oled.line(2, playerYPos, 2, playerYPos + 10);
  84. }
  85.  
  86. void MiddleLine(){
  87.   oled.line(64, 0, 64, 4);
  88.   oled.line(63, 0, 63, 4);
  89.   oled.line(64, 8, 64, 12);
  90.   oled.line(63, 8, 63, 12);
  91.   oled.line(64, 16, 64, 20);
  92.   oled.line(63, 16, 63, 20);
  93.   oled.line(64, 24, 64, 28);
  94.   oled.line(63, 24, 63, 28);
  95.   oled.line(64, 32, 64, 36);
  96.   oled.line(63, 32, 63, 36);
  97.   oled.line(64, 40, 64, 44);
  98.   oled.line(63, 40, 63, 44);
  99.   oled.line(64, 48, 64, 52);
  100.   oled.line(63, 48, 63, 52);
  101.   oled.line(64, 56, 64, 60);
  102.   oled.line(63, 56, 63, 60);
  103. }
  104.  
  105. //Main Menu
  106. void Scene0(){
  107.   x = analogRead(VRx);
  108.   mapX = map(x, 0, 1023, -512, 512);
  109.  
  110.   SW_state = digitalRead(SW);
  111.   if(mapX < -460)
  112.   {
  113.     SelectedButton = 1;
  114.     tone(3, freq, 100);
  115.   }
  116.   if(mapX > 460)
  117.   {
  118.     SelectedButton = 2;
  119.     tone(3, freq, 100);
  120.   }
  121.  
  122.   oled.clear();
  123.   oled.setCursor(titlePos, 1);
  124.   titlePos-=4;
  125.  
  126.   oled.setScale(1);
  127.   oled.print("Im-Pong-Sibble Arduino");
  128.   oled.setCursor(50, 3);
  129.   oled.print("Play");
  130.   oled.setCursor(45, 5);
  131.   oled.print("Credits");
  132.  
  133.   oled.setScale(2);
  134.   if(SelectedButton == 1)
  135.   {
  136.     oled.roundRect(24, 20, 104, 34, OLED_STROKE);
  137.     oled.roundRect(30, 37, 98, 51, OLED_STROKE);    
  138.   }
  139.   if(SelectedButton == 2)
  140.   {
  141.     oled.roundRect(30, 20, 98, 34, OLED_STROKE);
  142.     oled.roundRect(24, 37, 104, 51, OLED_STROKE);
  143.   }
  144.    
  145.   oled.update();
  146.  
  147.   if(SW_state == 0 && SelectedButton == 1)
  148.   {
  149.     Scene = 1;
  150.     tone(3, freq, 100);
  151.   }
  152.   if(SW_state == 0 && SelectedButton == 2)
  153.   {
  154.     Scene = 2;
  155.     tone(3, freq, 100);
  156.   }
  157.   if(titlePos < -140)
  158.   {
  159.     titlePos = 140;
  160.   }
  161. }
  162.  
  163. //Main Game
  164. void Scene1(){
  165.   oled.clear();
  166.   MiddleLine();
  167.   PlayerPaddle();
  168.   EnemyPaddle();
  169.   oled.circle(XDir, YDir, 2);
  170.   oled.update();
  171.   if(isGoingRight && isGoingUp)    {
  172.     XDir+=3;
  173.     YDir-=3;
  174.   }
  175.   if(!isGoingRight && isGoingUp)    {
  176.     XDir-=3;
  177.     YDir-=3;
  178.   }
  179.   if(isGoingRight && !isGoingUp)    {
  180.     XDir+=3;
  181.     YDir+=3;
  182.   }
  183.   if(!isGoingRight && !isGoingUp)    {
  184.     XDir-=3;
  185.     YDir+=3;
  186.   }
  187.   if(XDir >= 119)
  188.   {
  189.     isGoingRight = false;
  190.     tone(3, freq, 100);
  191.   }
  192.   if(YDir == 5)
  193.   {
  194.     isGoingUp = false;
  195.     tone(3, freq, 100);
  196.   }
  197.   if(YDir == 59)
  198.   {
  199.     isGoingUp = true;
  200.     tone(3, freq, 100);
  201.   }
  202.   enemyYPos = YDir;
  203.   if(XDir <= 9 && XDir >= 7 && XDir != 5)
  204.   {
  205.     if(YDir == playerYPos || YDir == playerYPos + 1 || YDir == playerYPos + 2 || YDir == playerYPos + 3 || YDir == playerYPos + 4 || YDir == playerYPos + 5 || YDir == playerYPos + 6 || YDir == playerYPos + 7 || YDir == playerYPos || YDir == playerYPos + 8 || YDir == playerYPos + 9 || YDir == playerYPos - 1 || YDir == playerYPos - 2 || YDir == playerYPos - 3 || YDir == playerYPos - 4 || YDir == playerYPos - 5 || YDir == playerYPos - 6 || YDir == playerYPos - 7 || YDir == playerYPos || YDir == playerYPos - 8 || YDir == playerYPos - 9)
  206.     {
  207.       isGoingRight = true;
  208.       tone(3, freq, 100);
  209.     }
  210.   }
  211.   if(XDir < 0)
  212.   {
  213.     Scene = 3;
  214.   }
  215.   x = analogRead(VRx);
  216.   mapX = map(x, 0, 1023, -512, 512);
  217.  
  218.   if(mapX < -460)
  219.   {
  220.     playerYPos-=3;
  221.   }
  222.   if(mapX > 460)
  223.   {
  224.     playerYPos+=3;
  225.   }
  226.   if(playerYPos < 10)
  227.   {
  228.     playerYPos = 10;
  229.   }
  230.   if(playerYPos > 54)
  231.   {
  232.     playerYPos = 54;
  233.   }
  234. }
  235.  
  236. //Credits
  237. void Scene2(){
  238.   oled.clear();
  239.   oled.setScale(2);
  240.   oled.setCursor(0, 0);
  241.   oled.print("Credits");
  242.   oled.setCursor(0, 3);
  243.   oled.setScale(1);
  244.   oled.print("Daradici Andras");
  245.   oled.setCursor(0, 4);
  246.   oled.setScale(1);
  247.   oled.print("- Coding");
  248.   oled.setCursor(0, 5);
  249.   oled.setScale(1);
  250.   oled.print("Press down joystick");
  251.   oled.setCursor(0, 6);
  252.   oled.setScale(1);
  253.   oled.print("to go back to");
  254.   oled.setCursor(0, 7);
  255.   oled.setScale(1);
  256.   oled.print("the menu.");
  257.   oled.update();
  258.   Serial.println(Scene);
  259.   SW_state = digitalRead(SW);
  260.   if(SW_state == 0)
  261.   {
  262.     delay(500);
  263.     tone(3, freq, 100);
  264.     Scene = 0;
  265.   }
  266. }
  267.  
  268. //Game Over
  269. void Scene3(){
  270.   XDir = 64;
  271.   YDir = 32;
  272.   isGoingRight = true;
  273.   isGoingUp = true;
  274.   int freq2;
  275.   for(int i = 5; i > 0; i--)
  276.   {
  277.     freq2 = i * 100;
  278.     oled.clear();
  279.     oled.setScale(2);
  280.     oled.setCursor(2, 0);
  281.     oled.print(" You lost!");
  282.     oled.setScale(3);
  283.     oled.setCursor(56, 4);
  284.     oled.print(i);
  285.     oled.update();
  286.     tone(3, freq2, 100);
  287.     delay(1000);
  288.   }
  289.   freq2 = 1000;
  290.   tone(3, freq2, 100);
  291.   Scene = 0;
  292. }
  293.  
  294. //Intro
  295. void Scene4(){
  296.   oled.clear();
  297.   oled.setScale(1);
  298.   oled.update();
  299.   for(int i = -80; i < 30; i+=10)
  300.   {
  301.     oled.clear();
  302.     oled.setCursor(i, 1);
  303.     oled.print("Andrewstudios");
  304.     oled.update();
  305.     delay(10);
  306.   }
  307.   for(int i = 130; i >= 40; i-=10)
  308.   {
  309.     oled.setCursor(i, 2);
  310.     oled.print("presents        ");
  311.     oled.update();
  312.     delay(10);
  313.   }
  314.   for(int i = 130; i >= 20; i-=10)
  315.   {
  316.     oled.setCursor(i, 3);
  317.     oled.print("Im-Pong-Sibble        ");
  318.     oled.update();
  319.     delay(10);
  320.   }
  321.   delay(3000);
  322.   int freq2 = 1500;
  323.   tone(3, freq2, 100);
  324.   Scene = 0;
  325. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement