From dad94bb8106ea3170b6b5047372ca7ea8cef6118 Mon Sep 17 00:00:00 2001 From: msb_64243 <940710867@qq.com> Date: Wed, 10 Aug 2022 21:32:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/tank/Bullet.java | 297 ++++++++++++++++++++------------------- 1 file changed, 150 insertions(+), 147 deletions(-) diff --git a/src/com/tank/Bullet.java b/src/com/tank/Bullet.java index 2160b69..1b38b2e 100644 --- a/src/com/tank/Bullet.java +++ b/src/com/tank/Bullet.java @@ -1,147 +1,150 @@ -package com.tank; - -import java.awt.Graphics; -import java.awt.Rectangle; - -import com.tank.abstractfactory.BaseBullet; - -/** - * 定义子弹类 - * @author leiwei - * - */ -public class Bullet extends BaseBullet{ - - //子弹位置 - private int x; - private int y; - //子弹方向 - private Dir dir; - - //子弹速度 - private static final int SPEED = PropertyMrg.getInt("bulletSpeed"); - - //设置子弹的大小width,height - public static int width = ResouceMgr.bulletD.getWidth(); - - public static int height = ResouceMgr.bulletD.getHeight(); - //定义子弹是否存活 - private boolean living = true; - - TankFrame tf=null; - - private TankGroup group = TankGroup.BAD; - - - Rectangle rect = new Rectangle(); - - - - - public Dir getDir() { - return dir; - } - - public void setDir(Dir dir) { - this.dir = dir; - } - - public Bullet(int x, int y, Dir dir,TankGroup group,TankFrame tf) { - super(); - this.x = x; - this.y = y; - this.dir = dir; - this.tf = tf; - this.group = group; - rect.x = this.x; - rect.y = this.y; - rect.width = width; - rect.height = height; - //将子弹添加子弹队列中 - tf.bullets.add(this); - } - //子弹显示出来 - @Override - public void paint(Graphics p){ - if(!this.living){ - tf.bullets.remove(this); - } - /*//获取画笔原来颜色 - Color color = p.getColor(); - //设置将要画出的子弹颜色 - p.setColor(Color.red); - p.fillOval(x,y,width,height); - //画完子弹后将画笔颜色还原 - p.setColor(color);*/ - //将子弹根据方向画出来 - switch (dir) { - case LEFT: - p.drawImage(ResouceMgr.bulletL, x, y, null); - break; - case UP: - p.drawImage(ResouceMgr.bulletU, x, y, null); - break; - case RIGHT: - p.drawImage(ResouceMgr.bulletR, x, y, null); - break; - case DOWN: - p.drawImage(ResouceMgr.bulletD, x, y, null); - break; - } - - moving(); - } - - - public void moving(){ - //通过判断方向进行子弹的移动。 - switch(dir){ - case LEFT: - x-=SPEED; - break; - case UP: - y-=SPEED; - break; - case RIGHT: - x+=SPEED; - break; - case DOWN: - y+=SPEED; - break; - default: - break; - } - if(x<0 || y<0 || x > TankFrame.GAME_WIDTH || y>TankFrame.GAME_HEIGHT){ - this.living =false; - } - rect.x = this.x; - rect.y = this.y; - } - - @Override - public void collideWith(Tank t) { - if(this.group.equals(t.getGroup())){ - return; - } - - if(this.rect.intersects(t.rect)){ - t.die(); - this.die(); - int eX = t.getX() + Tank.width/2 - Explode.width/2; - int eY = t.getY() + Tank.height/2 - Explode.height/2; - tf.explodes.add(tf.gf.createExplode(eX, eY, tf)); - } - } - - public TankGroup getGroup() { - return group; - } - - public void setGroup(TankGroup group) { - this.group = group; - } - - private void die() { - this.living =false; - } -} +package com.tank; + +import java.awt.Graphics; +import java.awt.Rectangle; + +import com.tank.abstractfactory.BaseBullet; + +/** + * 定义子弹类 + * @author leiwei + * + */ +public class Bullet extends BaseBullet{ + + //子弹位置 + private int x; + private int y; + //子弹方向 + private Dir dir; + + //子弹速度 + private static final int SPEED = PropertyMrg.getInt("bulletSpeed"); + + //设置子弹的大小width,height + public static int width = ResouceMgr.bulletD.getWidth(); + + public static int height = ResouceMgr.bulletD.getHeight(); + //定义子弹是否存活 + private boolean living = true; + + TankFrame tf=null; + + private TankGroup group = TankGroup.BAD; + + + Rectangle rect = new Rectangle(); + + + + + public Dir getDir() { + return dir; + } + + public void setDir(Dir dir) { + this.dir = dir; + } + + public Bullet(int x, int y, Dir dir,TankGroup group,TankFrame tf) { + super(); + this.x = x; + this.y = y; + this.dir = dir; + this.tf = tf; + this.group = group; + rect.x = this.x; + rect.y = this.y; + rect.width = width; + rect.height = height; + //将子弹添加子弹队列中 + tf.bullets.add(this); + } + //子弹显示出来 + @Override + public void paint(Graphics p){ + if(!this.living){ + tf.bullets.remove(this); + } + /*//获取画笔原来颜色 + Color color = p.getColor(); + //设置将要画出的子弹颜色 + p.setColor(Color.red); + p.fillOval(x,y,width,height); + //画完子弹后将画笔颜色还原 + p.setColor(color);*/ + //将子弹根据方向画出来 + switch (dir) { + case LEFT: + p.drawImage(ResouceMgr.bulletL, x, y, null); + break; + case UP: + p.drawImage(ResouceMgr.bulletU, x, y, null); + break; + case RIGHT: + p.drawImage(ResouceMgr.bulletR, x, y, null); + break; + case DOWN: + p.drawImage(ResouceMgr.bulletD, x, y, null); + break; + } + + moving(); + } + + + public void moving(){ + //通过判断方向进行子弹的移动。 + switch(dir){ + case LEFT: + x-=SPEED; + break; + case UP: + y-=SPEED; + break; + case RIGHT: + x+=SPEED; + break; + case DOWN: + y+=SPEED; + break; + default: + break; + } + if(x<0 || y<0 || x > TankFrame.GAME_WIDTH || y>TankFrame.GAME_HEIGHT){ + this.living =false; + } + rect.x = this.x; + rect.y = this.y; + } + + /** + * 碰撞方法 + */ + @Override + public void collideWith(Tank t) { + if(this.group.equals(t.getGroup())){ + return; + } + + if(this.rect.intersects(t.rect)){ + t.die(); + this.die(); + int eX = t.getX() + Tank.width/2 - Explode.width/2; + int eY = t.getY() + Tank.height/2 - Explode.height/2; + tf.explodes.add(tf.gf.createExplode(eX, eY, tf)); + } + } + + public TankGroup getGroup() { + return group; + } + + public void setGroup(TankGroup group) { + this.group = group; + } + + private void die() { + this.living =false; + } +}