Guest User

Retro Consola Pong

a guest
Jun 28th, 2019
465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <TVout.h>
  2. #include <fontALL.h>
  3.  
  4. #define JOY_BUTTON  3
  5. #define JOY_RIGHT   6
  6. #define JOY_LEFT    5
  7. #define JOY_UP      4
  8. #define JOY_DOWN    2
  9.  
  10. TVout TV;
  11.  
  12. int screen_width;
  13.  
  14. int font_size = 8;
  15.  
  16. int player_x, player_y;
  17. int player_size = 4;
  18.  
  19. unsigned int score;
  20.  
  21. int ball_x, ball_y;
  22. bool ball_left;
  23. bool ball_up;
  24. int ball_wait;
  25.  
  26. void updateScreen()
  27. {
  28.   //clear screen
  29.   TV.clear_screen();
  30.  
  31.   //draw score
  32.   TV.print("score: ");
  33.   TV.println(score);
  34.  
  35.   //draw ball
  36.   TV.print_char(ball_x*font_size, ball_y*font_size, 'o');
  37.  
  38.   //draw player
  39.   for (int i=0; i < player_size; i++)
  40.   {
  41.     TV.print_char((i+player_x)*font_size, player_y*font_size, '#');
  42.   }    
  43. }
  44.  
  45. void playBallSound()
  46. {
  47.   tone(11,666,25);
  48. }
  49.  
  50. void resetGame()
  51. {
  52.   player_x = screen_width/2 - player_size/2;
  53.   player_y = 11;
  54.   score = 0;
  55.  
  56.   ball_x = screen_width/2;
  57.   ball_y = player_y-1;
  58.   ball_left = true;
  59.   ball_up = true;
  60.   ball_wait = 0;
  61. }
  62.  
  63. void setup()  
  64. {
  65.   //joystick
  66.   pinMode(JOY_BUTTON, INPUT);
  67.   pinMode(JOY_RIGHT, INPUT);
  68.   pinMode(JOY_LEFT, INPUT);
  69.   pinMode(JOY_UP, INPUT);
  70.   pinMode(JOY_DOWN, INPUT);
  71.  
  72.   digitalWrite(JOY_BUTTON, HIGH);
  73.   digitalWrite(JOY_RIGHT, HIGH);
  74.   digitalWrite(JOY_LEFT, HIGH);
  75.   digitalWrite(JOY_UP, HIGH);
  76.   digitalWrite(JOY_DOWN, HIGH);
  77.  
  78.   //Serial.begin(9600);
  79.  
  80.   TV.begin(PAL);
  81.   TV.select_font(font8x8);
  82.  
  83.   screen_width = TV.char_line();
  84.  
  85.   resetGame();
  86. }
  87.  
  88. void loop()
  89. {
  90.   //check ball  
  91.   if (digitalRead(JOY_LEFT)==0 && player_x > 0)
  92.   {
  93.     //left
  94.     player_x--;
  95.   }
  96.   else if(digitalRead(JOY_RIGHT)==0 && player_x < screen_width - player_size)
  97.   {
  98.     //right
  99.     player_x++;
  100.   }
  101.  
  102.   if (++ball_wait > 1)
  103.   {
  104.     ball_wait = 0;
  105.  
  106.     if (ball_left)
  107.     {
  108.       // go right
  109.       if (--ball_x <= 0)
  110.       {
  111.         ball_x = 0;
  112.         ball_left = false;
  113.  
  114.         playBallSound();
  115.       }
  116.     }
  117.     else if (++ball_x >= screen_width-1)
  118.     {
  119.       //go left
  120.       ball_x = screen_width-1;
  121.       ball_left = true;
  122.  
  123.       playBallSound();
  124.     }
  125.    
  126.     if (ball_up)
  127.     {      
  128.       if (--ball_y <= 1)
  129.       {
  130.         //score and go down
  131.         score++;
  132.        
  133.         ball_y = 1;
  134.         ball_up = false;
  135.  
  136.         playBallSound();
  137.       }
  138.     }
  139.     else
  140.     {
  141.       ball_y++;
  142.      
  143.       if (ball_y == player_y-1 && ball_x >= player_x && ball_x < player_x + player_size)
  144.       {
  145.         //go up
  146.         ball_up = true;  
  147.  
  148.         playBallSound();
  149.       }
  150.       else if (ball_y >= player_y)
  151.       {
  152.         //die
  153.         updateScreen();
  154.  
  155.         TV.delay_frame(20);
  156.        
  157.         resetGame();
  158.  
  159.         updateScreen();      
  160.        
  161.         TV.delay_frame(40);
  162.       }
  163.     }
  164.   }
  165.  
  166.   updateScreen();  
  167.    
  168.   TV.delay_frame(2);
  169. }
Advertisement
Add Comment
Please, Sign In to add comment