import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Random;
public class TankClient extends Frame{
//2,创建tank对象
Tank tank = new Tank(300, 300);
//定义敌方坦克集合
ArrayList<dfTank> dflist = new ArrayList<dfTank>();
//定义子弹集合
static ArrayList<Bullet> bulist = new ArrayList<Bullet>();
//定义土墙集合
static ArrayList<Wall> coms = new ArrayList<Wall>();
//定义石砖集合
static ArrayList<metal> metalwall = new ArrayList<metal>();
//定义河流集合
static ArrayList<river> Rilist = new ArrayList<river>();
//定义树木集合
static ArrayList<tree> trlist = new ArrayList<tree>();
//定义家园集合
static ArrayList<home> holist = new ArrayList<home>();
int num = 0;
/*
* update方法主要用于更新画布
*/
@Override
public void update(Graphics g) {
//3,更新画布
/*1,创建一个画布,与窗体同大
*2,得到画布中的染料
*3,使用染料画图
*4,将你所画的图像印刻在窗体中
*/
Image img = this.createImage(Utils.Frame_width, Utils.Frame_height);
Graphics gs = img.getGraphics();
if(tank == null){
Toolkit tk = Toolkit.getDefaultToolkit();
Image img1 = tk.getImage(Tank.class.getResource("Images/gameover.jpg"));
gs.drawImage(img1, 0, 0, null);
g.drawImage(img, 0, 0, null);
return;
}
//显示家园
for(int i = 0; i < holist.size() ; i++){
home h = holist.get(i);
h.draw(gs);
}
//显示河流
for(int i = 0; i < Rilist.size() ; i++){
river r = Rilist.get(i);
//己方坦克撞到河流
Rectangle rd1 = new Rectangle(r.x,r.y,r.width,r.height);
Rectangle rd2 = new Rectangle(tank.x,tank.y,tank.width,tank.height);
if(rd1.intersects(rd2) == true){
tank.x = tank.oldx;
tank.y = tank.oldy;
}
r.draw(gs);
}
//显示石砖
for(int i = 0 ; i < metalwall.size() ; i ++){
metal sz = metalwall.get(i);
//石砖与坦克相撞
Rectangle rd1 = new Rectangle(sz.x,sz.y,sz.width,sz.height);
Rectangle rd2 = new Rectangle(tank.x,tank.y,tank.width,tank.height);
if(rd1.intersects(rd2) == true){
tank.x = tank.oldx;
tank.y = tank.oldy;
}
for(int j = 0 ; j < dflist.size() ; j ++){
dfTank szz = dflist.get(j);
//石砖与敌方坦克相撞
Rectangle rd4 = new Rectangle(szz.x,szz.y,szz.width,szz.height);
if(rd1.intersects(rd4) == true){
szz.x = szz.oldx;
szz.y = szz.oldy;
}
}
sz.draw(gs);
}
//显示土墙
for(int i = 0 ; i < coms.size() ; i ++){
Wall tq = coms.get(i);
//坦克撞到土墙
Rectangle rd1 = new Rectangle(tq.x,tq.y,tq.width,tq.height);
Rectangle rd2 = new Rectangle(tank.x,tank.y,tank.width,tank.height);
if(rd1.intersects(rd2) == true){
tank.x = tank.oldx;
tank.y = tank.oldy;
}
tq.draw(gs);
}
tank.draw(gs);
//呈现敌方坦克
for(int i = 0 ; i < dflist.size() ; i ++){
dfTank df = dflist.get(i);
//敌方坦克撞到己方坦克
Rectangle rd1 = new Rectangle(df.x,df.y,df.width,df.height);
Rectangle rd2 = new Rectangle(tank.x,tank.y,tank.width,tank.height);
if(rd1.intersects(rd2) == true){
df.x = df.oldx;
df.y = df.oldy;
}
for(int j = 0 ; j < Rilist.size() ; j ++){
//敌方坦克撞到河流
river r = Rilist.get(j);
Rectangle rd5 = new Rectangle(r.x,r.y,r.width,r.height);
if(rd5.intersects(rd1) == true){
df.x = df.oldx;
df.y = df.oldy;
}
}
df.draw(gs);
}
//显示树木
for(int i = 0; i < trlist.size() ; i++){
tree t = trlist.get(i);
t.draw(gs);
}
//呈现子弹
for(int i = 0 ; i < bulist.size() ; i ++){
Bullet bu = bulist.get(i);
bu.draw(gs);
if(bu.flag == false){
//判断敌方子弹和我相撞
Rectangle rd1 = new Rectangle(bu.x,bu.y,tank.width,tank.height);
Rectangle rd2 = new Rectangle(tank.x,tank.y,tank.width,tank.height);
Rectangle rd3 = new Rectangle(bu.x,bu.y,tank.width,tank.height);
if(rd1.intersects(rd2) == true){
bulist.remove(i);
if(tank.xl <= 0){
tank = null;
return;
}
tank.xl -= 5;
}
for(int j = 0 ; j < metalwall.size() ; j ++){
//子弹打到石砖
metal sz = metalwall.get(j);
Rectangle rd5 = new Rectangle(sz.x,sz.y,sz.width,sz.height);
if(rd5.intersects(rd3) == true){
bulist.remove(bu);
}
}
for(int j = 0 ; j < Rilist.size() ; j ++){
//子弹打到河流
river sz = Rilist.get(j);
Rectangle rd5 = new Rectangle(sz.x,sz.y,sz.width,sz.height);
if(rd5.intersects(rd3) == true){
bulist.remove(bu);
}
}
for(int j = 0 ; j < holist.size() ; j ++){
//子弹打到家园
home h = holist.get(j);
Rectangle rd5 = new Rectangle(h.x,h.y,h.width,h.height);
if(rd5.intersects(rd3) == true){
tank = null;
return;
}
}
}
else if(bu.flag == true){
Rectangle rd3 = new Rectangle(bu.x,bu.y,tank.width,tank.height);
for(int j = 0 ; j < dflist.size() ; j ++){
//己方子弹打到敌方坦克
dfTank df = dflist.get(j);
Rectangle rd4 = new Rectangle(df.x,df.y,df.width,df.height);
if(rd3.intersects(rd4) == true){
bulist.remove(bu);
dflist.remove(df);
}
}
for(int j = 0 ; j < metalwall.size() ; j ++){
//子弹打到石砖
metal sz = metalwall.get(j);
Rectangle rd5 = new Rectangle(sz.x,sz.y,sz.width,sz.height);
if(rd5.intersects(rd3) == true){
bulist.remove(bu);
}
}
for(int j = 0 ; j < Rilist.size() ; j ++){
//子弹打到河流
river sz = Rilist.get(j);
Rectangle rd5 = new Rectangle(sz.x,sz.y,sz.width,sz.height);
if(rd5.intersects(rd3) == true){
bulist.remove(bu);
}
}
for(int j = 0 ; j < holist.size() ; j ++){
//子弹打到家园
home h = holist.get(j);
Rectangle rd5 = new Rectangle(h.x,h.y,h.width,h.height);
if(rd5.intersects(rd3) == true){
tank = null;
return;
}
}
}
for(int j = 0 ; j < coms.size() ; j ++){
Wall tq = coms.get(j);
//子弹与土墙相撞
Rectangle rd1 = new Rectangle(tq.x,tq.y,tq.width,tq.height);
Rectangle rd2 = new Rectangle(bu.x,bu.y,bu.width,bu.height);
if(rd1.intersects(rd2) == true){
coms.remove(tq);
bulist.remove(bu);
}
}
}
g.drawImage(img, 0, 0, null);
}
/*
* 显示一个窗体:
* 1,继承Frame
* 2,设置窗体属性
*
*/
public TankClient(){
//设置窗体大小
this.setSize(Utils.Frame_width, Utils.Frame_height);
//是否可见
this.setVisible(true);
//不允许改变大小
this.setResizable(false);
//设置背景颜色
this.setBackground(Color.gray);
//设置标题
this.setTitle("坦克大战");
//允许关闭
//内部类
/*1,给窗体添加一个事件
*2,事件接管
*3,获取事件,并处理该事件
*/
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
//终止程序
System.exit(1);
}
});
//添加事件,用于接收用户点击键盘
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if(tank == null){
return;
}
//向上
if(e.getKeyCode() == 87 || e.getKeyCode() == 38){
num = 0;
tank.y -= 10;
//获取工具箱
Toolkit tk = Toolkit.getDefaultToolkit();
//新建图片
tank.img = tk.getImage(Tank.class.getResource("Images/tankU.gif"));
//向下
}else if(e.getKeyCode() == 83 || e.getKeyCode() == 40){
num = 1;
tank.y += 10;
//获取工具箱
Toolkit tk = Toolkit.getDefaultToolkit();
//新建图片
tank.img = tk.getImage(Tank.class.getResource("Images/tankD.gif"));
//向左
}else if(e.getKeyCode() == 65
评论0