当前位置: 代码迷 >> Java相关 >> 请大家帮帮忙 麻烦大家了
  详细解决方案

请大家帮帮忙 麻烦大家了

热度:326   发布时间:2010-05-03 11:25:13.0
请大家帮帮忙 麻烦大家了
编了一个坦克 但不会发子弹  请问哪里有问题 谢谢

package com.tank;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class MyTankGame1 extends JFrame{

    MyPanel mp=null;

    public static void main(String[] args) {
        MyTankGame1 mtk=new MyTankGame1();
    }
    public MyTankGame1()
    {
        mp=new MyPanel();
        this.addKeyListener(mp);
        this.add(mp);
        this.setSize(400,300);
        this.setTitle("坦克大战");
        this.setLocation(100, 100);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

}
class MyPanel extends JPanel implements KeyListener
{
    int speed=2;
    MyTank my;
    Vector<EnemyTank> et=new Vector<EnemyTank>();
    public MyPanel()
    {
        my=new MyTank(200,200,0,1);
        for(int i=0;i<3;i++)
        {
            EnemyTank eta=new EnemyTank(10+i*50, 10, 2, speed);
            et.add(eta);
        }
    }
    public void paint(Graphics g)
    {
        int i=0;
        my.setSpeed(speed);
        super.paint(g);
        g.fillRect(0, 0, 400, 300);
        this.drawTank(my.getX(), my.getY(), g, my.getDirect(),my.getType());
        for(i=0;i<et.size();i++)
        {
            this.drawTank(et.get(i).getX(), et.get(i).getY(), g, et.get(i).getDirect(), et.get(i).getType());
        }
    }
    public void drawTank(int x,int y,Graphics g,int direct,int type)
    {
        switch(type)
        {
        case 0:
            g.setColor(Color.GREEN);
            break;
        case 1:
            g.setColor(Color.GRAY);
        }
        switch(direct)
        {
        case 0:   
            g.fill3DRect(x-10,y-15, 5, 30, false);
            g.fill3DRect(x+5,y-15, 5, 30, false);
            g.fill3DRect(x-5, y-10, 10, 20,false);
            g.fillOval(x-5,y-5, 10, 10);
            g.drawLine(x, y, x, y-15);break;
        case 1:
            g.fill3DRect(x-15, y-10, 30, 5, false);
            g.fill3DRect(x-15, y+5, 30, 5, false);
            g.fill3DRect(x-10, y-5, 20, 10, false);
            g.fillOval(x-5, y-5, 10, 10);
            g.drawLine(x, y, x+15, y);break;
        case 2:
            g.fill3DRect(x-10,y-15, 5, 30, false);
            g.fill3DRect(x+5,y-15, 5, 30, false);
            g.fill3DRect(x-5, y-10, 10, 20,false);
            g.fillOval(x-5,y-5, 10, 10);
            g.drawLine(x, y, x, y+15);break;
        case 3:
            g.fill3DRect(x-15, y-10, 30, 5, false);
            g.fill3DRect(x-15, y+5, 30, 5, false);
            g.fill3DRect(x-10, y-5, 20, 10, false);
            g.fillOval(x-5, y-5, 10, 10);
            g.drawLine(x, y, x-15, y);break;
        }
    }
   
    @Override
    public void keyPressed(KeyEvent e) {
        // TODO Auto-generated method stub
        if(e.getKeyCode()==KeyEvent.VK_DOWN)
        {
            my.moveDown();
            this.repaint();
        }else if(e.getKeyCode()==KeyEvent.VK_UP)
        {
            my.moveUp();
            this.repaint();
        }else if(e.getKeyCode()==KeyEvent.VK_LEFT)
        {
            my.moveLeft();
            this.repaint();
        }else if(e.getKeyCode()==KeyEvent.VK_RIGHT)
        {
            my.moveRight();
            this.repaint();
        }
    }
    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub
        
    }
    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub
        
    }
}

搜索更多相关的解决方案: 麻烦  

----------------解决方案--------------------------------------------------------
  相关解决方案