Advertisement
xFazz

Untitled

Oct 29th, 2020
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.58 KB | None | 0 0
  1.  
  2. PA4
  3.  
  4. //variables - Pacman
  5. float pacmanX = 80;
  6. float pacmanY = 80;
  7. float xDelta = 10;
  8. float yDelta = 10;
  9. final float PACMAN_SIZE = 70;
  10.  
  11. //variables - Ghost
  12. float ghostX = 600;
  13. float ghostY = 250;
  14. float ghostXDelta;
  15. float ghostYDelta;
  16. float GHOST_SIZE = 100;
  17.  
  18. //variables - Dots
  19. float [] dotsX;
  20. float [] dotsY;
  21. int numOfDots = 50;
  22.  
  23. // misc variables
  24. float dist;
  25. int playerScore = 0;
  26.  
  27. /*
  28. * setup - prepares environment size and color
  29. */
  30.  
  31. void setup() {
  32. //set canvas size and Pacman's color
  33. size(1200, 500);
  34.  
  35. //define dot positions
  36. dotsX = new float[numOfDots];
  37. dotsY = new float[numOfDots];
  38. assignDotPositions();
  39.  
  40. }
  41.  
  42. /*
  43. * draw - move pacman on diagonals and bounce off top
  44. * and bottom while rapping around left to right
  45. */
  46.  
  47. void draw() {
  48. background(0);
  49. drawDots();
  50. drawPacman();
  51. drawGhost();
  52. //move pacman
  53. //makePacmanBounce(); // Check bounds and make Pacman bounce off top/bottom
  54. makePacmanWrap(); // Allows for right/left wrapping
  55.  
  56. // move ghost
  57. moveGhost();
  58.  
  59. //Allows Pacman to eat dots
  60. for(int i = 0; i < numOfDots; i++) {
  61. dist = sqrt((dotsX[i] - pacmanX) * (dotsX[i] - pacmanX) + (dotsY[i] - pacmanY) * (dotsY[i] - pacmanY));
  62. if(dist <= 35.00) {
  63. dotsX[i] = -100;
  64. dotsY[i] = -100;
  65. playerScore++;
  66. break;
  67. }
  68. }
  69.  
  70. //display score
  71. displayScore();
  72. }
  73.  
  74. /*
  75. * drawPacman - draws a packman a the given x, y
  76. */
  77.  
  78. void drawPacman() {
  79. fill(255, 255, 0);
  80. arc(pacmanX, pacmanY, PACMAN_SIZE, PACMAN_SIZE, 0.52, 5.76);
  81. }
  82.  
  83. /*
  84. * makePacmanBounce - redirects pacman to opposite direction
  85. * when hitting top/bottom
  86. *
  87. * PA4 UPDATE: In PA4, this is now replaced by the keyPressed function. This function
  88. * checks to see which arrow key is pressed, either UP, DOWN, RIGHT, LEFT, and moves
  89. * Pacman accordingly in that direction
  90. *
  91. * The previous makePacmanBounce function has been commented out.
  92. */
  93.  
  94. //void makePacmanBounce() {
  95. // pacmanX += xDelta; // Always going to be increasing
  96. // pacmanY += yDelta; // Decreasing or increasing depending on top/bottom boundaries
  97. // if (((pacmanY > height - (PACMAN_SIZE)/2)) || (pacmanY < 35)) { // He falls between these two points
  98. // yDelta = -yDelta;
  99. // }
  100. //}
  101.  
  102. void keyPressed() {
  103. if (key == CODED) {
  104. if (keyCode == UP) {
  105. pacmanY -= yDelta;
  106. } else if (keyCode == DOWN) {
  107. pacmanY += yDelta;
  108. } else if (keyCode == RIGHT) {
  109. pacmanX += xDelta;
  110. } else if (keyCode == LEFT) {
  111. pacmanX -= xDelta;
  112. }
  113. }
  114. }
  115.  
  116. /*
  117. * makePacmanWrap - Wraps Pacman right, left, top, and bottom
  118. */
  119.  
  120. void makePacmanWrap() {
  121. if (pacmanX > width + (PACMAN_SIZE)/2) { // Right hand side
  122. pacmanX = -((PACMAN_SIZE)/2);
  123. } else if (pacmanX < 0 - (PACMAN_SIZE)/2) { // Left hand side
  124. pacmanX = width + ((PACMAN_SIZE)/2);
  125. } else if (pacmanY < 0 - (PACMAN_SIZE)/2) { // Top of screen
  126. pacmanY = height + ((PACMAN_SIZE)/2);
  127. } else if (pacmanY > height + (PACMAN_SIZE)/2) { // Bottom of screen
  128. pacmanY = -((PACMAN_SIZE)/2);
  129. }
  130. }
  131.  
  132. /*
  133. * drawGhost - Draws the ghost as an upside down half circle with a pink color
  134. */
  135.  
  136. void drawGhost() {
  137. fill(255,105,180);
  138. arc(ghostX, ghostY, 100, 100, PI, TWO_PI); // Creats an upside down half ellipse.
  139. }
  140.  
  141. /*
  142. * moveGhost - Gives the ghost a random movement
  143. * Also checks to make sure the ghost is within certain bounds
  144. */
  145.  
  146. void moveGhost() {
  147. //ghostXDelta = random(-20.5, 20.5);
  148. //ghostYDelta = random(-20.5, 20.5);
  149.  
  150. //ghostX += ghostXDelta;
  151. //ghostY += ghostYDelta;
  152.  
  153. //if((ghostX > width - 50) || (ghostX < 50)) {
  154. // ghostXDelta = -ghostXDelta;
  155.  
  156. //} else if((ghostY > height - 50) || (ghostY < 50)) {
  157. // ghostYDelta = -ghostYDelta;
  158. // }
  159.  
  160. float targetX = pacmanX; // Ghost moves at a "delay". targetX facilitates this delay
  161. ghostX += (targetX - ghostX) * 0.02;
  162.  
  163. float targetY = pacmanY;
  164. ghostY += (targetY - ghostY) * 0.02;
  165.  
  166. }
  167.  
  168. /*
  169. * assignDotPositions - For each number of dots, make random x and y
  170. * coordinates within the screen
  171. */
  172.  
  173. void assignDotPositions() {
  174. for(int i = 0; i < numOfDots; i++) {
  175. dotsX[i] = random(10, 1180);
  176. dotsY[i] = random(10, 490);
  177. }
  178.  
  179. }
  180.  
  181. /*
  182. * drawDots - Draws the previously assigned dots onto the screen
  183. */
  184.  
  185. void drawDots() {
  186. fill(255, 255, 0);
  187. for(int i = 0; i < numOfDots; i++) {
  188. ellipse(dotsX[i], dotsY[i], 5, 5);
  189. }
  190. }
  191.  
  192. /*
  193. * displayScore - Displays the score in the upper right hand corner of the screen
  194. */
  195.  
  196. void displayScore() {
  197. fill(255);
  198. textSize(40);
  199. text("Score: " + playerScore, 1000, 40);
  200. }
  201.  
  202.  
  203. Planning
  204.  
  205.  
  206. //First shape
  207.  
  208. float xDelta = 10, yDelta = 10;
  209. float x = 250, y = 250;
  210. final float SHAPE_SIZE = 50;
  211.  
  212. //Second shape
  213.  
  214. float secondX = 400, secondY = 400;
  215. float secondXDelta = 5, secondYDelta = 5;
  216. final float SHAPE_SIZE2 = 100;
  217.  
  218.  
  219. float dist;
  220. int playerScore = 0;
  221.  
  222. void setup() {
  223. size(500, 500);
  224. }
  225.  
  226. void draw() {
  227. background(0);
  228. fill(255, 255, 0);
  229. rect(x, y, SHAPE_SIZE, SHAPE_SIZE);
  230.  
  231. makeShapeWrap();
  232. displayScore();
  233. secondShape();
  234. secondShapeMove();
  235. }
  236.  
  237. void keyPressed() {
  238. if (key == CODED) {
  239. if (keyCode == UP) {
  240. y -= yDelta;
  241. } else if (keyCode == DOWN) {
  242. y += yDelta;
  243. } else if (keyCode == RIGHT) {
  244. x += xDelta;
  245. } else if (keyCode == LEFT) {
  246. x -= xDelta;
  247. }
  248. }
  249. }
  250.  
  251. void makeShapeWrap() {
  252. if (x > width + (SHAPE_SIZE)/2) { // Right hand side
  253. x = -((SHAPE_SIZE)/2);
  254. } else if (x < 0 - (SHAPE_SIZE)/2) { // Left hand side
  255. x = width + ((SHAPE_SIZE)/2);
  256. } else if (y < 0 - (SHAPE_SIZE)/2) { // Top of screen
  257. y = height + ((SHAPE_SIZE)/2);
  258. } else if (y > height + (SHAPE_SIZE)/2) { // Bottom of screen
  259. y = -((SHAPE_SIZE)/2);
  260. }
  261. }
  262.  
  263. void displayScore() {
  264. textSize(40);
  265. text("Score: " + playerScore, 325, 40);
  266. }
  267.  
  268. void secondShape() {
  269. fill(0, 0, 255);
  270. rect(secondX, secondY, SHAPE_SIZE2, SHAPE_SIZE2);
  271. }
  272.  
  273. void secondShapeMove() {
  274. float targetX = x; // Mouse at a "delay". targetX facilitates this delay
  275. secondX += (targetX - secondX) * 0.02;
  276.  
  277. float targetY = y;
  278. secondY += (targetY - secondY) * 0.02;
  279.  
  280. //float targetX = x; // Mouse at a "delay". targetX facilitates this delay
  281. //secondX += (targetX - secondX) * 0.1;
  282.  
  283. //float targetY = y;
  284. //secondY += (targetY - secondY) * 0.1;
  285.  
  286.  
  287.  
  288.  
  289. // // Keep the x and y values within the screen
  290.  
  291. // if (((y > height - (SHAPE_SIZE2)/2)) || (mouseY < 50)) {
  292. // secondY = (SHAPE_SIZE2)/2;
  293. //}
  294.  
  295. // if (((x > width - (SHAPE_SIZE2)/2)) || (x < 50)) {
  296. // secondX = (SHAPE_SIZE2)/2;
  297. // }
  298. }
  299.  
  300.  
  301.  
  302.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement