1、说明
??写了14个版本,终于完成了,项目结构如下
2、游戏效果
(1)击中敌人
(2)被敌人击中
3、代码如下
(1)TankGame 主方法
package cn.bloghut;
import cn.bloghut.component.MyPanel;
import cn.bloghut.service.Recorder;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Scanner;
public class TankGame extends JFrame {
private MyPanel panel;
public static void main(String[] args) {
new TankGame();
}
public TankGame(){
System.out.println("请输入选择: 1 表示新游戏,2是继续上局");
Scanner scanner = new Scanner(System.in);
this.panel = new MyPanel(scanner.next());
scanner.close();
new Thread(panel).start();
this.addKeyListener(panel);
this.add(panel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("闲言-坦克大战");
int width = Toolkit.getDefaultToolkit().getScreenSize().width;
int height = Toolkit.getDefaultToolkit().getScreenSize().height;
this.setResizable(false);
width = (width - 1300) / 2;
height = (height - 750) / 2;
this.setBounds(width,height,1300,750);
this.setBackground(Color.black);
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
Recorder.keepRecord();
System.exit(0);
}
});
}
}
(2)MyPanel 视图
package cn.bloghut.component;
import cn.bloghut.pojo.*;
import cn.bloghut.service.PlayAudio;
import cn.bloghut.service.Recorder;
import cn.bloghut.service.Shot;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.util.Vector;
public class MyPanel extends JPanel implements KeyListener, Runnable {
Hero hero = null;
Vector<EnemyTank> enemyTanks = new Vector<>();
Vector<Bomb> bombs = new Vector<>();
Image img1 = null;
Image img2 = null;
Image img3 = null;
Vector<Node> nodes = null;
int enemyTankSize = 3;
public MyPanel(String type) {
File file = new File(Recorder.getRecordFile());
if (!file.exists()){
type = "1";
System.out.println("文件不存在只能开启新游戏");
}else {
nodes = Recorder.getNodesAndTanks();
}
hero = new Hero(150, 150);
hero.setSpeed(5);
switch (type) {
case "1":
for (int i = 0; i < enemyTankSize; i++) {
EnemyTank enemyTank = new EnemyTank(100 * (i + 1), 0);
enemyTank.setDirection(2);
new Thread(enemyTank).start();
Shot shot = new Shot(enemyTank.getX() + 20, enemyTank.getY() + 60, enemyTank.getDirection());
enemyTank.getShots().add(shot);
enemyTank.setEnemyTanks(enemyTanks);
new Thread(shot).start();
enemyTanks.add(enemyTank);
}
break;
case "2":
for (int i = 0;i < nodes.size();i++){
Node node = nodes.get(i);
EnemyTank enemyTank = new EnemyTank(node.getX(),node.getY());
enemyTank.setDirection(node.getDirection());
new Thread(enemyTank).start();
Shot shot = new Shot(enemyTank.getX() + 20, enemyTank.getY() + 60, enemyTank.getDirection());
enemyTank.getShots().add(shot);
enemyTank.setEnemyTanks(enemyTanks);
new Thread(shot).start();
enemyTanks.add(enemyTank);
}
break;
default:
System.out.println("你的输入有误");
}
String parent = System.getProperty("user.dir");
parent += "\\";
img1 = Toolkit.getDefaultToolkit().getImage(parent + "bomb_1.gif");
img2 = Toolkit.getDefaultToolkit().getImage(parent + "bomb_2.gif");
img3 = Toolkit.getDefaultToolkit().getImage(parent + "bomb_3.gif");
Recorder.setEnemyTanks(enemyTanks);
new PlayAudio("bg.wav").start();
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.fillRect(0, 0, 1000, 750);
showInfo(g);
if (hero != null && hero.isLive()) {
drawTank(hero.getX(), hero.getY(), g, hero.getDirection(), 1);
}
if (hero.getShot() != null && hero.getShot().isLive()) {
g.draw3DRect(hero.getShot().getX(), hero.getShot().getY(), 2, 2, false);
}
if (hero.getShots().size() > 0) {
for (int i = 0; i < hero.getShots().size(); i++) {
Shot shot = hero.getShots().get(i);
if (shot != null && shot.isLive()) {
g.draw3DRect(shot.getX(), shot.getY(), 2, 2, false);
} else {
hero.getShots().remove(shot);
}
}
}
for (int i = 0; i < enemyTanks.size(); i++) {
EnemyTank enemyTank = enemyTanks.get(i);
if (enemyTank.isLive()) {
drawTank(enemyTank.getX(), enemyTank.getY(), g, enemyTank.getDirection(), 0);
Vector<Shot> shots = enemyTank.getShots();
for (int j = 0; j < shots.size(); j++) {
Shot shot = shots.get(j);
if (shot.isLive()) {
g.draw3DRect(shot.getX(), shot.getY(), 1, 1, false);
} else {
enemyTank.getShots().remove(shot);
}
}
}
}
for (int i = 0; i < bombs.size(); i++) {
Bomb bomb = bombs.get(i);
if (bomb.getLife() > 6) {
g.drawImage(img1, bomb.getX(), bomb.getY(), 60, 60, this);
} else if (bomb.getLife() > 3) {
g.drawImage(img1, bomb.getX(), bomb.getY(), 60, 60, this);
} else {
g.drawImage(img1, bomb.getX(), bomb.getY(), 60, 60, this);
}
bomb.lifeDown();
if (bomb.getLife() == 0) {
bombs.remove(bomb);
}
}
}
public void showInfo(Graphics g) {
g.setColor(Color.BLACK);
Font font = new Font("宋体", Font.BOLD, 25);
g.setFont(font);
g.drawString("您累计击毁敌方坦克", 1020, 30);
drawTank(1020, 60, g, 0, 0);
g.setColor(Color.BLACK);
g.drawString(Recorder.getAllEnemyTankTotal() + "", 1080, 100);
}
public void drawTank(int x, int y, Graphics g, int direction, int type) {
switch (type) {
case 0://0 敌人的坦克
g.setColor(Color.cyan);
break;
case 1://1 我们的坦克
g.setColor(Color.yellow);
break;
}
switch (direction) {
case 0:
g.fill3DRect(x, y, 10, 60, false);
g.fill3DRect(x + 30, y, 10, 60, false);
g.fill3DRect(x + 10, y + 10, 20, 40, false);
g.fillOval(x + 10, y + 20, 20, 20);
g.drawLine(x + 20, y + 30, x + 20, y);
break;
case 1:
g.fill3DRect(x, y, 60, 10, false);
g.fill3DRect(x, y + 30, 60, 10, false);
g.fill3DRect(x + 10, y + 10, 40, 20, false);
g.fillOval(x + 20, y + 10, 20, 20);
g.drawLine(x + 30, y + 20, x + 60, y + 20);
break;
case 2:
g.fill3DRect(x, y, 10, 60, false);
g.fill3DRect(x + 30, y, 10, 60, false);
g.fill3DRect(x + 10, y + 10, 20, 40, false);
g.fillOval(x + 10, y + 20, 20, 20);
g.drawLine(x + 20, y + 30, x + 20, y + 60);
break;
case 3:
g.fill3DRect(x, y, 60, 10, false);
g.fill3DRect(x, y + 30, 60, 10, false);
g.fill3DRect(x + 10, y + 10, 40, 20, false);
g.fillOval(x + 20, y + 10, 20, 20);
g.drawLine(x + 30, y + 20, x, y + 20);
break;
default:
System.out.println("暂时没有处理");
}
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
if (KeyEvent.VK_W == code) {
hero.setDirection(0);
hero.moveUp();
} else if (KeyEvent.VK_D == code) {
hero.setDirection(1);
hero.moveRight();
} else if (KeyEvent.VK_S == code) {
hero.setDirection(2);
hero.moveDown();
} else if (KeyEvent.VK_A == code) {
hero.setDirection(3);
hero.moveLeft();
}
if (KeyEvent.VK_J == code) {
hero.shotEnemyTank();
}
this.repaint();
}
public void hitTank(Vector<Shot> shots, Tank tank) {
for (int i = 0; i < shots.size(); i++) {
hitTank(shots.get(i), tank);
}
}
public void hitTank(Shot s, Tank tank) {
switch (tank.getDirection()) {
case 0://坦克向上
case 2://坦克向下
if (s.getX() > tank.getX() && s.getX() < tank.getX() + 40 &&
s.getY() > tank.getY() && s.getY() < tank.getY() + 60) {
s.setLive(false);
tank.setLive(false);
enemyTanks.remove(tank);
if (!(tank instanceof Hero)) {
Recorder.addlEnemyTankTotal();
}
Bomb bomb = new Bomb(tank.getX(), tank.getY());
bombs.add(bomb);
}
break;
case 1://坦克向右
case 3://坦克向左
if (s.getX() > tank.getX() && s.getX() < tank.getX() + 60 &&
s.getY() > tank.getY() && s.getY() < tank.getY() + 40) {
s.setLive(false);
tank.setLive(false);
enemyTanks.remove(tank);
if (!(tank instanceof Hero)) {
Recorder.addlEnemyTankTotal();
}
Bomb bomb = new Bomb(tank.getX(), tank.getY());
bombs.add(bomb);
}
break;
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void run() {
while (true) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
hitEnemy();
hitHero();
this.repaint();
}
}
public void hitEnemy() {
if (hero.getShot() != null && hero.getShot().isLive()) {
for (int i = 0; i < enemyTanks.size(); i++) {
EnemyTank enemyTank = enemyTanks.get(i);
hitTank(hero.getShots(), enemyTank);
}
}
}
public void hitHero() {
if (enemyTanks.size() > 0) {
for (int i = 0; i < enemyTanks.size(); i++) {
EnemyTank enemyTank = enemyTanks.get(i);
Vector<Shot> shots = enemyTank.getShots();
if (shots.size() > 0) {
for (int j = 0; j < shots.size(); j++) {
Shot shot = shots.get(j);
if (hero.isLive() && shot.isLive()) {
hitTank(shots, hero);
}
}
}
}
}
}
}
(3)Bomb 爆炸类
package cn.bloghut.pojo;
public class Bomb {
private int x,y;
private int life = 9;
private boolean isLive = true;
public Bomb(int x, int y) {
this.x = x;
this.y = y;
}
public void lifeDown(){
if (life > 0){
life--;
}else {
isLive = 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 int getLife() {
return life;
}
public void setLife(int life) {
this.life = life;
}
public boolean isLive() {
return isLive;
}
public void setLive(boolean live) {
isLive = live;
}
}
(4)EnemyTank 敌方坦克类
package cn.bloghut.pojo;
import cn.bloghut.service.Shot;
import java.util.Vector;
@SuppressWarnings("All")
public class EnemyTank extends Tank implements Runnable {
private Vector<Shot> shots = new Vector<>();
private Vector<EnemyTank> enemyTanks = new Vector<>();
public EnemyTank(int x, int y) {
super(x, y);
}
public Vector<Shot> getShots() {
return shots;
}
public void setShots(Vector<Shot> shots) {
this.shots = shots;
}
public Vector<EnemyTank> getEnemyTanks() {
return enemyTanks;
}
public void setEnemyTanks(Vector<EnemyTank> enemyTanks) {
this.enemyTanks = enemyTanks;
}
public boolean isTouchEnemyTank(){
switch (this.getDirection()){
case 0:
for (int i = 0;i< enemyTanks.size();i++){
EnemyTank enemyTank = enemyTanks.get(i);
if (this != enemyTank){
if (enemyTank.getDirection() == 0 || enemyTank.getDirection() == 2){
if (this.getX() >= enemyTank.getX() && this.getX() <= enemyTank.getX() + 40 &&
this.getY() >= enemyTank.getY() && this.getY() <= enemyTank.getY() + 60){
return true;
}
if (this.getX() + 40 >= enemyTank.getX() && this.getX() + 40 <= enemyTank.getX() + 60 &&
this.getY() >= enemyTank.getY() && this.getY() <= enemyTank.getY() + 40){
return true;
}
}
if (enemyTank.getDirection() == 1 || enemyTank.getDirection() == 3){
if (this.getX() >= enemyTank.getX() && this.getX() <= enemyTank.getX() + 60 &&
this.getY() >= enemyTank.getY() && this.getY() <= enemyTank.getY() + 40){
return true;
}
if (this.getX() + 40 >= enemyTank.getX() && this.getX() + 40 <= enemyTank.getX() + 60 &&
this.getY() >= enemyTank.getY() && this.getY() <= enemyTank.getY() + 40){
return true;
}
}
}
}
break;
case 1:
for (int i = 0;i< enemyTanks.size();i++){
EnemyTank enemyTank = enemyTanks.get(i);
if (this != enemyTank){
if (enemyTank.getDirection() == 0 || enemyTank.getDirection() == 2){
if (this.getX() + 60 >= enemyTank.getX() && this.getX() + 60 <= enemyTank.getX() + 40 &&
this.getY() >= enemyTank.getY() && this.getY() <= enemyTank.getY() + 60){
return true;
}
if (this.getX() + 60 >= enemyTank.getX() && this.getX() + 60 <= enemyTank.getX() + 40 &&
this.getY() + 40 >= enemyTank.getY() && this.getY() + 40 <= enemyTank.getY() + 60){
return true;
}
}
if (enemyTank.getDirection() == 1 || enemyTank.getDirection() == 3){
if (this.getX() + 60 >= enemyTank.getX() && this.getX() + 60 <= enemyTank.getX() + 60 &&
this.getY() >= enemyTank.getY() && this.getY() <= enemyTank.getY() + 40){
return true;
}
if (this.getX() + 60 >= enemyTank.getX() && this.getX() + 60 <= enemyTank.getX() + 60 &&
this.getY() + 40 >= enemyTank.getY() && this.getY() + 40 <= enemyTank.getY() + 40){
return true;
}
}
}
}
break;
case 2:
for (int i = 0;i< enemyTanks.size();i++){
EnemyTank enemyTank = enemyTanks.get(i);
if (this != enemyTank){
if (enemyTank.getDirection() == 0 || enemyTank.getDirection() == 2){
if (this.getX() >= enemyTank.getX() && this.getX() <= enemyTank.getX() + 40 &&
this.getY() + 60 >= enemyTank.getY() && this.getY() + 60 <= enemyTank.getY() + 60){
return true;
}
if (this.getX() + 40 >= enemyTank.getX() && this.getX() + 40 <= enemyTank.getX() + 40 &&
this.getY() + 60 >= enemyTank.getY() && this.getY() + 60 <= enemyTank.getY() + 60){
return true;
}
}
if (enemyTank.getDirection() == 1 || enemyTank.getDirection() == 3){
if (this.getX() >= enemyTank.getX() && this.getX() <= enemyTank.getX() + 60 &&
this.getY() + 60 >= enemyTank.getY() && this.getY() + 60 <= enemyTank.getY() + 40){
return true;
}
if (this.getX() + 40 >= enemyTank.getX() && this.getX() + 40 <= enemyTank.getX() + 60 &&
this.getY() + 60 >= enemyTank.getY() && this.getY() + 60 <= enemyTank.getY() + 40){
return true;
}
}
}
}
break;
case 3:
for (int i = 0;i< enemyTanks.size();i++){
EnemyTank enemyTank = enemyTanks.get(i);
if (this != enemyTank){
if (enemyTank.getDirection() == 0 || enemyTank.getDirection() == 2){
if (this.getX() >= enemyTank.getX() && this.getX() <= enemyTank.getX() + 40 &&
this.getY() >= enemyTank.getY() && this.getY() <= enemyTank.getY() + 60){
return true;
}
if (this.getX() >= enemyTank.getX() && this.getX() <= enemyTank.getX() + 40 &&
this.getY() + 40 >= enemyTank.getY() && this.getY() + 40 <= enemyTank.getY() + 60){
return true;
}
}
if (enemyTank.getDirection() == 1 || enemyTank.getDirection() == 3){
if (this.getX() >= enemyTank.getX() && this.getX() <= enemyTank.getX() + 60 &&
this.getY() >= enemyTank.getY() && this.getY() <= enemyTank.getY() + 40){
return true;
}
if (this.getX() >= enemyTank.getX() && this.getX() <= enemyTank.getX() + 60 &&
this.getY() + 40 >= enemyTank.getY() && this.getY() + 40 <= enemyTank.getY() + 40){
return true;
}
}
}
}
break;
}
return false;
}
@Override
public void run() {
while (true) {
if (isLive() && shots.size() < 3) {
Shot s = null;
switch (getDirection()) {
case 0:
s = new Shot(getX() + 20, getY(), 0);
break;
case 1:
s = new Shot(getX() + 60, getY() + 20, 1);
break;
case 2:
s = new Shot(getX() + 20, getY() + 60, 2);
break;
case 3:
s = new Shot(getX(), getY() + 20, 3);
break;
}
shots.add(s);
new Thread(s).start();
}
for (int i = 0; i < 30; i++) {
switch (getDirection()) {
case 0://向上
if (getY() > 0 && !isTouchEnemyTank()){
moveUp();
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
case 1://向右
if (getX() + 60 < 1000 && !isTouchEnemyTank()){
moveRight();
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
case 2://向下
if (getY() + 60 < 722 && !isTouchEnemyTank()){
moveDown();
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
case 3://向左
if (getX() > 0 && !isTouchEnemyTank()){
moveLeft();
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
}
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
int direction = (int) (Math.random() * 4);
setDirection(direction);
if (!isLive()) {
break;
}
}
}
}
(5)Hero 我方坦克类
package cn.bloghut.pojo;
import cn.bloghut.service.Shot;
import java.util.Vector;
public class Hero extends Tank{
private Shot shot = null;
private Vector<Shot> shots = new Vector<>();
public Hero(int x, int y) {
super(x, y);
}
public void shotEnemyTank(){
if (shots.size() >= 5){
return;
}
switch (getDirection()){
case 0:
shot = new Shot(getX() + 20,getY(),0);
break;
case 1:
shot = new Shot(getX() + 60,getY() +20,1);
break;
case 2:
shot = new Shot(getX() + 20,getY() + 60,2);
break;
case 3:
shot = new Shot(getX(),getY() + 20,3);
break;
}
shots.add(shot);
new Thread(shot).start();
}
public Vector<Shot> getShots() {
return shots;
}
public void setShots(Vector<Shot> shots) {
this.shots = shots;
}
public Shot getShot() {
return shot;
}
public void setShot(Shot shot) {
this.shot = shot;
}
}
(6)Node 记录敌方坦克类(继续上局游戏用)
package cn.bloghut.pojo;
public class Node {
private int x;
private int y;
private int direction;
public Node(int x, int y, int direction) {
this.x = x;
this.y = y;
this.direction = direction;
}
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 int getDirection() {
return direction;
}
public void setDirection(int direction) {
this.direction = direction;
}
}
(7)Tank 坦克父类
package cn.bloghut.pojo;
public class Tank {
private int x;
private int y;
private int direction;
private int speed = 1;
private boolean isLive = true;
public Tank(int x, int y) {
this.x = x;
this.y = y;
}
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 int getDirection() {
return direction;
}
public void setDirection(int direction) {
this.direction = direction;
}
public void moveUp(){
if (y > 0){
y-=speed;
}
}
public void moveRight(){
if (x + 60 < 1000){
x+=speed;
}
}
public void moveDown(){
if (y + 60 < 722){
y+=speed;
}
}
public void moveLeft(){
if (x > 0){
x-=speed;
}
}
public boolean isLive() {
return isLive;
}
public void setLive(boolean live) {
isLive = live;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
}
(8)PlayAudio 播放背景音乐类
package cn.bloghut.service;
import javax.sound.sampled.*;
import java.io.*;
public class PlayAudio extends Thread {
private String filename;
public PlayAudio(String wavfile) {
String path = System.getProperty("user.dir");
filename = path+"\\"+wavfile;
}
@Override
public void run() {
AudioInputStream audioInputStream = null;
try {
audioInputStream = AudioSystem.getAudioInputStream(new BufferedInputStream(new FileInputStream(filename)));
} catch (Exception e1) {
e1.printStackTrace();
return;
}
AudioFormat format = audioInputStream.getFormat();
SourceDataLine auline = null;
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
try {
auline = (SourceDataLine) AudioSystem.getLine(info);
auline.open(format);
} catch (Exception e) {
e.printStackTrace();
return;
}
auline.start();
int nBytesRead = 0;
byte[] abData = new byte[512];
try {
while (nBytesRead != -1) {
nBytesRead = audioInputStream.read(abData, 0, abData.length);
if (nBytesRead >= 0)
auline.write(abData, 0, nBytesRead);
}
} catch (IOException e) {
e.printStackTrace();
return;
} finally {
auline.drain();
auline.close();
}
}
}
(9)Recorder 记录游戏数据类
package cn.bloghut.service;
import cn.bloghut.pojo.EnemyTank;
import cn.bloghut.pojo.Node;
import java.io.*;
import java.util.Vector;
public class Recorder {
private static int allEnemyTankTotal = 0;
private static BufferedWriter bufferedWriter = null;
private static BufferedReader bufferedReader = null;
private static String recordFile;
private static Vector<EnemyTank> enemyTanks = null;
private static Vector<Node> nodes = new Vector<>();
static {
String parentPath = System.getProperty("user.dir");
recordFile = parentPath + "\\record.txt";
}
public static Vector<Node> getNodesAndTanks(){
try {
bufferedReader = new BufferedReader(new FileReader(recordFile));
allEnemyTankTotal = Integer.parseInt(bufferedReader.readLine());
String dataLine = "";
while ((dataLine = bufferedReader.readLine()) != null){
String[] split = dataLine.split(" ");
if (split.length == 3){
Node node = new Node(Integer.parseInt(split[0]), Integer.parseInt(split[1]), Integer.parseInt(split[2]));
nodes.add(node);
}
}
}catch (Exception e){
e.printStackTrace();
}finally {
if (bufferedReader != null){
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return nodes;
}
public static void setEnemyTanks(Vector<EnemyTank> enemyTanks) {
Recorder.enemyTanks = enemyTanks;
}
public static int getAllEnemyTankTotal() {
return allEnemyTankTotal;
}
public static void setAllEnemyTankTotal(int allEnemyTankTotal) {
Recorder.allEnemyTankTotal = allEnemyTankTotal;
}
public static void addlEnemyTankTotal(){
allEnemyTankTotal++ ;
}
public static void keepRecord(){
try {
bufferedWriter = new BufferedWriter(new FileWriter(recordFile));
bufferedWriter.write(allEnemyTankTotal+"\r\n");
if (enemyTanks.size() > 0){
for (int i = 0; i < enemyTanks.size(); i++) {
EnemyTank enemyTank = enemyTanks.get(i);
if (enemyTank.isLive()){
String record = enemyTank.getX() + " " + enemyTank.getY() + " " + enemyTank.getDirection();
bufferedWriter.write(record);
bufferedWriter.newLine();
}
}
}
}catch (Exception e){
e.printStackTrace();
}finally {
if (bufferedWriter != null){
try {
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static String getRecordFile() {
return recordFile;
}
public static void setRecordFile(String recordFile) {
Recorder.recordFile = recordFile;
}
}
(10)Shot 子弹类
package cn.bloghut.service;
public class Shot implements Runnable{
private int x;
private int y;
private int direction = 0;
private int speed = 2;
private boolean isLive = true;
@Override
public void run() {
while (true){
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
switch (direction){
case 0:
y -= speed;
break;
case 1:
x += speed;
break;
case 2://向下
y += speed;
break;
case 3://向左
x -= speed;
break;
}
System.out.println("x==>"+x+" y===>"+y);
if (!(x >= 0 && x <= 1000 && y >= 0 && y <= 750 && isLive)){
isLive = false;
System.out.println("子弹线程退出");
break;
}
}
}
public Shot(int x, int y, int direction) {
this.x = x;
this.y = y;
this.direction = direction;
}
public boolean isLive() {
return isLive;
}
public void setLive(boolean live) {
isLive = live;
}
public int getDirection() {
return direction;
}
public void setDirection(int direction) {
this.direction = direction;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
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;
}
@Override
public String toString() {
return "Shot{" +
"x=" + x +
", y=" + y +
", direction=" + direction +
", speed=" + speed +
", isLive=" + isLive +
'}';
}
}
?? 注意:本工程是模块化进行开发的,所以一些文件(
图片、存档路径、音乐)的路径需要改成自己对应。
|