Advertisement
Guest User

Processing Game

a guest
May 26th, 2015
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. File 1 - Main
  2.  
  3. Catcher catcher; // One catcher object
  4. Timer timer; // One timer object
  5. Drop[] drops; // An array of drop objects
  6.  
  7.  
  8. int totalDrops = 0; // totalDrops
  9. float speedIncrease = 0;
  10. int timeRemaining = 3600;
  11.  
  12. void setup() {
  13. size(500,700);
  14. smooth();
  15. catcher = new Catcher(32); // Create the catcher with a radius of 32 (default size)
  16. drops = new Drop[1000]; // Create 1000 spots in the array
  17. timer = new Timer(1000); // Create a timer that goes off every 2 seconds
  18. timer.start(); // Starting the time
  19. runner = loadImage("runner.png");
  20.  
  21.  
  22. images[0] = loadImage("fruit1.png");
  23. images[1] = loadImage("fruit2.png");
  24. images[2] = loadImage("fruit3.png");
  25. images[3] = loadImage("fruit4.png");
  26.  
  27. }
  28. void draw() {
  29. if(timeRemaining>0){
  30. background(255);
  31. text("Score: "+score,10,10);
  32. text("SpeedINC: "+speedIncrease,10,20);
  33. text("Time Remaining: "+timeRemaining/60,10,30);
  34. text("fruitR "+fruitR,10,40);
  35. timeRemaining--;
  36. println(frameRate);
  37.  
  38. // Set catcher location
  39. catcher.setLocation(mouseX,360);
  40. // Display the catcher
  41. catcher.display();
  42.  
  43. // Check the timer
  44. if (timer.isFinished()) {
  45. // Deal with raindrops
  46. // Initialize one drop
  47. drops[totalDrops] = new Drop();
  48. // Increment totalDrops
  49. totalDrops ++ ;
  50.  
  51. // If we hit the end of the array
  52. if (totalDrops >= drops.length) {
  53. totalDrops = 0; // Start over
  54. }
  55. timer.start();
  56. }
  57.  
  58. // Move and display all drops
  59. for (int i = 0; i < totalDrops; i++ ) {
  60. drops[i].move();
  61. // Drop.randomFruit();
  62. if(imageSet=false){
  63. fruitR = int(random(0,3));
  64. }
  65. drops[i].display();
  66. if (catcher.intersect(drops[i])) {
  67. drops[i].caught();
  68. }
  69. }
  70. }else{
  71. background(255,255,255);
  72. text("Game Over. You Scored: "+score,100,100);
  73. }
  74. }
  75.  
  76.  
  77. File 2 - Catcher
  78.  
  79. PImage runner;
  80. class Catcher {
  81. float r,powerR; // radius
  82. color col; // color
  83. float x,y; // location
  84. float powerX,powerY;
  85.  
  86. Catcher(float tempR) {
  87. r = tempR;
  88. col = color(50,10,10,150);
  89. x = 0;
  90. y = 0;
  91. }
  92.  
  93. void setLocation(float tempX, float tempY) {
  94. x = tempX;
  95. y = tempY;
  96. }
  97.  
  98. void display() {
  99. stroke(0);
  100. fill(col);
  101. ellipse(x,y,r*2,r*2);
  102. //image(runner,x-r,y-r,r*2+4,r*2+4);
  103. }
  104.  
  105. // A function that returns true or false based on
  106. // if the catcher intersects a raindrop
  107. boolean intersect(Drop d) {
  108. // Calculate distance
  109. float distance = dist(x,y,d.x,d.y);
  110.  
  111. // Compare distance to sum of radii
  112. if (distance < r*2 + d.r) {
  113. return true;
  114. } else {
  115. return false;
  116. }
  117. }
  118. }
  119.  
  120.  
  121. File 3 - Drop
  122.  
  123. int score = 0;
  124. boolean imageSet=false;
  125. float fruitR;
  126.  
  127. PImage[] images = new PImage[4]; //image array
  128.  
  129. class Drop {
  130. float x,y; // Variables for location of raindrop
  131. float speed; // Speed of raindrop
  132. color c;
  133. float r; // Radius of raindrop
  134.  
  135. Drop() {
  136. r = 8; // All raindrops are the same size
  137. x = random(width); // Start with a random x location
  138. y = -r*4; // Start a little above the window
  139. speed = random(1,3)+speedIncrease; // Pick a random speed
  140. c = color(50,100,150); // Color
  141. }
  142.  
  143. // Move the raindrop down
  144. void move() {
  145. // Increment by speed
  146. y += speed;
  147. }
  148. // Check if it hits the bottom
  149. boolean reachedBottom() {
  150. // If we go a little beyond the bottom
  151. if (y > height + r*4) {
  152. return true;
  153. } else {
  154. return false;
  155. }
  156. }
  157.  
  158. // void randomFruit(){
  159. // if(imageSet=false){
  160. // fruitR = int(random(0,3));
  161. // }
  162. // }
  163.  
  164. // Display the raindrop
  165. void display() {
  166. imageSet = true;
  167. image(images[int(fruitR)],x,y,r*2,r*2);
  168. imageSet = false;
  169. }
  170.  
  171. // If the drop is caught
  172. void caught() {
  173. score++;
  174. if(speedIncrease<10){
  175. speedIncrease+=0.1;
  176. }
  177. // Stop it from moving by setting speed equal to zero
  178. speed = 0;
  179. // Set the location to somewhere way off-screen
  180. y = - 1000;
  181. }
  182. }
  183.  
  184.  
  185. File 4 - Timer
  186.  
  187. class Timer {
  188.  
  189. int savedTime; // When Timer started
  190. int totalTime; // How long Timer should last
  191.  
  192.  
  193. Timer(int tempTotalTime) {
  194. totalTime = tempTotalTime;
  195. }
  196.  
  197. // Starting the timer
  198. void start() {
  199. // When the timer starts it stores the current time in milliseconds.
  200. savedTime = millis();
  201. }
  202.  
  203. // The function isFinished() returns true if 5,000 ms have passed.
  204. // The work of the timer is farmed out to this method.
  205. boolean isFinished() {
  206. // Check how much time has passed
  207. int passedTime = millis()- savedTime;
  208. if (passedTime > totalTime) {
  209. return true;
  210. } else {
  211. return false;
  212. }
  213. }
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement