# 刚开始学习java，用java写了一个坦克小游戏的程序，但是有点有关问题，希望高手可以指点一下(2)

www.MyException.Cn  网友分享于：2013-02-03  浏览：181次

break;

}
//判断坦克的方向
switch(direct)
{
//向上
case 0:
g.setColor(Color.CYAN);//定义画笔颜色
g.fill3DRect(x, y, 8, 60,false);
g.fill3DRect(x+28, y, 8, 60,false);
g.fill3DRect(x+8, y+10, 20, 40,false);
g.fillOval(x+8, y+20, 20, 20);
g.drawLine(x+17, y+30, x+17, y-4);
break;
//向下
case 1:
g.setColor(Color.CYAN);//定义画笔颜色
g.fill3DRect(x, y, 8, 60,false);
g.fill3DRect(x+28, y, 8, 60,false);
g.fill3DRect(x+8, y+10, 20, 40,false);
g.fillOval(x+8, y+20, 20, 20);
g.drawLine(x+17, y+30, x+17, y+63);
break;
//向左
case 2:
g.setColor(Color.CYAN);//定义画笔颜色
g.fill3DRect(x, y, 60, 8,false);
g.fill3DRect(x, y+28, 60, 8,false);
g.fill3DRect(x+10, y+8, 40, 20,false);
g.fillOval(x+19, y+8, 20, 20);
g.drawLine(x+22, y+17, x-4, y+17);
break;
//向右
case 3:
g.setColor(Color.CYAN);//定义画笔颜色
g.fill3DRect(x, y, 60, 8,false);
g.fill3DRect(x, y+28, 60, 8,false);
g.fill3DRect(x+10, y+8, 40, 20,false);
g.fillOval(x+19, y+8, 20, 20);
g.drawLine(x+22, y+17, x+66, y+17);
break;

}

}
//画出墙的函数
public void drawQiang(int x,int y,Graphics g,int type)
{
//判断墙的类型
switch(type)
{
case 0:
g.setColor(Color.pink);//定义画笔颜色
for(int i=0;i<50;i++)
{

g.fill3DRect(x+(i*5), y, 5, 30,false);
}

break;
case 1:
g.setColor(Color.red);//定义画笔颜色
for(int i=0;i<50;i++)
{
g.fill3DRect(x+(i*5), y, 5, 30,false);
}
break;

}

}
//键按下的处理
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if(e.getKeyCode()==KeyEvent.VK_UP)
{

this.wantank.setDriect(0);

this.wantank.moveUp();
System.out.println(x);
System.out.println(y);

}else if(e.getKeyCode()==KeyEvent.VK_DOWN)
{
this.wantank.setDriect(1);

this.wantank.moveDown();
}else if(e.getKeyCode()==KeyEvent.VK_LEFT)
{
this.wantank.setDriect(2);

this.wantank.moveLeft();
}else if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{
this.wantank.setDriect(3);

this.wantank.moveRight();
}
this.repaint();
}
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub

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

}

}
//坦克类
class Tank
{

//表示坦克的横坐标,纵坐标
int x=0;
int y=0;
//坦克的方向变量
int driect=0;
//表示速度
int speed=5;
//构造函数
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;
}
public int getDriect() {
return driect;
}
public void setDriect(int driect) {
this.driect = driect;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}

}

//玩家的坦克
class WanTank extends Tank
{

public WanTank(int x,int y)
{

super(x,y);
}

//坦克向上移动
public void moveUp()
{

System.out.println("你好");
y-=speed;

System.out.println(x);

System.out.println(y);
}
//坦克向下移动
public void moveDown()
{
y+=speed;
}
//坦克向左移动
public void moveLeft()
{