目录
1.敌人坦克发射子弹
1.功能分析
让敌人的坦克也能够发射多颗子弹
- 在敌人坦克类,使用Vector保存多个Shot
- 当每创建一个敌人坦克对象,给该敌人对象初始化一个Shot对象,同时启动Shot
- 在绘制敌人坦克时,需要变量敌人坦克对象Vertor,绘制所有的子弹,当子弹isLive==false时就从Vertor移除
2.代码:
1.EnemyTank类:
java
public class EnemyTank extends Tank {
Vector<Shot> shots=new Vector<>();
boolean isLive=true;
public EnemyTank(int x, int y) {
super(x, y);
}
}
我们在敌人tank类里面增加了一组Vector集合用于存shot类,以及设置一个标记为了识别子弹是否被销毁。
2.MyPanel类
java
for (int e = 0; e < enemyTankSize; e++) {
EnemyTank enemyTank=new EnemyTank(100*(e+1),0);
enemyTank.setDirect(3);
Shot shot = new Shot(enemyTank.getX() + 20, enemyTank.getY() + 60, enemyTank.getDirect());
enemyTank.shots.add(shot);
new Thread(shot).start();
enemyTanks.add(enemyTank);
}
我们在初始化敌人坦克的时候,我们加入一颗子弹。并开启子弹的线程。
java
for (int i=0;i<enemyTanks.size();i++) {
EnemyTank enemyTank = enemyTanks.get(i);
draw_tank(enemyTank.getX(), enemyTank.getY(), g, enemyTank.getDirect(), 1);
//画出子弹
for (int j = 0; j < enemyTank.shots.size(); j++) {
//取出子弹
Shot shot = enemyTank.shots.get(j);
//绘制
if(shot.isLive()){
g.fill3DRect(shot.getX(),shot.getY(),2,2,false);
}
else {
enemyTank.shots.remove(shot);
}
}
}
}
我们在MyPanel类里面的 paint方法里面加了判断子弹,以及绘制子弹动画并移除的语句。
2.敌人消失
1.功能分析:
- 敌人消失,就是让子弹在遇到坦克的时候让enemytank和子弹的标记为false
- 然后在绘制坦克的时候判断标记。
2.代码
1.EnemyTank类:
java
public class EnemyTank extends Tank {
Vector<Shot> shots=new Vector<>();
boolean isLive=true;
public EnemyTank(int x, int y) {
super(x, y);
}
}
在类里面加一行:
java
boolen isLive=true;
这里变量类型你可以自己设置,对于这种我们一般建议用default。
2.MyPanel类
java
public void hitTank(Shot s, EnemyTank enemyTank){
switch (enemyTank.getDirect()){
case 0:
case 2:
if(s.getX()>enemyTank.getX()&&s.getX()<enemyTank.getX()+40
&&s.getY()>enemyTank.getY()&&s.getY()<enemyTank.getY()+60){
s.setLive(false);
enemyTank.isLive=false;
enemyTanks.remove(enemyTank);
}
break;
case 1:
case 3:
if (s.getX()>enemyTank.getX()&&s.getX()<enemyTank.getX()+60
&&s.getY()>enemyTank.getY()&&s.getY()<enemyTank.getY()+40) {
s.setLive(false);
enemyTank.isLive=false;
enemyTanks.remove(enemyTank);
}
break;
}
}
因为我们设置的坦克是矩形的也就说他是一个规则的形状,那么在判断子弹和坦克的接触时,我们可以判断坦克的方向获得坦克的坐标然后和子弹的坐标相匹配。从而得到子弹是否进入了我们坦克的区域然后标记我们的坦克和子弹,并将坦克移除集合。
java
for (int i=0;i<enemyTanks.size();i++) {
EnemyTank enemyTank = enemyTanks.get(i);
if(enemyTank.isLive){//确定坦克被击中
draw_tank(enemyTank.getX(), enemyTank.getY(), g, enemyTank.getDirect(), 1);
//画出子弹
for (int j = 0; j < enemyTank.shots.size(); j++) {
//取出子弹
Shot shot = enemyTank.shots.get(j);
//绘制
if(shot.isLive()){
g.fill3DRect(shot.getX(),shot.getY(),2,2,false);
}
else {
enemyTank.shots.remove(shot);
}
}
}
}
在绘制敌人的坦克的时候我我们加上一层if判断,判断是否还为true,不为true将子弹移除集合。这里我们为了不让子弹线程在移除后继续运行,我们对shot类run方法条件进行修改
3.Shot类
java
if (!(x>=0&&x<=1000&&y>=0&&y<=750)&&isLive) {
isLive=false;
break;
}
在判段条件里加了一个isLive,在确定子弹销毁的时候,就结束子弹线程。
3.爆炸效果
1.功能分析
- 实现爆炸的动态图,我们可以用一张,或多张图,去绘制同一个爆炸的生命周期。、
- 爆炸是紧跟在子弹之后的,所以我们去写hitTank方法。
2.代码
1.Bomb类
java
public class Bomb {
int x,y;
int life=16;//炸弹生命周期
boolean isLive=true;
public Bomb(int x, int y) {
this.x = x;
this.y = y;
}
//减少生命值
public void lifeDown(){
if(life>0){
life--;
}else {
isLive=false;
}
}
}
我们在这里设置生命周期,以及坐标,和标准位
2.MyPanel类
java
Vector<Bomb> bombs=new Vector<>();
Image image1=null;
int enemyTankSize=3;
public MyPanel(){
hero=new Hero(100,100);
hero.setSpeed(1);
//初始化敌人的tank
for (int e = 0; e < enemyTankSize; e++) {
EnemyTank enemyTank=new EnemyTank(100*(e+1),0);
enemyTank.setDirect(3);
Shot shot = new Shot(enemyTank.getX() + 20, enemyTank.getY() + 60, enemyTank.getDirect());
enemyTank.shots.add(shot);
new Thread(shot).start();
enemyTanks.add(enemyTank);
}
//初始化
image1=Toolkit.getDefaultToolkit().getImage("./bomb1.gif");
}
在初始化的时候,增加了一个bombs集合来存储爆炸的效果,以及设置一次爆炸所需要的gif图片,当然也可以是两个,三个去做这个爆炸效果。
java
public void hitTank(Shot s, EnemyTank enemyTank){
switch (enemyTank.getDirect()){
case 0:
case 2:
if(s.getX()>enemyTank.getX()&&s.getX()<enemyTank.getX()+40
&&s.getY()>enemyTank.getY()&&s.getY()<enemyTank.getY()+60){
s.setLive(false);
enemyTank.isLive=false;
Bomb bomb=new Bomb(enemyTank.getX(), enemyTank.getY());
bombs.add(bomb);
enemyTanks.remove(enemyTank);
}
break;
case 1:
case 3:
if (s.getX()>enemyTank.getX()&&s.getX()<enemyTank.getX()+60
&&s.getY()>enemyTank.getY()&&s.getY()<enemyTank.getY()+40) {
s.setLive(false);
enemyTank.isLive=false;
Bomb bomb=new Bomb(enemyTank.getX(), enemyTank.getY());
bombs.add(bomb);
enemyTanks.remove(enemyTank);
}
break;
}
}
在hitTank方法里面增加一次bomb效果
java
for (int i = 0; i <bombs.size() ; i++) {
Bomb bomb = bombs.get(i);
//根据bomb对象的life值去画出炸弹
if(bomb.life>0){
g.drawImage(image1,bomb.x,bomb.y,60,60,this);
}
bomb.lifeDown();
//life为0
if(bomb.life==0){
bombs.remove(bomb);
}
}
并在paint去绘制图像,完成一个生命周期的图画绘制。然后移除
最终代码:
1.MyPanel类
java
package game;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;
//绘图区
//为了监听键盘事件,实现接口KeyListener
//将子弹变成线程使用
public class MyPanel extends JPanel implements KeyListener,Runnable {
//定义我的坦克
Hero hero=null;
//定义敌人的坦克,放入到vector里面
Vector<EnemyTank> enemyTanks=new Vector<>();
Vector<Bomb> bombs=new Vector<>();
Image image1=null;
int enemyTankSize=3;
public MyPanel(){
hero=new Hero(100,100);
hero.setSpeed(1);
//初始化敌人的tank
for (int e = 0; e < enemyTankSize; e++) {
EnemyTank enemyTank=new EnemyTank(100*(e+1),0);
enemyTank.setDirect(3);
Shot shot = new Shot(enemyTank.getX() + 20, enemyTank.getY() + 60, enemyTank.getDirect());
enemyTank.shots.add(shot);
new Thread(shot).start();
enemyTanks.add(enemyTank);
}
//初始化
image1=Toolkit.getDefaultToolkit().getImage("./bomb1.gif");
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.fillRect(0,0,1000,750);//
//画出坦克-封装方法
draw_tank(hero.getX(),hero.getY(),g,hero.getDirect(),0);
//画出hero射击的子弹
if(hero.shot!=null&&hero.shot.isLive())
{
g.fill3DRect(hero.shot.getX(),hero.shot.getY(),2,2,false);
}
//画炸弹
for (int i = 0; i <bombs.size() ; i++) {
Bomb bomb = bombs.get(i);
//根据bomb对象的life值去画出炸弹
if(bomb.life>0){
g.drawImage(image1,bomb.x,bomb.y,60,60,this);
}
bomb.lifeDown();
//life为0
if(bomb.life==0){
bombs.remove(bomb);
}
}
//画出敌人的坦克
for (int i=0;i<enemyTanks.size();i++) {
EnemyTank enemyTank = enemyTanks.get(i);
if(enemyTank.isLive){//确定坦克被击中
draw_tank(enemyTank.getX(), enemyTank.getY(), g, enemyTank.getDirect(), 1);
//画出子弹
for (int j = 0; j < enemyTank.shots.size(); j++) {
//取出子弹
Shot shot = enemyTank.shots.get(j);
//绘制
if(shot.isLive()){
g.fill3DRect(shot.getX(),shot.getY(),2,2,false);
}
else {
enemyTank.shots.remove(shot);
}
}
}
}
}
//编写方法,画出坦克
/**
*
* @param x 坦克左上角x的坐标
* @param y 坦克的左上角y的坐标
* @param g 画笔
* @param direct 坦克方向
* @param type 坦克类型
*/
public void draw_tank(int x,int y,Graphics g,int direct,int type){
switch (type){
case 0:
g.setColor(Color.CYAN);
break;
case 1:
g.setColor(Color.orange);
break;
}
//根据tank的方向绘制
//根据坦克的方向绘制对应形象的坦克
switch (direct){
case 0://表示向上
g.fill3DRect( x, y,10,60,false);
g.fill3DRect( x+30, y,10,60,false);
g.fill3DRect( x+10, y+10,20,40,false);
g.fillOval(x+10,y+20,20,20);
g.drawLine(x+20,y+30,x+20,y);
break;
case 1://表示向右
g.fill3DRect( x, y,60,10,false);
g.fill3DRect( x, y+30,60,10,false);
g.fill3DRect( x+10, y+10,40,20,false);
g.fillOval(x+20,y+10,20,20);
g.drawLine(x+30,y+20,x+60,y+20);
break;
case 2://表示向左
g.fill3DRect( x, y,60,10,false);
g.fill3DRect( x, y+30,60,10,false);
g.fill3DRect( x+10, y+10,40,20,false);
g.fillOval(x+20,y+10,20,20);
g.drawLine(x+30,y+20,x,y+20);
break;
case 3://表示向下
g.fill3DRect( x, y,10,60,false);
g.fill3DRect( x+30, y,10,60,false);
g.fill3DRect( x+10, y+10,20,40,false);
g.fillOval(x+10,y+20,20,20);
g.drawLine(x+20,y+30,x+20,y+60);
break;
default:
System.out.println("暂时没有处理");
}
}
//编写方法,判断子弹是否击中
public void hitTank(Shot s, EnemyTank enemyTank){
switch (enemyTank.getDirect()){
case 0:
case 2:
if(s.getX()>enemyTank.getX()&&s.getX()<enemyTank.getX()+40
&&s.getY()>enemyTank.getY()&&s.getY()<enemyTank.getY()+60){
s.setLive(false);
enemyTank.isLive=false;
Bomb bomb=new Bomb(enemyTank.getX(), enemyTank.getY());
bombs.add(bomb);
enemyTanks.remove(enemyTank);
}
break;
case 1:
case 3:
if (s.getX()>enemyTank.getX()&&s.getX()<enemyTank.getX()+60
&&s.getY()>enemyTank.getY()&&s.getY()<enemyTank.getY()+40) {
s.setLive(false);
enemyTank.isLive=false;
Bomb bomb=new Bomb(enemyTank.getX(), enemyTank.getY());
bombs.add(bomb);
enemyTanks.remove(enemyTank);
}
break;
}
}
@Override
public void keyTyped(KeyEvent e) {
}
//处理wsad按下的键
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_W)
{
//改变坦克的方向
hero.setDirect(0);
hero.move_up();
} else if (e.getKeyCode()==KeyEvent.VK_D) {
hero.setDirect(1);
hero.move_right();
} else if (e.getKeyCode()==KeyEvent.VK_A) {
hero.setDirect(2);
hero.move_left();
} else if (e.getKeyCode()==KeyEvent.VK_S) {
hero.setDirect(3);
hero.move_down();
}
if(e.getKeyCode()==KeyEvent.VK_J) {
System.out.println("用户按下j");
hero.shotEnemyTank();
}
this.repaint();
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void run() {
while (true) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
//判断是否击中敌人坦克
if(hero.shot!=null&&hero.shot.isLive()){
//遍历敌人所有的坦克
for (int i = 0; i < enemyTanks.size(); i++) {
EnemyTank enemyTank = enemyTanks.get(i);
hitTank(hero.shot,enemyTank);
}
}
this.repaint();
}
}
}
2.Bomb类
java
package game;
public class Bomb {
int x,y;
int life=16;//炸弹生命周期
boolean isLive=true;
public Bomb(int x, int y) {
this.x = x;
this.y = y;
}
//减少生命值
public void lifeDown(){
if(life>0){
life--;
}else {
isLive=false;
}
}
}
3.Shot类
java
package game;
public class Shot implements Runnable{
//子弹的坐标
private int x;
private int y;
private int direct=0;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getDirect() {
return direct;
}
public void setDirect(int direct) {
this.direct = direct;
}
public boolean isLive() {
return isLive;
}
public void setLive(boolean live) {
isLive = live;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
private boolean isLive=true;
public Shot(int x, int y, int direct) {
this.x = x;
this.y = y;
this.direct = direct;
}
private int speed=2;
@Override
public void run() {
while (true){
try {
Thread.sleep(50);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
//根据方向改变
switch (direct){
case 0://上
y-=speed;
break;
case 1://右
x+=speed;
break;
case 2:
x-=speed;
break;
case 3:
y+=speed;
break;
}
System.out.println("x="+x+"y="+y);
//当子弹碰到敌人坦克时结束线程
if (!(x>=0&&x<=1000&&y>=0&&y<=750)&&isLive) {
isLive=false;
break;
}
}
}
}
4.EnemyTank类
java
package game;
import java.util.Vector;
public class EnemyTank extends Tank {
Vector<Shot> shots=new Vector<>();
boolean isLive=true;
public EnemyTank(int x, int y) {
super(x, y);
}
}