import java.awt.*; //java抽象窗口工具集
import javax.swing.*; //功能更强的Swing类库
import java.awt.event.*; //事件处理包
import java.util.EventListener; //事件监听
public class Tank extends JFrame //游戏窗口,继承JFrame类
{
Tank(String title) //自定义游戏窗口类型
{
this.setTitle(title); //名称
this.setSize(440,630); //窗口大小
this.setLocation(300,50); //窗口位置
this.setBackground(Color.red); //背景颜色
myTank mT = new myTank(); //实例化"我的坦克"
this.add(mT); //将"我的坦克"加入窗口
this.addKeyListener(mT); //键盘监听器监听键盘信息从而控制坦克
new Thread(mT).start(); //通过调用Thread类的start()方法来启动一个"我的坦克"线程
}
public static void main(String[] args) //实例化窗口
{
Tank newTank = new Tank("IIB Tank"); //窗口名
newTank.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
newTank.setVisible(true); //窗口的关闭操作
}
};
class myTank extends JPanel implements KeyListener, Runnable //我的坦克类,继承JPanel(面板)类,实现KeyListener接口
{ //还有Runnable(多线程)接口
//myTank
private int x; // 坦克
private int y; //的位置
private int direction; //坦克的方向
private int tankspeed; //坦克的移动速度
private int bulletspeed; //子弹的速度
private int bulletX; // 子弹
private int bulletY; //的位置
private int bulletDirection; //子弹的方向
private int missilespeed; //敌方子弹速度
private int horsepower; //坦克血槽
private int score; //分数
private int degree; //游戏难度
private int fight; //开炮
private int fired; //被击中
private int number = 6; //敌人个数
private int [] enemyX; //敌人坦
private int [] enemyY; //克坐标
private int [] enemyDirection; //敌人坦克方向
private int [] missileX; //敌方子弹
private int [] missileY; //位置
private int [] missileDirection; //敌人子弹方向
private int [] missile; //
// Bullet
//private int myBulletX;
//private int myBulletY;
// private int[] enemyBulletX;
// private int[] enemyBulletY;
// Construct
myTank() //初始化
{
// initialize the enemy
enemyX = new int[number]; //
enemyY = new int[number]; //
missile = new int[number]; //
missileX = new int[number]; //敌方坦克每个数组对象代表一辆坦克
missileY = new int[number]; //
enemyDirection = new int[number]; //
missileDirection = new int[number]; //
for(int i=0; i<number; i++)
{
enemyX[i] = (int) (Math.random()*300);
enemyY[i] = (int) (Math.random()*500);
enemyDirection[i] = ((int) (Math.random()*4))+1;
//enemyX[i] += 40;
//enemyY[i] += 40;
//missileX[i] = -7;
//missileY[i] = -7;
missilespeed = 8;
missileDirection[i] = enemyDirection[i];
missile[i] = 1;
}
// 初始化己方坦克
direction = ((int) (Math.random()*4))+1;
tankspeed = 10;
bulletspeed = 15;
horsepower = 60; //HP==60
degree = 8;
fired = 0;
x = 200;
y = 300;
bulletX = -7; // 子弹在屏幕外
bulletY = -7;
fight = 0;
}
ImageIcon img = new ImageIcon("111.jpg"); //图片位置
public void paintComponent(Graphics g)
{
g.drawImage(img.getImage(), 0, 0, null); //背景
g.setColor(Color.RED); //血槽颜色
g.drawString("HP:",10,20); //位置
g.fillRect(40,10,horsepower*5,10); //血量
g.drawRect(40,10,300,10); //血色边框
g.drawString("Score:" + score, 10, 40); //分数
if(fired == 1)
{
g.setColor(Color.red); //
Font fnt = new Font("dialog",Font.ITALIC+Font.BOLD,30); //字体位置
g.setFont(fnt); //及风格
g.drawString("We are fired!!", 145, 150); //
fired = 0;
}
//我的坦克颜色
if(direction == 1 || direction == 3) //上下
{
g.setColor(Color.green);
g.fillRect(x, y, 40, 40);
int sign = (int) (2.5 - 1.5*direction);
g.setColor(Color.BLUE);
g.fillRect(x + 17, y - sign*20, 6, 20);
}
if(direction == 2 || direction == 4) //左右
{
g.setColor(Color.green);
g.fillRect(x, y, 40, 40);
int sign = (int) (4 - 1.5*direction);
g.setColor(Color.BLUE);
g.fillRect(x - sign*20, y + 17, 20, 6);
}
g.setColor(Color.GREEN);
g.fillRect(bulletX, bulletY, 5, 5);
//敌方方坦克颜色
for(int i=0;i<number;i++)
{
g.setColor(Color.gray);
if(enemyDirection[i] == 1 || enemyDirection[i] == 3)
{
g.fillRect(enemyX[i], enemyY[i], 40, 40);
int sign = (int) (2.5 - 1.5*enemyDirection[i]);
g.setColor(Color.RED);
g.fillRect(enemyX[i] + 17, enemyY[i] - sign*20, 6, 20);
}
if(enemyDirection[i] == 2 || enemyDirection[i] == 4)
{
g.fillRect(enemyX[i], enemyY[i], 40, 40);
int sign = (int) (4 - 1.5*enemyDirection[i]);
g.setColor(Color.RED);
g.fillRect(enemyX[i] - sign*20, enemyY[i] + 17, 20, 6);
}
g.setColor(Color.PINK);
g.fillOval(missileX[i], missileY[i], 10, 10);
}
}
public void keyReleased(KeyEvent e){} //坦克的控制
public void keyTyped(KeyEvent e){}
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_UP) //上
{
direction = 1;
y -= tankspeed;
if(y<0)
y += tankspeed;
}
if(e.getKeyCode() == KeyEvent.VK_LEFT) //下
{
direction = 2;
x-= tankspeed;
if(x<0)
x += tankspeed;
}
if(e.getKeyCode() == KeyEvent.VK_DOWN) //左
{
direction = 3;
y += tankspeed;
if(y>600)
y -= tankspeed;
}
if(e.getKeyCode() == KeyEvent.VK_RIGHT) //右
{
direction = 4;
x += ta