Missile.java
上传用户:kikomiki
上传日期:2021-10-31
资源大小:373k
文件大小:2k
源码类别:

游戏

开发平台:

Java

  1. import java.awt.*;
  2. import java.util.List;
  3. public class Missile {
  4. public static final int XSPEED = 10;
  5. public static final int YSPEED = 10;
  6. public static final int WIDTH = 10;
  7. public static final int HEIGHT = 10;
  8. int x, y;
  9. Tank.Direction dir;
  10. private boolean good;
  11. private boolean live = true;
  12. private TankClient tc;
  13. public Missile(int x, int y, Tank.Direction dir) {
  14. this.x = x;
  15. this.y = y;
  16. this.dir = dir;
  17. }
  18. public Missile(int x, int y, boolean good, Tank.Direction dir, TankClient tc) {
  19. this(x, y, dir);
  20. this.good = good;
  21. this.tc = tc;
  22. }
  23. public void draw(Graphics g) {
  24. if(!live) {
  25. tc.missiles.remove(this);
  26. return;
  27. }
  28. Color c = g.getColor();
  29. g.setColor(Color.BLACK);
  30. g.fillOval(x, y, WIDTH, HEIGHT);
  31. g.setColor(c);
  32. move();
  33. }
  34. private void move() {
  35. switch(dir) {
  36. case L:
  37. x -= XSPEED;
  38. break;
  39. case LU:
  40. x -= XSPEED;
  41. y -= YSPEED;
  42. break;
  43. case U:
  44. y -= YSPEED;
  45. break;
  46. case RU:
  47. x += XSPEED;
  48. y -= YSPEED;
  49. break;
  50. case R:
  51. x += XSPEED;
  52. break;
  53. case RD:
  54. x += XSPEED;
  55. y += YSPEED;
  56. break;
  57. case D:
  58. y += YSPEED;
  59. break;
  60. case LD:
  61. x -= XSPEED;
  62. y += YSPEED;
  63. break;
  64. case STOP:
  65. break;
  66. }
  67. if(x < 0 || y < 0 || x > TankClient.GAME_WIDTH || y > TankClient.GAME_HEIGHT) {
  68. live = false;
  69. }
  70. }
  71. public boolean isLive() {
  72. return live;
  73. }
  74. public Rectangle getRect() {
  75. return new Rectangle(x, y, WIDTH, HEIGHT);
  76. }
  77. public boolean hitTank(Tank t) {
  78. if(this.live && this.getRect().intersects(t.getRect()) && t.isLive() && this.good != t.isGood()) {
  79. t.setLive(false);
  80. this.live = false;
  81. Explode e = new Explode(x, y, tc);
  82. tc.explodes.add(e);
  83. return true;
  84. }
  85. return false;
  86. }
  87. public boolean hitTanks(List<Tank> tanks) {
  88. for(int i=0; i<tanks.size(); i++) {
  89. if(hitTank(tanks.get(i))) {
  90. return true;
  91. }
  92. }
  93. return false;
  94. }
  95. public boolean hitWall(Wall w) {
  96. if(this.live && this.getRect().intersects(w.getRect())) {
  97. this.live = false;
  98. return true;
  99. }
  100. return false;
  101. }
  102. }