1、版本地址
https://github.com/phs999/DesignPatterns/tree/9af503c47d25dfea6a0a848438725afbc3e4a424
2、实现效果
(1)敌方坦克随机移动;
(2)敌方坦克随机发射炮弹;
(3)坦克运动范围边界控制;
(4)我方子弹遇到敌方坦克后,子弹与坦克消失;
3、评价
功能基本实现,但是代码之前耦合较多,互相调用互相依赖。
4、关键代码
package phs999.tank;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
public class TankFrame extends Frame {
Tank myTank = new Tank(200, 400, Dir.UP,Group.GOOD,this);
List<Bullet> bullets =new ArrayList<>();
List<Tank> enemyTanks=new ArrayList<>();
List<Explode> explodes=new ArrayList<>();
static final int GAME_WIDTH=800,GAME_HEIGHT=600;
public TankFrame() {
setSize(GAME_WIDTH, GAME_HEIGHT);// 像素px
setResizable(false);// 禁止改变大小
setTitle("tank war");
setVisible(true);// 设置可见性
addKeyListener(new MykeyListener());
// 设置关闭
addWindowListener(new WindowAdapter() {
/**
* Invoked when a window is in the process of being closed. The close operation
* can be overridden at this point.
*
* @param e
*/
@Override
public void windowClosing(WindowEvent e) {
// super.windowClosing(e);
System.exit(0);
}
});
}
Image offScreenImage = null;
@Override
public void update(Graphics g) {
if (offScreenImage == null) {
offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
}
Graphics gOffScreen = offScreenImage.getGraphics(