Advertisement
Guest User

Untitled

a guest
May 27th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. int gameScreen = 0;
  2. int ballX, ballY;
  3. int ballSize = 20;
  4. int ballColor = color(0);
  5. float gravity = 1;
  6. float ballSpeedVert = 0;
  7. float airFriction = 0.0001;
  8. float friction = 0.1;
  9. color racketColor = color(0);
  10. float racketWidth = 100;
  11. float racketHeight = 10;
  12. int racketBounceRate = 20;
  13. float ballSpeedHorizon = 10;
  14.  
  15. int wallSpeed = 5;
  16. int wallInterval = 1000;
  17. float lastAddTime = 0;
  18. int minGapHeight = 200;
  19. int maxGapHeight = 300;
  20. int wallWidth = 80;
  21. color wallColors = color(0);
  22. ArrayList<int[]> walls = new ArrayList<int[]>();
  23. void setup()
  24. {
  25. size(500,500);
  26. ballX=width/4;
  27. ballY=height/5;
  28. }
  29. void draw()
  30. {
  31. if(gameScreen == 0) initScreen();
  32. if(gameScreen == 1) gameScreen();
  33. if(gameScreen == 2) gameOverScreen();
  34. }
  35. void initScreen()
  36. {
  37. background(0);
  38. textAlign(CENTER);
  39. text("Click to start", height/2, width/2);
  40. }
  41. void gameScreen()
  42. {
  43. background(255);
  44. drawBall();
  45. applyGravity();
  46. watchRacketBounce();
  47. keepInScreen();
  48. drawRacket();
  49. applyHorizontalSpeed();
  50. wallAdder();
  51. wallHandler();
  52. }
  53. void gameOverScreen()
  54. {
  55.  
  56. }
  57. public void mousePressed()
  58. {
  59. if(gameScreen == 0) startGame();
  60. }
  61. void startGame()
  62. {
  63. gameScreen=1;
  64. }
  65. void drawBall()
  66. {
  67. fill(ballColor);
  68. ellipse(ballX, ballY, ballSize, ballSize);
  69. }
  70. void applyGravity()
  71. {
  72. ballSpeedVert += gravity;
  73. ballY += ballSpeedVert;
  74. ballSpeedVert -= ballSpeedVert * airFriction;
  75. }
  76. void keepInScreen()
  77. {
  78. if(ballY+(ballSize/2) > height) makeBounceBottom(height);
  79. if(ballY-(ballSize/2) < 0) makeBounceTop(0);
  80. if(ballX-(ballSize/2) < 0) makeBounceLeft(0);
  81. if(ballX+(ballSize/2) > width) makeBounceRight(width);
  82. }
  83. void makeBounceBottom(int surface)
  84. {
  85. ballY = surface - (ballSize/2);
  86. ballSpeedVert *= -1;
  87. ballSpeedVert -= ballSpeedVert * friction;
  88. }
  89. void makeBounceTop(int surface)
  90. {
  91. ballY = surface + (ballSize/2);
  92. ballSpeedVert *= -1;
  93. ballSpeedVert -= ballSpeedVert * friction;
  94. }
  95. void drawRacket()
  96. {
  97. fill(racketColor);
  98. rectMode(CENTER);
  99. rect(mouseX,mouseY,racketWidth,racketHeight);
  100. }
  101. void watchRacketBounce()
  102. {
  103. float overhead = mouseY - pmouseY;
  104. if((ballX+(ballSize/2) > mouseX - (racketWidth/2)) &&
  105. (ballX-(ballSize/2) < mouseX + (racketWidth/2)))
  106. {
  107. if(dist(ballX,ballY, ballX, mouseY) <=(ballSize/2)+abs(overhead))
  108. {
  109. makeBounceBottom(mouseY);
  110. ballSpeedHorizon = (ballX-mouseX)/5;
  111. if(overhead<0)
  112. {
  113. ballY+=overhead;
  114. ballSpeedVert+=overhead;
  115. }
  116. }
  117. }
  118. }
  119. void applyHorizontalSpeed()
  120. {
  121. ballX += ballSpeedHorizon;
  122. ballSpeedHorizon -= (ballSpeedHorizon * airFriction);
  123. }
  124. void makeBounceLeft(int surface)
  125. {
  126. ballX = surface +(ballSize/2);
  127. ballSpeedHorizon *= -1;
  128. ballSpeedHorizon -= ballSpeedHorizon * friction;
  129. }
  130. void makeBounceRight(int surface)
  131. {
  132. ballX = surface -(ballSize/2);
  133. ballSpeedHorizon *= -1;
  134. ballSpeedHorizon -= ballSpeedHorizon * friction;
  135. }
  136. void wallAdder()
  137. {
  138. if(millis()-lastAddTime>wallInterval)
  139. {
  140. int randHeight = round(random(minGapHeight, maxGapHeight));
  141. int randY = round(random(0,height-randHeight));
  142. int[] randWall = {width, randY, wallWidth, randHeight};
  143. walls.add(randWall);
  144. lastAddTime = millis();
  145. }
  146. }
  147. void wallHandler()
  148. {
  149. for(int i = 0; i<walls.size(); i++)
  150. {
  151. wallRemover(i);
  152. wallMover(i);
  153. watchWallCollision(i);
  154. wallDrawer(i);
  155. }
  156. }
  157. void wallRemover(int index)
  158. {
  159. int[] wall = walls.get(index);
  160. if(wall[0] + wall[2] <=0) walls.remove(index);
  161. }
  162. void wallMover(int index)
  163. {
  164. int[] wall = walls.get(index);
  165. wall[0]-=wallSpeed;
  166. }
  167. void wallDrawer(int index)
  168. {
  169. int[] wall = walls.get(index);
  170. int gapWallX = wall[0];
  171. int gapWallY = wall[1];
  172. int gapWallWidth = wall[2];
  173. int gapWallHeight = wall[3];
  174. rectMode(CORNER);
  175. fill(wallColors);
  176. rect(gapWallX, 0, gapWallWidth, gapWallY);
  177. rect(gapWallX, gapWallY+gapWallHeight,gapWallWidth,
  178. height-gapWallY-gapWallHeight);
  179. }
  180. void watchWallCollision(int index)
  181. {
  182. int[] wall = walls.get(index);
  183. int gapWallX = wall[0];
  184. int gapWallY = wall[1];
  185. int gapWallWidth = wall[2];
  186. int gapWallHeight = wall[3];
  187. int wallTopX = gapWallX;
  188. int wallTopY = 0;
  189. int wallTopWidth = gapWallWidth;
  190. int wallTopHeight = gapWallY;
  191. int wallBottomX = gapWallX;
  192. int wallBottomY = gapWallY + gapWallHeight;
  193. int wallBottomWidth = gapWallWidth;
  194. int wallBottomHeight = height-gapWallY-gapWallHeight;
  195.  
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement