java坦克大战项目

Java简单的编程小项目,对坦克大战小游戏做了一些改动,有图形化界面,
计时器,血条,技能条,音乐播放,双人模式和单人模式,地图设计等等。

Freee类:界面设计


package tank;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
public class Freee {

static JFrame jf = new JFrame("坦克大战");
private static void firtPage() {
	// 1.设置窗体大小和标题
	
	jf.setPreferredSize(new Dimension(500, 300));
	// 2.设置关闭窗口就是关闭程序
	jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	// 最精准的布局模式空布局
	jf.setLayout(null);
	// 设置定位
	JLabel jl = new JLabel("坦克大战", JLabel.CENTER);
	jl.setPreferredSize(new Dimension(680, 30));
	jf.add(jl);
	jl.setBounds(0, 0, 480, 30);
	jl.setFont(new Font("宋体", Font.BOLD, 24));
	jl.setForeground(Color.decode("#375a7f"));
	// 菜单栏
	// 新建一个菜单条
	JMenuBar jb = new JMenuBar();
	jf.add(jb);
	jb.setBounds(0, 40, 690, 30);
	jb.setBackground(Color.decode("#65991a"));
	// 新建一个菜单选项
	JMenu jmenu = new JMenu("开始游戏");
	jmenu.setPreferredSize(new Dimension(100, 30));
	jmenu.setForeground(Color.white);
	jb.add(jmenu);
	JMenuItem j = new JMenuItem("开始游戏");
	jmenu.add(j);
	// 新建一个菜单项
	
	JMenu jmenu0 = new JMenu("游戏模式");
	jmenu0.setPreferredSize(new Dimension(100, 30));
	jmenu0.setForeground(Color.white);
	jb.add(jmenu0);
	
	// 新建一个菜单项

	JMenuItem jm = new JMenuItem("单人模式");
	JMenuItem jmi = new JMenuItem("双人模式");
 
	jmenu0.add(jm);
	jmenu0.add(jmi);


	// 以下是显示位移的地方
	// 放置图片
	JLabel jl3 = new JLabel(new ImageIcon("E:/java程序/新建文件夹/TankWarL/src/image/124.png"));
	jf.add(jl3);
	jl3.setBounds(0, 70, 480, 200);
	//开始监听事件
   j.addActionListener(new ActionListener() {
		
		@Override
		public void actionPerformed(ActionEvent e) {
			//销毁当前页面
			closeThis();
			//打开一个新的页面
			new TankClient2().lauchFrame();
		}
	});
	jm.addActionListener(new ActionListener() {
		
		@Override
		public void actionPerformed(ActionEvent e) {
			//销毁当前页面
			closeThis();
			//打开一个新的页面
			new TankClient2().lauchFrame();
		}
	});
    jmi.addActionListener(new ActionListener() {
		
		@Override
		public void actionPerformed(ActionEvent e) {
			//销毁当前页面
			closeThis();
			//打开一个新的页面
			new TankClient().lauchFrame();
		}
	});
	// 3.设置窗体可见
	jf.pack();
	jf.setVisible(true);
	jf.setResizable(false);
}
public static void closeThis(){
	jf.dispose();
	}
public static void main(String[] args) {
	firtPage();
}}

Tank类
我方坦克设计


package tank;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Tank {
public static final int XSPEED = 5;
public static final int YSPEED = 5;

public static final int WIDTH = 10;
public static final int HEIGHT = 10;

private boolean live = true;
private BloodBar bb = new BloodBar();

private int life = 100;
private  int skill=20;
private SkillBar cb=new  SkillBar();
TankClient tc;

private boolean good;

private int x, y;
private int oldX, oldY;

private static Random r = new Random();

private boolean bL=false, bU=false, bR=false, bD = false;
enum Direction {L, LU, U, RU, R, RD, D, LD, STOP};

private Direction dir = Direction.STOP;
private Direction ptDir = Direction.D;

private int step = r.nextInt(12) + 3;

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

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

public void draw(Graphics g) {
	if(!live) {
		if(!good) {
			tc.tanks.remove(this);
		}
		return;
	}
	
	Color c = g.getColor();
	if(good) g.setColor(Color.RED);
	else g.setColor(Color.BLUE);


	switch(ptDir) {
	case L:
	case R:
		g.fill3DRect(x-6, y-5, 30, 5,false );
		g.fill3DRect(x-6, y+10, 30, 5, false);
		g.fill3DRect(x-2, y, 20, 10, false);
		
		g.fillOval(x, y, WIDTH, HEIGHT);
		g.setColor(c);
		
		break;
	case LU:
	case RU:
	case U:
	
	case RD:
	case D:
	case LD:
		g.fill3DRect(x-4, y-10, 5, 30,false );
		g.fill3DRect(x+11, y-10, 5, 30, false);
		g.fill3DRect(x+1, y-5, 10, 20, false);
		
		g.fillOval(x, y, WIDTH, HEIGHT);
		g.setColor(c);
		break;
	}
	if(good) { bb.draw(g);cb.draw(g);}
	
	
	switch(ptDir) {
	case L:
		g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x, y + Tank.HEIGHT/2);
		break;
	case LU:
		g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x, y);
		break;
	case U:
		g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH/2, y);
		break;
	case RU:
		g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH, y);
		break;
	case R:
		g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH, y + Tank.HEIGHT/2);
		break;
	case RD:
		g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH, y + Tank.HEIGHT);
		break;
	case D:
		g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH/2, y + Tank.HEIGHT);
		break;
	case LD:
		g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x, y + Tank.HEIGHT);
		break;
	}
	
	move();
}

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(this.dir != Direction.STOP) {
		this.ptDir = this.dir;
	}
	
	if(x < 0) x = 0;
	if(y < 30) y = 30;
	if(x + Tank.WIDTH > TankClient.GAME_WIDTH) x = TankClient.GAME_WIDTH - Tank.WIDTH;
	if(y + Tank.HEIGHT > TankClient.GAME_HEIGHT) y = TankClient.GAME_HEIGHT - Tank.HEIGHT;
	
	if(!good) {
		Direction[] dirs = Direction.values();
		if(step == 0) {
			step = r.nextInt(12) + 3;
			int rn = r.nextInt(dirs.length);
			dir = dirs[rn];
		}			
		step --;
		
		if(r.nextInt(40) > 38) this.fire();
	}		
}

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

public void keyPressed(KeyEvent e) {
	int key = e.getKeyCode();
	switch(key) {
	case KeyEvent.VK_LEFT :
		bL = true;
		break;
	case KeyEvent.VK_UP :
		bU = true;
		break;
	case KeyEvent.VK_RIGHT :
		bR = true;
		break;
	case KeyEvent.VK_DOWN :
		bD = true;
		break;
	}
	locateDirection();
}
public void keyPressed2(KeyEvent e) {
	int key = e.getKeyCode();
	switch(key) {
	case KeyEvent.VK_A :
		bL = true;
		break;
	case KeyEvent.VK_W :
		bU = true;
		break;
	case KeyEvent.VK_D :
		bR = true;
		break;
	case KeyEvent.VK_S :
		bD = true;
		break;
	}
	locateDirection();
}

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

public void keyReleased(KeyEvent e) {
	int key = e.getKeyCode();
	switch(key) {
	case KeyEvent.VK_CONTROL:
		fire();
		break;
	case KeyEvent.VK_LEFT :
		bL = false;
		break;
	case KeyEvent.VK_UP :
		bU = false;
		break;
	case KeyEvent.VK_RIGHT :
		bR = false;
		break;
	case KeyEvent.VK_DOWN :
		bD = false;
		break;
	case KeyEvent.VK_L :
		superFire();
		break;
	}
	locateDirection();		
}
public void keyReleased2(KeyEvent e) {
	int key = e.getKeyCode();
	switch(key) {
	case KeyEvent.VK_ENTER:
		fire();
		break;
	case KeyEvent.VK_A :
		bL = false;
		break;
	case KeyEvent.VK_W :
		bU = false;
		break;
	case KeyEvent.VK_D :
		bR = false;
		break;
	case KeyEvent.VK_S :
		bD = false;
		break;
	case KeyEvent.VK_J :
		superFire();
		break;
	}
	locateDirection();		
}
public Missile fire() {
	if(!live) 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.tc);
	tc.missiles.add(m);
	return m;
}

public Missile fire(Direction dir) {
	if(!live) 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.tc);
	tc.missiles.add(m);
	return m;
}

public Rectangle getRect() {
	return new Rectangle(x, y, WIDTH, HEIGHT);
}

public boolean isLive() {
	return live;
}

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

public boolean isGood() {
	return good;
}

public boolean collidesWithWall(Wall w) {
	if(this.live && this.getRect().intersects(w.getRect())) {
		this.stay();
		return true;
	}
	return false;
}

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

private void superFire() {
	Direction[] dirs = Direction.values();
	for(int i=0; i<8; i++) {
		fire(dirs[i]);
	}
	skill--;
}

public int getLife() {
	return life;
}

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

private class BloodBar {
	public void draw(Graphics g) {
		Color c = g.getColor();
		g.setColor(Color.RED);
		g.drawRect(x-4, y-20, WIDTH, 5);
		int w = WIDTH * life/100 ;
		g.fillRect(x-4, y-20, w, 5);
		g.setColor(c);
	}
}
private class SkillBar {
	public void draw(Graphics g) {
		Color c = g.getColor();
		g.setColor(Color.BLUE);
		g.drawRect(x-4, y-30, WIDTH, 5);
		int w = WIDTH * skill/20 ;
		g.fillRect(x-4, y-30, w, 5);
		g.setColor(c);
	}
}
public boolean eat(Blood b) {
	if(this.live && b.isLive() && this.getRect().intersects(b.getRect())) {
		this.life = 100;
		this.skill=20;
		b.setLive(false);
		return true;
	}
	return false;
}}

tank2
敌方坦克设计


package tank;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;

import javax.swing.JOptionPane;
public class Tank2 {
public static final int XSPEED = 5;
public static final int YSPEED = 5;

public static final int WIDTH = 10;
public static final int HEIGHT = 10;

private boolean live = true;
private BloodBar bb = new BloodBar();

private int life = 100;
private  int skill=20;
private SkillBar cb=new  SkillBar();
TankClient2 tc2;

private boolean good;

private int x, y;
private int oldX, oldY;

private static Random r = new Random();

private boolean bL=false, bU=false, bR=false, bD = false;
enum Direction {L, LU, U, RU, R, RD, D, LD, STOP};

private Direction dir = Direction.STOP;
private Direction ptDir = Direction.D;

private int step = r.nextInt(12) + 3;



public Tank2(boolean good, Direction dir, TankClient2 tc2) {	
	x = r.nextInt(tc2.GAME_WIDTH-WIDTH+1);//初始化时在窗口范围内的随机坐标生成坦克
	y = 40+r.nextInt(tc2.GAME_HEIGHT-HEIGHT-40+1);//减去窗口的标题栏高度40
	this.good = good;
	oldX = x;
	oldY = y;
	this.tc2 = tc2;// 初始化tc
	this.dir = dir;// 指定坦克对象的方向
}


public void draw(Graphics g) {
	if(!live) {
		if(!good) {
			tc2.tanks2.remove(this);
		}
		return;
	}
	
	Color c = g.getColor();
	if(good) g.setColor(Color.RED);
	else g.setColor(Color.BLUE);
	switch(ptDir) {
	case L:
	case R:
		g.fill3DRect(x-6, y-5, 30, 5,false );
		g.fill3DRect(x-6, y+10, 30, 5, false);
		g.fill3DRect(x-2, y, 20, 10, false);
		
		g.fillOval(x, y, WIDTH, HEIGHT);
		g.setColor(c);
		
		break;
	case LU:
	case RU:
	case U:
	
	case RD:
	case D:
	case LD:
		g.fill3DRect(x-4, y-10, 5, 30,false );
		g.fill3DRect(x+11, y-10, 5, 30, false);
		g.fill3DRect(x+1, y-5, 10, 20, false);
		
		g.fillOval(x, y, WIDTH, HEIGHT);
		g.setColor(c);
		break;
	}
	
	
	
	
	switch(ptDir) {
	case L:
		g.drawLine(x + Tank2.WIDTH/2, y + Tank2.HEIGHT/2, x, y + Tank2.HEIGHT/2);
		break;
	case LU:
		g.drawLine(x + Tank2.WIDTH/2, y + Tank2.HEIGHT/2, x, y);
		break;
	case U:
		g.drawLine(x + Tank2.WIDTH/2, y + Tank2.HEIGHT/2, x + Tank2.WIDTH/2, y);
		break;
	case RU:
		g.drawLine(x + Tank2.WIDTH/2, y + Tank2.HEIGHT/2, x + Tank2.WIDTH, y);
		break;
	case R:
		g.drawLine(x + Tank2.WIDTH/2, y + Tank2.HEIGHT/2, x + Tank2.WIDTH, y + Tank2.HEIGHT/2);
		break;
	case RD:
		g.drawLine(x + Tank2.WIDTH/2, y + Tank2.HEIGHT/2, x + Tank2.WIDTH, y + Tank2.HEIGHT);
		break;
	case D:
		g.drawLine(x + Tank2.WIDTH/2, y + Tank2.HEIGHT/2, x + Tank2.WIDTH/2, y + Tank2.HEIGHT);
		break;
	case LD:
		g.drawLine(x + Tank2.WIDTH/2, y + Tank2.HEIGHT/2, x, y + Tank2.HEIGHT);
		break;
	}
	g.setColor(c);
	if(good) { bb.draw(g);cb.draw(g);}
	move();
}


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(this.dir != Direction.STOP) {
		this.ptDir = this.dir;
	}
	
	if(x < 0) x = 0;
	if(y < 30) y = 30;
	if(x + Tank2.WIDTH > TankClient2.GAME_WIDTH) x = TankClient2.GAME_WIDTH - Tank2.WIDTH;
	if(y + Tank2.HEIGHT > TankClient2.GAME_HEIGHT) y = TankClient2.GAME_HEIGHT - Tank2.HEIGHT;
	
	if(!good) {
		Direction[] dirs = Direction.values();
		if(step == 0) {
			step = r.nextInt(12) + 3;
			int rn = r.nextInt(dirs.length);
			dir = dirs[rn];
		}			
		step --;
		
		if(r.nextInt(40) > 38) this.fire();
	}		
}

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

public void keyPressed(KeyEvent e) {
	int key = e.getKeyCode();
	switch(key) {
	case KeyEvent.VK_LEFT :
		bL = true;
		break;
	case KeyEvent.VK_UP :
		bU = true;
		break;
	case KeyEvent.VK_RIGHT :
		bR = true;
		break;
	case KeyEvent.VK_DOWN :
		bD = true;
		break;
	
	case KeyEvent.VK_F2 :
		if(!this.live) {
			this.live = true;
			this.life = 100;
			this.sk
  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值