import java.awt.Color;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
import javax.swing.*;
public class GameFrame extends JFrame {
public static boolean isGameOver = false;
public static final int SPEED = 6;
JPanel panel = new JPanel();
JLabel tank = new JLabel(new ImageIcon("4.png"));
int[] pos = new int[]{0,0, 1,0, 2,0, 3,0, 4,0, 5,0, 6,0, 7,0, 8,0, 9,0, 10,0, 0,1, 0,2, 0,3, 0,4, 0,5, 0,6, 0,7, 0,8, 0,9, 0,10, 0,11, 0,12, 1,12, 2,12, 3,12, 4,12, 5,12, 6,12, 7,12, 8,12, 9,12, 10,12, 10,1, 10,2, 10,3, 10,4, 10,5, 10,6, 10,7, 10,8, 10,9, 10,10, 10,11, 10,12, 1,3, 2,3, 3,3, 4,3, 5,3, 6,3, 5,8, 6,8, 7,8, 8,8, 9,8, 10,8 ,11,8 };
JLabel[] walls = new JLabel[pos.length/2];
//记录上一次的方向按键(坦克的方向)
int lastKeyCode = KeyEvent.VK_UP;
//记录上一次发射子弹的时间
long lastShootTime = 0;
//记录动画
boolean aniFlag = true;
ImageIcon[] tankIcons = new ImageIcon[]{new ImageIcon("0.png"), new ImageIcon("1.png"), new ImageIcon("2.png"), new ImageIcon("3.png"), new ImageIcon("4.png"), new ImageIcon("5.png"), new ImageIcon("6.png"), new ImageIcon("7.png")};
JLabel[] enemies = new JLabel[8];
public GameFrame() {
this.setTitle("坦克大战欢迎您");
this.setIconImage(Toolkit.getDefaultToolkit().createImage("2.png"));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(200, 60, 535, 650);
this.setContentPane(panel);
panel.setLayout(null);
panel.setBackground(Color.black);
panel.add(tank);
tank.setBounds(240, 480, 48, 48);
for (int i = 0; i < walls.length; i++) {
walls[i] = new JLabel(new ImageIcon("wall.png"));
panel.add(walls[i]);
walls[i].setBounds(pos[i * 2] * 48, pos[i * 2 + 1] * 48, 48 , 48);
}
for (int i = 0; i < enemies.length; i++) {
enemies[i] = new JLabel(new ImageIcon("enemy.png"));
panel.add(enemies[i]);
enemies[i].setBounds((i+1) * 48, 48, 48 , 48);
EnemyThread et = new EnemyThread(enemies[i], this);
et.start();
}
this.setVisible(true);
this.addKeyListener(new KeyListener() {
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
public void keyPressed(KeyEvent e) {
int xSpeed = 0, ySpeed = 0;
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT :
lastKeyCode = e.getKeyCode();
if (tank.getX() - SPEED >= 0) {
xSpeed = -SPEED;
tank.setIcon(tankIcons[aniFlag?2:3]);
aniFlag=!aniFlag;
}
break;
case KeyEvent.VK_RIGHT :
lastKeyCode = e.getKeyCode();
if (tank.getX() + tank.getWidth() + SPEED<=panel.getWidth()) {
xSpeed = SPEED;
tank.setIcon(tankIcons[aniFlag?6:7]);
aniFlag=!aniFlag;
}
break;
case KeyEvent.VK_UP :
lastKeyCode = e.getKeyCode();
if (tank.getY() - SPEED>=0) {
ySpeed = -SPEED;
tank.setIcon(tankIcons[aniFlag?4:5]);
aniFlag=!aniFlag;
}
break;
case KeyEvent.VK_DOWN :
lastKeyCode = e.getKeyCode();
if (tank.getY() + tank.getHeight() + SPEED<=panel.getHeight()) {
ySpeed = SPEED;
tank.setIcon(tankIcons[aniFlag?0:1]);
aniFlag=!aniFlag;
}
break;
case KeyEvent.VK_SPACE :
if (System.currentTimeMillis() - lastShootTime < 1000) return;
lastShootTime = System.currentTimeMillis();
JLabel bullet = new JLabel();
panel.add(bullet);
switch (lastKeyCode) {
case KeyEvent.VK_LEFT:
bullet.setIcon(new ImageIcon("bulletLeft.png"));
bullet.setBounds(tank.getX() - 16, tank.getY() + 16, 16, 16);
break;
case KeyEvent.VK_RIGHT:
bullet.setIcon(new ImageIcon("bulletRight.png"));
bullet.setBounds(tank.getX() + tank.getWidth(), tank.getY() + 16, 16, 16);
break;
case KeyEvent.VK_UP:
bullet.setIcon(new ImageIcon("bulletUp.png"));
bullet.setBounds(tank.getX() + 16, tank.getY() - 16, 16, 16);
break;
case KeyEvent.VK_DOWN:
bullet.setIcon(new ImageIcon("bulletDown.png"));
bullet.setBounds(tank.getX() + 16, tank.getY() + tank.getHeight(), 16, 16);
break;
}
BulletThread thread = new BulletThread(bullet, GameFrame.this, lastKeyCode);
thread.start();
break;
default:
return;
}
tank.setBounds(tank.getX() + xSpeed, tank.getY() + ySpeed, tank.getWidth(), tank.getHeight());
//判断坦克是否撞墙
for (int i = 0; i < walls.length; i++) {
if (!walls[i].isVisible()) continue;
if (tank.getBounds().intersects(walls[i].getBounds())) {
tank.setBounds(tank.getX() - xSpeed, tank.getY() - ySpeed, tank.getWidth(), tank.getHeight());
}
}
}
});
}
public static void main(String[] args) {
new GameFrame();
}
}