Advertisement
vencinachev

Game-NBU

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