当前位置:网站首页>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); } }
边栏推荐
- Common interview questions of network and concurrent programming
- * Jetpack 笔记 Room 的使用
- [golang] leetcode - 292 Nim games (Mathematics)
- [c language] output the students within the specified score range with the structure
- 使用mysql判断日期是星期几
- 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
- 基于TI AM5728 + Artix-7 FPGA开发板(DSP+ARM) 5G通信测试手册
- BigDecimal基本使用与闭坑介绍
- 实现可以继续上局
- 己方坦克发射子弹
猜你喜欢
制造出静态坦克

使用Visdom對損失函數進行監控

Non recursive traversal of binary tree
![[C语言]用结构体把输入的指定分数范围内的学生输出](/img/40/cbd7fe5aafbaeb6237e6d257455e5e.png)
[C语言]用结构体把输入的指定分数范围内的学生输出

排序的循环链表
Mysql深入完全学习---阶段1---学习总述
![[c language] output the students within the specified score range with the structure](/img/40/cbd7fe5aafbaeb6237e6d257455e5e.png)
[c language] output the students within the specified score range with the structure

2023年西安交通大学管理学院MPAcc提前批面试网报通知

Surveillance des fonctions de perte avec visdom

On the sequence traversal of binary tree Ⅱ
随机推荐
MATLAB 保存imshow绘制图片到指定文件夹中的两种方法
Force deduction 33 questions, search rotation sorting array
Niuke brush questions part7
Quanzhi technology T3 development board (4-core arm cortex-a7) - mqtt communication protocol case
[c language] output the students within the specified score range with the structure
实现可以继续上局
[c language] output the average score and the student data below or equal to the average score with the structure
Some problems of DC-DC bootstrap capacitor
全志科技T3开发板(4核ARM Cortex-A7)——视频开发案例
论工作流选型
用户信息管理的功能开发
力扣刷题——根据二叉树创建字符串
H.264概念
[C语言]用结构体按分数高低降序输出学生的姓名和分数
非递归实现二叉树的前、中、后序遍历
* Jetpack 笔记 LifeCycle ViewModel 与LiveData的了解
EasyCwmp源码分析
Common interview questions of network and concurrent programming
牛客刷题——两种排序方法
[Golang]力扣Leetcode - 292. Nim 游戏(数学)