Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Welcome back to CS00b!
- // Please share webcams and open VM: www.ktbyte.com/vm
- // Please open project from last week!
- // GLOBAL VARIABLES
- boolean firing = false;
- float x;
- float y;
- float xSpeed;
- float ySpeed;
- float lastX = -100; // where did last cannonball land
- PImage bombImg;
- void setup() {
- size(600, 400);
- bombImg = loadImage("http://findicons.com/files/icons/1332/danger_mouse/256/bomb.png");
- }
- void draw() {
- background(128, 196, 255);
- fill(0, 128, 0);
- noStroke(); // removes borders
- rect(0, 300, width, 200);
- // 4 inputs: x, y (UL corner), width, height
- stroke(255, 0, 0); // red
- strokeWeight(9); // thick line
- // line(lastX - 15, 300 - 15, lastX + 15, 300 + 15);
- // line(lastX + 15, 300 - 15, lastX - 15, 300 + 15);
- fill(255, 0, 0);
- textAlign(CENTER, CENTER);
- textSize(48);
- text("X", lastX, 300);
- drawTrajectory();
- if (firing) {
- // draw cannonball
- noStroke();
- fill(0); // black
- // ellipse(x, y, 30, 30);
- //imageMode(CENTER);
- drawImage(bombImg, x, y, frameCount*-4, 64, 64);
- x += xSpeed;
- y += ySpeed;
- ySpeed += 0.05;
- if (y > 300) {
- firing = false;
- lastX = x;
- }
- }
- }
- void mousePressed() {
- // only fire again if not already firing
- if (!firing) {
- firing = true;
- // cannon speed is proportional to distance to center
- // at ground level
- xSpeed = mouseX - width / 2;
- ySpeed = mouseY - 300;
- float power = dist(mouseX, mouseY, width/2, 300);
- xSpeed /= 50;
- ySpeed /= 50;
- // scales up to power level 5, scales down from original
- // power level
- x = width / 2;
- y = 300;
- }
- }
- // i want a function that draws an arc for the cannonball's trajectory
- void drawTrajectory() {
- // when you create local variables the same name as global, they
- // "shadow" the global variables - Java will assume you're talking
- // about the local variables not the globals w/in this functions
- float x = width / 2;
- float y = 300;
- float xSpeed = mouseX - width / 2;
- float ySpeed = mouseY - 300;
- float power = dist(mouseX, mouseY, width/2, 300);
- xSpeed /= 50;
- ySpeed /= 50;
- // we want to loop through all different cannonball positions
- // that we'd see if we fired the cannonball, until that position
- // would hit the ground
- while (y <= 300) {
- float nextX = x + xSpeed; // position
- float nextY = y + ySpeed;
- stroke(128); // gray
- strokeWeight(1);
- line(x, y, nextX, nextY);
- x = nextX;
- y = nextY;
- ySpeed += 0.05; // change in position
- }
- // we know once the loop is over, the ball would have hit the ground
- noStroke();
- fill(0, 0, 0, 100); // 100/255 opacity means it'll be faded
- ellipse(x, 300, 30, 30);
- }
- // need a function that draws any image at any position & angle & dimensions
- void drawImage(PImage img, float x, float y, float angle, int w, int h) {
- translate(x, y);
- rotate(radians(angle)); // can convert input degrees to radians
- imageMode(CENTER);
- image(img, 0, 0, w, h);
- }
- // gives the angle in degrees for direction the "cannon" is facing
- float firingDirection() {
- return degrees(atan2(width / 2 - mouseX, 300 - mouseY));
- // arctangent finds angle based on distances
- }
- // homework: draw a cannon using the drawImage and firingDirection funcs
- // replace or add one more image (rotated) to the program - be creative
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement