Advertisement
Guest User

Arduino Pong

a guest
Nov 23rd, 2010
4,169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.28 KB | None | 0 0
  1. /********
  2.  * Arduino Pong
  3.  * By Pete Lamonica
  4.  *
  5.  * A simple implementation of Pong on the Arduino using a TV for output.
  6.  *
  7.  * Video here: www.youtube.com/watch?v=m_PoMv7Mt28
  8.  *
  9.  * Creative Commons Non-Commerical/Attribution/Share-Alike license
  10.  * creativecommons.org/licenses/by-nc-sa/2.0/
  11.  *********/
  12.  
  13. #include <TVout.h>
  14.  
  15. #define WHEEL_ONE_PIN 0 //analog
  16. #define WHEEL_TWO_PIN 0 //analog
  17. #define BUTTON_ONE_PIN 2 //digital
  18.  
  19. #define PADDLE_HEIGHT 12
  20. #define PADDLE_WIDTH 2
  21.  
  22. #define RIGHT_PADDLE_X (TV.horz_res()-4)
  23. #define LEFT_PADDLE_X 2
  24.  
  25. #define IN_GAME 0 //in game state
  26. #define IN_MENU 1 //in menu state
  27. #define GAME_OVER 2 //game over state
  28.  
  29. #define LEFT_SCORE_X (TV.horz_res()/2-15)
  30. #define RIGHT_SCORE_X (TV.horz_res()/2+10)
  31. #define SCORE_Y 4
  32.  
  33. #define MAX_Y_VELOCITY 3
  34. #define PLAY_TO 7
  35.  
  36. #define LEFT 0
  37. #define RIGHT 1
  38.  
  39. TVout TV;
  40. unsigned char x,y;
  41. boolean buttonStatus = false;
  42. int wheelOnePosition = 0;
  43. int wheelTwoPosition = 0;
  44. int rightPaddleY = 0;
  45. int leftPaddleY = 0;
  46. unsigned char ballX = 0;
  47. unsigned char ballY = 0;
  48. char ballVolX = 1;
  49. char ballVolY = 1;
  50.  
  51. int leftPlayerScore = 0;
  52. int rightPlayerScore = 0;
  53.  
  54. int frame = 0;
  55. int state = IN_MENU;
  56.  
  57. void processInputs() {
  58.   wheelOnePosition = analogRead(WHEEL_ONE_PIN);
  59.   wheelTwoPosition = analogRead(WHEEL_TWO_PIN);
  60.   buttonStatus = (digitalRead(BUTTON_ONE_PIN) == HIGH);
  61. }
  62.  
  63. void drawGameScreen() {
  64.   TV.clear_screen();
  65.   //draw right paddle
  66.   rightPaddleY = ((wheelOnePosition / 8) * (TV.vert_res()-PADDLE_HEIGHT))/ 128;
  67.   x = RIGHT_PADDLE_X;
  68.   for(int i=0; i<PADDLE_WIDTH; i++) {
  69.     TV.draw_line(x+i,rightPaddleY,x+i,rightPaddleY+PADDLE_HEIGHT,1);
  70.   }
  71.  
  72.   //draw left paddle
  73.   leftPaddleY = ((wheelTwoPosition / 8) * (TV.vert_res()-PADDLE_HEIGHT))/ 128;
  74.   x = LEFT_PADDLE_X;
  75.   for(int i=0; i<PADDLE_WIDTH; i++) {
  76.     TV.draw_line(x+i,leftPaddleY,x+i,leftPaddleY+PADDLE_HEIGHT,1);
  77.   }
  78.  
  79.   //draw score
  80.   TV.print_char(LEFT_SCORE_X,SCORE_Y,'0'+leftPlayerScore);
  81.   TV.print_char(RIGHT_SCORE_X,SCORE_Y,'0'+rightPlayerScore);
  82.  
  83.   //draw net
  84.   for(int i=1; i<TV.vert_res() - 4; i+=6) {
  85.     TV.draw_line(TV.horz_res()/2,i,TV.horz_res()/2,i+3, 1);
  86.   }
  87.  
  88.   //draw ball
  89.   TV.set_pixel(ballX, ballY, 2);
  90. }
  91.  
  92. //player == LEFT or RIGHT
  93. void playerScored(byte player) {
  94.   if(player == LEFT) leftPlayerScore++;
  95.   if(player == RIGHT) rightPlayerScore++;
  96.  
  97.   //check for win
  98.   if(leftPlayerScore == PLAY_TO || rightPlayerScore == PLAY_TO) {
  99.     state = GAME_OVER;
  100.   }
  101.  
  102.   ballVolX = -ballVolX;
  103. }
  104.  
  105. void drawMenu() {
  106.   x = 0;
  107.   y = 0;
  108.   char volX = 1;
  109.   char volY = 1;
  110.   TV.clear_screen();
  111.   TV.select_font(_8X8);
  112.   TV.print_str(10, 5, "Arduino Pong");
  113.   TV.select_font(_5X7);
  114.   TV.print_str(22, 35, "Press Button");
  115.   TV.print_str(50, 45, "To Start");
  116.   delay(1000);
  117.   while(!buttonStatus) {
  118.     processInputs();
  119.     TV.delay_frame(3);
  120.     if(x + volX < 1 || x + volX > TV.horz_res() - 1) volX = -volX;
  121.     if(y + volY < 1 || y + volY > TV.vert_res() - 1) volY = -volY;
  122.     if(TV.get_pixel(x + volX, y + volY)) {
  123.       TV.set_pixel(x + volX, y + volY, 0);
  124.      
  125.       if(TV.get_pixel(x + volX, y - volY) == 0) {
  126.         volY = -volY;
  127.       }
  128.       else if(TV.get_pixel(x - volX, y + volY) == 0) {
  129.         volX = -volX;
  130.       }
  131.       else {
  132.         volX = -volX;
  133.         volY = -volY;
  134.       }
  135.     }
  136.     TV.set_pixel(x, y, 0);
  137.     x += volX;
  138.     y += volY;
  139.     TV.set_pixel(x, y, 1);
  140.   }
  141.   TV.select_font(_5X7);
  142.   state = IN_GAME;
  143. }
  144.  
  145. void setup()  {
  146.   Serial.begin(9600);
  147.   x=0;
  148.   y=0;
  149.   TV.start_render(_NTSC);   //for devices with only 1k sram(m168) use TV.begin(_NTSC,128,56)
  150.  
  151.   ballX = TV.horz_res() / 2;
  152.   ballY = TV.vert_res() / 2;
  153. }
  154.  
  155. void loop() {
  156.   processInputs();
  157.  
  158.   if(state == IN_MENU) {
  159.     drawMenu();
  160.   }
  161.   if(state == IN_GAME) {
  162.     if(frame % 3 == 0) { //every third frame
  163.       ballX += ballVolX;
  164.       ballY += ballVolY;
  165.  
  166.       if(ballY <= 1 || ballY >= TV.vert_res()-1) ballVolY = -ballVolY;
  167.       if(ballVolX < 0 && ballX == LEFT_PADDLE_X+PADDLE_WIDTH-1 && ballY >= leftPaddleY && ballY <= leftPaddleY + PADDLE_HEIGHT) {
  168.         ballVolX = -ballVolX;
  169.         ballVolY += 2 * ((ballY - leftPaddleY) - (PADDLE_HEIGHT / 2)) / (PADDLE_HEIGHT / 2);
  170.       }
  171.       if(ballVolX > 0 && ballX == RIGHT_PADDLE_X && ballY >= rightPaddleY && ballY <= rightPaddleY + PADDLE_HEIGHT) {
  172.         ballVolX = -ballVolX;
  173.         ballVolY += 2 * ((ballY - rightPaddleY) - (PADDLE_HEIGHT / 2)) / (PADDLE_HEIGHT / 2);
  174.       }
  175.  
  176.       //limit vertical speed
  177.       if(ballVolY > MAX_Y_VELOCITY) ballVolY = MAX_Y_VELOCITY;
  178.       if(ballVolY < -MAX_Y_VELOCITY) ballVolY = -MAX_Y_VELOCITY;
  179.  
  180.       if(ballX <= 1) {
  181.         playerScored(RIGHT);
  182.       }
  183.       if(ballX >= TV.horz_res() - 1) {
  184.         playerScored(LEFT);
  185.       }
  186.     }
  187.     if(buttonStatus) Serial.println((int)ballVolX);
  188.  
  189.     drawGameScreen();
  190.   }
  191.   if(state == GAME_OVER) {
  192.     drawGameScreen();
  193.     TV.select_font(_8X8);
  194.     TV.print_str(29,25,"GAME");
  195.     TV.print_str(68,25,"OVER");
  196.     while(!buttonStatus) {
  197.       processInputs();
  198.       delay(50);
  199.     }
  200.     TV.select_font(_5X7); //reset the font
  201.     //reset the scores
  202.     leftPlayerScore = 0;
  203.     rightPlayerScore = 0;
  204.     state = IN_MENU;
  205.   }
  206.  
  207.  
  208.   TV.delay_frame(1);
  209.   if(++frame == 60) frame = 0; //increment and/or reset frame counter
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement