對于這個(gè)小游里面的彎跡類的抽象很重要,對坦克及其它類我在這里面就不定義了,其實(shí)J2SE的API里面就有關(guān)于圖形重疊的算法,就是這個(gè)intersects()方法,具體偽代碼如下:

創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),交口企業(yè)網(wǎng)站建設(shè),交口品牌網(wǎng)站建設(shè),網(wǎng)站定制,交口網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,交口網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
public boolean collidesWithTanks(java.util.ListTank tanks) {
for(int i=0; itanks.size(); i++) {
Tank t = tanks.get(i);
if(this != t) {
if(this.live t.isLive() this.getRect().intersects(t.getRect())) {
this.stay();
t.stay();
return true;
}
}
}
return false;
}
您可以根據(jù)自己的實(shí)際需求來改寫,在我的百度文庫里面有關(guān)于“坦克大戰(zhàn)”的所有代碼,如果有需要我可以把代碼發(fā)給你,可以悶鬧雹通過百度HI聯(lián)系螞帆我。
給小球類定義一個(gè)方法:碰撞;然后當(dāng)周圍環(huán)境的坐標(biāo)到球心的距離等于小球的半徑時(shí),小球的運(yùn)動(dòng)路徑算法就應(yīng)該是軸對稱的。先判斷之前的運(yùn)動(dòng)方向,然后根據(jù)運(yùn)動(dòng)方向確定新的運(yùn)動(dòng)方向。這個(gè)其實(shí)就是線性方程做小球的運(yùn)動(dòng)軌跡而已。
import?java.awt.Color;
import?java.awt.Graphics;
import?java.awt.event.WindowAdapter;
import?java.awt.event.WindowEvent;
import?java.util.Random;
import?javax.swing.JFrame;
import?javax.swing.JPanel;
public?class?RunningBallDemo?extends?JFrame?{
public?static?void?main(String?args[])?{
new?悉毀森RunningBallDemo();
}
public?RunningBallDemo()?{
Ball?ballPanel?=?new?Ball(5,?5);
getContentPane().add(ballPanel);
setBackground(Color.BLACK);
addWindowListener(new?WindowAdapter()?{
public?void?windowClosing(WindowEvent?e)?{
System.exit(0);
}
});
setSize(350,?350);
setVisible(true);
Thread?thread1?=?new?Thread(ballPanel);
thread1.start();
}
}
class?Ball?extends?JPanel?implements?Runnable?{
int?rgb?=?0;
Color?color;
int?x,?y;
int?dx?=?5,?dy?=?5;
Ball(int?x,?int?y)?{
this.x?余液=?x;
this.y?=?y;
}
@Override
protected?void?paintComponent(Graphics?g)?{
super.paintComponent(g);
setBackground(Color.BLACK);
g.setColor(color);
g.fillOval(x,?y,?50,?50);
}
public?void?run()?{
while?(true)?{
if?(x?=?0)?{
dx?=?5;
updateBallColor();
}?else?if?((x?+?50)?=?getWidth())?{
dx?=?-5;
updateBallColor();
}
if?(y?=?睜畝0)?{
dy?=?5;
updateBallColor();
}?else?if?((y?+?50)?=?getHeight())?{
dy?=?-5;
updateBallColor();
}
x?=?x?+?dx;
y?=?y?+?dy;
repaint();
try?{
Thread.sleep(25);
}?catch?(InterruptedException?e)?{
;
}
}
}
public?void?updateBallColor()?{
rgb?=?new?Random().nextInt();
color?=?new?Color(rgb);
}
}
下面悄早森那個(gè)方法是想返回什么?如果是當(dāng)前Tank與其他任意Tank有碰撞就返睜氏回
,可以寫成
public boolean collidesWithTanks (ListTank tanks) {
tanks.remove(this);//這里刪除當(dāng)前對象,下面就不用啟畝老判斷了
for (int i = 0; i tanks.size(); i++) {
Tank tank =tanks.get(i);
//if (this != tank ) {
if(this.collidesWithTank(tank))
return true; //如果和任意一個(gè)有碰撞就返回true
}
return false;
}
網(wǎng)站名稱:java運(yùn)動(dòng)碰撞代碼 java 碰撞
網(wǎng)頁地址:http://www.chinadenli.net/article4/ddpeooe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、、品牌網(wǎng)站制作、標(biāo)簽優(yōu)化、網(wǎng)站設(shè)計(jì)、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)