结合设计模式之前的分支

master
Your Name 2 years ago
parent 181a59fbf8
commit 34d95d5d37

@ -0,0 +1,145 @@
package com.mashibing.tank;
import java.awt.Graphics;
import java.awt.Rectangle;
public class Bullet {
private static final int SPEED = 10;
private int x,y;
public static int width =ResourceMgr.bulletD.getWidth();
public static int height =ResourceMgr.bulletD.getHeight();
private boolean living = true;
private Dir dir;
private TankFrame tf;
private Group group = Group.BAD;
Rectangle rect = new Rectangle();
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public boolean isLiving() {
return living;
}
public void setLiving(boolean living) {
this.living = living;
}
public Dir getDir() {
return dir;
}
public void setDir(Dir dir) {
this.dir = dir;
}
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
public Bullet(int x, int y, Dir dir,Group group,TankFrame tf) {
super();
this.x = x;
this.y = y;
this.dir = dir;
this.tf = tf;
this.group = group;
rect.x = this.x;
rect.y = this.y;
rect.width = width;
rect.height = height;
}
public void paint(Graphics g) {
if(!living ){
tf.bullets.remove(this);
}
switch(dir){
case LEFT:
g.drawImage(ResourceMgr.bulletL, x, y, null);
break;
case UP:
g.drawImage(ResourceMgr.bulletU, x, y, null);
break;
case RIGHT:
g.drawImage(ResourceMgr.bulletR, x, y, null);
break;
case DOWN:
g.drawImage(ResourceMgr.bulletD, x, y, null);
break;
}
moving();
}
private void moving() {
switch(dir){
case LEFT:
x-=SPEED;
break;
case RIGHT:
x+=SPEED;
break;
case DOWN:
y+=SPEED;
break;
case UP:
y-=SPEED;
break;
}
rect.x = this.x;
rect.y = this.y;
if(x < 0 || y <0 || x > TankFrame.GAME_WIDTH || y > TankFrame.GAME_HEIGHT) living = false;
}
public void collideWith(Tank tank,Graphics g) {
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.y + Tank.HEIGHT/2 -Explode.height/2;
tf.explodes.add(new Explode(eX,eY, tf));
}
}
private void die(){
this.living = false;
}
}

@ -0,0 +1,13 @@
package com.mashibing.tank;
public class DefaultFireStrategy implements FireStrategy{
@Override
public void fire(Tank t) {
int bX = t.x + Tank.WIDTH/2 -Bullet.width/2;
int bY = t.y + Tank.HEIGHT/2 -Bullet.height/2;
// tf.bullets.add(new Bullet(bX,bY,this.dir,this.group,this.tf)) ;
}
}

@ -0,0 +1,7 @@
package com.mashibing.tank;
public enum Dir {
LEFT,UP,RIGHT,DOWN
}

@ -0,0 +1,44 @@
package com.mashibing.tank;
import java.awt.Graphics;
import java.awt.Rectangle;
/**
*
* @author My
*
*/
public class Explode {
private int x,y;
public static int width =ResourceMgr.explodes[0].getWidth();
public static int height =ResourceMgr.explodes[0].getHeight();
private boolean living = true;
private int step = 0;
private TankFrame tf;
public Explode(int x, int y,TankFrame tf) {
super();
this.x = x;
this.y = y;
this.tf = tf;
}
public void paint(Graphics g) {
g.drawImage(ResourceMgr.explodes[step++],x,y,null);
if(step>= ResourceMgr.explodes.length)
tf.explodes.remove(this);
}
}

@ -0,0 +1,6 @@
package com.mashibing.tank;
public interface FireStrategy {
void fire(Tank t);
}

@ -0,0 +1,6 @@
package com.mashibing.tank;
public enum Group {
GOOD,BAD;
}

@ -0,0 +1,24 @@
package com.mashibing.tank;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
public class ImageUtil {
public static BufferedImage rotateImage(final BufferedImage bufferedimage,
final int degree) {
int w = bufferedimage.getWidth();
int h = bufferedimage.getHeight();
int type = bufferedimage.getColorModel().getTransparency();
BufferedImage img;
Graphics2D graphics2d;
(graphics2d = (img = new BufferedImage(w, h, type))
.createGraphics()).setRenderingHint(
RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2d.rotate(Math.toRadians(degree), w / 2, h / 2);
graphics2d.drawImage(bufferedimage, 0, 0, null);
graphics2d.dispose();
return img;
}
}

@ -0,0 +1,26 @@
package com.mashibing.tank;
import java.io.IOException;
import java.util.Properties;
public class PropertyMgr {
static Properties props = new Properties();
static {
try {
props.load(PropertyMgr.class.getClassLoader().getResourceAsStream("config"));
} catch (IOException e) {
e.printStackTrace();
}
}
public static Object get(String key){
if(props == null){
return null;
} else{
return props.get(key);
}
}
}

@ -0,0 +1,40 @@
package com.mashibing.tank;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ResourceMgr {
public static BufferedImage goodTankL, goodTankU,goodTankR,goodTankD;
public static BufferedImage badTankL, badTankU,badTankR,badTankD;
public static BufferedImage bulletL, bulletU,bulletR,bulletD;
public static BufferedImage[] explodes = new BufferedImage[16];
static{
try {
goodTankU = ImageIO.read(ResourceMgr.class.getClassLoader().getResourceAsStream("images/GoodTank1.png"));
goodTankL = ImageUtil.rotateImage(goodTankU, -90);
goodTankR = ImageUtil.rotateImage(goodTankU, -90);
goodTankD = ImageUtil.rotateImage(goodTankU, -90);
badTankU = ImageIO.read(ResourceMgr.class.getClassLoader().getResourceAsStream("images/BadTank1.png"));
badTankL = ImageUtil.rotateImage(badTankU, -90);
badTankR = ImageUtil.rotateImage(badTankU, 90);
badTankD = ImageUtil.rotateImage(badTankU, 180);
bulletU = ImageIO.read(ResourceMgr.class.getClassLoader().getResourceAsStream("images/bulletU.png"));
bulletL = ImageUtil.rotateImage(bulletU, -90);
bulletR = ImageUtil.rotateImage(bulletU, 90);
bulletD = ImageUtil.rotateImage(bulletU, 180);
for (int i = 0; i < 16; i++) {
explodes[i] = ImageIO.read(ResourceMgr.class.getClassLoader().getResourceAsStream("images/e" +(i+1) +".gif"));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

@ -1,28 +1,21 @@
package com.mashibing.tank;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class T {
public static void main(String[] args) {
public static void main(String[] args) throws InterruptedException {
Frame f = new Frame();
f.setVisible(true);
f.setSize(800,600);
f.setResizable(false);
f.setTitle("tank war");
f.addWindowFocusListener(new WindowAdapter() {
TankFrame tf = new TankFrame();
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
int initTankCout = Integer.parseInt((String)PropertyMgr.get("initTankCount"));
for (int i = 0; i < initTankCout; i++) {
tf.tanks.add(new Tank(50 + i*30, 200,Dir.UP,Group.BAD,tf));
}
while(true){
Thread.sleep(50);
tf.repaint();
}
}
}

@ -0,0 +1,171 @@
package com.mashibing.tank;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Random;
public class Tank {
int x,y;
Dir dir = Dir.DOWN;
private static final int SPEED = 2;
public static int WIDTH = ResourceMgr.goodTankD.getWidth();
public static int HEIGHT = ResourceMgr.goodTankD.getHeight();
private Random random = new Random();
private boolean moving = true;
private TankFrame tf = null;
private boolean living = true;
private Group group = Group.BAD;
Rectangle rect = new Rectangle();
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Dir getDir() {
return dir;
}
public void setDir(Dir dir) {
this.dir = dir;
}
public boolean isMoving() {
return moving;
}
public void setMoving(boolean moving) {
this.moving = moving;
}
public Tank(int x, int y, Dir dir,Group group ,TankFrame tf) {
super();
this.x = x;
this.y = y;
this.dir = dir;
this.group = group;
this.tf = tf;
}
public void paint(Graphics g) {
if(!living) tf.tanks.remove(this);
switch (dir){
case LEFT:
g.drawImage(this.group == Group.GOOD ?ResourceMgr.goodTankL : ResourceMgr.badTankL,x,y,null);
break;
case RIGHT:
g.drawImage(this.group == Group.GOOD ?ResourceMgr.goodTankR : ResourceMgr.badTankR,x,y,null);
break;
case UP:
g.drawImage(this.group == Group.GOOD ?ResourceMgr.goodTankU : ResourceMgr.badTankU,x,y,null);
break;
case DOWN:
g.drawImage(this.group == Group.GOOD ?ResourceMgr.goodTankD : ResourceMgr.badTankD,x,y,null);
break;
}
moving();
}
private void moving() {
if(!moving) return;
switch(dir){
case LEFT:
x-=SPEED;
break;
case RIGHT:
x+=SPEED;
break;
case DOWN:
y+=SPEED;
break;
case UP:
y-=SPEED;
break;
}
if(this.group == Group.BAD && random.nextInt(100) > 95){
this.fire();
}
if(this.group == Group.GOOD) return;
if(this.group == Group.BAD && random.nextInt(100) > 95) randomDir();
boundsCheck();
rect.x = this.x;
rect.y = this.y;
}
private void boundsCheck() {
if(this.x < 0) x = 2;
if (this.y < 30) y = 30;
if(this.x > TankFrame.GAME_WIDTH - Tank.WIDTH) x = TankFrame.GAME_WIDTH -Tank.WIDTH;
if(this.y > TankFrame.GAME_HEIGHT) y = TankFrame.GAME_HEIGHT- Tank.HEIGHT;
}
private void randomDir() {
this.dir = Dir.values()[random.nextInt(4)];
}
public void fire() {
int bX = this.x + Tank.WIDTH/2 -Bullet.width/2;
int bY = this.y + Tank.HEIGHT/2 -Bullet.height/2;
tf.bullets.add(new Bullet(bX,bY,this.dir,this.group,this.tf)) ;
}
public void die() {
this.living = false;
}
}

@ -0,0 +1,170 @@
package com.mashibing.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.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
public class TankFrame extends Frame{
static final int GAME_WIDTH =800;
static final int GAME_HEIGHT = 600;
Tank myTank = new Tank(200,200,Dir.DOWN,Group.GOOD,this);
// Bullet b = new Bullet(300,300,Dir.DOWN,Group.GOOD,this);
List<Bullet> bullets = new ArrayList<>();
List<Tank> tanks = new ArrayList<>();
List<Explode> explodes = new ArrayList<>();
// Explode e = new Explode(100, 100, this);
public TankFrame(){
setSize(GAME_WIDTH,GAME_HEIGHT);
setResizable(false);
setTitle("tank war");
setVisible(true);
this.addKeyListener(new MykeyListener());
//匿名内部类
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
Image offScreenImage = null;
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){
g.setColor(Color.WHITE);
g.drawString("子弹的数量" + bullets.size(), 10, 60);
g.drawString("敌人的数量" + tanks.size(), 10, 80);
g.drawString("爆炸的数量" + explodes.size(), 10, 100);
myTank.paint(g);
for (int i = 0; i < bullets.size(); i++) {
bullets.get(i).paint(g);
}
for (int i = 0; i < tanks.size(); i++) {
tanks.get(i).paint(g);
}
for (int i = 0; i < explodes.size(); i++) {
explodes.get(i).paint(g);
}
for (int i = 0; i < bullets.size(); i++) {
for (Tank t : tanks) {
bullets.get(i).collideWith(t,g);
}
}
// e.paint(g);
}
class MykeyListener extends KeyAdapter{
boolean bL = false;
boolean bU = false;
boolean bR = 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_UP:
bU = true;
break;
case KeyEvent.VK_RIGHT:
bR = true;
break;
case KeyEvent.VK_DOWN:
bD = true;
break;
case KeyEvent.VK_CONTROL:
myTank.fire();
break;
default:
break;
}
setMainTankDir();
}
@Override
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
switch(key){
case KeyEvent.VK_LEFT:
bL = false;
break;
case KeyEvent.VK_UP:
bU = false;
break;
case KeyEvent.VK_RIGHT:
bR = false;
break;
case KeyEvent.VK_DOWN:
bD = false;
break;
default:
break;
}
setMainTankDir();
}
private void setMainTankDir() {
if(!bL && !bU && !bR && !bD) myTank.setMoving(false);
else{
myTank.setMoving(true);
if(bL) myTank.setDir(Dir.LEFT);
if(bU) myTank.setDir(Dir.UP);
if(bR) myTank.setDir(Dir.RIGHT);
if(bD) myTank.setDir(Dir.DOWN);
}
}
}
}

@ -0,0 +1 @@
initTankCount=10

Binary file not shown.

After

Width:  |  Height:  |  Size: 824 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 868 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 936 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 855 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 839 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 839 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 852 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 847 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 312 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1022 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 814 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 460 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 280 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 565 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 846 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 864 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 863 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 861 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 863 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 673 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 670 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 680 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 677 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 660 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 691 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 674 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 271 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Loading…
Cancel
Save