Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dp gamemodel #1

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 22 additions & 21 deletions tank/src/com/mashibing/tank/Bullet.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,33 @@
import java.awt.Graphics;
import java.awt.Rectangle;

public class Bullet {
public class Bullet extends GameObject {
private static final int SPEED = 6;
public static int WIDTH = ResourceMgr.bulletD.getWidth();
public static int HEIGHT = ResourceMgr.bulletD.getHeight();

Rectangle rect = new Rectangle();
public Rectangle rect = new Rectangle();


private int x, y;
private Dir dir;

private boolean living = true;
TankFrame tf = null;
private Group group = Group.BAD;

public Group group = Group.BAD;

public Bullet(int x, int y, Dir dir, Group group, TankFrame tf) {
public Bullet(int x, int y, Dir dir, Group group) {
this.x = x;
this.y = y;
this.dir = dir;
this.group = group;
this.tf = tf;


rect.x = this.x;
rect.y = this.y;
rect.width = WIDTH;
rect.height = HEIGHT;

GameModel.getInstance().add(this);

}

Expand All @@ -41,7 +43,7 @@ public void setGroup(Group group) {

public void paint(Graphics g) {
if(!living) {
tf.bullets.remove(this);
GameModel.getInstance().remove(this);
}

switch(dir) {
Expand Down Expand Up @@ -87,20 +89,19 @@ private void move() {

}

public void collideWith(Tank tank) {
if(this.group == tank.getGroup()) return;

if(rect.intersects(tank.rect)) {
tank.die();
this.die();
int eX = tank.getX() + Tank.WIDTH/2 - Explode.WIDTH/2;
int eY = tank.getY() + Tank.HEIGHT/2 - Explode.HEIGHT/2;
tf.explodes.add(new Explode(eX, eY, tf));
}

}


private void die() {
public void die() {
this.living = false;
}

@Override
public int getWidth() {
return WIDTH;
}

@Override
public int getHeight() {
return HEIGHT;
}
}
20 changes: 14 additions & 6 deletions tank/src/com/mashibing/tank/Explode.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,22 @@
import java.awt.Graphics;
import java.awt.Rectangle;

public class Explode {
public class Explode extends GameObject {
public static int WIDTH = ResourceMgr.explodes[0].getWidth();
public static int HEIGHT = ResourceMgr.explodes[0].getHeight();

private int x, y;


//private boolean living = true;
TankFrame tf = null;

private int step = 0;

public Explode(int x, int y, TankFrame tf) {
public Explode(int x, int y) {
this.x = x;
this.y = y;
this.tf = tf;

new Thread(()->new Audio("audio/explode.wav").play()).start();
GameModel.getInstance().add(this);
}


Expand All @@ -29,11 +28,20 @@ public void paint(Graphics g) {
g.drawImage(ResourceMgr.explodes[step++], x, y, null);

if(step >= ResourceMgr.explodes.length)
tf.explodes.remove(this);
GameModel.getInstance().remove(this);


}

@Override
public int getWidth() {
return WIDTH;
}

@Override
public int getHeight() {
return HEIGHT;
}


}
139 changes: 139 additions & 0 deletions tank/src/com/mashibing/tank/GameModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package com.mashibing.tank;

import java.awt.Color;
import java.awt.Graphics;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import com.mashibing.tank.cor.ColliderChain;

public class GameModel {

private static final GameModel INSTANCE = new GameModel();

static {
INSTANCE.init();
}

Tank myTank;

// List<Bullet> bullets = new ArrayList<>();
// List<Tank> tanks = new ArrayList<>();
// List<Explode> explodes = new ArrayList<>();
ColliderChain chain = new ColliderChain();

private List<GameObject> objects = new ArrayList<>();

public static GameModel getInstance() {
return INSTANCE;
}

private GameModel() {}

private void init() {
// 初始化主战坦克
myTank = new Tank(200, 400, Dir.DOWN, Group.GOOD);

int initTankCount = Integer.parseInt((String) PropertyMgr.get("initTankCount"));

// 初始化敌方坦克
for (int i = 0; i < initTankCount; i++) {
new Tank(50 + i * 80, 200, Dir.DOWN, Group.BAD);
}

// 初始化墙
add(new Wall(150, 150, 200, 50));
add(new Wall(550, 150, 200, 50));
add(new Wall(300, 300, 50, 200));
add(new Wall(550, 300, 50, 200));
}

public void add(GameObject go) {
this.objects.add(go);
}

public void remove(GameObject go) {
this.objects.remove(go);
}

public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.WHITE);
// g.drawString("子弹的数量:" + bullets.size(), 10, 60);
// g.drawString("敌人的数量:" + tanks.size(), 10, 80);
// g.drawString("爆炸的数量:" + explodes.size(), 10, 100);
g.setColor(c);

myTank.paint(g);
for (int i = 0; i < objects.size(); i++) {
objects.get(i).paint(g);
}

// 互相碰撞
for (int i = 0; i < objects.size(); i++) {
for (int j = i + 1; j < objects.size(); j++) { // Comparator.compare(o1,o2)
GameObject o1 = objects.get(i);
GameObject o2 = objects.get(j);
// for
chain.collide(o1, o2);
}
}

// for (int i = 0; i < bullets.size(); i++) {
// for (int j = 0; j < tanks.size(); j++)
// bullets.get(i).collideWith(tanks.get(j));
// }

}

public Tank getMainTank() {
return myTank;
}

public void save() {
File f = new File("c:/mashibing/tank.data");
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream(f));
oos.writeObject(myTank);
oos.writeObject(objects);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(oos != null) {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

public void load() {
File f = new File("c:/mashibing/tank.data");
try {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f));
myTank = (Tank)ois.readObject();
objects = (List)ois.readObject();


} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}

}
12 changes: 12 additions & 0 deletions tank/src/com/mashibing/tank/GameObject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.mashibing.tank;

import java.awt.Graphics;
import java.io.Serializable;

public abstract class GameObject implements Serializable {
public int x, y;

public abstract void paint(Graphics g);
public abstract int getWidth();
public abstract int getHeight();
}
5 changes: 0 additions & 5 deletions tank/src/com/mashibing/tank/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@ public class Main {
public static void main(String[] args) throws InterruptedException {
TankFrame tf = new TankFrame();

int initTankCount = Integer.parseInt((String)PropertyMgr.get("initTankCount"));

//初始化敌方坦克
for(int i=0; i<initTankCount; i++) {
tf.tanks.add(new Tank(50 + i*80, 200, Dir.DOWN, Group.BAD, tf));
}

new Thread(()->new Audio("audio/war1.wav").loop()).start();

Expand Down
4 changes: 4 additions & 0 deletions tank/src/com/mashibing/tank/PropertyMgr.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ public static Object get(String key) {
return props.get(key);
}

//int getInt(key)
//getString(key)

public static void main(String[] args) {
System.out.println(PropertyMgr.get("initTankCount"));

}
}
Loading