Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
602
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.33 KB | None | 0 0
  1. /*The problem is how to track the fly miles of the craft. If the user press the key
  2. but there is a collision so the crast is not moving the miles should not increase*/
  3.  
  4. //when the character is C Craft craft = new Craft();
  5.  
  6.  
  7. class Craft extends Entity {
  8.  
  9.     int fuel = 10;
  10.     //how many enemies in the game? health value should be less than the enemy
  11.     int health = 5;
  12.     //health = 0 means live = false
  13.     boolean live;
  14.  
  15.     // images with different directions are loaded
  16.     String rotated_image = new String("craft-up.png");
  17.  
  18.     Craft(int x, int y, int h, int w)
  19.     {
  20.         super(x, y, h, w);
  21.         showImage(rotated_image);
  22.     }
  23.  
  24.     void display()
  25.     {
  26.         stroke(255);
  27.         fill(175);
  28.  
  29.         if(keyPressed)
  30.         {
  31.             keyPressed();
  32.  
  33.             if(acceleration.x == 0 && acceleration.y == -3) rotated_image = "craft-up.png";
  34.             if(acceleration.x == 0 && acceleration.y ==  3) rotated_image = "craft-down.png";
  35.             if(acceleration.x == 3 && acceleration.y ==  0) rotated_image = "craft-right.png";
  36.             if(acceleration.x == -3 && acceleration.y == 0) rotated_image = "craft-left.png";
  37.  
  38.             if( (int) acceleration.x != 0 || (int) acceleration.y != 0)
  39.             {
  40.                 if( ! sound.isPlaying("engine") ) sound.play("engine", random(0.9, 1.2) );
  41.             }
  42.  
  43.         }
  44.  
  45.         if(  (int) acceleration.x == 0 && (int) acceleration.y == 0 )
  46.         {
  47.             if( sound.isPlaying("engine") ) sound.stop("engine");
  48.         }
  49.  
  50.         showImage(rotated_image);
  51.  
  52.  
  53.  
  54.  
  55.       //  checkBoundaryCollision();
  56.  
  57.  
  58.  
  59.         move();
  60.         keyReleased();
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.     }
  68.  
  69.     void keyPressed()
  70.     {
  71.         if(keyPressed || key == CODED)
  72.         {
  73.             if(keyCode == UP)
  74.             {
  75.                 acceleration.y = -3;
  76.             }
  77.             if(keyCode == DOWN)
  78.             {
  79.                 acceleration.y = 3;
  80.             }
  81.             if(keyCode == LEFT)
  82.             {
  83.                 acceleration.x = -3;
  84.             }
  85.             if(keyCode == RIGHT)
  86.             {
  87.                 acceleration.x = 3;
  88.             }
  89.         }
  90.     }
  91.  
  92.     void keyReleased()
  93.     {
  94.         if(key == CODED)
  95.         {
  96.  
  97.             if(keyCode == UP)
  98.             {
  99.                 velocity.y = 0;
  100.                 acceleration.y = 0;
  101.             }
  102.             if(keyCode == DOWN)
  103.             {
  104.                 velocity.y = 0;
  105.                 acceleration.y = 0;
  106.             }
  107.             if(keyCode == LEFT)
  108.             {
  109.                 velocity.x = 0;
  110.                 acceleration.x = 0;
  111.             }
  112.             if(keyCode == RIGHT)
  113.             {
  114.                 velocity.x = 0;
  115.                 acceleration.x = 0;
  116.             }
  117.  
  118.             //if(keyCode == UP || keyCode == DOWN || keyCode == LEFT ||
  119.  
  120.  
  121.  
  122.         }
  123.     }
  124.  
  125.     //check window's boundary
  126.     void checkBoundaryCollision()
  127.     {
  128.          if(location.x > width - w/2)
  129.          {
  130.              location.x = width - w/2;
  131.          }
  132.          else if(craft.location.x < 0)
  133.          {
  134.               location.x = 0;
  135.          }
  136.          else if(craft.location.y > height - h/2)
  137.          {
  138.               location.y = height - h/2;
  139.          }
  140.          else if (craft.location.y < 0)
  141.          {
  142.               location.y = 0;
  143.          }
  144.  
  145.     }
  146.  
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement