当前位置:网站首页>Dynamic explosion effect
Dynamic explosion effect
2022-06-11 18:37:00 【C_ x_ three hundred and thirty】
List of articles
Defining entity classes
The location of the bomb
The life cycle of the bomb [ It can be understood as the intensity of shock , The explosion intensity changes from strong to weak ]
public class Boom { int x; int y; // The life cycle of the bomb int life=9; boolean isLive=true; public Boom(int x, int y) { this.x = x; this.y = y; } public void lifeTime(){ if(life>0){ life--; }else { isLive=false; } } }
Painting dynamics
- When does it explode ?
- When the bullet hit the tank, it exploded
- How to show the explosion effect
- This requires the method of drawing pictures to start the demonstration , You can find some process diagrams of static bomb explosion , Then when the health of the bomb is lower than 6 Show an effect , When higher than 6. Show another explosion effect
- How to use code to realize ?
- At the beginning, we can initialize a collection to store , When the bullet hits the tank , We just put a bomb in the assembly , And then show the explosion effect
// Define bomb set When the submunition hits the tank, store the bomb Vector<Boom> booms=new Vector<>(); // Define the picture of the explosion Image image1=null; Image image2=null; Image image3=null;// Initialize picture image1=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/b1.jpg")); image2=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/b2.jpg")); image3=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/b3.jpg")); 


You can add your own desired explosion effect according to your preference
🧛*️🧛*️🧛*️ Be careful !!!
The format of the picture must be JPG Format , Otherwise, it will not show
Sketchpad thread
- In order to ensure that it can monitor the explosion effect when the bullet hits the tank at any time
- You should also use the sketchpad as a thread , Has been running , The termination condition is when we click the Cancel button in the window , Thread termination
- So the finished version of the sketchpad code is as follows :
public class Mypanel extends JPanel implements KeyListener,Runnable { // Define my tank Hero hero=null; // Define enemy tanks Vector<EnemyTank> enemyTanks=new Vector<>(); // Initialize the number of enemy tanks int enemySize=3; // Define bomb set When the submunition hits the tank, store the bomb Vector<Boom> booms=new Vector<>(); // Define the picture of the explosion Image image1=null; Image image2=null; Image image3=null; public Mypanel(){ hero = new Hero(100,100);// Initialize your own tank hero.setSpeed(10);// set speed // Initialize enemy tanks And put the bullet And start the thread for (int i = 0; i < enemySize; i++) { // Define a tank EnemyTank enemyTank=new EnemyTank(100*(i+1),0); // Set tank direction enemyTank.setDirect(2); // Set enemy tank speed enemyTank.setSpeed(1); // Define a bullet Shot shot = new Shot(enemyTank.getX()+20,enemyTank.getY()+60,2); // Start the tank thread new Thread(enemyTank).start(); // Add bullets to the enemy tank collection enemyTank.shots.add(shot); // Start bullet thread new Thread(shot).start(); // Add tanks to enemy tanks collection enemyTanks.add(enemyTank); } // Initialize picture image1=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/b1.jpg")); image2=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/b2.jpg")); image3=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/b3.jpg")); } @Override public void paint(Graphics g) { super.paint(g); // Draw the size of the game interface g.fillRect(0,0,1000,750); // Draw your own tank drawTank(hero.getX(),hero.getY(),g,hero.getDirect(),0); // Draw the explosive effect for (int i = 0; i < booms.size(); i++) { Boom boom=booms.get(i); if(boom.life>6){ g.drawImage(image2,boom.x,boom.y,80,80,this); } else if (boom.life>3){ g.drawImage(image1,boom.x,boom.y,80,80,this); } else { g.drawImage(image3,boom.x,boom.y,80,80,this); } // Reduce bomb health With dynamic explosion effect boom.lifeTime(); // If a bomb has a health value of 0 from booms Delete if(boom.life<=0){ booms.remove(boom); } } // Draw enemy tanks for (int i = 0; i < enemyTanks.size(); i++) { EnemyTank enemyTank = enemyTanks.get(i); if(enemyTank.leap){ // Draw enemy tanks drawTank(enemyTank.getX(),enemyTank.getY(),g,enemyTank.getDirect(),1); // Draw enemy bullets for (int j = 0; j < enemyTank.shots.size(); j++) { // Take the bullet first Shot shot=enemyTank.shots.get(j); // Draw a bullet First judge whether the coordinates are out of bounds If the boundary is crossed, the shots Remove from the set Otherwise draw if(!shot.isLive){ enemyTank.shots.remove(j); } else{ g.draw3DRect(shot.x,shot.y,3,3,false); } } } } // Draw your own bullets if(hero.shot!=null&&hero.shot.isLive==true){ g.draw3DRect(hero.shot.x,hero.shot.y,3,3,false); } } /** * * @param x The upper left corner of the tank x coordinate * @param y The upper left corner of the tank y coordinate * @param g paint brush * @param direct Direction * @param type Tank type ( own / The enemy ) */ public void drawTank(int x,int y,Graphics g,int direct,int type){ //0 My tank 1 Enemy tanks switch (type){ case 0: g.setColor(Color.cyan); break; case 1: g.setColor(Color.green); break; } // Direction (w 0 On 1d 2 Right s 3 Next a Left ) switch (direct){ case 0:// g.fill3DRect(x,y,10,60,false);// Left wheel g.fill3DRect(x+30,y,10,60,false);// Right wheel g.fill3DRect(x+10,y+10,20,40,false);// Body frame g.fillOval(x+10,y+20,20,20);// The lid g.drawLine(x+20,y,x+20,y+20);// Barrel break; case 1:// g.fill3DRect(x,y,60,10,false);// Left wheel g.fill3DRect(x,y+30,60,10,false);// Right wheel g.fill3DRect(x+10,y+10,40,20,false);// Body frame g.fillOval(x+20,y+10,20,20);// The lid g.drawLine(x+60,y+20,x+20,y+20);// Barrel break; case 2:// g.fill3DRect(x,y,10,60,false);// Left wheel g.fill3DRect(x+30,y,10,60,false);// Right wheel g.fill3DRect(x+10,y+10,20,40,false);// Body frame g.fillOval(x+10,y+20,20,20);// The lid g.drawLine(x+20,y+20,x+20,y+60);// Barrel break; case 3:// g.fill3DRect(x,y,60,10,false);// Left wheel g.fill3DRect(x,y+30,60,10,false);// Right wheel g.fill3DRect(x+10,y+10,40,20,false);// Body frame g.fillOval(x+20,y+10,20,20);// The lid g.drawLine(x,y+20,x+20,y+20);// Barrel break; } } @Override public void keyTyped(KeyEvent e) { } @Override public void keyPressed(KeyEvent e) { if(e.getKeyCode()==KeyEvent.VK_W){ if(hero.getY()>0){ hero.moveUp(); } hero.setDirect(0); } else if (e.getKeyCode()==KeyEvent.VK_D) { if(hero.getX()+60<1000){ hero.moveRight(); } hero.setDirect(1); } else if (e.getKeyCode()==KeyEvent.VK_S) { if (hero.getY()+60<750){ hero.moveDown(); } hero.setDirect(2); }else if (e.getKeyCode()==KeyEvent.VK_A) { if (hero.getX()>0){ hero.moveLeft(); } hero.setDirect(3); } if (e.getKeyCode()==KeyEvent.VK_J){ hero.shotEnemyTank(); } this.repaint(); } @Override public void keyReleased(KeyEvent e) { } // Determine whether the bullet hit the tank public void hitEnemyTank(Shot shot,EnemyTank enemyTank){ switch (enemyTank.getDirect()){ case 0: case 2: if(shot.x>enemyTank.getX()&&shot.x<enemyTank.getX()+40 &&shot.y>enemyTank.getY()&&shot.y<enemyTank.getY()+60){ shot.isLive=false; enemyTank.leap=false; enemyTanks.remove(enemyTank); Boom boom=new Boom(enemyTank.getX(),enemyTank.getY()); booms.add(boom); } break; case 1: case 3: if(shot.x>enemyTank.getX()&&shot.x<enemyTank.getX()+60 &&shot.y>enemyTank.getY()&&shot.y<enemyTank.getY()+40){ shot.isLive=false; enemyTank.leap=false; enemyTanks.remove(enemyTank); Boom boom=new Boom(enemyTank.getX(),enemyTank.getY()); booms.add(boom); } break; } } @Override public void run() { while (true){ try { Thread.sleep(100); } catch (InterruptedException e) { throw new RuntimeException(e); } if(hero.shot!=null && hero.shot.isLive){ for (int i = 0; i < enemyTanks.size(); i++) { EnemyTank enemyTank=enemyTanks.get(i); hitEnemyTank(hero.shot,enemyTank); } } this.repaint(); } } }
Frame add Sketchpad thread
The foundation of Sketchpad is running in the framework we defined at the beginning , So we should start the sketchpad thread in the framework constructor
public class TankGame03 extends JFrame { private Mypanel mp=null; public static void main(String[] args) { TankGame03 tankGame01 = new TankGame03(); } public TankGame03() { mp=new Mypanel(); Thread thread = new Thread(mp); thread.start(); this.add(mp); this.addKeyListener(mp); this.setSize(1030,810); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } }
边栏推荐
猜你喜欢

Force deduction questions -- create a string based on a binary tree
Mysql深入完全学习---阶段1---学习总述

map和set

为何TI的GPMC并口,更常被用于连接FPGA、ADC?我给出3个理由

JS实现全屏展示的具体方法

* Jetpack 笔记 LifeCycle ViewModel 与LiveData的了解

动态爆炸效果

Signal processing and capture
Mysql从0到1的完全深入学习--阶段二---基础篇

Force deduction 23 questions, merging K ascending linked lists
随机推荐
[c language] output the students with the highest scores with a structure. There can be multiple highest scores
Mysql从0到1的完全深入学习--阶段二---基础篇
牛客刷题——把字符串转换成整数
为何TI的GPMC并口,更常被用于连接FPGA、ADC?我给出3个理由
SAP BTP 上 workflow 和 Business Service 的 project 管理
Async leads to unexpected function results and changes the intention of the original code; await is only valid in async functions and the top level bodies of modules
MMA-Self-defining function
软件测试技术复习
Combination sum of 39 questions
力扣39题组合总和
排序的循环链表
系统的可扩展型
牛客刷题——不要二
Niu Ke's question -- finding the least common multiple
[c language] limit the number of searches and output the maximum value found in the number of internal searches
* Jetpack 笔记 LifeCycle ViewModel 与LiveData的了解
Niuke brush questions part8
己方坦克发射子弹
Niu Ke's questions -- two sorting methods
map和set