發(fā)射子彈軌跡代碼java 編寫子彈程序

關(guān)于java中拋物線軌跡的模擬

僵尸每次移動,計(jì)算出僵尸的坐標(biāo)再+上僵尸移動的速度差,重新計(jì)算拋物線。經(jīng)過我自主研發(fā),我確定沒有問題。給分我吧,哥們

歷城ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!

關(guān)于java中模擬拋物線軌跡的問題

看了這套題目感覺很有興趣,就花了一個中午親手給你寫了一個類似的例子,相信可以幫助你對這個游戲有很好的理解,從右向左那個是僵尸,點(diǎn)一下鼠標(biāo)就出現(xiàn)植物,我只是起到一個拋磚引玉的作用。代碼如下(絕對可以用的代碼):

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;

//?用三點(diǎn)確定一個拋物線的方法,計(jì)算彈道

int?x1=0;

int?y1=500;

int?x2=(z.x-6*20)/2;

int?y2=300;??//?拋物線高度200個像素

int?x3=z.x-6*20;?//?假設(shè)擊中僵尸用3秒鐘,在這3秒鐘內(nèi)僵尸向前移動了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怎么讓小飛機(jī)連續(xù)射子彈

這種是傳統(tǒng)的Sprite圖塊技術(shù),不用JLabel這樣的已經(jīng)固定用途的“標(biāo)簽”控件...

你需要設(shè)計(jì)一個基本的功能:

比如有6張子彈的各種形態(tài)-激發(fā),爆炸等狀態(tài)的圖,編號0-6,要能按順序播放這個形態(tài)

這種圖塊叫sprite

你可以起名一個子彈束類class Bullet ,每次激發(fā)為一束子彈即一個bullet對象。

記錄下子彈的飛行坐標(biāo),作用范圍,運(yùn)動狀態(tài)和狀態(tài)對應(yīng)的圖片,是否碰撞,是誰發(fā)的子彈

最后還要讓圖形界面能繪制出來,比如Canvas,JPanel的Graphics能根據(jù)bullet的數(shù)據(jù)繪制出圖片,而邏輯處理能判斷出子彈的狀態(tài)是否擊中。

圖形方面最好需要用兩個大的int[]像素塊作為顯示的屏幕,一個int[]在內(nèi)存繪制,一個int[]交給graphic顯示,實(shí)現(xiàn)流暢的雙緩沖。(現(xiàn)在AWT/swing,javafx等的基礎(chǔ)功能已經(jīng)超過一代二代的PS有余...)

總之,知識點(diǎn)很多,有過去40年左右的各種技巧...

分享名稱:發(fā)射子彈軌跡代碼java 編寫子彈程序
文章源于:http://bm7419.com/article2/ddepsoc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、動態(tài)網(wǎng)站、自適應(yīng)網(wǎng)站、App設(shè)計(jì)網(wǎng)站內(nèi)鏈、建站公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營