坦克大战(一期)-设计模式-备忘录模式

DesignPatterns
bingor 2 years ago
parent b45b55167a
commit db587a297d

@ -12,8 +12,8 @@ public class TankDemo {
TankFrame tankFrame = new TankFrame(); TankFrame tankFrame = new TankFrame();
//现实当中,虽然可以过按键来改变方块的坐标并且重新刷新画板来实现移动,但是敌方的坦克应该是自动在跑 //现实当中,虽然可以过按键来改变方块的坐标并且重新刷新画板来实现移动,但是敌方的坦克应该是自动在跑
while (true) { while (true) {
// Thread.sleep(50); Thread.sleep(50);
Thread.sleep(500); // Thread.sleep(500);
tankFrame.repaint(); tankFrame.repaint();
} }
} }

@ -81,6 +81,8 @@ public class TankFrame extends Frame {
case KeyEvent.VK_RIGHT : bR = true; break; case KeyEvent.VK_RIGHT : bR = true; break;
case KeyEvent.VK_UP : bU = true; break; case KeyEvent.VK_UP : bU = true; break;
case KeyEvent.VK_DOWN : bD = true; break; case KeyEvent.VK_DOWN : bD = true; break;
case KeyEvent.VK_S: GameModel.getInstance().save();
case KeyEvent.VK_L: GameModel.getInstance().load();
default: break; default: break;
} }
setDir(); setDir();

@ -2,12 +2,14 @@ package com.msb.inter;
import com.msb.model.Tank; import com.msb.model.Tank;
import java.io.Serializable;
/** /**
* @Author bingor * @Author bingor
* @Date 2022/10/8 11:21 * @Date 2022/10/8 11:21
* @Description: com.msb * @Description: com.msb
* @Version: 1.0 * @Version: 1.0
*/ */
public interface FireStrategy { public interface FireStrategy extends Serializable {
public void fire(Tank tank); public void fire(Tank tank);
} }

@ -25,13 +25,13 @@ public class DefaultFireStrategy implements FireStrategy {
int bX = tank.getX() + Tank.WIDTH/2 - Bullet.WIDTH/2; int bX = tank.getX() + Tank.WIDTH/2 - Bullet.WIDTH/2;
int bY = tank.getY()+Tank.HEIGHT/2-Bullet.HEIGHT/2; int bY = tank.getY()+Tank.HEIGHT/2-Bullet.HEIGHT/2;
// new Bullet(bX, bY, tank.getDir(), tank.getGroup(), tank.getGameModel()); // new Bullet(bX, bY, tank.getDir(), tank.getGroup(), tank.getGameModel());
// new Bullet(bX, bY, tank.getDir(), tank.getGroup()); new Bullet(bX, bY, tank.getDir(), tank.getGroup());
GameModel.INSTANCE.add( /*GameModel.INSTANCE.add(
new RectDecorator( new RectDecorator(
new TailDecorator( new TailDecorator(
new Bullet(bX, bY, tank.getDir(), tank.getGroup()) new Bullet(bX, bY, tank.getDir(), tank.getGroup())
) )
) )
); );*/
} }
} }

@ -19,6 +19,7 @@ import com.msb.model.abstracts.BaseExplode;
import com.msb.model.abstracts.GameObject; import com.msb.model.abstracts.GameObject;
import java.awt.*; import java.awt.*;
import java.io.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -126,4 +127,29 @@ public class GameModel {
public void remove(GameObject gameObject) { public void remove(GameObject gameObject) {
gameObjects.remove(gameObject); gameObjects.remove(gameObject);
} }
public void save() {
File file = new File("F:\\bingor\\temp\\tank.data");
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(myTank);
oos.writeObject(gameObjects);
} catch (IOException e) {
e.printStackTrace();
}
}
public void load() {
File file = new File("F:\\bingor\\temp\\tank.data");
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new FileInputStream(file));
myTank = (Tank) ois.readObject();
gameObjects = (List<GameObject>) ois.readObject();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
} }

@ -6,6 +6,7 @@ package com.msb.model.abstracts;/**
*/ */
import java.awt.*; import java.awt.*;
import java.io.Serializable;
/** /**
*@ClassName GameObject *@ClassName GameObject
@ -14,7 +15,7 @@ import java.awt.*;
*@Date 2022/10/11 9:23 *@Date 2022/10/11 9:23
*@Version 3.0 *@Version 3.0
*/ */
public abstract class GameObject { public abstract class GameObject implements Serializable {
// protected int x,y; // protected int x,y;
public int x,y; public int x,y;
public abstract void paint(Graphics g); public abstract void paint(Graphics g);

Loading…
Cancel
Save