4.1 初始化

master
terry 3 years ago
parent a9c714596a
commit fd6665a536

Binary file not shown.

@ -0,0 +1,117 @@
package com.demo.tank.course4;
import java.awt.*;
public class Bullet {
private int x, y;
private Direction direction;
private static final int SPEED = 5;
public static final int WIDTH = ResourceManager.bulletD.getWidth();
public static final int HEIGHT = ResourceManager.bulletD.getHeight();
private boolean live = true;
private Group group = Group.BAD;
private TankFrameV4 tf;
public Bullet(int x, int y, Direction direction, Group group, TankFrameV4 tf) {
this.x = x;
this.y = y;
this.direction = direction;
this.group = group;
this.tf = tf;
}
public void paint(Graphics g){
if(!live){
tf.bullets.remove(this);
}
switch (direction){
case UP:
g.drawImage(ResourceManager.bulletU, x, y, null);
break;
case DOWN:
g.drawImage(ResourceManager.bulletD, x, y, null);
break;
case LEFT:
g.drawImage(ResourceManager.bulletL, x, y, null);
break;
case RIGHT:
g.drawImage(ResourceManager.bulletR, x, y, null);
break;
}
move();
}
private void move() {
switch (direction){
case UP: y -= SPEED;
break;
case DOWN: y += SPEED;
break;
case LEFT: x -= SPEED;
break;
case RIGHT: x += SPEED;
break;
default:
break;
}
if(x < 0 || y < 0 || x > TankFrameV4.GAME_WIDTH || y > TankFrameV4.GAME_HEIGHT){
live = false;
}
}
//检测是否跟坦克碰撞
public void collideWith(Tank tank){
//关闭队友伤害
if(this.group == tank.getGroup()) return;
Rectangle bulletRect = new Rectangle(x, y, WIDTH, HEIGHT);
Rectangle tankRect = new Rectangle(tank.getX(), tank.getY(), Tank.WIDTH, Tank.HEIGHT);
if(bulletRect.intersects(tankRect)){
tank.die();
this.die();
}
}
private void die() {
this.live = false;
}
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 Direction getDirection() {
return direction;
}
public void setDirection(Direction direction) {
this.direction = direction;
}
public boolean isLive() {
return live;
}
public void setLive(boolean live) {
this.live = live;
}
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
}

@ -0,0 +1,5 @@
package com.demo.tank.course4;
public enum Direction {
LEFT, RIGHT, UP, DOWN;
}

@ -0,0 +1,46 @@
package com.demo.tank.course4;
import com.demo.tank.util.Audio;
import java.awt.*;
public class Explode {
private int x, y;
public static final int WIDTH = ResourceManager.explodes[0].getWidth();
public static final int HEIGHT = ResourceManager.explodes[0].getHeight();
private boolean living = true;
private TankFrameV4 tf;
private int step = 0;
public Explode(int x, int y, TankFrameV4 tf) {
this.x = x;
this.y = y;
this.tf = tf;
// new Thread(() -> new Audio("audio/explode.wav").play()).start();
}
public void paint(Graphics g){
g.drawImage(ResourceManager.explodes[step++], x, y, null);
if(step >= ResourceManager.explodes.length){
step = 0;
}
}
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;
}
}

@ -0,0 +1,5 @@
package com.demo.tank.course4;
public enum Group {
GOOD, BAD
}

@ -0,0 +1,21 @@
package com.demo.tank.course4;
import com.demo.tank.util.Audio;
import java.io.IOException;
public class MainV4 {
public static void main(String[] args) throws InterruptedException, IOException {
TankFrameV4 tf = new TankFrameV4();
for(int i = 0; i < 5; i++){
tf.enemyTanks.add(new Tank(50 + i*80, 200, Direction.DOWN, Group.BAD, tf));
}
// new Thread(() -> new Audio("audio/war1.wav").loop()).start();
while (true){
Thread.sleep(50);
tf.repaint();
}
}
}

@ -0,0 +1,33 @@
package com.demo.tank.course4;
import com.demo.tank.util.ImageUtil;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
public class ResourceManager {
public static BufferedImage tankL, tankR, tankU, tankD;
public static BufferedImage bulletL, bulletR, bulletU, bulletD;
public static BufferedImage[] explodes = new BufferedImage[16];
static {
try {
tankU = ImageIO.read(ResourceManager.class.getClassLoader().getResourceAsStream("images/BadTank1.png"));
tankL = ImageUtil.rotateImage(tankU, -90);
tankR = ImageUtil.rotateImage(tankU, 90);
tankD = ImageUtil.rotateImage(tankU, 180);
bulletL = ImageIO.read(ResourceManager.class.getClassLoader().getResourceAsStream("images/bulletL.gif"));
bulletR = ImageIO.read(ResourceManager.class.getClassLoader().getResourceAsStream("images/bulletR.gif"));
bulletU = ImageIO.read(ResourceManager.class.getClassLoader().getResourceAsStream("images/bulletU.gif"));
bulletD = ImageIO.read(ResourceManager.class.getClassLoader().getResourceAsStream("images/bulletD.gif"));
for (int i= 0; i<16; i++){
explodes[i] = ImageIO.read(ResourceManager.class.getClassLoader().getResourceAsStream("images/e"+ (i+1)+".gif"));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

@ -0,0 +1,121 @@
package com.demo.tank.course4;
import java.awt.*;
import java.util.Random;
public class Tank {
private int x,y;
private Direction dir;
private static final int SPEED = 8;
private boolean moving = false;
private boolean living = true;
private Group group = Group.BAD;
private TankFrameV4 tankFrame;
public static final int WIDTH = ResourceManager.tankD.getWidth();
public static final int HEIGHT = ResourceManager.tankD.getHeight();
private Random random = new Random();
public Tank(int x, int y, Direction dir, Group group, TankFrameV4 tankFrame) {
this.x = x;
this.y = y;
this.dir = dir;
this.group = group;
this.tankFrame = tankFrame;
}
public void paint(Graphics g) {
if(!living) tankFrame.enemyTanks.remove(this);
//根据方向绘制坦克
switch (dir){
case UP:
g.drawImage(ResourceManager.tankU, x, y, null);
break;
case DOWN:
g.drawImage(ResourceManager.tankD, x, y, null);
break;
case LEFT:
g.drawImage(ResourceManager.tankL, x, y, null);
break;
case RIGHT:
g.drawImage(ResourceManager.tankR, x, y, null);
break;
}
move();
}
public void move(){
//如果没有移动 return
if(!moving) return;
switch (dir){
case UP: y -= SPEED;
break;
case DOWN: y += SPEED;
break;
case LEFT: x -= SPEED;
break;
case RIGHT: x += SPEED;
break;
default:
break;
}
// if(random.nextInt(10) > 8) this.fire();
}
public void fire() {
int bx = x + Tank.WIDTH/2 - Bullet.WIDTH/2;
int by = y + Tank.HEIGHT/2 - Bullet.HEIGHT/2;
tankFrame.bullets.add(new Bullet(bx, by, this.dir, this.group, tankFrame));
}
public void die(){
this.living = false;
}
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 Direction getDir() {
return dir;
}
public void setDir(Direction dir) {
this.dir = dir;
}
public boolean isMoving() {
return moving;
}
public void setMoving(boolean moving) {
this.moving = moving;
}
public boolean isLiving() {
return living;
}
public void setLiving(boolean living) {
this.living = living;
}
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
}

@ -0,0 +1,151 @@
package com.demo.tank.course4;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
public class TankFrameV4 extends Frame {
public static final int GAME_WIDTH = 800;
public static final int GAME_HEIGHT = 600;
Image image = null;
Tank tank = new Tank(500, 500, Direction.UP, Group.GOOD,this);
// Bullet bullet = new Bullet(520, 440, Direction.UP);
List<Bullet> bullets = new ArrayList();
List<Tank> enemyTanks = new ArrayList<>();
Explode explode = new Explode(200, 300, this);
public TankFrameV4(){
setVisible(true);
setBounds(500, 200 , GAME_WIDTH, GAME_HEIGHT);
setResizable(false);
setTitle("tank war");
this.addKeyListener(new MyKeyListener());
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
@Override
public void update(Graphics g) {
if(image == null){
image = this.createImage(GAME_WIDTH, GAME_HEIGHT);
}
Graphics imgGraphic = image.getGraphics();
Color color = g.getColor();
imgGraphic.setColor(Color.BLACK);
imgGraphic.fillRect(0,0, GAME_WIDTH, GAME_HEIGHT);
imgGraphic.setColor(color);
paint(imgGraphic);
g.drawImage(image, 0, 0, null);
}
@Override
public void paint(Graphics g){
//打印出子弹数量
Color color = g.getColor();
g.setColor(Color.WHITE);
g.drawString("当前子弹数量:" + bullets.size(), 60, 50);
g.drawString("当前敌人数量:" + enemyTanks.size(), 60, 80);
g.setColor(color);
tank.paint(g);
explode.paint(g);
for(int i = 0; i< enemyTanks.size(); i++){
enemyTanks.get(i).paint(g);
}
for (int i = 0; i< bullets.size(); i++){
bullets.get(i).paint(g);
}
for(int i = 0; i< bullets.size(); i++){
for(int j=0; j< enemyTanks.size(); j++){
bullets.get(i).collideWith(enemyTanks.get(j));
}
}
// for(Iterator<Bullet> it = bullets.iterator(); it.hasNext();){
// Bullet b = it.next();
// if(!b.isLive()){
// it.remove();
// }
// }
//
// for (Bullet b : bullets){
// b.paint(g);
// }
}
class MyKeyListener extends KeyAdapter{
boolean bL = false;
boolean bR = false;
boolean bU = false;
boolean bD = false;
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()){
case KeyEvent.VK_A:
bL = true;
break;
case KeyEvent.VK_D:
bR = true;
break;
case KeyEvent.VK_W:
bU = true;
break;
case KeyEvent.VK_S:
bD = true;
break;
default:
break;
}
setTankDirection();
}
@Override
public void keyReleased(KeyEvent e) {
switch (e.getKeyCode()){
case KeyEvent.VK_A:
bL = false;
break;
case KeyEvent.VK_D:
bR = false;
break;
case KeyEvent.VK_W:
bU = false;
break;
case KeyEvent.VK_S:
bD = false;
break;
case KeyEvent.VK_J:
tank.fire();
break;
default:
break;
}
setTankDirection();
}
public void setTankDirection(){
if(!bL && !bR && !bU && !bD){
tank.setMoving(false);
}else{
tank.setMoving(true);
if(bL) tank.setDir(Direction.LEFT);
if(bR) tank.setDir(Direction.RIGHT);
if(bU) tank.setDir(Direction.UP);
if(bD) tank.setDir(Direction.DOWN);
}
}
}
}

@ -0,0 +1,99 @@
package com.demo.tank.util;
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");
a.loop();
}
}

@ -0,0 +1,24 @@
package com.demo.tank.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;
}
}

Before

Width:  |  Height:  |  Size: 824 B

After

Width:  |  Height:  |  Size: 824 B

Before

Width:  |  Height:  |  Size: 868 B

After

Width:  |  Height:  |  Size: 868 B

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 1.2 KiB

Before

Width:  |  Height:  |  Size: 936 B

After

Width:  |  Height:  |  Size: 936 B

Before

Width:  |  Height:  |  Size: 1.0 KiB

After

Width:  |  Height:  |  Size: 1.0 KiB

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 1.2 KiB

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 1.2 KiB

Before

Width:  |  Height:  |  Size: 1.3 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

Before

Width:  |  Height:  |  Size: 1.3 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

Before

Width:  |  Height:  |  Size: 8.0 KiB

After

Width:  |  Height:  |  Size: 8.0 KiB

Before

Width:  |  Height:  |  Size: 8.0 KiB

After

Width:  |  Height:  |  Size: 8.0 KiB

Before

Width:  |  Height:  |  Size: 8.2 KiB

After

Width:  |  Height:  |  Size: 8.2 KiB

Before

Width:  |  Height:  |  Size: 8.2 KiB

After

Width:  |  Height:  |  Size: 8.2 KiB

Before

Width:  |  Height:  |  Size: 855 B

After

Width:  |  Height:  |  Size: 855 B

Before

Width:  |  Height:  |  Size: 839 B

After

Width:  |  Height:  |  Size: 839 B

Before

Width:  |  Height:  |  Size: 839 B

After

Width:  |  Height:  |  Size: 839 B

Before

Width:  |  Height:  |  Size: 852 B

After

Width:  |  Height:  |  Size: 852 B

Before

Width:  |  Height:  |  Size: 847 B

After

Width:  |  Height:  |  Size: 847 B

Before

Width:  |  Height:  |  Size: 312 B

After

Width:  |  Height:  |  Size: 312 B

Before

Width:  |  Height:  |  Size: 1.3 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 1.2 KiB

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB

Before

Width:  |  Height:  |  Size: 1022 B

After

Width:  |  Height:  |  Size: 1022 B

Before

Width:  |  Height:  |  Size: 814 B

After

Width:  |  Height:  |  Size: 814 B

Before

Width:  |  Height:  |  Size: 460 B

After

Width:  |  Height:  |  Size: 460 B

Before

Width:  |  Height:  |  Size: 280 B

After

Width:  |  Height:  |  Size: 280 B

Before

Width:  |  Height:  |  Size: 565 B

After

Width:  |  Height:  |  Size: 565 B

Before

Width:  |  Height:  |  Size: 846 B

After

Width:  |  Height:  |  Size: 846 B

Before

Width:  |  Height:  |  Size: 1.0 KiB

After

Width:  |  Height:  |  Size: 1.0 KiB

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 1.2 KiB

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 1.2 KiB

Before

Width:  |  Height:  |  Size: 1.3 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

Before

Width:  |  Height:  |  Size: 1.3 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

Before

Width:  |  Height:  |  Size: 864 B

After

Width:  |  Height:  |  Size: 864 B

Before

Width:  |  Height:  |  Size: 863 B

After

Width:  |  Height:  |  Size: 863 B

Before

Width:  |  Height:  |  Size: 861 B

After

Width:  |  Height:  |  Size: 861 B

Before

Width:  |  Height:  |  Size: 863 B

After

Width:  |  Height:  |  Size: 863 B

Before

Width:  |  Height:  |  Size: 673 B

After

Width:  |  Height:  |  Size: 673 B

Before

Width:  |  Height:  |  Size: 670 B

After

Width:  |  Height:  |  Size: 670 B

Before

Width:  |  Height:  |  Size: 680 B

After

Width:  |  Height:  |  Size: 680 B

Before

Width:  |  Height:  |  Size: 677 B

After

Width:  |  Height:  |  Size: 677 B

Before

Width:  |  Height:  |  Size: 660 B

After

Width:  |  Height:  |  Size: 660 B

Before

Width:  |  Height:  |  Size: 691 B

After

Width:  |  Height:  |  Size: 691 B

Before

Width:  |  Height:  |  Size: 674 B

After

Width:  |  Height:  |  Size: 674 B

Before

Width:  |  Height:  |  Size: 271 KiB

After

Width:  |  Height:  |  Size: 271 KiB

Before

Width:  |  Height:  |  Size: 1.3 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 1.2 KiB

Before

Width:  |  Height:  |  Size: 1.4 KiB

After

Width:  |  Height:  |  Size: 1.4 KiB

Before

Width:  |  Height:  |  Size: 1.4 KiB

After

Width:  |  Height:  |  Size: 1.4 KiB

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 1.2 KiB

Before

Width:  |  Height:  |  Size: 1.4 KiB

After

Width:  |  Height:  |  Size: 1.4 KiB

Before

Width:  |  Height:  |  Size: 1.7 KiB

After

Width:  |  Height:  |  Size: 1.7 KiB

Before

Width:  |  Height:  |  Size: 1.3 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

Loading…
Cancel
Save