当前位置:   article > 正文

Java实现坦克大战_javachengxutankedazhan

javachengxutankedazhan

为您提供一个详细并且简单的坦克大战游戏实现的Java代码:

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. public class TankGame extends JFrame {
  5. public static void main(String[] args) {
  6. new TankGame().launchFrame();
  7. }
  8. private void launchFrame() {
  9. this.setTitle("坦克大战");
  10. this.setSize(700, 600);
  11. this.setLocationRelativeTo(null);
  12. this.setResizable(false);
  13. this.addWindowListener(new WindowAdapter() {
  14. public void windowClosing(WindowEvent e) {
  15. System.exit(0);
  16. }
  17. });
  18. this.add(new GamePanel());
  19. this.setVisible(true);
  20. }
  21. }
  22. class GamePanel extends JPanel implements Runnable {
  23. private static final long serialVersionUID = 1L;
  24. private Tank tank;
  25. private Bullet bullet;
  26. private EnemyTank enemyTank;
  27. private boolean bGameOver = false;
  28. private boolean bPause = false;
  29. private Image offScreenImage = null;
  30. public GamePanel() {
  31. this.addKeyListener(new KeyAdapter() {
  32. public void keyPressed(KeyEvent e) {
  33. int code = e.getKeyCode();
  34. if (code == KeyEvent.VK_Q) {
  35. System.exit(0);
  36. }
  37. if (!bGameOver && !bPause) {
  38. switch (code) {
  39. case KeyEvent.VK_UP:
  40. tank.move(Dir.UP);
  41. break;
  42. case KeyEvent.VK_DOWN:
  43. tank.move(Dir.DOWN);
  44. break;
  45. case KeyEvent.VK_LEFT:
  46. tank.move(Dir.LEFT);
  47. break;
  48. case KeyEvent.VK_RIGHT:
  49. tank.move(Dir.RIGHT);
  50. break;
  51. case KeyEvent.VK_SPACE:
  52. bullet = tank.fire();
  53. break;
  54. }
  55. }
  56. }
  57. public void keyReleased(KeyEvent e) {
  58. int code = e.getKeyCode();
  59. if (!bGameOver && !bPause) {
  60. switch (code) {
  61. case KeyEvent.VK_UP:
  62. case KeyEvent.VK_DOWN:
  63. case KeyEvent.VK_LEFT:
  64. case KeyEvent.VK_RIGHT:
  65. tank.stop();
  66. break;
  67. }
  68. }
  69. }
  70. });
  71. this.tank = new Tank(150, 20, Dir.STOP, this);
  72. this.enemyTank = new EnemyTank(550, 500, Dir.UP, this);
  73. Thread t = new Thread(this);
  74. t.start();
  75. }
  76. public void paint(Graphics g) {
  77. g.setColor(Color.GREEN);
  78. g.fillRect(0, 0, 700, 600);
  79. if (bGameOver) {
  80. g.setColor(Color.RED);
  81. g.setFont(new Font("宋体", Font.BOLD, 50));
  82. g.drawString("游戏结束!", 200, 300);
  83. return;
  84. }
  85. if (bPause) {
  86. g.setColor(Color.RED);
  87. g.setFont(new Font("宋体", Font.BOLD, 50));
  88. g.drawString("游戏暂停", 200, 300);
  89. return;
  90. }
  91. if (tank.isLive()) {
  92. tank.draw(g);
  93. }
  94. if (enemyTank.isLive()) {
  95. enemyTank.draw(g);
  96. }
  97. if (bullet != null) {
  98. bullet.draw(g);
  99. // 碰撞检测
  100. if (enemyTank.isLive() && bullet.hit(enemyTank)) {
  101. bullet.setLive(false);
  102. enemyTank.setLive(false);
  103. bGameOver = true;
  104. }
  105. }
  106. }
  107. public void update(Graphics g) {
  108. if (offScreenImage == null) {
  109. offScreenImage = this.createImage(700, 600);
  110. }
  111. Graphics gOffScreen = offScreenImage.getGraphics();
  112. Color c = gOffScreen.getColor();
  113. gOffScreen.setColor(Color.GREEN);
  114. gOffScreen.fillRect(0, 0, 700, 600);
  115. gOffScreen.setColor(c);
  116. paint(gOffScreen);
  117. g.drawImage(offScreenImage, 0, 0, null);
  118. }
  119. public void run() {
  120. while (!bGameOver) {
  121. try {
  122. Thread.sleep(50);
  123. } catch (InterruptedException e) {
  124. e.printStackTrace();
  125. }
  126. if (!bPause && enemyTank.isLive()) {
  127. enemyTank.run();
  128. }
  129. if (bullet != null && bullet.isLive()) {
  130. bullet.run();
  131. }
  132. this.repaint();
  133. }
  134. }
  135. // 暂停游戏
  136. public void pauseGame() {
  137. bPause = true;
  138. }
  139. // 继续游戏
  140. public void continueGame() {
  141. bPause = false;
  142. }
  143. // 重新开始游戏
  144. public void restartGame() {
  145. this.tank = new Tank(150, 20, Dir.STOP, this);
  146. this.enemyTank = new EnemyTank(550, 500, Dir.UP, this);
  147. this.bullet = null;
  148. this.bGameOver = false;
  149. }
  150. }
  151. enum Dir {
  152. UP, DOWN, LEFT, RIGHT, STOP
  153. }
  154. class Tank {
  155. private int x, y;
  156. private Dir dir;
  157. private static final int SPEED = 10;
  158. private boolean live = true;
  159. private GamePanel gamePanel;
  160. public Tank(int x, int y, Dir dir, GamePanel gamePanel) {
  161. this.x = x;
  162. this.y = y;
  163. this.dir = dir;
  164. this.gamePanel = gamePanel;
  165. }
  166. public boolean isLive() {
  167. return live;
  168. }
  169. public void setLive(boolean live) {
  170. this.live = live;
  171. }
  172. public void draw(Graphics g) {
  173. if (!live) return;
  174. switch (dir) {
  175. case UP:
  176. g.drawImage(Tools.getImage("res/tankU.png"), x, y, null);
  177. break;
  178. case DOWN:
  179. g.drawImage(Tools.getImage("res/tankD.png"), x, y, null);
  180. break;
  181. case LEFT:
  182. g.drawImage(Tools.getImage("res/tankL.png"), x, y, null);
  183. break;
  184. case RIGHT:
  185. g.drawImage(Tools.getImage("res/tankR.png"), x, y, null);
  186. break;
  187. }
  188. }
  189. public void move(Dir dir) {
  190. this.dir = dir;
  191. switch (dir) {
  192. case UP:
  193. y -= SPEED;
  194. break;
  195. case DOWN:
  196. y += SPEED;
  197. break;
  198. case LEFT:
  199. x -= SPEED;
  200. break;
  201. case RIGHT:
  202. x += SPEED;
  203. break;
  204. }
  205. // 边界检测
  206. if (x < 0) x = 0;
  207. if (y < 40) y = 40; // 避免将游戏菜单挡住,留一定区域给游戏菜单
  208. if (x > 630) x = 630;
  209. if (y > 530) y = 530;
  210. }
  211. public Bullet fire() {
  212. int bulletX = x + Tools.getImage("res/tankU.png").getWidth(null) / 2 - Tools.getImage("res/bulletU.gif").getWidth(null) / 2;
  213. int bulletY = y + Tools.getImage("res/tankU.png").getHeight(null) / 2 - Tools.getImage("res/bulletU.gif").getHeight(null) / 2;
  214. return new Bullet(bulletX, bulletY, dir, gamePanel);
  215. }
  216. public void stop() {
  217. this.dir = Dir.STOP;
  218. }
  219. // 坦克的碰撞检测
  220. public Rectangle getRectangle() {
  221. return new Rectangle(x, y, Tools.getImage("res/tankU.png").getWidth(null), Tools.getImage("res/tankU.png").getHeight(null));
  222. }
  223. }
  224. class EnemyTank extends Tank {
  225. private static final long serialVersionUID = 1L;
  226. private int step = 0;
  227. public EnemyTank(int x, int y, Dir dir, GamePanel gamePanel) {
  228. super(x, y, dir, gamePanel);
  229. }
  230. @Override
  231. public void run() {
  232. // 敌军坦克自动开火
  233. if (Math.random() > 0.9) {
  234. this.fire();
  235. }
  236. // 坦克随机转向和移动,每个方向停留10次才能真正执行移动操作
  237. if (step == 10) {
  238. step = 0; // 该步骤结束
  239. int r = (int) (Math.random() * 4); // 随机生成03的整数
  240. switch (r) {
  241. case 0:
  242. this.setDir(Dir.UP);
  243. break;
  244. case 1:
  245. this.setDir(Dir.RIGHT);
  246. break;
  247. case 2:
  248. this.setDir(Dir.DOWN);
  249. break;
  250. case 3:
  251. this.setDir(Dir.LEFT);
  252. break;
  253. }
  254. }
  255. step++;
  256. move(getDir());
  257. }
  258. }
  259. class Bullet {
  260. private int x, y;
  261. private Dir dir;
  262. private static final int SPEED = 20;
  263. private boolean live = true;
  264. private GamePanel gamePanel;
  265. public Bullet(int x, int y, Dir dir, GamePanel gamePanel) {
  266. this.x = x;
  267. this.y = y;
  268. this.dir = dir;
  269. this.gamePanel = gamePanel;
  270. }
  271. public boolean isLive() {
  272. return live;
  273. }
  274. public void setLive(boolean live) {
  275. this.live = live;
  276. }
  277. public void draw(Graphics g) {
  278. if (!live) return;
  279. switch (dir) {
  280. case UP:
  281. g.drawImage(Tools.getImage("res/bulletU.gif"), x, y, null);
  282. break;
  283. case DOWN:
  284. g.drawImage(Tools.getImage("res/bulletD.gif"), x, y, null);
  285. break;
  286. case LEFT:
  287. g.drawImage(Tools.getImage("res/bulletL.gif"), x, y, null);
  288. break;
  289. case RIGHT:
  290. g.drawImage(Tools.getImage("res/bulletR.gif"), x, y, null);
  291. break;
  292. }
  293. }
  294. public void run() {
  295. switch (dir) {
  296. case UP:
  297. y -= SPEED;
  298. break;
  299. case DOWN:
  300. y += SPEED;
  301. break;
  302. case LEFT:
  303. x -= SPEED;
  304. break;
  305. case RIGHT:
  306. x += SPEED;
  307. break;
  308. }
  309. // 边界检测
  310. if (x < 0 || y < 0 || x > 700 || y > 550) {
  311. live = false;
  312. return;
  313. }
  314. // 子弹与坦克的碰撞检测
  315. if (gamePanel.tank.isLive() && hit(gamePanel.tank)) {
  316. live = false;
  317. gamePanel.tank.setLive(false);
  318. gamePanel.bGameOver = true;
  319. }
  320. }
  321. // 子弹的碰撞检测
  322. public boolean hit(Tank tank) {
  323. if (this.isLive() && tank.isLive() && tank.getRectangle().intersects(new Rectangle(x, y, Tools.getImage("res/bulletU.gif").getWidth(null), Tools.getImage("res/bulletU.gif").getHeight(null)))) {
  324. return true;
  325. }
  326. return false;
  327. }
  328. }
  329. class Tools {
  330. public static Image getImage(String path) {
  331. return new ImageIcon(path).getImage();
  332. }
  333. }

以上是一个简单的坦克大战游戏Java实现代码,其中涉及到一些基本的Java语法和图形化界面的实现,您可以自行修改或者扩展它,实现您想要的更多功能。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/326331
推荐阅读
相关标签
  

闽ICP备14008679号