Advertisement
Astranome

TVPong

Jun 16th, 2020
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.43 KB | None | 0 0
  1. #include <TVout.h>
  2. #include <fontALL.h>
  3.  
  4. TVout TV;
  5. int x, y, a, c1, c2, yb1, yb2, tm, tsh, s;
  6. float ts;
  7. boolean paused = false;
  8.  
  9. void setup ( )
  10. {
  11.   TV.begin(NTSC, 120, 96);
  12.   TV.clear_screen();
  13.   TV.select_font(font6x8);
  14.   TV.println( 0, 50, "Welcome to Shimo" );
  15.   TV.delay (5000);
  16.   TV.clear_screen();
  17.   x = 60;
  18.   y = 48;
  19.   yb1 = 48;
  20.   yb2 = 48;
  21.   a = 8;
  22.   ts = 900.0;
  23.   s = 2;
  24. }
  25.  
  26. void loop ( )
  27. {
  28.   if(!paused)
  29.   {
  30.     TV.draw_line(60,0,60,96,1);
  31.     TV.select_font(font8x8);
  32.     racketsmove();
  33.     ballscount();
  34.     TV.print(1,1,c1); TV.print(18,1,":"); TV.print(26,1,c2);
  35.     tm = ts / 60;
  36.     ts -= 0.04;
  37.     if(ts < 0)
  38.     {
  39.       ts = 0;
  40.     }
  41.     TV.draw_rect(81,1,38,10,0,0);
  42.     TV.print(81,1,tm); TV.print(97,1,"."); TV.print(100,1,int(ts-(tm*60)));
  43.     ballcol();
  44.     /*if(ts < 600)
  45.     {
  46.       s = 4;
  47.     }
  48.     if(ts < 300)
  49.     {
  50.       s = 6;
  51.     }*/
  52.     ballmove(s, a);
  53.     TV.delay(50);
  54.     if(digitalRead(6) == HIGH)
  55.     {
  56.       paused = true;
  57.       delay(1000);
  58.     }
  59.   }
  60.   else
  61.   {
  62.     TV.println(40,4,"pause");
  63.     if(digitalRead(6) == HIGH)
  64.     {
  65.       paused = false;
  66.       delay(1000);
  67.       TV.clear_screen();
  68.     }
  69.   }
  70. }
  71.  
  72. void ballscount()
  73. {
  74.   if(x == 1)
  75.   {
  76.     c2++;
  77.   }
  78.   if(x == 119)
  79.   {
  80.     c1++;
  81.   }
  82.   if(c1 > c2 && ts == 0)
  83.   {
  84.     TV.println(10, 45, "Player 1 won!");
  85.     delay(10000);
  86.     restart();
  87.   }
  88.   else if(c1 < c2 && ts == 0)
  89.   {
  90.     TV.println(10, 45, "Player 2 won!");
  91.     delay(10000);
  92.     restart();
  93.   }
  94.   else if(c1 == c2 && ts == 0)
  95.   {
  96.     TV.println(10, 45, "You are equal");
  97.     delay(10000);
  98.     restart();
  99.   }
  100. }
  101.  
  102. void ballcol()
  103. {
  104.   if(x == 1 || x == 119 || (x == 10 && y < (yb1 + 3) && y > (yb1 - 3)) || (x == 110 && y < (yb2 + 3) && y > (yb2 - 3)))
  105.   {
  106.     if(a==1){a=5;}else if(a==2){a=8;}else if(a==3){a=7;}else if(a==4){a=6;}else if(a==5){a=1;}else if(a==6){a=4;}else if(a==7){a=3;}else if(a==8){a=2;}
  107.   }
  108.   if(x == 10 && y < (yb1 - 3) && y > (yb1 - 8))
  109.   {
  110.     a = 2;
  111.   }
  112.   if(x == 10 && y > (yb1 + 3) && y < (yb1 + 8))
  113.   {
  114.     a = 4;
  115.   }
  116.   if(x == 110 && y < (yb2 - 3) && y > (yb2 - 8))
  117.   {
  118.     a = 8;
  119.   }
  120.   if(x == 110 && y > (yb2 + 3) && y < (yb2 + 8))
  121.   {
  122.     a = 6;
  123.   }
  124.   if(y == 95 || y == 1)
  125.   {
  126.     if(a==1){a=5;}else if(a==2){a=4;}else if(a==3){a=7;}else if(a==4){a=2;}else if(a==5){a=1;}else if(a==6){a=8;}else if(a==7){a=3;}else if(a==8){a=6;}
  127.   }
  128. }
  129.  
  130. void racketsmove()
  131. {
  132.   TV.draw_line(10, yb1+8, 10, yb1-8, 0);
  133.   TV.draw_line(110, yb2+8, 110, yb2-8, 0);
  134.   if((yb1 - 8) > 1)
  135.   {
  136.     if(digitalRead(2) == HIGH)
  137.     {
  138.       yb1 -= 2;
  139.     }
  140.   }
  141.   if((yb1 + 8) < 95)
  142.   {
  143.     if(digitalRead(3) == HIGH)
  144.     {
  145.       yb1 += 2;
  146.     }
  147.   }
  148.   if((yb2 - 8) > 1)
  149.   {
  150.     if(digitalRead(4) == HIGH)
  151.     {
  152.       yb2 -= 2;
  153.     }
  154.   }
  155.   if((yb2 + 8) < 95)
  156.   {
  157.     if(digitalRead(5) == HIGH)
  158.     {
  159.       yb2 += 2;
  160.     }
  161.   }
  162.   TV.draw_line(10, yb1+8, 10, yb1-8, 1);
  163.   TV.draw_line(110, yb2+8, 110, yb2-8, 1);
  164. }
  165.  
  166. void ballmove(int vel, int angle)
  167. {
  168.   TV.set_pixel(x,y,0);
  169.   if(angle == 1)
  170.   {
  171.     y -= vel;
  172.   }
  173.   if(angle == 3)
  174.   {
  175.     x += vel;
  176.   }
  177.   if(angle == 5)
  178.   {
  179.     y += vel;
  180.   }
  181.   if(angle == 7)
  182.   {
  183.     x -= vel;
  184.   }
  185.   if(angle == 2)
  186.   {
  187.     x += round(vel/2);
  188.     y -= round(vel/2);
  189.   }
  190.   if(angle == 4)
  191.   {
  192.     x += round(vel/2);
  193.     y += round(vel/2);
  194.   }
  195.   if(angle == 6)
  196.   {
  197.     x -= round(vel/2);
  198.     y += round(vel/2);
  199.   }
  200.   if(angle == 8)
  201.   {
  202.     x -= round(vel/2);
  203.     y -= round(vel/2);
  204.   }
  205.   TV.set_pixel(x,y,1);
  206. }
  207. void restart()
  208. {
  209.   TV.clear_screen();
  210.   x = 60;
  211.   y = 48;
  212.   yb1 = 48;
  213.   yb2 = 48;
  214.   a = 8;
  215.   ts = 900.0;
  216.   c1 = 0;
  217.   c2 = 0;
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement