Guest User

Ping Pong Arduino Processing

a guest
Sep 26th, 2017
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1.  
  2. /*
  3. Program to create a game and gets its motion values serially from 3rd PORT
  4. Programed by B.Aswinth Raj
  5. Dated on : 21-08-2016
  6. */
  7.  
  8.  
  9.  
  10. import processing.serial.*;
  11.  
  12. Serial port;
  13.  
  14. int data;
  15. int movby;
  16. float x=300;
  17. float y=00;
  18. float spdy=6;
  19. float spdx=0;
  20. float posxr,posxl,posyr,posyl,ballpos;
  21. int score = 0;
  22. float speed=0;
  23. float flag=0;
  24. float ballsize=20;
  25. float temp;
  26.  
  27. void setup()
  28. {
  29. size(600,600,P2D);
  30. smooth();
  31. port = new Serial(this,Serial.list()[0],9600);
  32. println(Serial.list());
  33. }
  34.  
  35.  
  36.  
  37.  
  38. void draw()
  39. {
  40. if (port.available()>0 )
  41. {
  42. data=port.read();
  43. movby = int(map(float(data), 255,0,30,570));
  44. println(movby);
  45. }
  46. // println(data);
  47.  
  48.  
  49. if (flag==0)
  50. strtscrn();
  51. if (flag==1)
  52. game();
  53. if (flag==3)
  54. lastscrn();
  55.  
  56.  
  57.  
  58. fill(18,250,8);
  59. textSize(12);
  60. text("Created by asWINth raj", 245, 590);
  61.  
  62. textSize(22);
  63. text("Score:",10,20);
  64. text(score, 80, 20);
  65. fill(0, 102, 153);
  66. }
  67.  
  68.  
  69. void strtscrn()
  70. {score=0;
  71. cursor();
  72. background(255); //Color of the backgroud
  73. fill(18,250,8);
  74. textSize(52);
  75. text("WELCOME", 200,300);
  76. fill(18,250,80);
  77. textSize(32);
  78. text("Select your Level", 200,330);
  79. fill(180,250,8);
  80. textSize(22);
  81. text("1 2 3 4", 250,370);
  82. //println(mouseX, mouseY);
  83. if (mouseY > 340 && mouseY < 380)
  84. {
  85. cursor(HAND);
  86. if(mousePressed==true && flag==0)
  87. {
  88.  
  89. if (mouseX >240 && mouseX <270)
  90. {
  91. speed=1;
  92. }
  93. if (mouseX >280 && mouseX <300)
  94. {
  95. speed=2;
  96. }
  97. if (mouseX >320 && mouseX <350)
  98. {
  99. speed=3;
  100. }
  101. if (mouseX >350 && mouseX <380)
  102. {
  103. speed=4;
  104. }
  105. flag=1;
  106. }
  107.  
  108. }
  109.  
  110. }
  111.  
  112.  
  113. void game()
  114. {
  115. noCursor();
  116. background(0); //Color of the backgroud
  117. y=y+spdy; //speed and positon of ball in Y axis
  118. x=x+spdx; //speed and positon of ball in X axis
  119.  
  120. rectMode(CENTER);
  121. fill(16,22,162);
  122. rect(movby,530,60,10); //The plate
  123.  
  124. posxr=movby+40;
  125. posxl=movby-40;
  126. posyr=530+15;
  127. posyl=530-10;
  128.  
  129. if (( (posyl < y) && (y < posyr) ) && ( (posxl < x) && (x < posxr) )) //Plate and ball meeting
  130. {
  131. spdy=-(speed*3);
  132.  
  133.  
  134. if (x<movby) //Pad left deflection
  135. {
  136. ballpos=movby-x;
  137. spdx=-(ballpos/5);
  138. // println(ballpos);
  139. }
  140.  
  141. if (x>movby) //Pad right deflection
  142. {
  143. ballpos=x-movby;
  144. spdx=+(ballpos/5);
  145. //println(ballpos);
  146. }
  147.  
  148. }
  149.  
  150. if (x<=0 ) // Left margin deflection
  151. spdx=(speed*3);
  152.  
  153.  
  154. if (y<=0 ) // Top margin deflection
  155. {
  156. spdy=(speed*3);
  157. score=score+1;
  158. }
  159.  
  160. if (x>=600) // Right margin deflection
  161. spdx=-(speed*3);
  162.  
  163. if (y>=600)
  164. {
  165. background(250,0,0);
  166. x=300;
  167. y=0;
  168. flag=3;
  169. speed=1;
  170. }
  171.  
  172. fill(88,250,68);
  173. ellipse(x,y,ballsize,20); //The ball
  174.  
  175.  
  176. if(score>=15 && score<=20)
  177. {
  178. ellipse(random(600),random(600),20,20);
  179. ellipse(random(600),random(600),20,20);
  180. ellipse(random(600),y,20,20);
  181. ellipse(x,random(600),20,20);///The ball
  182. }
  183.  
  184. temp=x;
  185.  
  186.  
  187. }
  188.  
  189.  
  190. void lastscrn()
  191. {
  192. background(250,0,0);
  193. textSize(32);
  194. text("GAME OVER", 200,300);
  195. fill(18,250,80);
  196. textSize(22);
  197. text("Your Score:", 200,330);
  198. text(score, 320,330);
  199. fill(180,250,8);
  200.  
  201. text("Click to try again..", 200,370);
  202. if(mousePressed == true)
  203. flag =0;
  204. }
Add Comment
Please, Sign In to add comment