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

游戏

开发平台:

Java

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.util.List;
  4. import java.util.ArrayList;
  5. public class TankClient extends Frame {
  6. public static final int GAME_WIDTH = 800;
  7. public static final int GAME_HEIGHT = 600;
  8. Tank myTank = new Tank(50, 50, true, Tank.Direction.STOP, this);
  9. Wall w1 = new Wall(100, 200, 20, 150, this), w2 = new Wall(300, 100, 300, 20, this);
  10. List<Explode> explodes = new ArrayList<Explode>();
  11. List<Missile> missiles = new ArrayList<Missile>();
  12. List<Tank> tanks = new ArrayList<Tank>();
  13. Image offScreenImage = null;
  14. public void paint(Graphics g) {
  15. g.drawString("missiles count:" + missiles.size(), 10, 50);
  16. g.drawString("explodes count:" + explodes.size(), 10, 70);
  17. g.drawString("tanks    count:" + tanks.size(), 10, 90);
  18. for(int i=0; i<missiles.size(); i++) {
  19. Missile m = missiles.get(i);
  20. m.hitTanks(tanks);
  21. m.hitTank(myTank);
  22. m.hitWall(w1);
  23. m.hitWall(w2);
  24. m.draw(g);
  25. //if(!m.isLive()) missiles.remove(m);
  26. //else m.draw(g);
  27. }
  28. for(int i=0; i<explodes.size(); i++) {
  29. Explode e = explodes.get(i);
  30. e.draw(g);
  31. }
  32. for(int i=0; i<tanks.size(); i++) {
  33. Tank t = tanks.get(i);
  34. t.collidesWithWall(w1);
  35. t.collidesWithWall(w2);
  36. t.collidesWithTanks(tanks);
  37. t.draw(g);
  38. }
  39. myTank.draw(g);
  40. w1.draw(g);
  41. w2.draw(g);
  42. }
  43. public void update(Graphics g) {
  44. if(offScreenImage == null) {
  45. offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
  46. }
  47. Graphics gOffScreen = offScreenImage.getGraphics();
  48. Color c = gOffScreen.getColor();
  49. gOffScreen.setColor(Color.GREEN);
  50. gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
  51. gOffScreen.setColor(c);
  52. paint(gOffScreen);
  53. g.drawImage(offScreenImage, 0, 0, null);
  54. }
  55. public void lauchFrame() {
  56. for(int i=0; i<10; i++) {
  57. tanks.add(new Tank(50 + 40*(i+1), 50, false, Tank.Direction.D, this));
  58. }
  59. //this.setLocation(400, 300);
  60. this.setSize(GAME_WIDTH, GAME_HEIGHT);
  61. this.setTitle("TankWar");
  62. this.addWindowListener(new WindowAdapter() {
  63. public void windowClosing(WindowEvent e) {
  64. System.exit(0);
  65. }
  66. });
  67. this.setResizable(false);
  68. this.setBackground(Color.GREEN);
  69. this.addKeyListener(new KeyMonitor());
  70. setVisible(true);
  71. new Thread(new PaintThread()).start();
  72. }
  73. public static void main(String[] args) {
  74. TankClient tc = new TankClient();
  75. tc.lauchFrame();
  76. }
  77. private class PaintThread implements Runnable {
  78. public void run() {
  79. while(true) {
  80. repaint();
  81. try {
  82. Thread.sleep(50);
  83. } catch (InterruptedException e) {
  84. e.printStackTrace();
  85. }
  86. }
  87. }
  88. }
  89. private class KeyMonitor extends KeyAdapter {
  90. public void keyReleased(KeyEvent e) {
  91. myTank.keyReleased(e);
  92. }
  93. public void keyPressed(KeyEvent e) {
  94. myTank.keyPressed(e);
  95. }
  96. }
  97. }