坦克java代码 java tank程序代码( 二 )


hero.setSDC(hero.getSpeed(), 1, hero.getColor());
hero.moveDown();
} else if (arg0.getKeyCode() == KeyEvent.VK_A) {
hero.setSDC(hero.getSpeed(), 2, hero.getColor());
hero.moveLeft();
} else if (arg0.getKeyCode() == KeyEvent.VK_D) {
hero.setSDC(hero.getSpeed(), 3, hero.getColor());
hero.moveRight();
}
/**
* 这个repaint注释掉
*/
//this.repaint();
}
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
// 完毕
public MyPanel() {
hero = new MyTank(250, 250);
hero.setSDC(5, 2, 2);
for (int i = 0; iemsize; ++i) {
EmenyTank em = new EmenyTank((i + 1) * 60, 20);
em.setSDC(5, 1, 1);
emeny.add(em);
}
}
// 线程
/**
* 一秒钟60帧
*/
public void run() {
// TODO Auto-generated method stub
while(true){
this.repaint();
try {
Thread.sleep(1000 / 60);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
public void paint(Graphics g) {
super.paint(g);
// 画板坦克java代码 , 坦克得放在画板后头
g.fillRect(0, 0, 400, 400);
// paint敌人坦克
for (int i = 0; iemeny.size(); ++i) {
EmenyTank em = null;
em = emeny.get(i);
this.drawTank(em.getX(), em.getY(), g, em.getDirect(),
em.getColor());
}
// 画坦克java代码我自己的坦克
this.drawTank(hero.getX(), hero.getY(), g, hero.getDirect(),
hero.getColor());
// 画出坦克java代码我的子弹
for (int i = 0; ihero.shot.size(); i++) {
Shot myShot = hero.shot.get(i);
if (myShot != nullmyShot.live == true) {
g.draw3DRect(myShot.x, myShot.y, 2, 2, false);
}
if (myShot.live == false) {
hero.shot.remove(myShot);
}
}
}
public void drawTank(int x, int y, Graphics g, int direct, int color) {
// 判断坦克的颜色(敌我)然后画出坦克
switch (color) {
case 0:
g.setColor(Color.BLUE);
break;
case 1:
g.setColor(Color.YELLOW);
break;
case 2:
g.setColor(Color.GREEN);
break;
}
// 判断坦克的方向然后再画出坦克
switch (direct) {
case 0:
g.fill3DRect(x, y, 10, 30, false);
g.fill3DRect(x + 26, y, 10, 30, false);
g.fill3DRect(x + 10, y + 5, 16, 20, false);
g.drawLine(x + 18, y + 15, x + 18, y);
break;
case 1:
g.fill3DRect(x, y, 10, 30, false);
g.fill3DRect(x + 26, y, 10, 30, false);
g.fill3DRect(x + 10, y + 5, 16, 20, false);
g.drawLine(x + 18, y + 15, x + 18, y + 30);
break;
case 2:
g.fill3DRect(x + 3, y - 3, 30, 10, false);
g.fill3DRect(x + 3, y + 23, 30, 10, false);
g.fill3DRect(x + 8, y + 7, 20, 16, false);
g.drawLine(x + 18, y + 15, x + 3, y + 15);
break;
case 3:
g.fill3DRect(x + 3, y - 3, 30, 10, false);
g.fill3DRect(x + 3, y + 23, 30, 10, false);
g.fill3DRect(x + 8, y + 7, 20, 16, false);
g.drawLine(x + 18, y + 15, x + 33, y + 15);
break;
}
}
}
class EmenyTank extends Tank implements Runnable {
public EmenyTank(int x, int y) {
// TODO Auto-generated method stub
super(x, y);
}
public void run() {
}
}
class Shot implements Runnable {
protected int x;
protected int y;
protected int direct;
protected int speed = 4;
protected boolean live = true;
public void setX(int x) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setDirect(int direct) {

推荐阅读