Java学习Day15 坦克大战ver0.3
新增功能 当玩家按下j键就发射一颗子弹
import javax.swing.*;
public class TankGame03 extends JFrame {
MyPanel mp = null;
public static void main(String[] args) {
TankGame03 tankGame03 = 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类监听键盘是否传入j
public class MyPanel extends JPanel implements KeyListener, Runnable {
P1 p1 = null;
Vector<EnemyTank> enemyTanks = new Vector<>();
int enemyTankSize = 3;
public MyPanel() {
p1 = new P1(100, 100);
p1.setSpeed(40);
for (int i = 0; i < enemyTankSize; i++) {
enemyTanks.add(new EnemyTank(300 * (i + 1), 0));
}
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.fillRect(0, 0, 1000, 750);
drawTank(p1.getX(), p1.getY(), g, p1.getDirection(), 0);
for (int i = 0; i < enemyTanks.size(); i++) {
EnemyTank enemyTank = enemyTanks.get(i);
drawTank(enemyTank.getX(), enemyTank.getY(), g, enemyTank.getDirection(), 1);
}
if (p1.shot != null && p1.shot.isLive == true) {
System.out.println("子弹绘制");
g.setColor(Color.yellow);
g.fill3DRect(p1.shot.x, p1.shot.y, 2, 2, false);
}
}
public void drawTank(int x, int y, Graphics g, int direction, int type) {
switch (type) {
case 0://我们的tank
g.setColor(Color.yellow);
break;
case 1://敌人的tank
g.setColor(Color.red);
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 + 20, x + 20, y - 20);
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 + 20, y + 20, x + 80, 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 + 20, x + 20, y + 80);
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 + 20, y + 20, x - 20, y + 20);
break;
default:
System.out.println("没有处理");
}
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_W) {
p1.setDirection(0);
p1.moveUp();
} else if (e.getKeyCode() == KeyEvent.VK_S) {
p1.setDirection(2);
p1.moveDown();
} else if (e.getKeyCode() == KeyEvent.VK_A) {
p1.setDirection(3);
p1.moveLeft();
} else if (e.getKeyCode() == KeyEvent.VK_D) {
p1.setDirection(1);
p1.moveRight();
}
if (e.getKeyCode() == KeyEvent.VK_J) {
p1.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类发射子弹
public class Shot implements Runnable {
int x;
int y;
int direction = 0;
int speed = 2;
boolean isLive = true;
public Shot(int x, int y, int direction) {
this.x = x;
this.y = y;
this.direction = direction;
}
@Override
public void run() {
while (true) {
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+" "+y);
if (!(x >= 0 && x <= 1000 && y >= 0 && y <= 750)) {
isLive = false;
break;
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
其余类
Tank
public class Tank {
private int x;
private int y;
private int direction = 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 moveDown(){
y+=speed;
}
public void moveLeft(){
x-=speed;
}
public void moveRight(){
x+=speed;
}
public int getDirection() {
return direction;
}
public void setDirection(int direction) {
this.direction = direction;
}
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;
}
}
P1
public class P1 extends Tank {
Shot shot = null;
public P1(int x, int y) {
super(x, y);
}
public void shotEnemyTank() {
switch (getDirection()) {
case 0:
shot = new Shot(getX() + 20, getY() - 20, 0);
break;
case 1:
shot = new Shot(getX() + 80, getY() +20, 1);
break;
case 2:
shot = new Shot(getX() + 20, getY() + 80, 2);
break;
case 3:
shot = new Shot(getX() - 20, getY() + 20, 3);
break;
}
Thread thread = new Thread(shot);
thread.start();
}
}