Advertisement
LeventeDaradici

Im-Pong-Sibble- 2 Players - Arduino by Andrei Daradici

Dec 28th, 2021
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.67 KB | None | 0 0
  1. #include <GyverOLED.h>
  2.  
  3. GyverOLED<SSH1106_128x64> oled;
  4.  
  5. float x = 0.2f;
  6. float x2 = 0.2f;
  7.  
  8. int VRx = A0;
  9. int VRy = A1;
  10. int SW = 2;
  11.  
  12. int VRx2 = A2;
  13. int VRy2 = A3;
  14. int SW2 = 4;
  15.  
  16. int mapX = 0;
  17. int mapX2 = 0;
  18. int SW_state = 0;
  19.  
  20. int freq = 1000;
  21.  
  22. int SelectedButton = 1;
  23. int Scene = 4;
  24. int titlePos = 140;
  25.  
  26. int enemyYPos = 32;
  27. int playerYPos = 32;
  28. int player2YPos = 32;
  29.  
  30. int XDir = 64;
  31. int YDir = 32;
  32.  
  33. bool isGoingRight = true;
  34. bool isGoingUp = false;
  35.  
  36. void setup() {
  37.  
  38.   pinMode(VRx, INPUT);
  39.   pinMode(VRy, INPUT);
  40.   pinMode(3, OUTPUT);
  41.   pinMode(SW, INPUT_PULLUP);
  42.  
  43.   Serial.begin(9600);
  44.   oled.init();
  45.   oled.clear();
  46.   oled.update();
  47. }
  48.  
  49. void loop() {
  50.   oled.home();
  51.   //Scenes
  52.   while(Scene == 0){
  53.     Scene0();
  54.   }
  55.  
  56.   while(Scene == 1)  {
  57.     Scene1();
  58.   }
  59.  
  60.   while(Scene == 2)  {
  61.     Scene2();
  62.   }
  63.  
  64.   while(Scene == 3)  {
  65.     Scene3();
  66.   }
  67.   while(Scene == 4)
  68.   {
  69.     Scene4();
  70.   }
  71.   while(Scene == 5)  {
  72.     Scene5();
  73.   }
  74.   while(Scene == 6)  {
  75.     Scene6();
  76.   }
  77.   while(Scene == 7)  {
  78.     Scene7();
  79.   }
  80.   while(Scene == 8)  {
  81.     Scene8();
  82.   }
  83. }
  84.  
  85. void EnemyPaddle(){
  86.   oled.line(127, enemyYPos, 127, enemyYPos - 10);
  87.   oled.line(127, enemyYPos, 127, enemyYPos + 10);
  88.   oled.line(126, enemyYPos, 126, enemyYPos - 10);
  89.   oled.line(126, enemyYPos, 126, enemyYPos + 10);
  90.   oled.line(125, enemyYPos, 125, enemyYPos - 10);
  91.   oled.line(125, enemyYPos, 125, enemyYPos + 10);
  92. }
  93.  
  94. void PlayerPaddle(){
  95.   oled.line(0, playerYPos, 0, playerYPos - 10);
  96.   oled.line(0, playerYPos, 0, playerYPos + 10);
  97.   oled.line(1, playerYPos, 1, playerYPos - 10);
  98.   oled.line(1, playerYPos, 1, playerYPos + 10);
  99.   oled.line(2, playerYPos, 2, playerYPos - 10);
  100.   oled.line(2, playerYPos, 2, playerYPos + 10);
  101. }
  102.  
  103. void Player2Paddle(){
  104.   oled.line(127, player2YPos, 127, player2YPos - 10);
  105.   oled.line(127, player2YPos, 127, player2YPos + 10);
  106.   oled.line(126, player2YPos, 126, player2YPos - 10);
  107.   oled.line(126, player2YPos, 126, player2YPos + 10);
  108.   oled.line(125, player2YPos, 125, player2YPos - 10);
  109.   oled.line(125, player2YPos, 125, player2YPos + 10);
  110. }
  111.  
  112. void MiddleLine(){
  113.   oled.line(64, 0, 64, 4);
  114.   oled.line(63, 0, 63, 4);
  115.   oled.line(64, 8, 64, 12);
  116.   oled.line(63, 8, 63, 12);
  117.   oled.line(64, 16, 64, 20);
  118.   oled.line(63, 16, 63, 20);
  119.   oled.line(64, 24, 64, 28);
  120.   oled.line(63, 24, 63, 28);
  121.   oled.line(64, 32, 64, 36);
  122.   oled.line(63, 32, 63, 36);
  123.   oled.line(64, 40, 64, 44);
  124.   oled.line(63, 40, 63, 44);
  125.   oled.line(64, 48, 64, 52);
  126.   oled.line(63, 48, 63, 52);
  127.   oled.line(64, 56, 64, 60);
  128.   oled.line(63, 56, 63, 60);
  129. }
  130.  
  131. //Main Menu
  132. void Scene0(){
  133.   x = analogRead(VRx);
  134.   mapX = map(x, 0, 1023, -512, 512);
  135.  
  136.   SW_state = digitalRead(SW);
  137.   if(mapX < -460)
  138.   {
  139.     SelectedButton = 1;
  140.     tone(3, freq, 100);
  141.   }
  142.   if(mapX > 460)
  143.   {
  144.     SelectedButton = 2;
  145.     tone(3, freq, 100);
  146.   }
  147.  
  148.   oled.clear();
  149.   oled.setCursor(titlePos, 1);
  150.   titlePos-=4;
  151.  
  152.   oled.setScale(1);
  153.   oled.print("Im-Pong-Sibble Arduino");
  154.   oled.setCursor(50, 3);
  155.   oled.print("Play");
  156.   oled.setCursor(45, 5);
  157.   oled.print("Credits");
  158.  
  159.   oled.setScale(2);
  160.   if(SelectedButton == 1)
  161.   {
  162.     oled.roundRect(24, 20, 104, 34, OLED_STROKE);
  163.     oled.roundRect(30, 37, 98, 51, OLED_STROKE);    
  164.   }
  165.   if(SelectedButton == 2)
  166.   {
  167.     oled.roundRect(30, 20, 98, 34, OLED_STROKE);
  168.     oled.roundRect(24, 37, 104, 51, OLED_STROKE);
  169.   }
  170.    
  171.   oled.update();
  172.  
  173.   if(SW_state == 0 && SelectedButton == 1)
  174.   {
  175.     delay(500);
  176.     Scene = 5;
  177.     tone(3, freq, 100);
  178.   }
  179.   if(SW_state == 0 && SelectedButton == 2)
  180.   {
  181.     delay(500);
  182.     Scene = 2;
  183.     tone(3, freq, 100);
  184.   }
  185.   if(titlePos < -140)
  186.   {
  187.     titlePos = 140;
  188.   }
  189. }
  190.  
  191. //Singleplayer
  192. void Scene1(){
  193.   oled.clear();
  194.   MiddleLine();
  195.   PlayerPaddle();
  196.   EnemyPaddle();
  197.   oled.circle(XDir, YDir, 2);
  198.   oled.update();
  199.   if(isGoingRight && isGoingUp)    {
  200.     XDir+=3;
  201.     YDir-=3;
  202.   }
  203.   if(!isGoingRight && isGoingUp)    {
  204.     XDir-=3;
  205.     YDir-=3;
  206.   }
  207.   if(isGoingRight && !isGoingUp)    {
  208.     XDir+=3;
  209.     YDir+=3;
  210.   }
  211.   if(!isGoingRight && !isGoingUp)    {
  212.     XDir-=3;
  213.     YDir+=3;
  214.   }
  215.   if(XDir >= 119)
  216.   {
  217.     isGoingRight = false;
  218.     tone(3, freq, 100);
  219.   }
  220.   if(YDir == 5)
  221.   {
  222.     isGoingUp = false;
  223.     tone(3, freq, 100);
  224.   }
  225.   if(YDir == 59)
  226.   {
  227.     isGoingUp = true;
  228.     tone(3, freq, 100);
  229.   }
  230.   enemyYPos = YDir;
  231.   if(XDir <= 9 && XDir >= 7 && XDir != 5)
  232.   {
  233.     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)
  234.     {
  235.       isGoingRight = true;
  236.       tone(3, freq, 100);
  237.     }
  238.   }
  239.   if(XDir < 0)
  240.   {
  241.     Scene = 3;
  242.   }
  243.   x = analogRead(VRx);
  244.   mapX = map(x, 0, 1023, -512, 512);
  245.  
  246.   if(mapX < -460)
  247.   {
  248.     playerYPos-=3;
  249.   }
  250.   if(mapX > 460)
  251.   {
  252.     playerYPos+=3;
  253.   }
  254.   if(playerYPos < 10)
  255.   {
  256.     playerYPos = 10;
  257.   }
  258.   if(playerYPos > 54)
  259.   {
  260.     playerYPos = 54;
  261.   }
  262. }
  263.  
  264. //Credits
  265. void Scene2(){
  266.   oled.clear();
  267.   oled.setScale(2);
  268.   oled.setCursor(0, 0);
  269.   oled.print("Credits");
  270.   oled.setCursor(0, 3);
  271.   oled.setScale(1);
  272.   oled.print("Daradici Andras");
  273.   oled.setCursor(0, 4);
  274.   oled.setScale(1);
  275.   oled.print("- Coding");
  276.   oled.setCursor(0, 5);
  277.   oled.setScale(1);
  278.   oled.print("Press down joystick");
  279.   oled.setCursor(0, 6);
  280.   oled.setScale(1);
  281.   oled.print("to go back to");
  282.   oled.setCursor(0, 7);
  283.   oled.setScale(1);
  284.   oled.print("the menu.");
  285.   oled.update();
  286.   Serial.println(Scene);
  287.   SW_state = digitalRead(SW);
  288.   if(SW_state == 0)
  289.   {
  290.     delay(500);
  291.     tone(3, freq, 100);
  292.     Scene = 0;
  293.   }
  294. }
  295.  
  296. //Game Over
  297. void Scene3(){
  298.   XDir = 64;
  299.   YDir = 32;
  300.   playerYPos = 32;
  301.   player2YPos = 32;
  302.   isGoingRight = true;
  303.   isGoingUp = true;
  304.   int freq2;
  305.   for(int i = 5; i > 0; i--)
  306.   {
  307.     freq2 = i * 100;
  308.     oled.clear();
  309.     oled.setScale(2);
  310.     oled.setCursor(2, 0);
  311.     oled.print(" You lost!");
  312.     oled.setScale(3);
  313.     oled.setCursor(56, 4);
  314.     oled.print(i);
  315.     oled.update();
  316.     tone(3, freq2, 100);
  317.     delay(1000);
  318.   }
  319.   freq2 = 1000;
  320.   tone(3, freq2, 100);
  321.   Scene = 0;
  322. }
  323.  
  324. //Intro
  325. void Scene4(){
  326.   oled.clear();
  327.   oled.setScale(1);
  328.   oled.update();
  329.   for(int i = -80; i < 30; i+=10)
  330.   {
  331.     oled.clear();
  332.     oled.setCursor(i, 1);
  333.     oled.print("Andrewstudios");
  334.     oled.update();
  335.     delay(10);
  336.   }
  337.   for(int i = 130; i >= 40; i-=10)
  338.   {
  339.     oled.setCursor(i, 2);
  340.     oled.print("presents        ");
  341.     oled.update();
  342.     delay(10);
  343.   }
  344.   for(int i = 130; i >= 20; i-=10)
  345.   {
  346.     oled.setCursor(i, 3);
  347.     oled.print("Im-Pong-Sibble        ");
  348.     oled.update();
  349.     delay(10);
  350.   }
  351.   delay(3000);
  352.   int freq2 = 1500;
  353.   tone(3, freq2, 100);
  354.   Scene = 0;
  355. }
  356.  
  357. //Mode select
  358. void Scene5(){
  359.   oled.clear();
  360.   oled.setScale(1);
  361.   x = analogRead(VRx);
  362.   mapX = map(x, 0, 1023, -512, 512);
  363.  
  364.   SW_state = digitalRead(SW);
  365.   if(mapX < -460)
  366.   {
  367.     SelectedButton = 1;
  368.     tone(3, freq, 100);
  369.   }
  370.   if(mapX > 460)
  371.   {
  372.     SelectedButton = 2;
  373.     tone(3, freq, 100);
  374.   }
  375.   oled.setCursor(30, 1);
  376.   oled.print("Mode select");
  377.   oled.setCursor(30, 3);
  378.   oled.print("Single Player");
  379.   oled.setCursor(35, 5);
  380.   oled.print("Two Player");
  381.  
  382.   oled.setScale(2);
  383.   if(SelectedButton == 1)
  384.   {
  385.     oled.roundRect(10, 20, 118, 34, OLED_STROKE);
  386.     oled.roundRect(18, 37, 110, 51, OLED_STROKE);  
  387.   }
  388.   if(SelectedButton == 2)
  389.   {
  390.     oled.roundRect(18, 20, 110, 34, OLED_STROKE);
  391.     oled.roundRect(10, 37, 118, 51, OLED_STROKE);
  392.   }
  393.   int freq3 = 1300;
  394.  
  395.   if(SW_state == 0 && SelectedButton == 1)
  396.   {
  397.     Scene = 1;
  398.     tone(3, freq3, 100);
  399.   }
  400.   if(SW_state == 0 && SelectedButton == 2)
  401.   {
  402.     Scene = 6;
  403.     tone(3, freq3, 100);
  404.   }
  405.   oled.update();
  406. }
  407.  
  408. //Two player
  409. void Scene6(){
  410.   oled.clear();
  411.   MiddleLine();
  412.   PlayerPaddle();
  413.   Player2Paddle();
  414.   oled.circle(XDir, YDir, 2);
  415.   oled.update();
  416.   if(isGoingRight && isGoingUp)    {
  417.     XDir+=3;
  418.     YDir-=3;
  419.   }
  420.   if(!isGoingRight && isGoingUp)    {
  421.     XDir-=3;
  422.     YDir-=3;
  423.   }
  424.   if(isGoingRight && !isGoingUp)    {
  425.     XDir+=3;
  426.     YDir+=3;
  427.   }
  428.   if(!isGoingRight && !isGoingUp)    {
  429.     XDir-=3;
  430.     YDir+=3;
  431.   }
  432.   if(YDir == 5)
  433.   {
  434.     isGoingUp = false;
  435.     tone(3, freq, 100);
  436.   }
  437.   if(YDir == 59)
  438.   {
  439.     isGoingUp = true;
  440.     tone(3, freq, 100);
  441.   }
  442.   if(XDir <= 9 && XDir >= 7 && XDir != 5)
  443.   {
  444.     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)
  445.     {
  446.       isGoingRight = true;
  447.       tone(3, freq, 100);
  448.     }
  449.   }
  450.   if(XDir <= 123 && XDir >= 121 && XDir != 130)
  451.   {
  452.     if(YDir == player2YPos || YDir == player2YPos + 1 || YDir == player2YPos + 2 || YDir == player2YPos + 3 || YDir == player2YPos + 4 || YDir == player2YPos + 5 || YDir == player2YPos + 6 || YDir == player2YPos + 7 || YDir == player2YPos || YDir == player2YPos + 8 || YDir == player2YPos + 9 || YDir == player2YPos - 1 || YDir == player2YPos - 2 || YDir == player2YPos - 3 || YDir == player2YPos - 4 || YDir == player2YPos - 5 || YDir == player2YPos - 6 || YDir == player2YPos - 7 || YDir == player2YPos || YDir == player2YPos - 8 || YDir == player2YPos - 9)
  453.     {
  454.       isGoingRight = false;
  455.       tone(3, freq, 100);
  456.     }
  457.   }
  458.   if(XDir < 3)
  459.   {
  460.     Scene = 7;
  461.   }
  462.   if(XDir > 130)
  463.   {
  464.     Scene = 8;
  465.   }
  466.   x = analogRead(VRx);
  467.   x2 = analogRead(VRx2);
  468.   mapX = map(x, 0, 1023, -512, 512);
  469.   mapX2 = map(x2, 0, 1023, -512, 512);
  470.   if(mapX < -460)
  471.   {
  472.     playerYPos-=3;
  473.   }
  474.   if(mapX > 460)
  475.   {
  476.     playerYPos+=3;
  477.   }
  478.   if(mapX2 < -460)
  479.   {
  480.     player2YPos-=3;
  481.   }
  482.   if(mapX2 > 460)
  483.   {
  484.     player2YPos+=3;
  485.   }
  486.   if(playerYPos < 10)
  487.   {
  488.     playerYPos = 10;
  489.   }
  490.   if(playerYPos > 54)
  491.   {
  492.     playerYPos = 54;
  493.   }
  494.   if(player2YPos < 10)
  495.   {
  496.     player2YPos = 10;
  497.   }
  498.   if(player2YPos > 54)
  499.   {
  500.     player2YPos = 54;
  501.   }
  502. }
  503.  
  504. //Player 2 win
  505. void Scene7(){
  506.   XDir = 64;
  507.   YDir = 32;
  508.   playerYPos = 32;
  509.   player2YPos = 32;
  510.   isGoingRight = true;
  511.   isGoingUp = true;
  512.   int freq2;
  513.   for(int i = 5; i > 0; i--)
  514.   {
  515.     freq2 = i * 100;
  516.     oled.clear();
  517.     oled.setScale(1);
  518.     oled.setCursor(5, 0);
  519.     oled.print("Player 2 win!");
  520.     oled.setScale(3);
  521.     oled.setCursor(56, 4);
  522.     oled.print(i);
  523.     oled.update();
  524.     tone(3, freq2, 100);
  525.     delay(1000);
  526.   }
  527.   freq2 = 1000;
  528.   tone(3, freq2, 100);
  529.   Scene = 0;
  530. }
  531.  
  532. //Player 1 win
  533. void Scene8(){
  534.   XDir = 64;
  535.   YDir = 32;
  536.   playerYPos = 32;
  537.   player2YPos = 32;
  538.   isGoingRight = true;
  539.   isGoingUp = true;
  540.   int freq2;
  541.   for(int i = 5; i > 0; i--)
  542.   {
  543.     freq2 = i * 100;
  544.     oled.clear();
  545.     oled.setScale(1);
  546.     oled.setCursor(5, 0);
  547.     oled.print("Player 1 win!");
  548.     oled.setScale(3);
  549.     oled.setCursor(56, 4);
  550.     oled.print(i);
  551.     oled.update();
  552.     tone(3, freq2, 100);
  553.     delay(1000);
  554.   }
  555.   freq2 = 1000;
  556.   tone(3, freq2, 100);
  557.   Scene = 0;
  558. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement