Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.94 KB | None | 0 0
  1.  
  2. //start game: push button vs turn one of the pot
  3. //turn pot -> move paddle
  4. const byte ANODE_PINS[8] = {13, 12, 11, 10, 9, 8, 7, 6};
  5. const byte CATHODE_PINS[8] = {A3, A2, A1, A0, 5, 4, 3, 2};
  6. const byte INPUT_P1 = A4;
  7. const byte INPUT_P2 = A5;
  8. int ballX, ballY, dx, dy, p1Pos, p2Pos;
  9. int pattern[8][8] = {{0,0,0,0,0,0,0,0},
  10. {0,0,0,0,0,0,0,0},
  11. {0,0,0,0,0,0,0,0},
  12. {0,0,0,0,0,0,0,0},
  13. {0,0,0,0,0,0,0,0},
  14. {0,0,0,0,0,0,0,0},
  15. {0,0,0,0,0,0,0,0},
  16. {0,0,0,0,0,0,0,0}};
  17. int one[8][4] = {{0,0,0,0},
  18. {0,0,1,0},
  19. {0,0,1,0},
  20. {0,0,1,0},
  21. {0,0,1,0},
  22. {0,0,1,0},
  23. {0,0,1,0},
  24. {0,0,0,0}};
  25. int two[8][4] = {{0,0,0,0},
  26. {0,0,0,0},
  27. {0,0,0,0},
  28. {0,0,0,0},
  29. {0,0,0,0},
  30. {0,0,0,0},
  31. {0,0,0,0},
  32. {0,0,0,0}};
  33. int two[8][4] = {{0,0,0,0},
  34. {0,0,0,0},
  35. {0,0,0,0},
  36. {0,0,0,0},
  37. {0,0,0,0},
  38. {0,0,0,0},
  39. {0,0,0,0},
  40. {0,0,0,0}};
  41. int two[8][4] = {{0,0,0,0},
  42. {0,0,0,0},
  43. {0,0,0,0},
  44. {0,0,0,0},
  45. {0,0,0,0},
  46. {0,0,0,0},
  47. {0,0,0,0},
  48. {0,0,0,0}};
  49. int two[8][4] = {{0,0,0,0},
  50. {0,0,0,0},
  51. {0,0,0,0},
  52. {0,0,0,0},
  53. {0,0,0,0},
  54. {0,0,0,0},
  55. {0,0,0,0},
  56. {0,0,0,0}};
  57. int two[8][4] = {{0,0,0,0},
  58. {0,0,0,0},
  59. {0,0,0,0},
  60. {0,0,0,0},
  61. {0,0,0,0},
  62. {0,0,0,0},
  63. {0,0,0,0},
  64. {0,0,0,0}};
  65. //anodes on y, cathodes x
  66. int p1Score, p2Score;
  67. bool playGame;//false until turn pot
  68. bool ballUp, ballLeft = true;
  69.  
  70. unsigned long readUserInput = 0;
  71.  
  72. void setup() {
  73. Serial.begin(115200);
  74. for (byte i = 0; i < 8; i++) {
  75. pinMode(ANODE_PINS[i], OUTPUT);
  76. pinMode(CATHODE_PINS[i], OUTPUT);
  77. digitalWrite(ANODE_PINS[i], HIGH);
  78. digitalWrite(CATHODE_PINS[i], HIGH);
  79. }
  80.  
  81. pinMode(INPUT_P1, INPUT);
  82. pinMode(INPUT_P2, INPUT);
  83.  
  84. playGame = false;
  85. p1Score = 0;
  86. p2Score = 0;
  87.  
  88. restart();
  89. }
  90.  
  91. //starts ball at random postion
  92. void restart(){
  93. dx = 1;
  94. dy = 1;
  95. p1Pos = 3;
  96. p2Pos = 3;
  97. ballY = random(1,5);
  98. ballX = random(1,6);
  99.  
  100. }
  101.  
  102. void loop() {
  103. //
  104. if (millis() > readUserInput + 600) {
  105. handleCollision();
  106. updatePlayerPos();
  107. updateBall();
  108.  
  109. updateScreen();
  110. readUserInput = millis();
  111. }
  112.  
  113. if(p1Score >= 3 || p2Score >= 3){
  114. //end the game
  115.  
  116. }
  117. draw();
  118.  
  119. }
  120.  
  121. void handleCollision() {
  122. //if ball hits paddle
  123. if (ballY + dy == 0 && (ballX + dx >= p1Pos && ballX + dx < p1Pos + 3)) {
  124. dy *= -1;
  125. } else if (ballY == -1) {
  126. updateScore();
  127. restart();
  128. return;
  129. }
  130. if (ballY + dy == 7 && (ballX + dx >= p2Pos && ballX + dx < p2Pos + 3)) {
  131. dy *= -1;
  132. } else if (ballY == 8) {
  133. updateScore();
  134. restart();
  135. return;
  136. }
  137. if (ballX == 7) {
  138. dx *= -1;
  139. }
  140. if (ballX == 0) {
  141. dx *= -1;
  142. }
  143. }
  144.  
  145.  
  146. void updateBall() {
  147. ballY += dy;
  148. ballX += dx;
  149. // if (ballY == 7) {
  150. // dy *= -1;
  151. // }
  152. // if (ballY == 0) {
  153. // dy *= -1;
  154. // }
  155.  
  156. }
  157.  
  158. void draw(){
  159. for (byte i = 0; i < 8; i++) {
  160. for (byte j = 0; j < 8; j++) {
  161. if (pattern[i][j] == 1) {
  162. digitalWrite(CATHODE_PINS[j], LOW);
  163. } else {
  164. digitalWrite(CATHODE_PINS[j], HIGH);
  165. }
  166. }
  167. digitalWrite(ANODE_PINS[i], LOW);
  168. delayMicroseconds(5);
  169. digitalWrite(ANODE_PINS[i], HIGH);
  170. }
  171. }
  172.  
  173. void updateScreen() {
  174. for (int i = 0; i < 8; i++){
  175. for (int j = 0; j < 8; j++){
  176. if (i == ballY && j == ballX) {
  177. pattern[i][j] = 1;
  178. } else {
  179. pattern[i][j] = 0;
  180. }
  181. }
  182. }
  183. for(int i = 0; i < 8; i++){
  184. if(i >= p1Pos && i < p1Pos + 3){
  185. pattern[0][i] = 1;
  186. }else{
  187. if (ballX != i) pattern[0][i] = 0;
  188. }
  189. if(i >= p2Pos && i < p2Pos + 3){
  190. pattern[7][i] = 1;
  191. }else{
  192. if (ballX != i) pattern[7][i] = 0;
  193. }
  194. }
  195.  
  196. }
  197. void updatePlayerPos(){
  198. p1Pos = readPot1(analogRead(INPUT_P1), p1Pos);
  199. p2Pos = readPot2(analogRead(INPUT_P2), p2Pos);
  200. p1Pos = max(p1Pos, 0);
  201. p2Pos = max(p2Pos, 0);
  202. p1Pos = min(p1Pos, 5);
  203. p2Pos = min(p2Pos, 5);
  204. }
  205.  
  206. int readPot1(int potVal, int playerPos){
  207. if(potVal < 50){
  208. playerPos++;
  209. } else if(potVal > 200){
  210. playerPos--;
  211. }
  212. return playerPos;
  213. }
  214.  
  215. int readPot2(int potVal, int playerPos){
  216. if(potVal >= 960){
  217. playerPos--;
  218. } else if(potVal < 750){
  219. playerPos++;
  220. }
  221. return playerPos;
  222. }
  223.  
  224. void displayScore(number, bool whichHalf){
  225. clearBoard();
  226. //0
  227. if (whichHalf)
  228. for i 1...8
  229. for j 1...4
  230. pattern ij = number ij
  231. else
  232. for i 1.....8
  233. for j 5.....8
  234. pattern ij = number ij
  235. }
  236.  
  237. void updateScore(){
  238. int currScore;
  239. //update the players score...via var that keeps track which row the ball ended at
  240. if(ballY == 0){
  241. p1Score++;
  242. currScore = p1Score;
  243. } else {
  244. p2Score++;
  245. currScore = p2Score;
  246. }
  247. //turn all lights off
  248. clearBoard();
  249.  
  250. //pattern score in some way for a hot sec
  251. displayScore();
  252. }
  253.  
  254. void clearBoard(){
  255. for(int i = 0; i < 8; i++){
  256. for(int j = 0; j < 8; j++){
  257. pattern[i][j] = 0;
  258. }
  259. }
  260. draw();
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement