Advertisement
vencinachev

Game-NBU2

Feb 10th, 2021
859
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.46 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.  
  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.       fill(#FCD545);
  95.     } else {
  96.       fill(#ff0000);
  97.     }
  98.     ellipse(x, y, 2*r, 2*r);
  99.   }
  100. }
  101.  
  102. void shootCannon(){
  103.   if ((mousePressed || (keyPressed && key == ' ')) && objectInMotion == false){
  104.     r = 5;
  105.     targetHit = false;
  106.     objectInMotion = true;
  107.     time = 0;
  108.     x0 = cannonX+cannonLength*cos(-cannonAngle);
  109.     y0 = cannonY+cannonLength*sin(-cannonAngle);
  110.     v0x = speed * cos(cannonAngle);
  111.     v0y = speed * sin(cannonAngle);
  112.   }
  113.  
  114.   if (objectInMotion){
  115.     x = x0 + v0x * time;
  116.     y = y0 - v0y * time + 0.5 * gravity * time * time - wind * time;
  117.   }
  118. }
  119.  
  120. void aimCannon() {
  121.   if (objectInMotion == false){
  122.      if (keyPressed) {
  123.       if (keyCode == RIGHT){
  124.         cannonAngle += cannonAngleStep;
  125.       } else if (keyCode == LEFT){
  126.         cannonAngle -= cannonAngleStep;
  127.       } else if (keyCode == UP){
  128.         cannonY -= 3;
  129.       } else if (keyCode == DOWN){
  130.         cannonY += 3;
  131.       }
  132.     }
  133.   }
  134. }
  135.  
  136. void checkTarget(){
  137.   if (x <= -10){
  138.     objectInMotion = false;
  139.   }
  140.   if (x <= targetDistance && !targetHit
  141.   && y < height/2 + height/22*10 && y > height/2 - height/22*10){
  142.     targetHit = true;
  143.     r += 50;
  144.     for (int i = 1; i <= 10; i++){
  145.       if (y < height/2 + height/22*i && y > height/2 - height/22*i){
  146.         score = 11 - i;
  147.         total += score;
  148.         break;
  149.       }
  150.        score = 0;
  151.     }
  152.   }
  153. }
  154.  
  155.  void drawScore(){
  156.    textSize(30);
  157.    fill(255);
  158.    rect(280, 15, 180, 50);
  159.    fill(#0000FF);
  160.    text("Score: " + score, 300, 50);
  161.    
  162.    fill(255);
  163.    rect(480, 15, 180, 50);
  164.    fill(#FF00FF);
  165.    text("Total: " + total, 500, 50);
  166.    
  167.    fill(255);
  168.    rect(680, 15, 250, 50);
  169.    fill(#4F9BC9);
  170.    text("Wind: " + nf(wind, 1, 3) + " SE", 700, 50);
  171.    
  172.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement