Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. // Welcome back to CS00b!
  2.  
  3. // Please share webcams and open VM: www.ktbyte.com/vm
  4.  
  5. // Please open project from last week!
  6.  
  7. // GLOBAL VARIABLES
  8. boolean firing = false;
  9. float x;
  10. float y;
  11.  
  12. float xSpeed;
  13. float ySpeed;
  14.  
  15. float lastX = -100; // where did last cannonball land
  16.  
  17. PImage bombImg;
  18.  
  19. void setup() {
  20. size(600, 400);
  21.  
  22. bombImg = loadImage("http://findicons.com/files/icons/1332/danger_mouse/256/bomb.png");
  23. }
  24.  
  25. void draw() {
  26. background(128, 196, 255);
  27.  
  28. fill(0, 128, 0);
  29. noStroke(); // removes borders
  30. rect(0, 300, width, 200);
  31. // 4 inputs: x, y (UL corner), width, height
  32.  
  33. stroke(255, 0, 0); // red
  34. strokeWeight(9); // thick line
  35. // line(lastX - 15, 300 - 15, lastX + 15, 300 + 15);
  36. // line(lastX + 15, 300 - 15, lastX - 15, 300 + 15);
  37. fill(255, 0, 0);
  38. textAlign(CENTER, CENTER);
  39. textSize(48);
  40. text("X", lastX, 300);
  41.  
  42. drawTrajectory();
  43.  
  44. if (firing) {
  45. // draw cannonball
  46. noStroke();
  47. fill(0); // black
  48. // ellipse(x, y, 30, 30);
  49. //imageMode(CENTER);
  50. drawImage(bombImg, x, y, frameCount*-4, 64, 64);
  51.  
  52. x += xSpeed;
  53. y += ySpeed;
  54.  
  55. ySpeed += 0.05;
  56.  
  57. if (y > 300) {
  58. firing = false;
  59. lastX = x;
  60. }
  61. }
  62. }
  63.  
  64. void mousePressed() {
  65. // only fire again if not already firing
  66. if (!firing) {
  67. firing = true;
  68. // cannon speed is proportional to distance to center
  69. // at ground level
  70. xSpeed = mouseX - width / 2;
  71. ySpeed = mouseY - 300;
  72. float power = dist(mouseX, mouseY, width/2, 300);
  73. xSpeed /= 50;
  74. ySpeed /= 50;
  75. // scales up to power level 5, scales down from original
  76. // power level
  77.  
  78. x = width / 2;
  79. y = 300;
  80. }
  81. }
  82.  
  83. // i want a function that draws an arc for the cannonball's trajectory
  84. void drawTrajectory() {
  85. // when you create local variables the same name as global, they
  86. // "shadow" the global variables - Java will assume you're talking
  87. // about the local variables not the globals w/in this functions
  88. float x = width / 2;
  89. float y = 300;
  90.  
  91. float xSpeed = mouseX - width / 2;
  92. float ySpeed = mouseY - 300;
  93. float power = dist(mouseX, mouseY, width/2, 300);
  94. xSpeed /= 50;
  95. ySpeed /= 50;
  96.  
  97. // we want to loop through all different cannonball positions
  98. // that we'd see if we fired the cannonball, until that position
  99. // would hit the ground
  100. while (y <= 300) {
  101. float nextX = x + xSpeed; // position
  102. float nextY = y + ySpeed;
  103.  
  104. stroke(128); // gray
  105. strokeWeight(1);
  106. line(x, y, nextX, nextY);
  107.  
  108. x = nextX;
  109. y = nextY;
  110.  
  111. ySpeed += 0.05; // change in position
  112. }
  113.  
  114. // we know once the loop is over, the ball would have hit the ground
  115. noStroke();
  116. fill(0, 0, 0, 100); // 100/255 opacity means it'll be faded
  117. ellipse(x, 300, 30, 30);
  118. }
  119.  
  120. // need a function that draws any image at any position & angle & dimensions
  121. void drawImage(PImage img, float x, float y, float angle, int w, int h) {
  122. translate(x, y);
  123. rotate(radians(angle)); // can convert input degrees to radians
  124. imageMode(CENTER);
  125. image(img, 0, 0, w, h);
  126. }
  127.  
  128.  
  129. // gives the angle in degrees for direction the "cannon" is facing
  130. float firingDirection() {
  131. return degrees(atan2(width / 2 - mouseX, 300 - mouseY));
  132. // arctangent finds angle based on distances
  133. }
  134.  
  135. // homework: draw a cannon using the drawImage and firingDirection funcs
  136. // replace or add one more image (rotated) to the program - be creative
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement