Last active
October 21, 2016 05:57
-
-
Save sabotai/d1949d92bd7f06f02d6ed69256e9de3b to your computer and use it in GitHub Desktop.
A Bounce Game
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| PVector ball, ballSpeed, ballSize; | |
| PVector gravity; | |
| PVector[] points = new PVector[2]; | |
| boolean above; | |
| int score = 0; | |
| boolean angleUp; | |
| float value = 0; | |
| void setup() { | |
| size(1920, 1080); | |
| //noStroke(); | |
| gravity = new PVector(0, 0.4); | |
| ball = new PVector(random(width), height/5); | |
| // if (ballSpeed == null){ //dont reset speed unless first time | |
| ballSpeed = new PVector(10, -10); | |
| // } | |
| ballSize = new PVector(100, 100); | |
| above = true; | |
| if (points[0] == null){ | |
| for (int i = 0; i < points.length; i++) { | |
| //randomly on bottom half of screen | |
| points[i] = new PVector(random(0, width), random(height/2, height)); | |
| } | |
| } | |
| } | |
| void draw() { | |
| background(255); | |
| //clock face attempt | |
| noFill(); | |
| strokeWeight(100); | |
| stroke(255,0,0); | |
| ellipse(points[1].x, points[1].y, width, width); | |
| noStroke(); | |
| fill(0); | |
| rect(0, 0, width * value/5, 20); | |
| fill(255,0,0); | |
| textSize(max(10, score)); | |
| textAlign(CENTER); | |
| text("SCORE: " + score, width/2, height/2); | |
| points[0].set(mouseX,mouseY); | |
| points[1].add(gravity); | |
| float angle = PVector.angleBetween(points[0], points[1]); | |
| //println("angle= " + angle); | |
| if (points[1].y > points[0].y){ | |
| angleUp = true; | |
| } else { | |
| angleUp = false; | |
| } | |
| fill(200,50,50); | |
| beginShape(); | |
| curveVertex(points[0].x, height* 0.98); | |
| for (int i = 0; i < points.length; i++) { | |
| curveVertex(points[i].x, points[i].y); | |
| ellipse(points[i].x, points[i].y, 10, 10); | |
| if (i>0) { | |
| line(points[i].x, points[i].y, points[i-1].x, points[i-1].y); | |
| aboveLine(ball, ballSize, points[i], points[i-1]); | |
| } | |
| } | |
| curveVertex(points[points.length-1].x, height * 0.98); | |
| endShape(); | |
| ball.add(ballSpeed); | |
| ballSpeed.add(gravity); | |
| checkWalls(); | |
| ellipse(ball.x, ball.y, ballSize.x, ballSize.y); | |
| //if (mousePressed) setup(); //reset | |
| if (mousePressed) { | |
| PVector mouse = new PVector(mouseX, mouseY); | |
| points[1] = PVector.lerp(points[1], mouse, 0.51); | |
| //points[1].set(mouseX, mouseY); | |
| } | |
| } | |
| void aboveLine(PVector b, PVector bS, PVector pt1, PVector pt2) { | |
| float w = pt2.x - pt1.x; | |
| value = int(abs(5 * (100/w))); | |
| println(value); | |
| //if (w > 100){ //distance too great to make points | |
| fill(255-abs(w)); | |
| //} | |
| if ((b.x > pt1.x && b.x < pt2.x) || (b.x > pt2.x && b.x < pt1.x)){ | |
| float pct = (b.x - pt1.x) / w; //percent traveled along x axis of line | |
| //println("pct = " + pct); | |
| PVector inter = PVector.lerp(pt1, pt2, pct); | |
| if (inter.y - ballSize.y/2 > b.y){ //currently above the line | |
| if (!above){ //if just crossed from below | |
| println("just crossed from below"); | |
| //ballSpeed.y *= -1; | |
| //ball.y = inter.y - 100; | |
| } | |
| above = true; | |
| //fill(200, 50, 50); | |
| } else { | |
| if (above){ | |
| println("just crossed from above"); | |
| ballSpeed.x *= 1.1; | |
| ballSpeed.y *= -1.05; | |
| //ballSpeed.sub(gravity); | |
| score+= value; | |
| println("scored! score=" + score); | |
| //ball.y = inter.y + 1; | |
| } | |
| above = false; | |
| // fill(50,200,50); | |
| } | |
| //println("above = " + above); | |
| //ellipse(inter.x, inter.y, 50, 50); | |
| } else { | |
| //fill(255); | |
| } | |
| } | |
| void checkWalls() { | |
| float rem = 0.9; | |
| if (ball.x < ballSize.x/2) { //left wall | |
| ball.x = ballSize.x/2; | |
| ballSpeed.x *= -rem; | |
| ballSpeed.y *= rem; | |
| } | |
| if (ball.x > width - ballSize.x/2) { //right wall | |
| ball.x = width- ballSize.x/2; | |
| ballSpeed.x *= -rem; | |
| ballSpeed.y *= rem; | |
| } | |
| if (ball.y < ballSize.y/2) { //top | |
| ball.y = ballSize.y/2; | |
| ballSpeed.x *= rem; | |
| ballSpeed.y *= -rem; | |
| } | |
| if (ball.y > height - ballSize.y/2) { //bottom | |
| /* | |
| ball.y = height - ballSize.y/2; | |
| ballSpeed.x *= rem; | |
| ballSpeed.y *= -rem; | |
| */ | |
| //score-= value; | |
| score = 0; | |
| println("lost 1... score=" + score); | |
| background(100); | |
| setup(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment