Advertisement
vencinachev

Game-NBU2

Feb 10th, 2021
781
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.61 KB | None | 0 0
  1. float cannonX, cannonY;
  2. float cannonLength = 50;  // How long the cannon on the tank extends
  3. float cannonAngle = PI*3/4;
  4. float cannonAngleStep = 0.005; // step to change cannonAngle
  5.  
  6. float gravity = 0.5;      // Strength of gravity
  7. float time;  // calculate time for the gravity component
  8. float deltat = 0.5;
  9. float wind; // Strength of wind
  10. float explodePositionX;
  11. float explodePositionY;
  12. boolean  explode = false;
  13. float explosionStrength = 1;
  14.  
  15. boolean objectInMotion = false;
  16. float targetDistance = 70; // distance from left boundary the target is positioned
  17. boolean targetHit = false;
  18. int score = 0;
  19. int total = 0;
  20. int cntAnimation = 0;
  21. float[] groundLevel;  // Y coordinate of the ground for each X coordinate
  22. float x = 100,y; // position of the projectile
  23. float x0, y0; //  start position of the projectile
  24. float v0x, v0y; // start speeds of the projectile
  25. float vx, vy; // current speeds of the projectile
  26. float r = 5;   // radius of the projectile
  27. float speed = 25.0; // speed of projectile
  28.  
  29. void  setup (){
  30.   size (1000,600);
  31.   cannonX = width-20;
  32.   cannonY = height*2/3 + random(-100, 0);   // set position of the cannon at 2/3 height measured from the upper boundary of the viewport
  33.   wind = random(0.5, 2);
  34.     // Initialize the ground level
  35.     // Implement random surface of the ground
  36.   groundLevel = new float[width];
  37.   for(int i = 0; i < width; i++) {
  38.    groundLevel[i] = height/3 + random(-10, 10);
  39.   }
  40. }
  41.  
  42. void draw(){
  43.   background(200);
  44.   time += deltat;
  45.   drawGround();
  46.   drawTarget();
  47.   drawCannon();
  48.   aimCannon();
  49.   shootCannon();
  50.   drawProjectile();
  51.   checkTarget();
  52.   drawScore();
  53. }
  54.  
  55. void drawGround(){
  56.     // See the groundLevel[] variable to know where to draw the ground
  57.    stroke(#666A68); // set dark gray
  58.    for(int x = 0; x < width; x++) {
  59.     line (x, height, x, height-groundLevel[x]); // draw in loop a vertikal line at lokation x
  60.   }
  61. }
  62.  
  63. void drawTarget(){
  64.  ellipseMode(RADIUS);
  65.  for (int i = 10; i > 0; i--){
  66.     if (i<4) {
  67.      fill(0);
  68.      stroke(255);
  69.     }
  70.     else{
  71.      fill(255);
  72.      stroke(0);
  73.     }
  74.   ellipse(targetDistance, height/2, 10*i, height/22*i);
  75.  }
  76.  stroke(2555,0,0);
  77.  line(targetDistance,height/22, targetDistance,height-height/22);
  78. }
  79.  
  80. void drawCannon(){
  81.   stroke(85,107,47); // set darkolivegreen color to draw the cannon
  82.   fill(85,107,47);
  83.   smooth();
  84.   strokeWeight(8);
  85.   // invert the sign of the angle in order to point the cannon upwards (and not downwards)
  86.   line(cannonX, cannonY, cannonX+cannonLength*cos(-cannonAngle), cannonY+cannonLength*sin(-cannonAngle));
  87.   strokeWeight(1); // reset to default widht
  88.   arc(cannonX, cannonY, cannonLength*3/4, cannonLength/2, -PI-PI/6, PI/6);
  89. }
  90.  
  91. void drawProjectile(){
  92.   if (objectInMotion) {
  93.     if(targetHit){
  94.       cntAnimation = 0;
  95.       fill(#FCD545);
  96.     } else {
  97.       cntAnimation++;
  98.       if (cntAnimation % 2 == 0){
  99.         fill(#ffff00);
  100.       } else {
  101.         fill(#ff0000);
  102.       }
  103.     }
  104.     ellipse(x, y, 2*r, 2*r);
  105.   }
  106. }
  107.  
  108. void shootCannon(){
  109.   if ((mousePressed || (keyPressed && key == ' ')) && objectInMotion == false){
  110.     r = 5;
  111.     targetHit = false;
  112.     objectInMotion = true;
  113.     time = 0;
  114.     x0 = cannonX+cannonLength*cos(-cannonAngle);
  115.     y0 = cannonY+cannonLength*sin(-cannonAngle);
  116.     v0x = speed * cos(cannonAngle);
  117.     v0y = speed * sin(cannonAngle);
  118.   }
  119.  
  120.   if (objectInMotion){
  121.     x = x0 + v0x * time;
  122.     y = y0 - v0y * time + 0.5 * gravity * time * time - wind * time;
  123.   }
  124. }
  125.  
  126. void aimCannon() {
  127.   if (objectInMotion == false){
  128.      if (keyPressed) {
  129.       if (keyCode == RIGHT){
  130.         cannonAngle += cannonAngleStep;
  131.       } else if (keyCode == LEFT){
  132.         cannonAngle -= cannonAngleStep;
  133.       } else if (keyCode == UP){
  134.         cannonY -= 3;
  135.       } else if (keyCode == DOWN){
  136.         cannonY += 3;
  137.       }
  138.     }
  139.   }
  140. }
  141.  
  142. void checkTarget(){
  143.   if (x <= -10){
  144.     objectInMotion = false;
  145.   }
  146.   if (x <= targetDistance && !targetHit
  147.   && y < height/2 + height/22*10 && y > height/2 - height/22*10){
  148.     targetHit = true;
  149.     r += 50;
  150.     for (int i = 1; i <= 10; i++){
  151.       if (y < height/2 + height/22*i && y > height/2 - height/22*i){
  152.         score = 11 - i;
  153.         total += score;
  154.         break;
  155.       }
  156.        score = 0;
  157.     }
  158.   }
  159. }
  160.  
  161.  void drawScore(){
  162.    textSize(30);
  163.    fill(255);
  164.    rect(280, 15, 180, 50);
  165.    fill(#0000FF);
  166.    text("Score: " + score, 300, 50);
  167.    
  168.    fill(255);
  169.    rect(480, 15, 180, 50);
  170.    fill(#FF00FF);
  171.    text("Total: " + total, 500, 50);
  172.    
  173.    fill(255);
  174.    rect(680, 15, 250, 50);
  175.    fill(#4F9BC9);
  176.    text("Wind: " + nf(wind, 1, 3) + " SE", 700, 50);
  177.    
  178.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement