Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- File 1 - Main
- Catcher catcher; // One catcher object
- Timer timer; // One timer object
- Drop[] drops; // An array of drop objects
- int totalDrops = 0; // totalDrops
- float speedIncrease = 0;
- int timeRemaining = 3600;
- void setup() {
- size(500,700);
- smooth();
- catcher = new Catcher(32); // Create the catcher with a radius of 32 (default size)
- drops = new Drop[1000]; // Create 1000 spots in the array
- timer = new Timer(1000); // Create a timer that goes off every 2 seconds
- timer.start(); // Starting the time
- runner = loadImage("runner.png");
- images[0] = loadImage("fruit1.png");
- images[1] = loadImage("fruit2.png");
- images[2] = loadImage("fruit3.png");
- images[3] = loadImage("fruit4.png");
- }
- void draw() {
- if(timeRemaining>0){
- background(255);
- text("Score: "+score,10,10);
- text("SpeedINC: "+speedIncrease,10,20);
- text("Time Remaining: "+timeRemaining/60,10,30);
- text("fruitR "+fruitR,10,40);
- timeRemaining--;
- println(frameRate);
- // Set catcher location
- catcher.setLocation(mouseX,360);
- // Display the catcher
- catcher.display();
- // Check the timer
- if (timer.isFinished()) {
- // Deal with raindrops
- // Initialize one drop
- drops[totalDrops] = new Drop();
- // Increment totalDrops
- totalDrops ++ ;
- // If we hit the end of the array
- if (totalDrops >= drops.length) {
- totalDrops = 0; // Start over
- }
- timer.start();
- }
- // Move and display all drops
- for (int i = 0; i < totalDrops; i++ ) {
- drops[i].move();
- // Drop.randomFruit();
- if(imageSet=false){
- fruitR = int(random(0,3));
- }
- drops[i].display();
- if (catcher.intersect(drops[i])) {
- drops[i].caught();
- }
- }
- }else{
- background(255,255,255);
- text("Game Over. You Scored: "+score,100,100);
- }
- }
- File 2 - Catcher
- PImage runner;
- class Catcher {
- float r,powerR; // radius
- color col; // color
- float x,y; // location
- float powerX,powerY;
- Catcher(float tempR) {
- r = tempR;
- col = color(50,10,10,150);
- x = 0;
- y = 0;
- }
- void setLocation(float tempX, float tempY) {
- x = tempX;
- y = tempY;
- }
- void display() {
- stroke(0);
- fill(col);
- ellipse(x,y,r*2,r*2);
- //image(runner,x-r,y-r,r*2+4,r*2+4);
- }
- // A function that returns true or false based on
- // if the catcher intersects a raindrop
- boolean intersect(Drop d) {
- // Calculate distance
- float distance = dist(x,y,d.x,d.y);
- // Compare distance to sum of radii
- if (distance < r*2 + d.r) {
- return true;
- } else {
- return false;
- }
- }
- }
- File 3 - Drop
- int score = 0;
- boolean imageSet=false;
- float fruitR;
- PImage[] images = new PImage[4]; //image array
- class Drop {
- float x,y; // Variables for location of raindrop
- float speed; // Speed of raindrop
- color c;
- float r; // Radius of raindrop
- Drop() {
- r = 8; // All raindrops are the same size
- x = random(width); // Start with a random x location
- y = -r*4; // Start a little above the window
- speed = random(1,3)+speedIncrease; // Pick a random speed
- c = color(50,100,150); // Color
- }
- // Move the raindrop down
- void move() {
- // Increment by speed
- y += speed;
- }
- // Check if it hits the bottom
- boolean reachedBottom() {
- // If we go a little beyond the bottom
- if (y > height + r*4) {
- return true;
- } else {
- return false;
- }
- }
- // void randomFruit(){
- // if(imageSet=false){
- // fruitR = int(random(0,3));
- // }
- // }
- // Display the raindrop
- void display() {
- imageSet = true;
- image(images[int(fruitR)],x,y,r*2,r*2);
- imageSet = false;
- }
- // If the drop is caught
- void caught() {
- score++;
- if(speedIncrease<10){
- speedIncrease+=0.1;
- }
- // Stop it from moving by setting speed equal to zero
- speed = 0;
- // Set the location to somewhere way off-screen
- y = - 1000;
- }
- }
- File 4 - Timer
- class Timer {
- int savedTime; // When Timer started
- int totalTime; // How long Timer should last
- Timer(int tempTotalTime) {
- totalTime = tempTotalTime;
- }
- // Starting the timer
- void start() {
- // When the timer starts it stores the current time in milliseconds.
- savedTime = millis();
- }
- // The function isFinished() returns true if 5,000 ms have passed.
- // The work of the timer is farmed out to this method.
- boolean isFinished() {
- // Check how much time has passed
- int passedTime = millis()- savedTime;
- if (passedTime > totalTime) {
- return true;
- } else {
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement