不知道有没有前辈遇到过,怎么解决的,实在不明白怎么就卡死了。
import java.awt.*; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.Random; import java.util.Vector; import javax.swing.*; public class Tank extends JFrame { MyPanel mypanel; public Tank(){ mypanel=new MyPanel();//面板实例化 Thread p=new Thread(mypanel);//启动面板线程 p.start(); this.addKeyListener(mypanel); this.add(mypanel);//添加面板 this.setTitle("坦克小游戏"); this.setIconImage((new ImageIcon("Image/tank.gif").getImage())); this.setBounds(200, 100, 920, 600); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { Tank tank=new Tank(); } } class Tank1{ int x,y; int direct=0; int color=0; int speed=3; public Tank1(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; } public int getDirect() { return direct; } public void setDirect(int direct) { this.direct = direct; } public int getClolor() { return color; } public void setClolor(int clolor) { this.color = clolor; } 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; } } class MyPanel extends JPanel implements KeyListener,Runnable{ MyTank me=null; MyPanel mypanel=null; Vector <EnemyTank> enemys=new Vector<EnemyTank> (); int size=5; //炸弹图片 Image image1=null; Image image2=null; Image image3=null; Image image4=null; Image image5=null; Image image6=null; Image image7=null; Image image8=null; Image image9=null; //炸弹集合 Vector <Bomb> bombs=new Vector<Bomb>(); public MyPanel(){ //初始化图片 image1=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/11.gif")); image2=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/12.gif")); image3=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/13.gif")); image4=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/11.gif")); image5=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/12.gif")); image6=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/13.gif")); image7=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/11.gif")); image8=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/12.gif")); image9=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/13.gif")); //我方Tank的初始位置 me=new MyTank(435,420); //初始化敌方坦克 for(int i=0;i<size;i++){ EnemyTank enemy=new EnemyTank(i*100+70,10); Thread tenemy=new Thread(enemy); tenemy.start(); enemy.setClolor(1); enemy.setDirect(1); enemys.add(enemy); } } public void paintComponent(Graphics g){ super.paintComponent(g); g.fillRect(0, 0, 900, 560); mypanel=new MyPanel(); //画我方坦克 mypanel.drawTank(me.getX(),me.getY(),g,me.getDirect(),me.getClolor()); //画敌方坦克 for(int i=0;i<enemys.size();i++){ EnemyTank enemy=enemys.get(i); if (enemy.islive){ this.drawTank(enemy.getX(), enemy.getY(), g, enemy.getDirect(), enemy.getClolor()); } else if(!enemy.islive){ enemys.remove(enemy); } } //画我方子弹 for(int i=0;i<me.bullets.size();i++){ Bullet b2=me.bullets.get(i); if(b2.islive==true){ mypanel.drawBullet(me.b.x-1, me.b.y-1, g, 3, 3,false); } if(b2.islive==false){ me.bullets.remove(b2); } } //画爆炸效果 for(int i=0;i<bombs.size();i++){ Bomb bomb=bombs.get(i); if(bomb.life>8){ g.drawImage(image1, bomb.x-15,bomb.y-15, 30, 30, this); } else if(bomb.life>7){ g.drawImage(image2, bomb.x-15, bomb.y-15,30, 30, this); } else if(bomb.life>6){ g.drawImage(image3, bomb.x-15, bomb.y-15,30, 30, this); } else if(bomb.life>5){ g.drawImage(image4, bomb.x-15, bomb.y-15,30, 30, this); } else if(bomb.life>4){ g.drawImage(image5, bomb.x-15, bomb.y-15,30, 30, this); } else if(bomb.life>3){ g.drawImage(image6, bomb.x-15, bomb.y-15,30, 30, this); } else if(bomb.life>2){ g.drawImage(image7, bomb.x-15, bomb.y-15,30, 30, this); } else if(bomb.life>1){ g.drawImage(image8, bomb.x-15, bomb.y-15,30, 30, this); } else if(bomb.life>0){ g.drawImage(image9, bomb.x-15, bomb.y-15,30, 30, this); } bomb.LifeDown(bomb.islive); if(bomb.life==0){ bombs.remove(bomb); } } } //函数hitTank判断是否击中坦克 public void hitTank(Bullet bu,EnemyTank en){ switch(en.direct){ case 0: case 1: if(bu.x>en.x&&bu.x<en.x+26&&bu.y>en.y&bu.y<en.y+30){ en.islive=false; bu.islive=false; Bomb bomb=new Bomb(bu.x,bu.y); bombs.add(bomb); } //System.out.println("攻击坦克被调用了"); break; case 3: case 4: if(bu.x>en.x&&bu.x<en.x+30&&bu.y>en.y&bu.y<en.y+26){ en.islive=false; bu.islive=false; Bomb bomb=new Bomb(bu.x,bu.y); bombs.add(bomb); } //System.out.println("攻击坦克被调用了"); break; default: break; } } //画子弹 public void drawBullet(int x,int y,Graphics g,int a,int b,boolean forr){ g.setColor(Color.RED); g.fill3DRect(x, y, a, b, forr); } //画坦克 public void drawTank(int x,int y,Graphics g,int direct,int color){ switch(color){ case 0: g.setColor(Color.YELLOW); break; case 1: g.setColor(Color.CYAN); break; default: g.setColor(Color.GRAY); } switch(direct){ case 0://上 g.fill3DRect(x, y, 5, 30, true); g.fill3DRect(x+5, y+5, 16, 20, false); g.fill3DRect(x+20, y, 5, 30, true); g.fillOval(x+8, y+10, 10, 10); g.drawLine(x+13, y+15, x+13, y-5); break; case 1://下 g.fill3DRect(x, y, 5, 30, true); g.fill3DRect(x+5, y+5, 16, 20, false); g.fill3DRect(x+20, y, 5, 30, true); g.fillOval(x+8, y+10, 10, 10); g.drawLine(x+13, y+15, x+13, y+35); break; case 2://左 g.fill3DRect(x, y, 30, 5, true); g.fill3DRect(x+5, y+5, 20, 16, false); g.fill3DRect(x, y+21, 30, 5, true); g.fillOval(x+10, y+8, 10, 10); g.drawLine(x+15, y+13, x+35, y+13); break; case 3://右 g.fill3DRect(x, y, 30, 5, true); g.fill3DRect(x+5, y+5, 20, 16, false); g.fill3DRect(x, y+21, 30, 5, true); g.fillOval(x+10, y+8, 10, 10); g.drawLine(x+15, y+13, x-5, y+13); break; } } //监听键盘 public void keyPressed(KeyEvent e) { if(e.getKeyCode()==KeyEvent.VK_W){//上 if(me.getY()>5){ me.setDirect(0); me.moveUp(); } } else if(e.getKeyCode()==KeyEvent.VK_S){//下 if(me.getY()<525){ me.setDirect(1); me.moveDown(); } } else if(e.getKeyCode()==KeyEvent.VK_A){//左 if(me.getX()>0){ me.setDirect(3); me.moveLeft(); } } else if(e.getKeyCode()==KeyEvent.VK_D){//右 if(me.getX()<865){ me.setDirect(2); me.moveRight(); } } else if(e.getKeyCode()==KeyEvent.VK_J ){//空格射击 if(me.bullets.size()<5){ me.shotEnemy(); } } repaint(); } public void keyReleased(KeyEvent arg0) { // TODO Auto-generated method stub } public void keyTyped(KeyEvent arg0) { // TODO Auto-generated method stub } //添加线程run public void run() { while(true){ try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } for(int i=0;i<me.bullets.size();i++){ Bullet b=me.bullets.get(i); if(b.islive){ for(int j=0;j<enemys.size();j++){ EnemyTank e=enemys.get(j); if(e.islive) { hitTank(b,e); } } } } repaint(); } } } //我方坦克类 class MyTank extends Tank1{ Bullet b=null; Vector <Bullet> bullets=new Vector<Bullet>(); public MyTank(int x, int y) { super(x, y); } public void shotEnemy(){ switch(direct){ case 0: b=new Bullet(x+13,y-5,0); bullets.add(b); break; case 1: b=new Bullet(x+13,y+35,1); bullets.add(b); break; case 2: b=new Bullet(x+35,y+13,2); bullets.add(b); break; case 3: b=new Bullet(x-5,y+13,3); bullets.add(b); break; default: break; } Thread t=new Thread(this.b); t.start(); } } //敌方坦克类 class EnemyTank extends Tank1 implements Runnable{ Random r=new Random(); boolean islive=true; int speed=8; Vector<Bullet> enemyBullet=new Vector<Bullet>(); public EnemyTank(int x, int y) { super(x, y); } public void run() { while(true){ try { Thread.sleep(50); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } direct=r.nextInt(4); switch(direct){ case 0: for(int i=0;i<20;i++){ if(y>10) moveUp(); if(y==10) break; try { Thread.sleep(100); } catch (Exception e) { e.printStackTrace(); } } break; case 1: for(int i=0;i<20;i++){ if(y<550) moveDown(); if(y==550) break; try { Thread.sleep(100); } catch (Exception e) { e.printStackTrace(); } } break; case 2: for(int i=0;i<20;i++){ if(x<890) moveRight(); if(x==890) break; try { Thread.sleep(100); } catch (Exception e) { e.printStackTrace(); } } break; case 3: for(int i=0;i<20;i++){ if(x>15) moveLeft(); if(x==13) break; try { Thread.sleep(100); } catch (Exception e) { e.printStackTrace(); } } break; //for }//switch if(islive==false){ break; } }//while }//run }//类体 //子弹类 class Bullet implements Runnable{ int x; int y; int speed=10; int direct; boolean islive=true; public Bullet(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) { e.printStackTrace(); } switch(direct){ case 0: y-=speed; break; case 1: y+=speed; break; case 2: x+=speed; break; case 3: x-=speed; break; default: break; } if(x<0||x>920||y<0||y>600){ islive=false; break; } } } } //爆炸类 class Bomb { int x; int y; int life=9; boolean islive=true; public Bomb(int x,int y){ this.x=x; this.y=y; } public void LifeDown(boolean islive){ if(life>0){ life--; } if(life<=0){ islive=false; } } }
慕的地8582982
精慕门9254224
heart_
相关分类