Created
October 27, 2016 21:05
-
-
Save sabotai/9413a74daca6401f33f469ec9c87343e to your computer and use it in GitHub Desktop.
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
float posX, posY; | |
float speedX, speedY; | |
float size; | |
float gravity = 0.4; | |
boolean isMoving; | |
PImage cat = new PImage(); | |
void setup(){ | |
size(1280,720); | |
cat = loadImage("Cat-PNG-6.png"); | |
posX = random(width); | |
posY = random(height); | |
size = 70; //ball size/diameter | |
speedX = 0; //dont move at first | |
speedY = 0; | |
isMoving = false; //i said dont move at first! | |
} | |
void draw(){ | |
initChecks(); | |
drawStuff(); | |
checkWalls(); | |
} | |
void drawStuff(){ | |
background(255); | |
imageMode(CENTER); //draw any image at the center of our cursor | |
image(cat, mouseX, mouseY); //meow | |
fill(200,0,0); //red fill color | |
ellipse(posX, posY, size, size); //draw our ball | |
} | |
void initChecks(){ | |
float distance = dist(mouseX, mouseY, posX, posY); //current distance between our cursor and the ball | |
if (distance < size/2){ //if the distance is less than the collision threshold | |
isMoving = true; //start moving our ball | |
speedX = mouseX - pmouseX; //take our previous mouseX position to figure out which direction the cursor was coming from | |
speedY = mouseY - pmouseY; //we will use the direction and speed of the cursor to determine the speed of our ball | |
} | |
} | |
void checkWalls(){ | |
if (isMoving){ //once the cat has struck | |
speedY += gravity; //gravity will add a downward force each frame | |
posX = posX + speedX; //move the x position by whatever speed | |
posY = posY + speedY; //move the y position by whatever speed | |
if (posX < 0 + size/2){ //left border collision | |
speedX = -speedX; //reverse the direction upon impact | |
posX = size/2; //move it to the border to prevent glitchy behavior | |
} | |
if (posX > width - size/2){ //right border collision | |
speedX = -speedX; | |
posX = width - size/2; | |
} | |
if (posY < 0 + size/2){ //top border collision | |
speedY = -speedY; | |
posY = size/2; | |
} | |
if (posY > height - size/2){ //bottom border collision | |
speedY = -0.8 * speedY; //they should slow down with each impact, speed at 80% each bounce | |
speedX *= 0.8; //also decelerate the x so we dont have it rolling around the ground | |
posY = height - size/2; //move it to the border so we dont get weird glitchy behavior | |
} | |
} | |
} | |
int giveMeFive(){ //if you call this int function with "giveMeFive()", it will be converted into the integer 5 | |
return 5; | |
} | |
boolean giveMeTruth(){ //if you call this boolean function with "giveMeTruth()", it will be converted into the boolean value false | |
return false; | |
} | |
String subway(float dollars){ //if you call this String function with "subway(" + some integer value + ");", it will run and return one of the two strings based on what parameter it was given | |
if (dollars > 4.35){ | |
return "a stale sandwich"; | |
} else { | |
return "get da fuq outta herr"; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment