Guest User

Untitled

a guest
May 4th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let arrow;          // Arrow object, what the player controls  
  2. let obj;            // Obj is a superclass for the blocks and trails,
  3.                     // As they move together with the same velocity and acceleration
  4. let trails = [];    // List for the trail circles behind the arrow
  5. let blocks = [];    // List for the blocks appearing on screen
  6. let score;          // ...the score.
  7.  
  8. function setup() {
  9.     noStroke();
  10.     createCanvas(constrain(windowWidth, 0, windowHeight * 2 / 3), windowHeight); // To get min. aspect ratio of 2 : 3
  11.     textAlign(CENTER, CENTER);
  12.  
  13.     score = 0;
  14.     arrow = new Arrow();
  15.     obj = new Obj();
  16.     blocks.push(new Block(
  17.         obj,
  18.         random(- width / 2, width / 4),
  19.         float(height) * -5 / 6,
  20.         random(width / 4, width / 2)
  21.         ));
  22. }
  23.  
  24. function draw() {
  25.     background(0);
  26.  
  27.     translate(width / 2 , height * 5 / 6);  // Using the arrow as origin
  28.  
  29.     // CREATING STUFF
  30.     if (blocks[blocks.length - 1].y >= - height / 3) {  // New block pushed each time the previous block reaches halfway through
  31.         blocks.push(new Block(
  32.             obj,
  33.             random(- width / 2, width / 4),
  34.             float(height) * -5 / 6,
  35.             random(width / 4, width / 2)
  36.             ));
  37.     }
  38.  
  39.     trails.push(new Trail(obj, arrow));
  40.    
  41.     // DELETING STUFF
  42.     if (trails.length > 30) {
  43.         trails.splice(0, 1);
  44.     }
  45.  
  46.     for (let i = trails.length - 1; i >= 0; i--) {
  47.         if (trails[i].y > float(height) / 6 + trails[i].r) {    // Deleting trails that get past the screen
  48.             trails.splice(i, 1);
  49.         }
  50.     }
  51.  
  52.     for (let i = blocks.length - 1; i >= 0; i--) {
  53.         if (blocks[i].y > float(height) / 6 + blocks[i].h) {    // Deleting blocks that get past the screen  
  54.             blocks.splice(i, 1);
  55.             score++;    // Each block gives a score.
  56.         }
  57.     }
  58.  
  59.     // CHECKING COLLISION
  60.     if (- width / 20.0 <= blocks[0].y && blocks[0].y <= width / 40.0 + blocks[0].h) {   // I *think* this is causing most of the unoptimisation.
  61.         if (blocks[0].hits(arrow)) {
  62.             gameOver();
  63.         }
  64.     }
  65.  
  66.     // MOVING STUFF
  67.     if (mouseIsPressed || touches.length !== 0) {   // Arrow moves as long as the mouse is pressed or screen is touched
  68.         obj.vel = constrain(obj.vel + obj.acc, 0, height / 40.0);
  69.     } else {    // Deceleration.
  70.         obj.vel = constrain(obj.vel - obj.acc, 0, height / 40.0);
  71.     }
  72.  
  73.     for (let trail of trails) {
  74.         trail.move();
  75.     }
  76.  
  77.     for (let block of blocks) {
  78.         block.move();
  79.     }
  80.  
  81.     arrow.update();
  82.    
  83.     // SHOWING STUFF
  84.     for (let trail of trails) {
  85.         trail.show();
  86.     }
  87.    
  88.     for (let block of blocks) {
  89.         block.show();
  90.     }
  91.  
  92.     arrow.show();
  93.  
  94.     textSize(width * 0.08);
  95.     //fill(91,192,255);
  96.     fill(50, 125, 255);
  97.     text(score, - width * 0.44, - height * 0.785);  // Score on top
  98. }
  99.  
  100. function gameOver() {
  101.     blocks = [];
  102.     trails = [];
  103.     arrow.y = height;
  104.     noLoop();
  105.     textSize(height / 10.0);
  106.     text("Game Over", 0, - height / 3.0);
  107. }
  108. function Obj() {
  109.     this.vel = 0.0;
  110.     this.acc = float(width) / 800;  // Default acceleration/deceleration for every moving object
  111. }
  112.  
  113. function Arrow() {
  114.     this.x = 0.0;
  115.     this.y = 0.0;
  116.  
  117.     this.update = function() {
  118.         this.x = width * 7.0 / 16 * Math.sin(frameCount * 0.05);  // sin gives an oscillating value between -1 and 1
  119.     };
  120.  
  121.     this.show = function() {
  122.         fill(255);
  123.         beginShape();  // 3 vertices for triangle
  124.         vertex(this.x, this.y - width / 20.0);
  125.         vertex(this.x - height / 50.0, this.y + width / 40.0);
  126.         vertex(this.x + height / 60.0, this.y + width / 40.0);
  127.         endShape(CLOSE);
  128.     };
  129. }
  130.  
  131. function Trail(obj, arrow) {
  132.     this.x = arrow.x;
  133.     this.y = arrow.y + width / 40.0;
  134.     this.r = width / 25.0;
  135.  
  136.     this.move = function() {
  137.         this.y += obj.vel;
  138.     };
  139.  
  140.     this.show = function() {
  141.         fill(50, 125, 255, 70);
  142.         ellipse(this.x, this.y, this.r);
  143.     };
  144. }
  145.  
  146. function Block(obj, x, y, w) {
  147.     this.x = x;
  148.     this.y = y;
  149.     this.w = w;
  150.     this.h = height / 20.0;
  151.  
  152.     this.show = function() {
  153.         fill(255);
  154.         rect(this.x, this.y - this.h, this.w, this.h);
  155.     };
  156.  
  157.     this.hits = function(arrow) {
  158.         return (arrow.x >= this.x && arrow.x - this.x <= this.w);
  159.     };
  160.  
  161.     this.move = function() {
  162.         this.y += obj.vel;
  163.     };
  164. }
Add Comment
Please, Sign In to add comment