Advertisement
Dalemaunder

Untitled

Aug 7th, 2017
596
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.92 KB | None | 0 0
  1. /*Original 'Pong' code taken from the Gamebuino libraries example sketches under the Intermediate section.
  2. *
  3. Edits made by Dale Maunder, 8/8/2017, edits add basic 1-axis joystick functionality via the I2C pins on the MAKERbuino console.
  4.  
  5. A compatible joystick can be found at https://www.adafruit.com/product/512 or other region specific stores,
  6. (I personally used a Duinotech joystick module from Jaycar Electronics, Australia).
  7.  
  8. Edits made by myself are indicated by lines of #'s surrounding the specific code, both original and added deprecated code has been commented out.
  9. */
  10.  
  11. #include <SPI.h>
  12. #include <Gamebuino.h>
  13. Gamebuino gb;
  14.  
  15. boolean paused = false;
  16. //player variables
  17. int player_score = 0;
  18. int player_h = 16;
  19. int player_w = 3;
  20. int player_x = 0;
  21. int player_y = (LCDHEIGHT-player_h)/2;
  22. int player_vy = 3; //Original value = 2, a higher value makes the player move faster.
  23. //oponent variables
  24. int oponent_score = 0;
  25. int oponent_h = 16;
  26. int oponent_w = 3;
  27. int oponent_x = LCDWIDTH-oponent_w;
  28. int oponent_y = (LCDHEIGHT-oponent_h)/2;
  29. int oponent_vy = 2;
  30. //ball variables
  31. int ball_size = 6;
  32. int ball_x = LCDWIDTH - ball_size - oponent_w - 1;
  33. int ball_y = (LCDHEIGHT-ball_size)/2;
  34. int ball_vx = 3;
  35. int ball_vy = 3;
  36.  
  37. extern const byte font5x7[]; //get the default large font
  38.  
  39. //############################################################
  40. const int JoyY_pin = A5; // Analog pin connected to joystick's Y axis output
  41. //const int JoyX_pin = A4; //X axis is not used but has been left in for reference
  42. int JoyY_pos = 0; // Variable to store position of joystick's Y axis
  43. //############################################################
  44.  
  45.  
  46.  
  47. void setup() {
  48. gb.begin();
  49. gb.display.setFont(font5x7); //change the font to the large one
  50. gb.titleScreen(F("Pong Solo"));
  51. gb.pickRandomSeed(); //pick a different random seed each time for games to be different
  52. gb.battery.show = false; //hide the battery indicator
  53.  
  54. //############################################################
  55. pinMode(JoyY_pin, INPUT); //Configures pin on microcontroller
  56. //pinMode(JoyX_pin, INPUT);
  57. //############################################################
  58.  
  59. }
  60.  
  61.  
  62.  
  63.  
  64. void loop() {
  65. //############################################################
  66. if(analogRead(JoyY_pin) >= 700){
  67. JoyY_pos = 1; //Joystick is in down position
  68. }
  69. else if(analogRead(JoyY_pin) <= 600){
  70. JoyY_pos = 2; //Joystick is in up position
  71. }
  72. else{
  73. JoyY_pos = 0; //Joystick is inside dead spot at or near its home positon
  74. }
  75. //############################################################
  76.  
  77.  
  78. if(gb.update()){
  79. //pause the game if C is pressed
  80. if(gb.buttons.pressed(BTN_C)){
  81. gb.titleScreen(F("Pong Solo"));
  82. gb.battery.show = false;
  83. gb.display.fontSize = 2;
  84. }
  85.  
  86. /*
  87. //original move the player
  88. if(gb.buttons.repeat(BTN_UP, 1)){
  89. player_y = max(0, player_y - player_vy);
  90. }
  91. if(gb.buttons.repeat(BTN_DOWN, 1)){
  92. player_y = min(LCDHEIGHT - player_h, player_y + player_vy);
  93. }
  94. */
  95.  
  96. //############################################################
  97. //edited move the player
  98.  
  99. if(JoyY_pos == 2){
  100. player_y = max(0, player_y - player_vy);
  101. }
  102. if(JoyY_pos == 1){
  103. player_y = min(LCDHEIGHT - player_h, player_y + player_vy);
  104. }
  105. //#######################################################################
  106.  
  107.  
  108.  
  109. //move the ball
  110. ball_x = ball_x + ball_vx;
  111. ball_y = ball_y + ball_vy;
  112.  
  113. //check for ball collisions
  114. //collision with the top border
  115. if(ball_y < 0){
  116. ball_y = 0;
  117. ball_vy = -ball_vy;
  118. gb.sound.playTick();
  119. }
  120. //collision with the bottom border
  121. if((ball_y + ball_size) > LCDHEIGHT){
  122. ball_y = LCDHEIGHT - ball_size;
  123. ball_vy = -ball_vy;
  124. gb.sound.playTick();
  125. }
  126. //collision with the player
  127. if(gb.collideRectRect(ball_x, ball_y, ball_size, ball_size, player_x, player_y, player_w, player_h)){
  128. ball_x = player_x + player_w;
  129. ball_vx = -ball_vx;
  130. gb.sound.playTick();
  131. }
  132. //collision with the oponent
  133. if(gb.collideRectRect(ball_x, ball_y, ball_size, ball_size, oponent_x, oponent_y, oponent_w, oponent_h)){
  134. ball_x = oponent_x - ball_size;
  135. ball_vx = -ball_vx;
  136. gb.sound.playTick();
  137. }
  138. //collision with the left side
  139. if(ball_x < 0){
  140. oponent_score = oponent_score + 1;
  141. gb.sound.playCancel();
  142. ball_x = LCDWIDTH - ball_size - oponent_w -1;
  143. ball_vx = -abs(ball_vx);
  144. ball_y = random(0,LCDHEIGHT-ball_size);
  145. }
  146. //collision with the right side
  147. if((ball_x + ball_size) > LCDWIDTH){
  148. player_score = player_score + 1;
  149. gb.sound.playOK();
  150. ball_x = LCDWIDTH - ball_size - oponent_w - 16; //place the ball on the oponent side
  151. ball_vx = -abs(ball_vx);
  152. ball_y = random(0,LCDHEIGHT-ball_size);
  153.  
  154. }
  155. //reset score when 10 is reached
  156. if((player_score == 10) || (oponent_score == 10)){
  157. player_score = 0;
  158. oponent_score = 0;
  159. }
  160.  
  161. //move the oponent
  162. if((oponent_y+(oponent_h/2)) < (ball_y+(ball_size/2))){ //if the ball is below the oponent
  163. oponent_y = oponent_y + oponent_vy; //move down
  164. oponent_y = min(LCDHEIGHT-oponent_h, oponent_y); //don't go out of the screen
  165. }
  166. else {
  167. oponent_y = oponent_y - oponent_vy; //move up
  168. oponent_y = max(0, oponent_y); //don't go out of the screen
  169. }
  170. }
  171.  
  172. //draw the score
  173. gb.display.fontSize = 2;
  174. gb.display.cursorX = 15;
  175. gb.display.cursorY = 16;
  176. gb.display.print(player_score);
  177.  
  178. gb.display.cursorX = 57;
  179. gb.display.cursorY = 16;
  180. gb.display.print(oponent_score);
  181. //draw the ball
  182. gb.display.fillRect(ball_x, ball_y, ball_size, ball_size);
  183. //draw the player
  184. gb.display.fillRect(player_x, player_y, player_w, player_h);
  185. //draw the oponent
  186. gb.display.fillRect(oponent_x,oponent_y, oponent_w, oponent_h);
  187.  
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement