java坦克大战爆炸效果_科学网—坦克大战3.1(加入爆炸动画效果,加入敌人多线程移动) - 倪升华的博文...

package com.test3;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Image;

import java.awt.Panel;

import java.awt.Toolkit;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import java.util.Vector;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class MyTankGame2 extends JFrame {

Mypanel mp = null;

public MyTankGame2() {

// TODO 自动生成的构造函数存

mp = new Mypanel();

this.add(mp);

Thread t=new Thread(mp);

t.start();

// 3,在事件源上注册监听

this.addKeyListener(mp);

this.setSize(400, 300);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true);

}

/**

* 监听的实现步骤

*  1,实现相应接口(keylistener,mouselistener···)

*  2,重写接口方法

*  3,在事件源上注册监听

* 4,事件传递靠事件对象(e.getactioncommand().equals())

*/

public static void main(String[] args) {

// TODO 自动生成的方法存根

MyTankGame2 mt = new MyTankGame2();

}

}

// 我的面板

// 1,实现相应接口(keylistener,mouselistener···)

// 2,重写接口方法

class Mypanel extends JPanel implements KeyListener ,Runnable{

// 定义我的坦克

Mytank hero = null;

//定义炸弹集合

Vectorbombs=new Vector();

//定义三张图片

Image image1=null;

Image image2=null;

Image image3=null;

//定义敌人坦克

Vector ets=new Vector();

int enemynumber=3;

int x = 200;

int y = 200;

public Mypanel() {

hero = new Mytank(x, y);

//初始化敌人坦克

for(int i=0;i

EnemyTank et=new EnemyTank((i+1)*50,0);

et.setDirect(2);

et.setColor(0);

Thread t=new Thread(et);

t.start();

ets.add(et);

}

//初始化

image1 =Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/baozha0.jpg"));

image2 =Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/baozha1.jpg"));

image3 =Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/baozha2.jpg"));

}

// 重写paint

public void paint(Graphics g) {

super.paint(g);

g.fillRect(0, 0, 400, 300);

this.drawTank(hero.getX(), hero.getY(), g, this.hero.getDirect(), 1);

//画出子弹

for(int i=0;i

{

Shot myshot=hero.ss.get(i);

if(myshot!=null&&myshot.isLive==true)

{

g.draw3DRect(myshot.x, myshot.y, 2, 2, false);

}

if(myshot.isLive==false){

hero.ss.remove(myshot);

}

}

//画炸弹

for(int i=0;i

{

Bomb b=bombs.get(i);

if(b.life>6){

g.drawImage(image1, b.x,b.y,30, 30, this);

}else if(b.life>3){

g.drawImage(image2, b.x, b.y, 30,30,this);

}else {

g.drawImage(image3, b.x, b.y, 30,30,this);

}

b.lifedown();

if(b.life==0){

bombs.remove(b);

}

}

//画出敌人坦克

for(int i=0;i

EnemyTank et=ets.get(i);

if(et.isLive){

this.drawTank(ets.get(i).getX(), ets.get(i).getY(), g, ets.get(i).getDirect(), 0);

}

}

}

//子弹是否集中敌人坦克

public void hitTank(Shot s,EnemyTank et) {

//判断坦克方向

switch (et.direct) {

case 0:

case 2:

if(s.x>et.x&&s.xet.y&&s.y

{

//击中

//子弹死亡

s.isLive=false;

//敌人死亡

et.isLive=false;

//创建炸弹,放入集合中

Bomb b=new Bomb(et.x,et. y);

bombs.add(b);

}

case 1:

case 3:

if(s.x>et.x&&s.xet.y&&s.y

{

//击中

//子弹死亡

s.isLive=false;

//敌人死亡

et.isLive=false;

Bomb b=new Bomb(et.x,et. y);

bombs.add(b);

}

break;

default:

break;

}

}

// 画出坦克的函数

public void drawTank(int x, int y, Graphics g, int direct, int type) {

switch (type) {

case 0:

g.setColor(Color.cyan);

break;

case 1:

g.setColor(Color.yellow);

break;

}

// 判断方向//坦克方向,0 up 2 down 1 right 3 left

switch (direct) {

// 向上

case 0:

// 画出坦克(封装)

g.fill3DRect(x, y, 5, 30, false);

g.fill3DRect(x + 15, y, 5, 30, false);

g.fill3DRect(x + 5, y + 5, 10, 20, false);

g.fillOval(x + 5, y + 10, 10, 10);

g.drawLine(x + 9, y + 15, x + 9, y);

break;

case 3:

//向左

g.fill3DRect(x, y, 30, 5, false);

g.fill3DRect(x, y+15, 30, 5, false);

g.fill3DRect(x+5, y+5, 20, 10, false);

g.fillOval(x+10, y+5, 10, 10);

g.drawLine(x+15, y+9, x, y+9);

break;

case 2:

// 向下

g.fill3DRect(x, y, 5, 30, false);

g.fill3DRect(x + 15, y, 5, 30, false);

g.fill3DRect(x + 5, y + 5, 10, 20, false);

g.fillOval(x + 5, y + 10, 10, 10);

g.drawLine(x + 9, y + 15, x + 9, y+30);

break;

case 1:

//向右

g.fill3DRect(x, y, 30, 5, false);

g.fill3DRect(x, y+15, 30, 5, false);

g.fill3DRect(x+5, y+5, 20, 10, false);

g.fillOval(x+10, y+5, 10, 10);

g.drawLine(x+15, y+9, x+30, y+9);

break;

}

}

@Override

public void keyTyped(KeyEvent e) {

// TODO 自动生成的方法存根

}

@Override

public void keyPressed(KeyEvent e) {

// TODO 自动生成的方法存根

if (e.getKeyCode() == KeyEvent.VK_S) {

this.hero.setDirect(2);

this.hero.moveDown();

} else if (e.getKeyCode() == KeyEvent.VK_W) {

this.hero.setDirect(0);

this.hero.moveUp();

} else if (e.getKeyCode() == KeyEvent.VK_A) {

this.hero.setDirect(3);

this.hero.moveLeft();

} else if (e.getKeyCode() == KeyEvent.VK_D) {

this.hero.setDirect(1);

this.hero.moveRight();

}

//开火

if (e.getKeyCode() == KeyEvent.VK_J) {

if(this.hero.ss.size()<5){

this.hero.shotEnemy();

}

}

this.repaint();

}

@Override

public void keyReleased(KeyEvent e) {

// TODO 自动生成的方法存根

}

@Override

public void run() {

// TODO 自动生成的方法存根

while (true)

{

try {

Thread.sleep(100);

} catch (InterruptedException e) {

// TODO 自动生成的 catch 块

e.printStackTrace();

}

//判断是否击中

for(int i=0;i

{

Shot myShot=hero.ss.get(i);

if(myShot.isLive){

for(int j=0;j

{

EnemyTank et=ets.get(j);

if(et.isLive){

this.hitTank(myShot, et);

}

}

}

}

this.repaint();

}

}

}

package com.test3;

import java.util.Vector;

class Tank { // 坦克横纵坐标

int x = 0;

int y = 0;

//设置速度

int speed=1;

int color;

public int getColor() {

return color;

}

public void setColor(int color) {

this.color = color;

}

public int getSpeed() {

return speed;

}

public void setSpeed(int speed) {

this.speed = speed;

}

//坦克方向,0 up 2 down 1 right 3 left

int direct=0;

public int getDirect() {

return direct;

}

public void setDirect(int direct) {

this.direct = direct;

}

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 Tank(int x, int y) {

this.x = x;

this.y = y;

}

}

class Mytank extends Tank {

//子弹

Shot s=null;

Vectorss=new Vector();

//向上

public void moveUp() {

y-=speed;

}

public void moveDown() {

y+=speed;

}

public void moveLeft() {

x-=speed;

}

public void moveRight() {

x+=speed;

}

public Mytank(int x, int y) {

super(x, y);

// TODO 自动生成的构造函数存根

}

public void shotEnemy()

{

switch (this.direct) {

case 0:

s=new Shot(x+10, y,0);

ss.add(s);

break;

case 1:

s=new Shot(x+30, y+10,1);

ss.add(s);

break;

case 2:

s=new Shot(x+10, y+30,2);

ss.add(s);

break;

case 3:

s=new Shot(x, y+10,3);

ss.add(s);

break;

}

Thread t=new Thread(s);

t.start();

}

}

//敌人坦克

class EnemyTank extends Tank implements Runnable

{

boolean isLive=true;

public EnemyTank(int x, int y) {

super(x, y);

// TODO 自动生成的构造函数存根

}

@Override

public void run() {

// TODO 自动生成的方法存根

while(true){

try {

Thread.sleep(50);

} catch (InterruptedException e) {

// TODO 自动生成的 catch 块

e.printStackTrace();

}

//坦克移动

switch (this.direct) {

case 0:

for(int i=0 ;i<30;i++){

y-=speed;

try {

Thread.sleep(30);

} catch (InterruptedException e) {

// TODO 自动生成的 catch 块

e.printStackTrace();

}

}

break;

case 1:

for(int i=0 ;i<30;i++){

x+=speed;

try {

Thread.sleep(30);

} catch (InterruptedException e) {

// TODO 自动生成的 catch 块

e.printStackTrace();

}

}

break;

case 2:

for(int i=0 ;i<30;i++){

y+=speed;

try {

Thread.sleep(30);

} catch (InterruptedException e) {

// TODO 自动生成的 catch 块

e.printStackTrace();

}

}

break;

case 3:

for(int i=0 ;i<30;i++){

x-=speed;

try {

Thread.sleep(30);

} catch (InterruptedException e) {

// TODO 自动生成的 catch 块

e.printStackTrace();

}

}

break;

}

//让坦克随机新方向

this.direct=(int)(Math.random()*4);

//判断是否死亡

if(this.isLive==false){

//退出线程

break;

}

}

}

}

//炸弹

class Bomb

{

int x,y;

int life=9;

boolean isLive=true;

public Bomb (int x ,int y) {

this.x=x;

this.y=y;

}

public  void lifedown() {

if(life>0){

life--;

}else{

this.isLive=false;

}

}

}

//子弹

class Shot implements Runnable

{

int x ;

int y ;

int direct;

int speed=2;

boolean isLive=true;

public Shot(int x,int y,int direct) {

// TODO 自动生成的构造函数存根

this.x=x;

this.y=y;

this.direct=direct;

}

@Override

public void run() {

// TODO 自动生成的方法存根

while(true){

try {

Thread.sleep(50);

} catch (InterruptedException e) {

// TODO 自动生成的 catch 块

e.printStackTrace();

}

switch (direct) {

case 0:

y-=speed;

break;

case 1:

x+=speed;

break;

case 2:

y+=speed;

break;

case 3:

x-=speed;

break;

}

System.err.println("子弹坐标"+x+","+y);

//判断子弹是否碰到边缘

if (x<0||x>400||y<0||y>300) {

this.isLive=false;

break;

}

}

}

}

转载本文请联系原作者获取授权,同时请注明本文来自倪升华科学网博客。

链接地址:http://blog.sciencenet.cn/blog-785542-715447.html

上一篇:kmeans算法修正2.0(实现多线程处理后,输出汇总结果)

下一篇:坦克大战3.2(敌人坦克可发子弹,加入边界限制)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值