该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
package TankGame;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import javax.swing.*;
public class TankGame extends JFrame implements ActionListener{
MyPanel mp = null;
public static void main(String[] args) {
TankGame tg = new TankGame();
}
public TankGame() {
mp = new MyPanel();
//创建面板线程
Thread t1 = new Thread(mp);
t1.start();
//mp.setBackground(Color.black);//设置游戏面板背景颜色
this.add(mp);//游戏面板加入当前窗体对象
this.setSize(400,300);
this.setTitle("坦克大战");//设置窗体标题
//this.setBackground(Color.BLACK);//设置窗体背景颜色
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);//关闭窗口同时终止java虚拟机,防止内存溢出
this.addKeyListener(mp);//注册监听游戏面板
}
@Override
public void actionPerformed(ActionEvent e) {
}
}
class MyPanel extends JPanel implements KeyListener,Runnable{//游戏面板
MyTank mt =null;//我的坦克
EnemyTank et = null;//敌人坦克
Vector ets = new Vector();//存放敌人坦克的集合(泛型)
public MyPanel() {
mt = new MyTank(175,150,0,0,true);
for(int i=0;i
et = new EnemyTank((i+1)*50,10,1,1,true);
ets.add(et);
}
}
public void paint(Graphics g) {//重写paint方法
//super.paint(g);//调用父类初始化
g.fillRect(0, 0, 410, 310);//游戏面板大小400*300
this.drawTank(mt.getX(), mt.getY(), mt.getDirect(), mt.getType(), mt.isLife(),g);//绘出我防坦克
//System.out.println(ets.size());//查看集合中有几辆敌人坦克
for(int i=0;i
EnemyTank et = ets.get(i);
this.drawTank(et.getX(), et.getY(), et.getDirect(), et.getType(), et.isLife(),g);//绘出我防坦克
}
//绘出子弹
System.out.println("子弹个数:"+mt.mts.size());
for(int i=0;i
Shot ms = this.mt.mts.get(i);
if (ms != null && ms.ShotLift == true) {
g.setColor(Color.RED);
g.fill3DRect(this.mt.getX(), this.mt.getY(), 1, 1, false);
}
if(ms.isShotLift()==false) {
mt.mts.remove(ms);
}
}
}
public void drawTank(int x,int y,int direct,int type,boolean life,Graphics g) {
//super.paint(g);//调用父类初始化(导致BUG,游戏面板背景颜色问题)
switch(type) {
case 0://我方坦克
g.setColor(Color.YELLOW);//黄色
break;
case 1://敌方坦克
g.setColor(Color.CYAN);//蓝色
break;
}
if (type==0) {
switch (direct) {
case 0://炮筒向上
g.fill3DRect(x, y, 5, 30, false);//左边矩形
g.fill3DRect(x + 5, y + 10, 10, 10, false);//中间矩形
g.fill3DRect(x + 15, y, 5, 30, false);//右边矩形
g.drawLine(x + 9, y + 15, x + 9, y - 2);//炮筒
g.setColor(Color.green);
g.fillOval(x + 5, y + 11, 8, 8);//中间圆形
break;
case 1://炮筒向下
g.fill3DRect(x, y, 5, 30, false);
g.fill3DRect(x + 5, y + 10, 10, 10, false);
g.fill3DRect(x + 15, y, 5, 30, false);
g.drawLine(x + 10, y + 15, x + 10, y + 32);
g.setColor(Color.red);
g.fillOval(x + 6, y + 11, 8, 8);
break;
case 2://炮筒向左
g.fill3DRect(x, y, 30, 5, false);//上面的矩形
g.fill3DRect(x + 10, y + 5, 10, 10, false);//中间的矩形
g.fill3DRect(x, y + 15, 30, 5, false);//下面的矩形
g.drawLine(x + 15, y + 10, x - 2, y + 10);//炮筒
g.setColor(Color.orange);
g.fillOval(x + 11, y + 6, 8, 8);//中间的圆
break;
case 3://炮筒向右
g.fill3DRect(x, y, 30, 5, false);//上面的矩形
g.fill3DRect(x + 10, y + 5, 10, 10, false);//中间的矩形
g.fill3DRect(x, y + 15, 30, 5, false);//下面的矩形
g.drawLine(x + 15, y + 10, x + 32, y + 10);//炮筒
g.setColor(Color.blue);
g.fillOval(x + 11, y + 6, 8, 8);//中间的圆
break;
}
}else if(type==1) {
switch (direct) {
case 0://炮筒向上
g.fill3DRect(x, y, 5, 30, false);//左边矩形
g.fill3DRect(x + 5, y + 10, 10, 10, false);//中间矩形
g.fill3DRect(x + 15, y, 5, 30, false);//右边矩形
g.drawLine(x + 9, y + 15, x + 9, y - 2);//炮筒
g.fillOval(x + 5, y + 11, 8, 8);//中间圆形
break;
case 1://炮筒向下
g.fill3DRect(x, y, 5, 30, false);
g.fill3DRect(x + 5, y + 10, 10, 10, false);
g.fill3DRect(x + 15, y, 5, 30, false);
g.drawLine(x + 10, y + 15, x + 10, y + 32);
g.fillOval(x + 6, y + 11, 8, 8);
break;
case 2://炮筒向左
g.fill3DRect(x, y, 30, 5, false);//上面的矩形
g.fill3DRect(x + 10, y + 5, 10, 10, false);//中间的矩形
g.fill3DRect(x, y + 15, 30, 5, false);//下面的矩形
g.drawLine(x + 15, y + 10, x - 2, y + 10);//炮筒
g.fillOval(x + 11, y + 6, 8, 8);//中间的圆
break;
case 3://炮筒向右
g.fill3DRect(x, y, 30, 5, false);//上面的矩形
g.fill3DRect(x + 10, y + 5, 10, 10, false);//中间的矩形
g.fill3DRect(x, y + 15, 30, 5, false);//下面的矩形
g.drawLine(x + 15, y + 10, x + 32, y + 10);//炮筒
g.fillOval(x + 11, y + 6, 8, 8);//中间的圆
break;
}
}
}
@Override
public void keyPressed(KeyEvent e) {//按下
if(e.getKeyCode()==87) {
System.out.println("W-向上");
this.mt.setDirect(0);
this.mt.moveUp();
}else if(e.getKeyCode()==KeyEvent.VK_S) {
System.out.println("S-向下");
this.mt.setDirect(1);
this.mt.moveDown();
}else if(e.getKeyCode()==65) {
System.out.println("A-向左");
this.mt.setDirect(2);
this.mt.moveLeft();
}else if(e.getKeyCode()==KeyEvent.VK_D) {
System.out.println("B-向右");
this.mt.setDirect(3);
this.mt.moveRight();
}
if(e.getKeyCode()==KeyEvent.VK_J) {//按J开火
System.out.println("J-开火");
if (this.mt.mts.size()
this.mt.MyTankShot();
}
}
this.repaint();//游戏画面重绘
}
@Override
public void run() {
while(true) {
try {
Thread.sleep(100);
}catch(Exception e) {
e.printStackTrace();
}
this.repaint();//重绘游戏面板
}
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
}