0.开篇-设计模式学习之坦克大战
程序员文章站
2024-03-22 19:25:40
...
目录
1、目的
通过一个项目的逐步迭代,来进行设计模式的思想学习。
2、参考
[1] 马士兵-坦克大战
[2] 慕课网-java设计模式精讲
3、我的代码地址
https://github.com/phs999/DesignPatterns
4、初版关键点与关系图
首先实现了以下几个关键点:
(1)实现一个可关闭窗口;
(2)实现方框坦克按方向键移动,不按方向键静止;
(3)实现子弹类发射子弹。
各类之间的关系图如下:
5、主要代码
package phs999.tank;
public class Main {
public static void main(String[] args) throws InterruptedException {
TankFrame tf = new TankFrame();
while(true) {
Thread.sleep(50);
tf.repaint();
}
}
}
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;
public class TankFrame extends Frame {
Tank tank = new Tank(200, 200, Dir.DOWN);
Bullet bullet =new Bullet(300, 300, Dir.DOWN);
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();
Color c = gOffScreen.getColor();
gOffScreen.setColor(Color.BLACK);
gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
gOffScreen.setColor(c);
paint(gOffScreen);
g.drawImage(offScreenImage, 0, 0, null);
}
@Override
public void paint(Graphics g) {
tank.paint(g);
bullet.paint(g);
//x += 10;
}
class MykeyListener extends KeyAdapter {
boolean bL=false;
boolean bR=false;
boolean bU=false;
boolean bD=false;
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
switch (key) {
case KeyEvent.VK_LEFT:
bL=true;
break;
case KeyEvent.VK_RIGHT:
bR=true;
break;
case KeyEvent.VK_UP:
bU=true;
break;
case KeyEvent.VK_DOWN:
bD=true;
break;
/*case KeyEvent.VK_CONTROL:
tank.fire();*/
default:
break;
}
// x+=200;
// y+=10;
// repaint();
setMainTankDir();
/*
* if (bL) { x-=10; } if (bR) { x+=10; } if (bU) { y-=10; } if (bD) { y+=10; }
*/
}
private void setMainTankDir() {
if (!bL && !bR && !bU && !bD) {
tank.setMoving(false);
}else {
tank.setMoving(true);
if (bL) {
tank.setDir(Dir.LEFT);
}
if (bR) {
tank.setDir(Dir.RIGHT);
}
if (bU) {
tank.setDir(Dir.UP);
}
if (bD) {
tank.setDir(Dir.DOWN);
}
}
}
@Override
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
switch (key) {
case KeyEvent.VK_LEFT:
bL=false;
break;
case KeyEvent.VK_RIGHT:
bR=false;
break;
case KeyEvent.VK_UP:
bU=false;
break;
case KeyEvent.VK_DOWN:
bD=false;
break;
default:
break;
}
setMainTankDir();
}
}
}
package phs999.tank;
import java.awt.Color;
import java.awt.Graphics;
public class Tank {
private int x, y;
private Dir dir = Dir.DOWN;
private boolean moving=false;
private static final int speed=5;
public boolean isMoving() {
return moving;
}
public void setMoving(boolean moving) {
this.moving = moving;
}
public Dir getDir() {
return dir;
}
public void setDir(Dir dir) {
this.dir = dir;
}
public Tank(int x, int y, Dir dir) {
super();
this.x = x;
this.y = y;
this.dir = dir;
}
public void paint(Graphics g) {
Color color=g.getColor();
g.setColor(Color.YELLOW);
g.fillRect(x, y, 50, 50);
g.setColor(color);
move();
}
private void move() {
if (!moving) {
return;
}
switch (dir) {
case LEFT:
x-=speed;
break;
case RIGHT:
x+=speed;
break;
case UP:
y-=speed;
break;
case DOWN:
y+=speed;
break;
default:
break;
}
}
}
package phs999.tank;
import java.awt.Color;
import java.awt.Graphics;
public class Bullet {
private static final int speed=10;
private static int WIDTH=5,HEIGHT=5;
private int x, y;
private Dir dir;
public Bullet(int x, int y, Dir dir) {
this.x = x;
this.y = y;
this.dir = dir;
}
public void paint(Graphics g) {
Color color=g.getColor();
g.setColor(Color.RED);
g.fillOval(x, y, WIDTH,HEIGHT);
g.setColor(color);
move();
}
private void move() {
switch (dir) {
case LEFT:
x-=speed;
break;
case RIGHT:
x+=speed;
break;
case UP:
y-=speed;
break;
case DOWN:
y+=speed;
break;
default:
break;
}
}
}
package phs999.tank;
public enum Dir {
LEFT,RIGHT,UP,DOWN
}