1、线程-应用到坦克大战
1.1、坦克大战 v0.3
1.1.1、代码实现
TankGame03.java
package tankgame3;
import javax.swing.*;
public class TankGame03 extends JFrame {
MyPanel mp = null;
public static void main(String[] args) {
TankGame03 TankGame01 = new TankGame03();
}
public TankGame03() {
mp = new MyPanel();
Thread thread = new Thread(mp);
thread.start();
this.add(mp);
this.setSize(1000, 750);
this.addKeyListener(mp);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
MyPanel.java
package tankgame3;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;
public class MyPanel extends JPanel implements KeyListener, Runnable {
Hero hero = null;
Vector<EnemyTank> enemyTanks = new Vector<>();
int enemyTankSize = 3;
public MyPanel() {
hero = new Hero(100, 100);
for (int i = 0; i < enemyTankSize; i++) {
EnemyTank enemyTank = new EnemyTank((100 * (i + 1)), 0);
enemyTank.setDirect(2);
enemyTanks.add(enemyTank);
}
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.fillRect(0, 0, 1000, 750);
drawTank(hero.getX(), hero.getY(), g, hero.getDirect(), 1);
if (hero.shot != null && hero.shot.isLive == true) {
System.out.println("子弹被绘制...");
g.draw3DRect(hero.shot.x, hero.shot.y, 1, 1, false);
}
for (int i = 0; i < enemyTanks.size(); i++) {
EnemyTank enemyTank = enemyTanks.get(i);
drawTank(enemyTank.getX(), enemyTank.getY(), g, enemyTank.getDirect(), 0);
}
}
public void drawTank(int x, int y, Graphics g, int direct, int type) {
switch (type) {
case 0:
g.setColor(Color.cyan);
break;
case 1:
g.setColor(Color.yellow);
break;
}
switch (direct) {
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) {
System.out.println(e.getKeyCode());
if (e.getKeyCode() == KeyEvent.VK_W) {
hero.setDirect(0);
hero.moveUp();
} else if (e.getKeyCode() == KeyEvent.VK_D) {
hero.setDirect(1);
hero.moveRight();
} else if (e.getKeyCode() == KeyEvent.VK_S) {
hero.setDirect(2);
hero.moveDown();
} else if (e.getKeyCode() == KeyEvent.VK_A) {
hero.setDirect(3);
hero.moveLeft();
}
if (e.getKeyCode() == KeyEvent.VK_J) {
System.out.println("用户按下了J, 开始射击.");
hero.shotEnemyTank();
}
this.repaint();
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void run() {
while (true) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.repaint();
}
}
}
Shot.java
package tankgame3;
public class Shot implements Runnable {
int x;
int y;
int direct = 0;
int speed = 2;
boolean isLive = true;
public Shot(int x, int y, int direct) {
this.x = x;
this.y = y;
this.direct = direct;
}
@Override
public void run() {
while (true) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
switch (direct) {
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)) {
System.out.println("子弹线程退出");
isLive = false;
break;
}
}
}
}
Tank.java
package tankgame3;
public class Tank {
private int x;
private int y;
private int direct = 0;
private int speed = 1;
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public void moveUp() {
y -= speed;
}
public void moveRight() {
x += speed;
}
public void moveDown() {
y += speed;
}
public void moveLeft() {
x -= speed;
}
public int getDirect() {
return direct;
}
public void setDirect(int direct) {
this.direct = direct;
}
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;
}
}
Hero.java
package tankgame3;
public class Hero extends Tank {
Shot shot = null;
public Hero(int x, int y) {
super(x, y);
}
public void shotEnemyTank() {
switch (getDirect()) {
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;
}
new Thread(shot).start();
}
}
EnemyTank.java
package tankgame3;
public class EnemyTank extends Tank {
public EnemyTank(int x, int y) {
super(x, y);
}
}
1.2、坦克大战 v0.4
1.2.1、代码实现
TankGame04.java
package tankgame4;
import javax.swing.*;
public class TankGame04 extends JFrame {
MyPanel mp = null;
public static void main(String[] args) {
TankGame04 tankGame04 = new TankGame04();
}
public TankGame04() {
mp = new MyPanel();
Thread thread = new Thread(mp);
thread.start();
this.add(mp);
this.setSize(1200, 950);
this.addKeyListener(mp);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
MyPanel.java
package tankgame4;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
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<>();
int enemyTankSize = 3;
Image image1 = null;
Image image2 = null;
Image image3 = null;
public MyPanel() {
hero = new Hero(500, 100);
for (int i = 0; i < enemyTankSize; i++) {
EnemyTank enemyTank = new EnemyTank((100 * (i + 1)), 0);
enemyTank.setDirect(2);
new Thread(enemyTank).start();
Shot shot = new Shot(enemyTank.getX() + 20, enemyTank.getY() + 60, enemyTank.getDirect());
enemyTank.shots.add(shot);
new Thread(shot).start();
enemyTanks.add(enemyTank);
}
image1 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_1.gif"));
image2 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_2.gif"));
image3 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_3.gif"));
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.fillRect(0, 0, 1000, 750);
if (hero != null && hero.isLive) {
drawTank(hero.getX(), hero.getY(), g, hero.getDirect(), 1);
}
if (hero.shot != null && hero.shot.isLive == true) {
g.draw3DRect(hero.shot.x, hero.shot.y, 1, 1, false);
}
for (int i = 0; i < bombs.size(); i++) {
Bomb bomb = bombs.get(i);
if (bomb.life > 6) {
g.drawImage(image1, bomb.x, bomb.y, 60, 60, this);
} else if (bomb.life > 3) {
g.drawImage(image2, bomb.x, bomb.y, 60, 60, this);
} else {
g.drawImage(image3, bomb.x, bomb.y, 60, 60, this);
}
bomb.lifeDown();
if (bomb.life == 0) {
bombs.remove(bomb);
}
}
for (int i = 0; i < enemyTanks.size(); i++) {
EnemyTank enemyTank = enemyTanks.get(i);
if (enemyTank.isLive) {
drawTank(enemyTank.getX(), enemyTank.getY(), g, enemyTank.getDirect(), 0);
for (int j = 0; j < enemyTank.shots.size(); j++) {
Shot shot = enemyTank.shots.get(j);
if (shot.isLive) {
g.draw3DRect(shot.x, shot.y, 1, 1, false);
} else {
enemyTank.shots.remove(shot);
}
}
}
}
}
public void drawTank(int x, int y, Graphics g, int direct, int type) {
switch (type) {
case 0:
g.setColor(Color.cyan);
break;
case 1:
g.setColor(Color.yellow);
break;
}
switch (direct) {
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("暂时没有处理");
}
}
public void hitEnemyTank() {
if (hero.shot != null && hero.shot.isLive) {
for (int i = 0; i < enemyTanks.size(); i++) {
EnemyTank enemyTank = enemyTanks.get(i);
hitTank(hero.shot, enemyTank);
}
}
}
public void hitHero() {
for (int i = 0; i < enemyTanks.size(); i++) {
EnemyTank enemyTank = enemyTanks.get(i);
for (int j = 0; j < enemyTank.shots.size(); j++) {
Shot shot = enemyTank.shots.get(j);
if (hero.isLive && shot.isLive) {
hitTank(shot, hero);
}
}
}
}
public void hitTank(Shot s, Tank enemyTank) {
switch (enemyTank.getDirect()) {
case 0:
case 2:
if (s.x > enemyTank.getX() && s.x < enemyTank.getX() + 40
&& s.y > enemyTank.getY() && s.y < enemyTank.getY() + 60) {
s.isLive = false;
enemyTank.isLive = false;
enemyTanks.remove(enemyTank);
Bomb bomb = new Bomb(enemyTank.getX(), enemyTank.getY());
bombs.add(bomb);
}
break;
case 1:
case 3:
if (s.x > enemyTank.getX() && s.x < enemyTank.getX() + 60
&& s.y > enemyTank.getY() && s.y < enemyTank.getY() + 40) {
s.isLive = false;
enemyTank.isLive = false;
Bomb bomb = new Bomb(enemyTank.getX(), enemyTank.getY());
bombs.add(bomb);
}
break;
}
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
System.out.println(e.getKeyCode());
if (e.getKeyCode() == KeyEvent.VK_W) {
hero.setDirect(0);
if (hero.getY() > 0) {
hero.moveUp();
}
} else if (e.getKeyCode() == KeyEvent.VK_D) {
hero.setDirect(1);
if (hero.getX() + 60 < 1000) {
hero.moveRight();
}
} else if (e.getKeyCode() == KeyEvent.VK_S) {
hero.setDirect(2);
if (hero.getY() + 60 < 750) {
hero.moveDown();
}
} else if (e.getKeyCode() == KeyEvent.VK_A) {
hero.setDirect(3);
if (hero.getX() > 0) {
hero.moveLeft();
}
}
if (e.getKeyCode() == KeyEvent.VK_J) {
if (hero.shot == null || !hero.shot.isLive) {
hero.shotEnemyTank();
}
}
this.repaint();
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void run() {
while (true) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
hitEnemyTank();
hitHero();
this.repaint();
}
}
}
Shot.java
package tankgame4;
public class Shot implements Runnable {
int x;
int y;
int direct = 0;
int speed = 2;
boolean isLive = true;
public Shot(int x, int y, int direct) {
this.x = x;
this.y = y;
this.direct = direct;
}
@Override
public void run() {
while (true) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
switch (direct) {
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)) {
System.out.println("子弹线程退出");
isLive = false;
break;
}
}
}
}
Bomb.java
package tankgame4;
public class Bomb {
int x, y;
int life = 9;
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;
}
}
}
Tank.java
package tankgame4;
public class Tank {
private int x;
private int y;
private int direct = 0;
private int speed = 1;
boolean isLive = true;
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public void moveUp() {
y -= speed;
}
public void moveRight() {
x += speed;
}
public void moveDown() {
y += speed;
}
public void moveLeft() {
x -= speed;
}
public int getDirect() {
return direct;
}
public void setDirect(int direct) {
this.direct = direct;
}
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;
}
}
Hero.java
package tankgame4;
import java.util.Vector;
public class Hero extends Tank {
Shot shot = null;
public Hero(int x, int y) {
super(x, y);
}
public void shotEnemyTank() {
switch (getDirect()) {
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;
}
new Thread(shot).start();
}
}
EnemyTank.java
package tankgame4;
import java.util.Vector;
public class EnemyTank extends Tank implements Runnable {
Vector<Shot> shots = new Vector<>();
boolean isLive = true;
public EnemyTank(int x, int y) {
super(x, y);
}
@Override
public void run() {
while (true) {
if (isLive && shots.size() < 1) {
Shot s = null;
switch (getDirect()) {
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();
}
switch (getDirect()) {
case 0:
for (int i = 0; i < 30; i++) {
if (getY() > 0) {
moveUp();
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
break;
case 1:
for (int i = 0; i < 30; i++) {
if (getX() + 60 < 1000) {
moveRight();
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
break;
case 2:
for (int i = 0; i < 30; i++) {
if (getY() + 60 < 750) {
moveDown();
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
break;
case 3:
for (int i = 0; i < 30; i++) {
if (getX() > 0) {
moveLeft();
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
break;
}
setDirect((int) (Math.random() * 4));
if (!isLive) {
break;
}
}
}
}
|