我的坦克动不起来?!!

来源:互联网 发布:centos卸载lamp环境 编辑:程序博客网 时间:2024/05/01 18:26
/*坦克大战:
 * 1.0版 :画出坦克(为了减少内存开销,用代码画出坦克而不是直接导图片)(2013-12-21)
 * 2.0版:通过指定的键控制坦克运行方向(2013-12-25)
 * */
package>MyPanel>
public static void main(String[] args) {

MyTankGame>

}
    public MyTankGame()
   {
mp=new MyPanel();
this.add(mp);
//注册监听
this.addKeyListener(mp);

this.setTitle("任和:模拟经典坦克大战");
this.setSize(410, 300);
this.setLocation(500, 200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
}
//面板(坦克的活动区域就在面板上面)
class MyPanel extends JPanel implements java.awt.event.KeyListener
{
//定义一个我的坦克
Hero>public MyPanel()
{
//我的坦克的初始位置
hero=new Hero(100,100);
}
public void paint(Graphics g)
{
super.paint(g);
g.fillRect(0, 0, 400, 300);

this.drawTank(hero.getX(), hero.getY(),  g, this.hero.direct, 1);

}
//画出坦克的函数
public void drawTank(int x,int y,Graphics g,int direct,int type)
{
//判断是什么类型的坦克
switch(type)
{
case 0:
g.setColor(Color.cyan);
break;
case 1:
g.setColor(Color.yellow);
break;
}
//判断方向
switch(direct)
{
case 0://炮筒初始向上
//自助坦克上面的矩形
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, 10, 10);
//画根线代表炮筒
g.drawLine(x+10, y+10, x+10, y);
break;
case 1://炮筒向右
g.fill3DRect(x, y, 30, 5,false);
g.fill3DRect(x+15, y, 30, 5,false);
g.fill3DRect(x+5, y+5, 20, 20,false);
g.fillOval(x+10, y+5, 10, 10);
g.drawLine(x+15, y+10, x+30, y+10);
break;
case 2://炮筒向下
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, 10, 10);
g.drawLine(x+10, y+15, x+10, y+30);
break;

case 3://炮筒向左
g.fill3DRect(x, y, 30, 5,false);
g.fill3DRect(x+15, y, 30, 5,false);
g.fill3DRect(x+5, y+5, 20, 10,false);
g.fillOval(x+10, y+5, 10, 10);
g.drawLine(x+15, y+10, x, y+10);
break;
}
}


@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub

}


//设置键盘上的a d w s分别表示向左,向右,向上,向下
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if(e.getKeyCode()==KeyEvent.VK_W)
{
//设置我的坦克向上的方向
this.hero.setDirect(0);
this.hero.moveup();
}else>{
//设置我的坦克向下的方向
this.hero.setDirect(2);
this.hero.movedown();
}else>{
//向左
this.hero.setDirect(3);
this.hero.moveleft();
}else>{
//向右
this.hero.setDirect(1);
this.hero.moveright();
}
//重绘panel
this.repaint();
}



@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub

}
}


//坦克类
class Tank
{
//坦克的横坐标
int x=0;
//坦克的纵坐标
int>
//假设0是向上的方向  1右  2下  3左
int>
//设置速度
int>
public int getSpeed() {
return speed;
}


public void setSpeed(int speed) {
this.speed = speed;
}


public int getDirect() {
return direct;
}


public void setDirect(int direct) {
this.direct = direct;
}


public Tank(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;
}

}
//我的坦克
class Hero extends Tank
{
//父类的构造函数初始化子类的成员变量
public Hero(int x,int y)
{
super(x,y);
}

//坦克速度的设置
public void moveup()
{
y-=speed;
}
public void moveright()
{
x+=speed;
}
public void movedown()
{
y+=speed;
}
public void moveleft()
{
x-=speed;
}
}


0 0