Advertisement
Guest User

Arduino_Pong

a guest
Jul 21st, 2012
598
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 17.74 KB | None | 0 0
  1. #include <U8glib.h> //Graphic library for GLCD
  2. #include <stdlib.h> //Standard IO from C, used for itoa
  3.  
  4. U8GLIB_ST7920_128X64 u8g(13,11,10, U8G_PIN_NONE); //Setting up the screen with the u8glib
  5.  
  6. /*########################## THIS CODE IS UNDER GPL LICENSE ###############################
  7.   #                                                                                       #
  8.   #  I'd like to thank the creator of the U8Glib -olikraus- for his work on GLCDs         #
  9.   #  I'd like to thank MrPantalon too for his software BitmapU8GlibCreator that came      #
  10.   #  really useful here.                                                                  #
  11.   #  Here's the link to the u8Glib : http://code.google.com/p/u8glib/                     #
  12.   #  Here's the link to BitmapU8GlibCreator : http://tinyurl.com/bvfuu2a                  #
  13.   #                                                                                       #
  14.   #  Don't hesitate to e-mail me for serious questions : antonin.aumetre@gmail.com        #
  15.   ###### THIS SOFTWARE WAS CREATED BY ANTONIN AUMETRE INSPIRED BY ATARI's 1972 PONG ######*/
  16.  
  17. //All this hexadecimal values are the bmp images used in the game, the were created using BitmapU8GlibCreator
  18.  
  19. //This one is the "Pong" at the beginning
  20. const uint8_t fond_2[] U8G_PROGMEM = {
  21.   0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,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,0x83,0xc0,0x0,0x10,0x3e,0x7,0xc3,0x1e,0x40,0x78,0x20,0x3f,0x7,0x80,0x0,0x7,0x1,0xc0,0x0,0x18,0x1c,0xf,0xce,0xe,0x40,0x78,0x30,0x1c,0x7,0x80,0x0,0x4,0x0,0x40,0x0,0xc,0x0,0x1f,0xc8,0x7,0x60,0x78,0x18,0x0,0x7,0x80,0x0,0x4,0x0,0x60,0x0,0xe,0x0,0x3f,0x88,0x7,0x20,0x78,0x1c,0x0,0x7,0x80,0x0,0x4,0x0,0x70,0x0,0x7,0x80,0xff,0x88,0x7,0xa0,0x78,0xf,0x0,0xc7,0x80,0x0,0x7,0xff,0xf8,0x0,0x7,0xff,0xff,0xf,0xff,0xdf,0xf8,0x7,0xff,0xff,0x80,
  28.   0x0,0x7,0xff,0xf8,0x0,0x3,0xff,0xff,0xf,0xff,0xdf,0xf8,0x3,0xff,0xff,0x80,0x0,0x3,0xff,0xf8,0x0,0x1,0xff,0xfc,0x7,0xff,0xcf,0xf8,0x1,0xff,0xff,0x80,0x0,0x1,0xff,0xf8,0x0,0x0,0x7f,0xf8,0x3,0xff,0xc7,0xf8,0x0,0x7f,0xff,0x80,0x0,0x0,0xff,0xf8,0x0,0x0,0xf,0xc0,0x1,0xff,0xc3,0xf8,0x0,0x1f,0xc3,0x80,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0
  29. };//128x64
  30.  
  31. //This one is the "This is" at the beginning
  32. const uint8_t fond_1[] U8G_PROGMEM = {
  33.   0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x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0,0x0,0x0,0x0,0x0,0x0
  41. }; //128x64
  42.  
  43. //These are the arrows, one empty and the other filled
  44. const uint8_t haut_blanc[] U8G_PROGMEM = {
  45.   0x18,0x0,0x24,0x0,0x42,0x0,0x81,0x0,0x81,0x0,0x66,0x0,0x24,0x0,0x24,0x0,
  46.   0x24,0x0,0x24,0x0,0x18,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0
  47. };
  48. const uint8_t haut_noir[] U8G_PROGMEM = {
  49.   0x30,0x78,0xfc,0x30,0x30,0x30,0x30,0x30};
  50.  
  51. //This is the ball
  52. const uint8_t balle[] U8G_PROGMEM = {
  53.   0x38,0x4c,0x8a,0x92,0x92,0x64,0x38,0x0
  54. };//7x7
  55.  
  56. //Declaration of functions
  57. void right_bounce(void);
  58. void left_bounce(void);
  59. void initialisation(void);
  60. void racket_movements(void);
  61. void game(void);
  62. void intro(void);
  63.  
  64. //Declaration of variables
  65. int L=2, R=52, a=0, b=0, Posx=64, Posy=32, x1=0, y1=0, scoreL=0, scoreR=0, ball_speed = 2;
  66. const unsigned short Right=A1, Left=A0, racket_speed=2;
  67. boolean start=false, x=false, y=false, angle_bounce=true, l=false, r=false;
  68.  
  69. void setup(void) {  
  70.   u8g.setColorIndex(1); //Setting the index as 1 ==> the functions will draw "black" pixels
  71.   pinMode(Right, INPUT); //Setting the A1 pin as input
  72.   pinMode(Left, INPUT); //Setting the A0 pin as input
  73.  
  74.   while(r==false || l==false){ //Here we wait for the players to push their joystick up
  75.     a=analogRead(Right); //Reading the analog values of each joystick
  76.     b=analogRead(Left);
  77.     if(a>800){ //I took 800 because I think it's a good value, the stick has to be pressed but not at 100%
  78.       r=true;
  79.     }
  80.     if(b>800){
  81.       l=true; //If the stick is pushed till the analog value is 800, we consider the left one as pressed
  82.     }
  83.     u8g.firstPage(); //That's the function as described on the website of the u8glib
  84.     do {
  85.       intro(); //This function draws the first screen
  86.     }
  87.     while(u8g.nextPage());
  88.   }
  89.   delay(2000); //After the two players pushed their joystick, we wait 2 seconds to start the game
  90. }
  91.  
  92. void loop(void){
  93.   if (start==false){ //Start is false by default and it will be set false whenever a point is marked
  94.     initialisation(); //When a point is marked, we have to initialise the game
  95.   }
  96.   racket_movements(); //This function determines the position of the two rackets depending on the joysticks movements
  97.  
  98.   //Here are described the movements of the ball depending on Posx and Posy
  99.   if(x==true){
  100.     Posx+=ball_speed; //The ball will move to the right
  101.   }
  102.   if(x==false){
  103.     Posx-=ball_speed; //The ball will move to the left
  104.   }
  105.   if(y==true && angle_bounce==true){ //angle_bounce is when the ball bounces on the racket with an angle, not directly
  106.     Posy+=ball_speed; //The ball will move down
  107.   }
  108.   if(y==false && angle_bounce==true){
  109.     Posy-=ball_speed; //The ball will move up
  110.   }
  111.   if(angle_bounce==false){ //If there's no angle, it's a direct shot
  112.     ball_speed=5; //As it's a direct shot, the speed is increased
  113.   }
  114.   else{
  115.     ball_speed=2; //If no direct shot, the ball moves slower
  116.   }
  117.  
  118.   //Horizontal limitations & bounce
  119.   if(Posy<=3){ //If the ball hits the upper wall
  120.     y=true; //As described before, the ball will bounce down
  121.   }
  122.   if(Posy>=60){ //If the ball hits the lower wall
  123.     y=false; //As described before, the ball will bounce up
  124.   }
  125.  
  126.   //Bounces on the rackets
  127.   //Bounce on the left racket
  128.   if (Posx<64){ //This tells arduino to look for bounces only if the ball is in the half of the screen where it can happend
  129.     left_bounce();
  130.   }
  131.   //Bounce on the right racket
  132.   if (Posx>64){ //This tells arduino to look for bounces only if the ball is in the half of the screen where it can happend
  133.     right_bounce();
  134.   }
  135.  
  136.   //Scoring
  137.   if(Posx<=0){ //If the ball went behind the left racket
  138.     scoreL++; //Increments the left score
  139.     start=false; //Initalize the game
  140.   }
  141.   if(Posx>=127){ //If the ball went behind the right racket
  142.     scoreR++; //Increments the right score
  143.     start=false; //Initalize the game
  144.   }
  145.  
  146.  
  147.   u8g.firstPage(); //This is the u8glib function to draw the screen
  148.   do {
  149.     game(); //This corresponds to the game screen
  150.   }
  151.   while(u8g.nextPage());
  152. }
  153.  
  154. void intro(void){
  155.   u8g.drawBitmapP(0,0,16, 64, fond_1); //Draws the beginning screen
  156.   u8g.drawBitmapP(0,0,16, 64, fond_2);
  157.   u8g.drawBitmapP(64,7,2, 16, haut_blanc); //Draws the empty arrows
  158.   u8g.drawBitmapP(104,7,2, 16, haut_blanc);
  159.   if(r==true){ //If the corresponding Joystick has been pushed, fill the arrow
  160.     u8g.drawBitmapP(105,9,1, 8, haut_noir);
  161.   }
  162.   if(l==true){
  163.     u8g.drawBitmapP(65,9,1, 8, haut_noir);
  164.   }
  165. }
  166.  
  167. void game(void) {
  168.   u8g.drawBitmapP(Posx-3,Posy-3,1,8,balle); //Draw the ball
  169.   u8g.drawFrame(0,0,128,64); //Draws a frame
  170.   for (int i=0;i<64;i=i+8){ //Draws a doted line at the middle
  171.     u8g.drawLine(64,i,64,i+4);
  172.   }  
  173.   u8g.drawLine(124,R,124,R+9); //Draws the right racket
  174.   u8g.drawLine(3,L,3,L+9); //Draws the left racket
  175.  
  176.   u8g.setFont(u8g_font_5x7); //Set a default font
  177.   /*The itoa function converts an int to a char. I used it here because the
  178.   function draw.Str only takes a char*/
  179.   char buffer1[33];
  180.   itoa(scoreR, buffer1, 10);
  181.   char buffer2[33];
  182.   itoa(scoreL, buffer2, 10);
  183.  
  184.   u8g.drawStr(54,8,buffer1); //Draws the score of each player
  185.   u8g.drawStr(66,8,buffer2);
  186. }
  187.  
  188. //FUNCTIONS
  189.  
  190. void initialisation(void){
  191.   Posx=64; //Setting the ball at the middle of the screen
  192.   Posy=32;
  193.   randomSeed(analogRead(A7)); //We use A7 to create a random seed wich is random by the noise on analogic pins
  194.   x1=random(0,100); //We take two random numbers from 0 to 100
  195.   y1=random(0,100);
  196.  
  197.   // All we do here is randomly give the ball a direction, given by a couple of numbers. For instance (Right, Up) or (Left, Up)
  198.   if(x1<50){
  199.     x=false;
  200.   }
  201.   if(x1>50){
  202.     x=true;
  203.   }
  204.   if(y1<50){
  205.     y=false;
  206.   }
  207.   if(y1>50){
  208.     y=true;
  209.   }
  210.   start=!start; // Start was false, we set it to true
  211. }
  212.  
  213. void racket_movements(void){
  214.   a=analogRead(Right); //We take some input from each joystick
  215.   b=analogRead(Left);  
  216.   //Controlling the racket on the right
  217.   if (a > 800 && R>2){
  218.     R-=racket_speed;
  219.   }
  220.   if (a < 200 && R<52){
  221.     R+=racket_speed;
  222.   }
  223.   //Controlling the racket on the left
  224.   if (b > 800 && L>2){
  225.     L-=racket_speed;
  226.   }
  227.   if (b < 200 && L<52){
  228.     L+=racket_speed;
  229.   }
  230. }
  231.  
  232. /* For the following functions, the if statements are built this way :
  233. if ( limits_of_the_part_of_the_racket_considered_on_the_X_axis  AND limits_of_the_part_of_the_racket_considered_on_the_Y_axis )
  234. This means we have a rectangle defined around the part of the racket we want to control on wich the ball will bounce.
  235. I described how it orks for the left racket, it's the same for the right one, only directions on wich the ball
  236. will go change.
  237. */
  238. void left_bounce(void){
  239.   if(Posx>2 && Posx<9 && Posy>=L-2 && Posy<L+3){ //Upper part of the racket
  240.     if(angle_bounce==false){ //If direct hit
  241.       y=false; //The ball goes up
  242.     }
  243.     x=true; //The ball moves to the right
  244.     angle_bounce=true; //If bounce on upper or lower side, stops direct shots
  245.   }
  246.   if(Posx>2 && Posx<9 && Posy>L+6 && Posy<=L+11){ //Lower part of the racket
  247.     if(angle_bounce==false){ //If direct hit
  248.       y=true;  //The ball goes down
  249.     }
  250.     x=true; //The ball moves to the right
  251.     angle_bounce=true; //If bounce on upper or lower side, stops direct shots
  252.   }
  253.   if(Posx>2 && Posx<9 && Posy>=L+3 && Posy<=L+6){ //Middle part of the racket
  254.     x=true; //The ball moves to the right
  255.     angle_bounce=false; //Do a direct shot
  256.   }
  257. }
  258.  
  259. void right_bounce(void){
  260.   if(Posx>117 && Posx<125 && Posy>=R-2 && Posy<R+3){ //Upper part of the racket
  261.     if(angle_bounce==false){
  262.       y=false;
  263.     }
  264.     x=false;
  265.     angle_bounce=true;
  266.   }
  267.   if(Posx>117 && Posx<125 && Posy>R+6 && Posy<=R+11){ //Lower part of the racket
  268.     if(angle_bounce==false){
  269.       y=true;
  270.     }
  271.     x=false;
  272.     angle_bounce=true;
  273.   }
  274.   if(Posx>117 && Posx<125 && Posy>=R+3 && Posy<=R+6){ //Middle part of the racket
  275.     x=false;
  276.     angle_bounce=false;
  277.   }
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement