diff --git a/src/main/java/com/msb/TankDemo.java b/src/main/java/com/msb/TankDemo.java index bbe5b64..0891a67 100644 --- a/src/main/java/com/msb/TankDemo.java +++ b/src/main/java/com/msb/TankDemo.java @@ -12,8 +12,8 @@ public class TankDemo { TankFrame tankFrame = new TankFrame(); //现实当中,虽然可以过按键来改变方块的坐标并且重新刷新画板来实现移动,但是敌方的坦克应该是自动在跑 while (true) { -// Thread.sleep(50); - Thread.sleep(500); + Thread.sleep(50); +// Thread.sleep(500); tankFrame.repaint(); } } diff --git a/src/main/java/com/msb/TankFrame.java b/src/main/java/com/msb/TankFrame.java index ec1be33..03b741d 100644 --- a/src/main/java/com/msb/TankFrame.java +++ b/src/main/java/com/msb/TankFrame.java @@ -81,6 +81,8 @@ public class TankFrame extends Frame { case KeyEvent.VK_RIGHT : bR = true; break; case KeyEvent.VK_UP : bU = 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; } setDir(); diff --git a/src/main/java/com/msb/inter/FireStrategy.java b/src/main/java/com/msb/inter/FireStrategy.java index 1388381..a5a8ada 100644 --- a/src/main/java/com/msb/inter/FireStrategy.java +++ b/src/main/java/com/msb/inter/FireStrategy.java @@ -2,12 +2,14 @@ package com.msb.inter; import com.msb.model.Tank; +import java.io.Serializable; + /** * @Author bingor * @Date 2022/10/8 11:21 * @Description: com.msb * @Version: 1.0 */ -public interface FireStrategy { +public interface FireStrategy extends Serializable { public void fire(Tank tank); } diff --git a/src/main/java/com/msb/inter/impl/DefaultFireStrategy.java b/src/main/java/com/msb/inter/impl/DefaultFireStrategy.java index 0dcc054..eaa0d09 100644 --- a/src/main/java/com/msb/inter/impl/DefaultFireStrategy.java +++ b/src/main/java/com/msb/inter/impl/DefaultFireStrategy.java @@ -25,13 +25,13 @@ public class DefaultFireStrategy implements FireStrategy { int bX = tank.getX() + Tank.WIDTH/2 - Bullet.WIDTH/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()); - GameModel.INSTANCE.add( + new Bullet(bX, bY, tank.getDir(), tank.getGroup()); + /*GameModel.INSTANCE.add( new RectDecorator( new TailDecorator( new Bullet(bX, bY, tank.getDir(), tank.getGroup()) ) ) - ); + );*/ } } diff --git a/src/main/java/com/msb/model/GameModel.java b/src/main/java/com/msb/model/GameModel.java index c03bb49..13cb17c 100644 --- a/src/main/java/com/msb/model/GameModel.java +++ b/src/main/java/com/msb/model/GameModel.java @@ -19,6 +19,7 @@ import com.msb.model.abstracts.BaseExplode; import com.msb.model.abstracts.GameObject; import java.awt.*; +import java.io.*; import java.util.ArrayList; import java.util.List; @@ -126,4 +127,29 @@ public class GameModel { public void remove(GameObject 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) ois.readObject(); + } catch (IOException | ClassNotFoundException e) { + e.printStackTrace(); + } + + } + } diff --git a/src/main/java/com/msb/model/abstracts/GameObject.java b/src/main/java/com/msb/model/abstracts/GameObject.java index a137895..0625ab2 100644 --- a/src/main/java/com/msb/model/abstracts/GameObject.java +++ b/src/main/java/com/msb/model/abstracts/GameObject.java @@ -6,6 +6,7 @@ package com.msb.model.abstracts;/** */ import java.awt.*; +import java.io.Serializable; /** *@ClassName GameObject @@ -14,7 +15,7 @@ import java.awt.*; *@Date 2022/10/11 9:23 *@Version 3.0 */ -public abstract class GameObject { +public abstract class GameObject implements Serializable { // protected int x,y; public int x,y; public abstract void paint(Graphics g);