原来自己已经写好了摁J键发射子弹并运行成功,但是想进行下一步子弹打中敌人后敌人消失时,代码写好了后子弹却发射不了了
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;
import javax.swing.*;
public class Mytank1 extends JFrame{
Mypanle mp=null;
public static void main(String[] args) {
// TODO Auto-generated method stub
Mytank1 mytank1=new Mytank1();
}
public Mytank1()
{
mp=new Mypanle();
Thread t=new Thread(mp);
t.start();
this.addKeyListener(mp);
this.add(mp);
this.setTitle("6");
this.setSize(400, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
//面板
class Mypanle extends JPanel implements KeyListener,Runnable
{
Hero hero=null;
Vector ets=new Vector();
int badsize=3;
public Mypanle()
{
//创建自己的tanke
hero=new Hero(100,100);
//创建敌人坦克
//int badsize=3;
for(int i=0;i<badsize;i++)
{
Badtank et=new Badtank((i+1)*50, 0);
et.setColor(0);
ets.add(et);
}
}
public void paint(Graphics g)
{
super.paint(g);
g.fillRect(0, 0, 400, 500);
this.drawtank(hero.getX(), hero.getY(), g, hero.direction, 1);
//画出敌人坦克
for(int i=0;i<ets.size();i++)
{
Badtank et=ets.get(i);
if(et.islive)
{
this.drawtank(et.getX(), et.getY(), g, et.direction, 0);
}
}
//画出子弹
for(int i=0;i<hero.ss.size();i++)
{
Shot myshot=hero.ss.get(i);
if(myshot!=null&&myshot.islive==true)
{
g.draw3DRect(myshot.x,myshot.y, 1,1, false);
}
if(myshot.islive==false)
{
hero.ss.remove(myshot);
}
}
}
//判断子弹是否打中坦克
/*public void hittank(Shot s,Badtank et)
{
//判断坦克方向
switch(et.direction)
{
case 0:
case 3:
//击中坦克
if(s.x>et.x&&s.x<et.x+20&&s.y>et.y&&s.y<et.y+30)
{
s.islive=false;
et.islive=false;
}
case 1:
case 2:
if(s.x>et.x&&s.x<et.x+30&&s.y>et.y&&s.y<et.y+20)
{
s.islive=false;
et.islive=false;
}
}
}*/
public void drawtank(int x,int y,Graphics g,int direction,int type)
{
switch(type)
{
case 0:
g.setColor(Color.YELLOW);
break;
case 1:
g.setColor(Color.CYAN);
break;
}
switch(direction)
{
//上
case 0:
//g.setColor(Color.CYAN);
g.fill3DRect(x, y,5,30,false);
g.fill3DRect(x+15, y,5,30,false);
g.fill3DRect(x+5, y+5,10,20,false);
g.fillOval(x+5, y+10, 9, 9);
g.drawLine(x+9,y+10,x+9,y);
break;
//右
case 1:
g.fill3DRect(x, y,30,5,false);
g.fill3DRect(x, y+15,30,5,false);
g.fill3DRect(x+5, y+5,20,10,false);
g.fillOval(x+10, y+5, 9, 9);
g.drawLine(x+15,y+10,x+30,y+10);
break;
//左
case 2:
g.fill3DRect(x, y,30,5,false);
g.fill3DRect(x, y+15,30,5,false);
g.fill3DRect(x+5, y+5,20,10,false);
g.fillOval(x+10, y+5, 9, 9);
g.drawLine(x,y+10,x+13,y+10);
break;
//下
case 3:
//g.setColor(Color.CYAN);
g.fill3DRect(x, y,5,30,false);
g.fill3DRect(x+15, y,5,30,false);
g.fill3DRect(x+5, y+5,10,20,false);
g.fillOval(x+5, y+10, 9, 9);
g.drawLine(x+9,y+13,x+9,y+30);
break;
}
}
@Override
public void keyPressed(KeyEvent e){
// TODO Auto-generated method stub
if(e.getKeyCode()==KeyEvent.VK_DOWN)
{
hero.moveDown();
//这两种方法都可;
//hero.y+=10;
hero.directd();
}
else if(e.getKeyCode()==KeyEvent.VK_UP)
{
hero.moveUp();
hero.directup();
}
else if(e.getKeyCode()==KeyEvent.VK_LEFT)
{
hero.moveLeft();
hero.directl();
}
else if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{
hero.moveRight();
hero.directR();
}
if(e.getKeyCode()==KeyEvent.VK_J)
{
if(this.hero.ss.size()<=4)
{
this.hero.fireshot();
}
}
repaint();
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true)
{
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//判断是否被击中
/*for(int i=0;i<hero.ss.size();i++)
{
//取出子弹
Shot myshot=hero.ss.get(i);
if(myshot.islive)
{
for(int j=0;j<ets.size();i++)
{
//取出敌人坦克
Badtank et=ets.get(j);
if(et.islive)
{
this.hittank(myshot, et);
}
}
}
}*/
this.repaint();
}
}
}
//子弹
class Shot implements Runnable
{
int x=0;
int y=0;
int speed=5;
int direct;
boolean islive=true;
public Shot(int x,int y,int direct)
{
this.x=x;
this.y=y;
this.direct=direct;
}
public void run()
{
while(true)
{
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
switch(direct)
{
case 0:
y-=speed;
break;
case 1:
x+=speed;
break;
case 2:
x-=speed;
break;
case 3:
y+=speed;
break;
}
if(x400||y>500||y<0)
{
islive=false;
break;
}
}
}
}
//敌人坦克
class Badtank extends Tank
{
boolean islive=true;
public Badtank(int x,int y)
{
super(x,y);
}
}
//坦克
class Tank
{
int x=0;
int y=0;
int color;
int direction=3;
int speed=10;
public int getColor() {
return color;
}
public void setColor(int color) {
this.color = color;
}
public int getDirect() {
return direction;
}
public void setDirect(int direct) {
this.direction = direct;
}
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;
}
public Tank(int x,int y)
{
this.x=x;
this.y=y;
}
//移动
/*public void moveUp(){
y-=speed;
}
public void moveDown(){
y+=speed;
}
public void moveLeft(){
x-=speed;
}
public void moveRight(){
x+=speed;
}
//方向
public void directup()
{
//向上
this.direction=0;
}
public void directR()
{
//向右
this.direction=1;
}
public void directl()
{
//向左
this.direction=2;
}
public void directd()
{
this.direction=3;
}*/
}
//我的坦克
class Hero extends Tank
{
Vector ss=new Vector();
Shot s=null;
public Hero(int x, int y) {
super(x, y);
// TODO Auto-generated constructor stub
}
public void fireshot()
{
switch(this.direction)
{
case 0:
s=new Shot(x+11,y,0);
ss.add(s);
break;
case 1:
s=new Shot(x+31,y+11,1);
ss.add(s);
break;
case 2:
s=new Shot(x,y+11,2);
ss.add(s);
break;
case 3:
s=new Shot(x+10,y+31,3);
ss.add(s);
break;
}
Thread t=new Thread(s);
t.start();
}
//移动
public void moveUp(){
y-=speed;
}
public void moveDown(){
y+=speed;
}
public void moveLeft(){
x-=speed;
}
public void moveRight(){
x+=speed;
}
//方向
public void directup()
{
//向上
this.direction=0;
}
public void directR()
{
//向右
this.direction=1;
}
public void directl()
{
//向左
this.direction=2;
}
public void directd()
{
this.direction=3;
}
}