当前位置:网站首页>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(); }
边栏推荐
- 力扣33题,搜索旋转排序数组
- Surveillance des fonctions de perte avec visdom
- 基于TI AM5728 + Artix-7 FPGA开发板(DSP+ARM) 5G通信测试手册
- [c language] output the students within the specified score range with the structure
- 牛客刷题——part7
- 实现可以继续上局
- Use egg Js+mongodb simple implementation of curdapi
- 高性能架构设计
- * Jetpack 笔记 使用DataBinding
- labelme进行图片数据标注
猜你喜欢

牛客刷题——把字符串转换成整数
开发中必备的文件的上传与下载
![[Golang]力扣Leetcode - 292. Nim 游戏(数学)](/img/82/54c3f6be9d08687b42cba0487380f0.png)
[Golang]力扣Leetcode - 292. Nim 游戏(数学)

ubuntu 安装psql以及运行相关命令
![[c language] compress strings and add markup characters](/img/b7/f7918f3ee0c409faffc70addd5ee65.png)
[c language] compress strings and add markup characters

牛客刷题——Fibonacci数列

Specific methods for JS to realize full screen display

Niu Ke's questions -- binary search tree and bidirectional linked list

使用mysql判断日期是星期几
![[C语言]用结构体按分数高低降序输出学生的姓名和分数](/img/41/b9dba88941560b296f4d7153b7c684.png)
[C语言]用结构体按分数高低降序输出学生的姓名和分数
随机推荐
学习使用LSTM和IMDB评论数据进行情感分析训练
[c language] output the average score and the student data below or equal to the average score with the structure
Niuke's brush question -- judgment of legal bracket sequence
为何TI的GPMC并口,更常被用于连接FPGA、ADC?我给出3个理由
Force buckle 34 finds the first and last positions of elements in a sorted array
SAP UI5 里 XML 视图根节点运行时实例化的分析
排序的循环链表
BigDecimal基本使用与闭坑介绍
Why is ti's GPMC parallel port more often used to connect FPGA and ADC? I give three reasons
* Jetpack 笔记 LifeCycle ViewModel 与LiveData的了解
Force deduction 33 questions, search rotation sorting array
Niuke brush questions part8
牛客刷题——Fibonacci数列
The HashSet collection stores student objects and traverses
. Net core redis hyperloglog type
TR-069 protocol introduction
The nearest common ancestor of binary tree
合并多棵二叉搜索树
Niu Ke swipes the question -- converting a string to an integer
使用Transformers将TF模型转化成PyTorch模型