坦克大战(一期)-敌我坦克图片区分开来

Network
bingor_yhj 2 years ago
parent 9ab12006b7
commit df25691dc0

@ -1,5 +1,7 @@
package com.msb; package com.msb;
import com.msb.util.ImageUtil;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.IOException; import java.io.IOException;
@ -12,21 +14,27 @@ import java.io.IOException;
*/ */
public class ResourcesMgr { public class ResourcesMgr {
public static BufferedImage tankL, tankR, tankU, tankD; public static BufferedImage goodTankL, goodTankR, goodTankU, goodTankD;
public static BufferedImage badTankL, badTankR, badTankU, badTankD;
public static BufferedImage bulletL, bulletR, bulletU, bulletD; public static BufferedImage bulletL, bulletR, bulletU, bulletD;
public static BufferedImage [] explodeImages = new BufferedImage[16]; public static BufferedImage [] explodeImages = new BufferedImage[16];
static { static {
try { try {
tankL = ImageIO.read(ResourcesMgr.class.getClassLoader().getResourceAsStream("images/tankL.gif")); goodTankU = ImageIO.read(ResourcesMgr.class.getClassLoader().getResourceAsStream("images/GoodTank1.png"));
tankR = ImageIO.read(ResourcesMgr.class.getClassLoader().getResourceAsStream("images/tankR.gif")); goodTankL = ImageUtil.rotateImage(goodTankU, -90);
tankU = ImageIO.read(ResourcesMgr.class.getClassLoader().getResourceAsStream("images/tankU.gif")); goodTankR = ImageUtil.rotateImage(goodTankU, 90);
tankD = ImageIO.read(ResourcesMgr.class.getClassLoader().getResourceAsStream("images/tankD.gif")); goodTankD = ImageUtil.rotateImage(goodTankU, 180);
bulletL = ImageIO.read(ResourcesMgr.class.getClassLoader().getResourceAsStream("images/bulletL.gif")); badTankU = ImageIO.read(ResourcesMgr.class.getClassLoader().getResourceAsStream("images/BadTank1.png"));
bulletR = ImageIO.read(ResourcesMgr.class.getClassLoader().getResourceAsStream("images/bulletR.gif")); badTankL = ImageUtil.rotateImage(badTankU, -90);
bulletU = ImageIO.read(ResourcesMgr.class.getClassLoader().getResourceAsStream("images/bulletU.gif")); badTankR = ImageUtil.rotateImage(badTankU, 90);
bulletD = ImageIO.read(ResourcesMgr.class.getClassLoader().getResourceAsStream("images/bulletD.gif")); badTankD = ImageUtil.rotateImage(badTankU, 180);
bulletU = ImageIO.read(ResourcesMgr.class.getClassLoader().getResourceAsStream("images/bulletU.png"));
bulletL = ImageUtil.rotateImage(bulletU, -90);
bulletR = ImageUtil.rotateImage(bulletU, 90);
bulletD = ImageUtil.rotateImage(bulletU, 180);
for (int i=1; i<=explodeImages.length ; i++) { for (int i=1; i<=explodeImages.length ; i++) {
explodeImages[i-1] = ImageIO.read(ResourcesMgr.class.getClassLoader().getResourceAsStream("images/e" + i + ".gif")); explodeImages[i-1] = ImageIO.read(ResourcesMgr.class.getClassLoader().getResourceAsStream("images/e" + i + ".gif"));

@ -17,8 +17,8 @@ public class Tank {
private boolean move = true; private boolean move = true;
//为了解决能够在坦克中发射子弹,将创建的子弹通过坦克发射出来,那么需要在坦克类中持有游戏窗口的引用 //为了解决能够在坦克中发射子弹,将创建的子弹通过坦克发射出来,那么需要在坦克类中持有游戏窗口的引用
private TankFrame tankFrame; private TankFrame tankFrame;
public static final int WIDTH = ResourcesMgr.tankD.getWidth(); public static final int WIDTH = ResourcesMgr.goodTankU.getWidth();
public static final int HEIGHT = ResourcesMgr.tankD.getHeight(); public static final int HEIGHT = ResourcesMgr.goodTankU.getHeight();
private boolean live = true; private boolean live = true;
private Random random = new Random(); private Random random = new Random();
private GroupEnum group = GroupEnum.BAD; private GroupEnum group = GroupEnum.BAD;
@ -41,12 +41,13 @@ public class Tank {
} }
switch (dir) { switch (dir) {
case UP: g.drawImage(ResourcesMgr.tankU, x, y, null); break; case UP: g.drawImage(this.group==GroupEnum.GOOD ? ResourcesMgr.goodTankU : ResourcesMgr.badTankU, x, y, null); break;
case DOWN: g.drawImage(ResourcesMgr.tankD, x, y, null); break; case DOWN: g.drawImage(this.group==GroupEnum.GOOD ? ResourcesMgr.goodTankD : ResourcesMgr.badTankD, x, y, null); break;
case LEFT: g.drawImage(ResourcesMgr.tankL, x, y, null); break; case LEFT: g.drawImage(this.group==GroupEnum.GOOD ? ResourcesMgr.goodTankL : ResourcesMgr.badTankL, x, y, null); break;
case RIGHT: g.drawImage(ResourcesMgr.tankR, x, y, null); break; case RIGHT: g.drawImage(this.group==GroupEnum.GOOD ? ResourcesMgr.goodTankR : ResourcesMgr.badTankR, x, y, null); break;
default: break; default: break;
} }
moving(); moving();
} }
@ -110,7 +111,9 @@ public class Tank {
public void die() { public void die() {
this.live = false; this.live = false;
tankFrame.explodes.add(new Explode(this.x, this.y, tankFrame)); int eX = this.x + WIDTH/2 - Explode.WIDTH/2;
int eY = this.y + HEIGHT/2 - Explode.HEIGHT/2;
tankFrame.explodes.add(new Explode(eX, eY, tankFrame));
} }
public GroupEnum getGroup() { public GroupEnum getGroup() {

@ -0,0 +1,24 @@
package com.msb.util;
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;
}
}
Loading…
Cancel
Save