当前位置:网站首页>Friendly tanks fire bullets
Friendly tanks fire bullets
2022-06-11 18:36:00 【C_ x_ three hundred and thirty】
List of articles
Create a bullet class
To fire a bullet , There must be a bullet class first
- It contains the location of the bullet
- The speed of the bullet
- The direction of the bullet
- The living condition of the bullet
The main point is that we should treat the bullets as a Threads treat , And when the shot goes out of bounds ( Find out the scope of our framework ) Stop the work of the thread when
public class Shot implements Runnable{
int x;
int y;
int direct=0;
int speed=5;
boolean isLive=true;
public Shot(int x, int y, int direct) {
this.x = x;
this.y = y;
this.direct = direct;
}
@Override
public void run() {
while (isLive){
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:
y+=speed;
break;
case 3:
x-=speed;
break;
}
When it hits a tank or crosses the boundary, it will disappear automatically
Thread termination
if(!(x>=0&&x<=1000&&y>=0&&y<=750&&isLive)){
isLive=false;
break;
}
}
}
}
Add bullets to friendly tanks
- To add another event listening event when we press J You can fire bullets when you need them , It is equivalent to starting the bullet thread
- By being in your own tank class , Get the position and direction of your tank, and then you can get the position and direction of the bullets , Then create a new bullet class from your tank class , And start the thread of the bullet to be fired
public class Hero extends Tank { Define a bullet Shot shot=null; public Hero(int x, int y) { super(x, y); } public void shotEnemyTank(){ Get the direction and position of the bullets to be fired according to your tanks This self can first determine the direction of the bullets to be fired according to the direction of the tank on the paper Then it will be easy to implement it by code switch (getDirect()){ case 0: shot=new Shot(getX()+20,getY(),0); break; case 1: shot=new Shot(getX()+60,getY()+20,1); break; case 2: shot=new Shot(getX()+20,getY()+60,2); break; case 3: shot=new Shot(getX(),getY()+20,3); break; } After creating the bullet, don't forget to start the bullet thread finally new Thread(shot).start(); } }
Add event monitoring mechanism for firing bullets
- Still MyPanel Inside keyPressed() Method when you press J Start the bullet thread when
- Here we are , The complete code of the event listening mechanism is as follows
@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); } 🧨🧨🧨 Monitor the firing of bullets if (e.getKeyCode()==KeyEvent.VK_J){ hero.shotEnemyTank(); } 🥽🥽🥽 Do not forget to redraw the panel Otherwise, it is a static effect this.repaint(); }
边栏推荐
- *Jetpack notes understanding of lifecycle ViewModel and livedata
- 学习使用LSTM和IMDB评论数据进行情感分析训练
- 防止敌方坦克重叠
- System learning typescript (V) - joint type
- Tips for using apipost
- [c language] output students' names and scores in descending order of scores with structures
- Quanzhi Technology T3 Development Board (4 Core ARM Cortex - A7) - mqtt Communication Protocol case
- *Use of jetpack notes room
- Sorted circular linked list
- 力扣刷题——根据二叉树创建字符串
猜你喜欢
随机推荐
力扣23题,合并K个升序链表
Niu Ke's questions -- binary search tree and bidirectional linked list
System learning typescript (V) - joint type
开发中必备的文件的上传与下载
[C语言]对一个数组的元素排序后平移元素
牛客刷题——求最小公倍数
高并发架构设计
力扣刷题——二叉树的层序遍历Ⅱ
全志科技T3开发板(4核ARM Cortex-A7)——视频开发案例
牛客刷题——part6
系统的可扩展型
排序的循环链表
The nearest common ancestor of binary tree
Sorted circular linked list
*Use of jetpack notes room
Summary of common mysql/redis interview questions
Oracle高级数据库复习
Specific methods for JS to realize full screen display
TI AM64x——最新16nm处理平台,专为工业网关、工业机器人而生
Apipost精妙使用技巧








