坦克抽象工厂类代码提交

master
msb_64243 3 years ago
commit 99a61ede8e

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="output" path="bin"/>
</classpath>

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Javatank</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

@ -0,0 +1,2 @@
eclipse.preferences.version=1
encoding/<project>=UTF-8

@ -0,0 +1,11 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

@ -0,0 +1,9 @@
#设置坦克数量
initTanCount=5
tankSpeed=5
bulletSpeed=10
gameWidth=1080
gameHeight=720
#fire
goodFS=com.tank.FourDirFireStrategy
badFS=com.tank.DefaultFireStrategy

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

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

@ -0,0 +1,101 @@
package com.tank;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.SourceDataLine;
public class Audio {
byte[] b = new byte[1024 * 1024 * 15];
public void loop() {
try {
while (true) {
int len = 0;
sourceDataLine.open(audioFormat, 1024 * 1024 * 15);
sourceDataLine.start();
//System.out.println(audioInputStream.markSupported());
audioInputStream.mark(12358946);
while ((len = audioInputStream.read(b)) > 0) {
sourceDataLine.write(b, 0, len);
}
audioInputStream.reset();
sourceDataLine.drain();
sourceDataLine.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private AudioFormat audioFormat = null;
private SourceDataLine sourceDataLine = null;
private DataLine.Info dataLine_info = null;
private AudioInputStream audioInputStream = null;
public Audio(String fileName) {
try {
audioInputStream = AudioSystem.getAudioInputStream(Audio.class.getClassLoader().getResource(fileName));
audioFormat = audioInputStream.getFormat();
dataLine_info = new DataLine.Info(SourceDataLine.class, audioFormat);
sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLine_info);
//FloatControl volctrl=(FloatControl)sourceDataLine.getControl(FloatControl.Type.MASTER_GAIN);
//volctrl.setValue(-40);//
} catch (Exception e) {
e.printStackTrace();
}
}
public void play() {
try {
byte[] b = new byte[1024*5];
int len = 0;
sourceDataLine.open(audioFormat, 1024*5);
sourceDataLine.start();
//System.out.println(audioInputStream.markSupported());
audioInputStream.mark(12358946);
while ((len = audioInputStream.read(b)) > 0) {
sourceDataLine.write(b, 0, len);
}
// audioInputStream.reset();
sourceDataLine.drain();
sourceDataLine.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void close() {
try {
audioInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
//Audio a = new Audio("audio/explode.wav");
//Audio a = new Audio("audio/war1.wav");
Audio a = new Audio("audio/tank_fire.wav");
a.loop();
// a.play();
}
}

@ -0,0 +1,147 @@
package com.tank;
import java.awt.Graphics;
import java.awt.Rectangle;
import com.tank.abstractfactory.BaseBullet;
/**
*
* @author leiwei
*
*/
public class Bullet extends BaseBullet{
//子弹位置
private int x;
private int y;
//子弹方向
private Dir dir;
//子弹速度
private static final int SPEED = PropertyMrg.getInt("bulletSpeed");
//设置子弹的大小width,height
public static int width = ResouceMgr.bulletD.getWidth();
public static int height = ResouceMgr.bulletD.getHeight();
//定义子弹是否存活
private boolean living = true;
TankFrame tf=null;
private TankGroup group = TankGroup.BAD;
Rectangle rect = new Rectangle();
public Dir getDir() {
return dir;
}
public void setDir(Dir dir) {
this.dir = dir;
}
public Bullet(int x, int y, Dir dir,TankGroup 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;
//将子弹添加子弹队列中
tf.bullets.add(this);
}
//子弹显示出来
@Override
public void paint(Graphics p){
if(!this.living){
tf.bullets.remove(this);
}
/*//获取画笔原来颜色
Color color = p.getColor();
//设置将要画出的子弹颜色
p.setColor(Color.red);
p.fillOval(x,y,width,height);
//画完子弹后将画笔颜色还原
p.setColor(color);*/
//将子弹根据方向画出来
switch (dir) {
case LEFT:
p.drawImage(ResouceMgr.bulletL, x, y, null);
break;
case UP:
p.drawImage(ResouceMgr.bulletU, x, y, null);
break;
case RIGHT:
p.drawImage(ResouceMgr.bulletR, x, y, null);
break;
case DOWN:
p.drawImage(ResouceMgr.bulletD, x, y, null);
break;
}
moving();
}
public void moving(){
//通过判断方向进行子弹的移动。
switch(dir){
case LEFT:
x-=SPEED;
break;
case UP:
y-=SPEED;
break;
case RIGHT:
x+=SPEED;
break;
case DOWN:
y+=SPEED;
break;
default:
break;
}
if(x<0 || y<0 || x > TankFrame.GAME_WIDTH || y>TankFrame.GAME_HEIGHT){
this.living =false;
}
rect.x = this.x;
rect.y = this.y;
}
@Override
public void collideWith(Tank t) {
if(this.group.equals(t.getGroup())){
return;
}
if(this.rect.intersects(t.rect)){
t.die();
this.die();
int eX = t.getX() + Tank.width/2 - Explode.width/2;
int eY = t.getY() + Tank.height/2 - Explode.height/2;
tf.explodes.add(tf.gf.createExplode(eX, eY, tf));
}
}
public TankGroup getGroup() {
return group;
}
public void setGroup(TankGroup group) {
this.group = group;
}
private void die() {
this.living =false;
}
}

@ -0,0 +1,29 @@
package com.tank;
public class DefaultFireStrategy implements FireStartegy {
/*
private static final DefaultFireStrategy defaultFireStrategy = new DefaultFireStrategy();
private DefaultFireStrategy() {
}
public static DefaultFireStrategy getDefualtFireStrategy(){
return defaultFireStrategy;
}
*/
@Override
public void fire(Tank t) {
//计算子弹的坐标
int bX = t.x + Tank.width/2-Bullet.width/3;
int bY = t.y + Tank.width/2-Bullet.height/3;
//可以添加多个子弹
new Bullet(bX,bY,t.dir,t.group,t.tf);
/*if(t.group == TankGroup.GOOD){
Audio a = new Audio("audio/tank_fire.wav");
a.play();
}*/
}
}

@ -0,0 +1,6 @@
package com.tank;
public enum Dir {
//设置方向
LEFT,RIGHT,UP,DOWN
}

@ -0,0 +1,42 @@
package com.tank;
import java.awt.Graphics;
import java.awt.Rectangle;
import com.tank.abstractfactory.BaseExplode;
/**
*
* @author leiwei
*
*/
public class Explode extends BaseExplode{
private int x;
private int y;
public static int width = ResouceMgr.explodes[0].getWidth();
public static int height = ResouceMgr.explodes[0].getHeight();
TankFrame tf=null;
private int step = 0;
public Explode(int x, int y,TankFrame tf) {
super();
this.x = x;
this.y = y;
this.tf = tf;
}
//显示爆炸
@Override
public void paint(Graphics p){
int eX = this.x+Tank.width/2-Explode.width/2;
int eY = this.y+Tank.width/2-Explode.height/2;
p.drawImage(ResouceMgr.explodes[step++], eX, eY, null);
if(step >= ResouceMgr.explodes.length){
tf.explodes.remove(this);
}
}
}

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

@ -0,0 +1,38 @@
package com.tank;
import java.util.ArrayList;
public class FourDirFireStrategy implements FireStartegy{
/*private static FourDirFireStrategy fourDirFireStrategy ;
private FourDirFireStrategy() {
}
public static FourDirFireStrategy getFourDirFir(){
if(fourDirFireStrategy == null){
synchronized (FourDirFireStrategy.class) {
if(fourDirFireStrategy == null){
fourDirFireStrategy = new FourDirFireStrategy();
}
}
}
return fourDirFireStrategy;
}*/
@Override
public void fire(Tank t) {
//计算子弹的坐标
int bX = t.x + Tank.width/2-Bullet.width/3;
int bY = t.y + Tank.width/2-Bullet.height/3;
Dir[] dirs = Dir.values();
//可以添加多个子弹
for(Dir dir : dirs){
new Bullet(bX,bY,dir,t.group,t.tf);
}
}
}

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save