⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tank.java

📁 坦克大战源代码,坦克大战源代码,坦克大战源代码
💻 JAVA
字号:
package org.MyTank;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.List;
import java.util.Random;

public class Tank {

    public static final int LIFE = 100;
    public static final double XSPEED = 5.0;
    public static final double YSPEED = 5.0;
    public static final int WIDTH = 30;
    public static final int HEIGHT = 30;

    private static Random r = new Random();
    private int life = LIFE;
    private int x, y, oldX, oldY;
    private boolean bL = false, bU = false, bR = false, bD = false;
    private Direction dir = Direction.STOP;
    private Direction ptDir = Direction.R;
    private boolean good;
    private boolean live = true;
    private int step = r.nextInt(15) + 5;
    
    BloodBar bb =new BloodBar();
    TankWarGUI tw;

    public Tank(int x, int y, boolean good) {
	this.x = x;
	this.y = y;
	this.good = good;
    }

    public Tank(int x, int y, boolean good, Direction dir, TankWarGUI tw) {
	this(x, y, good);
	this.dir = dir;
	this.tw = tw;
    }

    public void draw(Graphics g) {
	if (!this.live) {
	    if (!good)
		tw.tanks.remove(this);
	    return;
	}
	if(this.isGood()){
	    bb.draw(g);
	}
	Color c = g.getColor();
	if (good)
	    g.setColor(Color.cyan);
	else
	    g.setColor(Color.YELLOW);
	g.fillOval(x, y, WIDTH, HEIGHT);
	g.setColor(c);

	switch (ptDir) {
	case L:
	    g.drawLine(x + WIDTH / 2, y + HEIGHT / 2, x, y + HEIGHT / 2);
	    break;
	case LU:
	    g.drawLine(x + WIDTH / 2, y + HEIGHT / 2, x + WIDTH / 4 - 3, y
		    + HEIGHT / 4 - 3);
	    break;
	case U:
	    g.drawLine(x + WIDTH / 2, y + HEIGHT / 2, x + WIDTH / 2, y);
	    break;
	case RU:
	    g.drawLine(x + WIDTH / 2, y + HEIGHT / 2, x + (3 * WIDTH) / 4 + 3,
		    y + HEIGHT / 4 - 3);
	    break;
	case R:
	    g
		    .drawLine(x + WIDTH / 2, y + HEIGHT / 2, x + WIDTH, y
			    + HEIGHT / 2);
	    break;
	case RD:
	    g.drawLine(x + WIDTH / 2, y + HEIGHT / 2, x + (3 * WIDTH) / 4 + 3,
		    y + (3 * HEIGHT) / 4 + 3);
	    break;
	case D:
	    g
		    .drawLine(x + WIDTH / 2, y + HEIGHT / 2, x + WIDTH / 2, y
			    + HEIGHT);
	    break;
	case LD:
	    g.drawLine(x + WIDTH / 2, y + HEIGHT / 2, x + WIDTH / 4 - 3, y
		    + (3 * HEIGHT) / 4 + 3);
	    break;
	case STOP:
	    break;
	}

	this.move();
    }

    public void KeyReleased(KeyEvent e) {
	int key = e.getKeyCode();
	switch (key) {
	case KeyEvent.VK_CONTROL:
	    fire();
	    break;
	case KeyEvent.VK_LEFT:
	    this.bL = false;
	    break;
	case KeyEvent.VK_UP:
	    this.bU = false;
	    break;
	case KeyEvent.VK_RIGHT:
	    this.bR = false;
	    break;
	case KeyEvent.VK_DOWN:
	    this.bD = false;
	    break;
	case KeyEvent.VK_S :
	    superFire();
	    break;
	}
	this.locateDirection();
    }

    public void KeyPressed(KeyEvent e) {
	int key = e.getKeyCode();
	switch (key) {
	case KeyEvent.VK_LEFT:
	    this.bL = true;
	    break;
	case KeyEvent.VK_UP:
	    this.bU = true;
	    break;
	case KeyEvent.VK_RIGHT:
	    this.bR = true;
	    break;
	case KeyEvent.VK_DOWN:
	    this.bD = true;
	    break;
	}
	this.locateDirection();
    }

    private void move() {

	this.oldX = x;
	this.oldY = y;

	switch (dir) {
	case L:
	    x -= XSPEED;
	    break;
	case LU:
	    x -= XSPEED;
	    y -= YSPEED;
	    break;
	case U:
	    y -= YSPEED;
	    break;
	case RU:
	    x += XSPEED;
	    y -= YSPEED;
	    break;
	case R:
	    x += XSPEED;
	    break;
	case RD:
	    x += XSPEED;
	    y += YSPEED;
	    break;
	case D:
	    y += YSPEED;
	    break;
	case LD:
	    x -= XSPEED;
	    y += YSPEED;
	    break;
	case STOP:
	    break;
	}
	if (dir != Direction.STOP)
	    this.ptDir = dir;

	if (x < 0)
	    x = 0;
	if (y < 23)
	    y = 23;
	if (x + Tank.WIDTH > TankWarGUI.GAME_WIDTH)
	    x = TankWarGUI.GAME_WIDTH - Tank.WIDTH;
	if (y + Tank.HEIGHT > TankWarGUI.GAME_HEIGHT)
	    y = TankWarGUI.GAME_HEIGHT - Tank.HEIGHT;

	if (!good) {
	    Direction[] dirs = Direction.values();
	    if (step == 0) {
		step = r.nextInt(15) + 5;
		int rn = r.nextInt(dirs.length);
		dir = dirs[rn];
		if (r.nextInt(10) >= 5)
		    fire();
	    }
	    step--;
	}
    }

    private void stay() {
	this.x = this.oldX;
	this.y = this.oldY;
    }

    private void locateDirection() {
	if (!bL && !bU && !bR && bD)
	    dir = Direction.D;
	else if (!bL && !bU && bR && !bD)
	    dir = Direction.R;
	else if (!bL && bU && !bR && !bD)
	    dir = Direction.U;
	else if (bL && !bU && !bR && !bD)
	    dir = Direction.L;
	else if (!bL && !bU && bR && bD)
	    dir = Direction.RD;
	else if (bL && bU && !bR && !bD)
	    dir = Direction.LU;
	else if (!bL && bU && bR && !bD)
	    dir = Direction.RU;
	else if (bL && !bU && !bR && bD)
	    dir = Direction.LD;
	else if (!bL && !bU && !bR && !bD)
	    dir = Direction.STOP;
    }

    public Missile fire() {
	if (!this.isLive())
	    return null;
	int x = this.x + Tank.WIDTH / 2 - Missile.WIDTH / 2;
	int y = this.y + Tank.HEIGHT / 2 - Missile.HEIGHT / 2;
	Missile m = new Missile(x, y, good, ptDir, this.tw);
	tw.missiles.add(m);
	return m;
    }

    public Missile fire(Direction dir) {
	if (!this.isLive())
	    return null;
	int x = this.x + Tank.WIDTH / 2 - Missile.WIDTH / 2;
	int y = this.y + Tank.HEIGHT / 2 - Missile.HEIGHT / 2;
	Missile m = new Missile(x, y, good, dir, this.tw);
	tw.missiles.add(m);
	return m;
    }
    
    public Rectangle getRec() {
	return new Rectangle(x, y, WIDTH, HEIGHT);
    }

    public boolean isLive() {
	return live;
    }

    public void setLive(boolean live) {
	this.live = live;
    }

    public void collidesWithTank(Tank t) {
	if (this.getRec().intersects(t.getRec()) && this.isLive() && t.isLive()) {
	    this.stay();
	    t.stay();
	}
    }

    public boolean isGood() {
	return good;
    }

    public boolean equals(Tank t) {
	return (t.x == this.x && t.y == this.y);
    }

    public boolean collidesWithTanks(List<Tank> ts) {
	for (int i = 0; i < ts.size(); i++) {
	    Tank t = ts.get(i);
	    if (!this.equals(t)) {
		if (this.getRec().intersects(t.getRec()) && this.isLive()
			&& t.isLive()) {
		    this.stay();
		    t.stay();
		    return true;
		}
	    }
	}
	return false;
    }

    public boolean collidesWithWall(Wall w) {
	if (this.isLive() && this.getRec().intersects(w.getRec())) {
	    this.x = this.oldX;
	    this.y = this.oldY;
	    return true;
	}
	return false;
    }

    
    public int getLife() {
        return life;
    }

    
    public void setLife(int life) {
        this.life = life;
    }

    void superFire(){
	Direction[] dirs = Direction.values();
	for(int i=0; i<8; i++){
	    fire(dirs[i]);
	}
    }
    
    
    private class BloodBar {
	public void draw(Graphics g){
	    Color c = g.getColor();
	    g.setColor(Color.MAGENTA);
	    g.drawRect(x, y-10, WIDTH, 5);
	    g.setColor(Color.red);
	    int w = WIDTH * life/100;
	    g.fillRect(x, y-10, w, 5);
	    g.setColor(c);
	}
    }
    
    
}


















⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -