Advertisement
nawamkihafahd

Untitled

Dec 15th, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. package RedHazard;
  2.  
  3.  
  4. import java.awt.Color;
  5. import java.awt.Graphics;
  6. import java.util.Random;
  7. public class Obstacle
  8. {
  9. public int x, y, width = 25, height = 25;
  10. public int xspeed, yspeed;
  11. public Random random;
  12. private Redhazard rh;
  13. private int left;
  14. public Obstacle(Redhazard rh, int a)
  15. {
  16. this.rh = rh;
  17. this.random = new Random();
  18. left = a;
  19. spawn(left);
  20. }
  21. public void update(Ship ship)
  22. {
  23. x += xspeed;
  24. y += yspeed;
  25. if(CheckCollision(ship) > 0)
  26. {
  27. if(CheckCollision(ship) == 1)
  28. {
  29. ship.score -= 1;
  30. }
  31. spawn(left);
  32. }
  33. }
  34. public void spawn(int a)
  35. {
  36. left = a;
  37. if(left == 1)
  38. {
  39. this.x = 0;
  40. xspeed = random.nextInt(6) + 2;
  41. }
  42. else
  43. {
  44. this.x = Redhazard.rh.width;
  45. xspeed = -1*(random.nextInt(6) + 2);
  46. }
  47. this.y = random.nextInt(Redhazard.rh.height-150);
  48. yspeed = random.nextInt(4) + 2;
  49. }
  50. public int CheckCollision(Ship ship)
  51. {
  52. if(this.x + width > ship.x && this.x < ship.x + ship.width && this.y + height > ship.y && this.y < ship.y + ship.height)
  53. {
  54. return 1;
  55. }
  56. if(this.x < 0 || this.y > Redhazard.rh.height || this.x > Redhazard.rh.width)
  57. {
  58. return 2;
  59. }
  60. return 0;
  61. }
  62. public void render(Graphics g)
  63. {
  64. g.setColor(Color.RED);
  65. g.fillRect(x, y, width, height);
  66. }
  67.  
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement