「java子弹瞄准」java子弹发射问题
今天给各位分享java子弹瞄准的知识,其中也会对java子弹发射问题进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、关于java中模拟抛物线轨迹的问题
- 2、java窗口中如何实现鼠标点击一下发射一颗子弹
- 3、怎样用java编写跟踪子弹??我们才学到java面向对象编程。
- 4、java的子弹从枪口射出的问题。
- 5、在java中编写坦克大战时这样实现子弹发射击中敌方但敌方和子弹不消失
关于java中模拟抛物线轨迹的问题
看了这套题目感觉很有兴趣,就花了一个中午亲手给你写了一个类似的例子,相信可以帮助你对这个游戏有很好的理解,从右向左那个是僵尸,点一下鼠标就出现植物,我只是起到一个抛砖引玉的作用。代码如下(绝对可以用的代码):
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.event.MouseInputAdapter;
public class PlantsAndZombies extends JFrame {
private static final long serialVersionUID = 1L;
public static final int screenWidth=800;
public static final int screenHeight=600;
Printer printer;
Zombies zombies=new Zombies();
Thread T_Zombies;
Bullet bullet=new Bullet();
Thread T_Bullet;
public PlantsAndZombies(){
this.setSize(new Dimension(screenWidth,screenHeight));
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.addMouseListener(new Shoot(this));
this.setVisible(true);
printer=new Printer( this.getGraphics());
printer.Add(zombies);
printer.Add(bullet);
T_Zombies=new Thread(zombies);
T_Zombies.start();
T_Bullet=new Thread(bullet);
T_Bullet.start();
}
public void Shoot(){
bullet.getTarget(zombies);
}
public static void main(String[] args){
PlantsAndZombies game=new PlantsAndZombies();
}
public void run() {
while(true){
}
}
}
interface Drawable{
void drawMe(Graphics g);
}
class Zombies implements Drawable,Runnable{
public boolean isLive=true;
public int x=PlantsAndZombies.screenWidth;
public int y=500;
public void run() {
while(true){
if(x10){
x-=20;
}else x=PlantsAndZombies.screenWidth;
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void drawMe(Graphics g){
g.drawRect(x,y,20,50);
}
}
class Bullet implements Drawable,Runnable{
private int x=0;
private int y=500;
private Zombies _z;
private float a,b,c;
private float step;
public void getTarget(Zombies z){
_z=z;
// 用三点确定一个抛物线的方法,计算弹道
int x1=0;
int y1=500;
int x2=(z.x-6*20)/2;
int y2=300; // 抛物线高度200个像素
int x3=z.x-6*20; // 假设击中僵尸用3秒钟,在这3秒钟内僵尸向前移动了6*20个像素
int y3=500;
a=(float)((y2-y1)*(x3-x2)-(y3-y2)*(x2-x1))/(float)((x2*x2-x1*x1)*(x3-x2)-(x3*x3-x2*x2)*(x2-x1));
b=(float)((y2-y1)-a*(x2*x2-x1*x1))/(float)(x2-x1);
c=y1-a*x1*x1-b*x1;
step=(float)(x3-x1)/(float)(3*20);
}
public void run() {
while(true){
try {
x+=step;
y=(int)(a*x*x+b*x+c);
if(y500){
_z.isLive=false;
}
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void drawMe(Graphics g) {
g.drawRect(x,y,20,20);
}
}
class Printer extends Thread{
private VectorDrawable v=new VectorDrawable();
private Graphics _g;
public Printer(Graphics g){
_g=g;
this.start();
}
public void Add(Drawable o){
v.add(o);
}
public void run(){
while(true){
_g.clearRect(0,0,PlantsAndZombies.screenWidth,PlantsAndZombies.screenHeight);
for(Drawable o:v){
o.drawMe(_g);
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Shoot extends MouseInputAdapter{
private PlantsAndZombies _adaptee;
public Shoot(PlantsAndZombies adaptee){
_adaptee=adaptee;
}
public void mouseClicked(MouseEvent e) {
_adaptee.Shoot();
}
}
java窗口中如何实现鼠标点击一下发射一颗子弹
监听鼠标动作,
然后控制子弹图片移动。
然后跟目标做碰撞检测。
怎样用java编写跟踪子弹??我们才学到java面向对象编程。
什么意思,是编写游戏吗?从简单说,把所有子弹都加入集合,过一段时间,取出集合中所有元素,移动并判断是否击中目标,如果超出屏幕或击中目标,从集合中移除元素。其中还要用到多线程,如果不用多线程,程序会很卡。
java的子弹从枪口射出的问题。
我感觉是那个sleep的问题,不使用while循环,这种情况,一般的实现方法是
//定时器任务
Timer timer=new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
bulletY--;
repaint();
}
}, 0, 300);//即时开始,每隔0.3秒执行一次
在java中编写坦克大战时这样实现子弹发射击中敌方但敌方和子弹不消失
你是否有对子弹和坦克,这2个类进行完好的设计,你是否应该加一个生命状态的属性来实现此功能,比如加一个boolean islive;在绘图的方法中,绘制g.drawXXX之前,if(islive)一下,再然后在子弹命中的判断中,命中后把子弹的生命值和坦克的生命值都改变,而绘图的方法通过线程不断刷新,自然就消失了!
------最后,希望采纳!毕竟我们纯手打!
java子弹瞄准的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java子弹发射问题、java子弹瞄准的信息别忘了在本站进行查找喔。
发布于:2022-11-27,除非注明,否则均为
原创文章,转载请注明出处。