tank.java代碼 java tank程序代碼

java編程, Tank t1 level之間的關(guān)系是什么

t1和t2都是Tank類型的對象。

創(chuàng)新互聯(lián)從2013年成立,先為瓊山等服務(wù)建站,瓊山等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為瓊山企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

而level是Tank類中的一個變量,每個Tank對象都有一個level。

可以理解為在一個游戲中,每個坦克都有一個等級,t1和t2是坦克的名字。

樓上說錯了,level是Tank中的變量,不是t1中的方法

在java編寫坦克大戰(zhàn)游戲時,如何判斷兩輛坦克不能重疊運(yùn)動,有什么簡單的算法

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

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)系我。

這坦克可以上下左右移動!但是不能轉(zhuǎn)向!他的炮筒一直是朝上的! 說的詳細(xì)的加分!代碼得打出來

package Tank;

/*

* 坦克 類

*/

//導(dǎo)入相關(guān)的包

import java.awt.* ;

import java.awt.event.* ;

import java.util.List ;

import java.util.* ;

public class Tank

{

public static final int WIDTH = 30 ; //坦克的寬度

public static final int HEIGHT = 30 ; //坦克的高度

public static Random r = new Random(); //隨機(jī)數(shù)生成器

int temp = r.nextInt(12) + 3 ; //存放敵軍坦克移動的步數(shù)

int x , y ; //坦克在窗口中出現(xiàn)的位置

int oldX , oldY ; //用來存放坦克移動前的坐標(biāo),也就是坦克的上一步的位置

int speed = 5 ; //坦克的移動速度

private boolean good ; //該輛坦克的好壞,true為我方坦克,false為敵方坦克

private boolean live = true ; //坦克是否活著

private int life = 100 ; //坦克的生命值

private BloodBar bb = new BloodBar() ; //

//用來存放四個方向鍵是否被按下,默認(rèn)情況下都是false

private boolean bL = false ;

private boolean bU = false ;

private boolean bR = false ;

private boolean bD = false ;

Direction tankDir = Direction.STOP ; //坦克的方向,默認(rèn)是STOP

Direction ptDir = Direction.R ; //坦克炮筒的方向, 默認(rèn)是向右

public static ListMissile missiles = new ArrayListMissile(); //用來存放屏幕上打出去的子彈

//坦克的構(gòu)造方法

public Tank(int x , int y , boolean good)

{

//初始化坦克的位置 , 坦克的好壞

this.x = x ;

this.y = y ;

this.good = good ;

oldX = x ;

oldY = y ;

}

//畫出這輛坦克 的 方法

public void draw(Graphics g)

{

if (!getLive()) //如果坦克活著才畫,坦克死了就不畫了

{

return ;

}

if (good)

{

bb.draw(g);

}

Color c = g.getColor() ; //拿到默認(rèn)畫筆的顏色

//判斷,如果是好坦克,將畫筆顏色設(shè)置成藍(lán)色,如果是壞坦克,將畫筆顏色設(shè)置成灰色

if (good)

{

g.setColor(Color.BLUE);

}

else

{

g.setColor(Color.GRAY);

}

g.fillOval(x, y, WIDTH, HEIGHT); //畫實(shí)心圓,用來表示坦克

g.setColor(Color.BLACK);

//根據(jù)炮筒的方向,畫出坦克的炮筒

if (tankDir != Direction.STOP)

{

ptDir = tankDir ;

}

if (ptDir == Direction.L)

{

g.drawLine(x + WIDTH/2, y + HEIGHT/2, x, y + HEIGHT/2);

}

else if (ptDir == Direction.LU)

{

g.drawLine(x + WIDTH/2, y + HEIGHT/2, x, y);

}

else if (ptDir == Direction.U)

{

g.drawLine(x + WIDTH/2, y + HEIGHT/2, x + WIDTH/2, y);

}

else if (ptDir == Direction.RU)

{

g.drawLine(x + WIDTH/2, y + HEIGHT/2, x + WIDTH, y);

}

else if (ptDir == Direction.R)

{

g.drawLine(x + WIDTH/2, y + HEIGHT/2, x + WIDTH, y + HEIGHT/2);

}

else if (ptDir == Direction.RD)

{

g.drawLine(x + WIDTH/2, y + HEIGHT/2, x + WIDTH, y + HEIGHT);

}

else if (ptDir == Direction.D)

{

g.drawLine(x + WIDTH/2, y + HEIGHT/2, x + WIDTH/2, y + HEIGHT);

}

else if (ptDir == Direction.LD)

{

g.drawLine(x + WIDTH/2, y + HEIGHT/2, x, y + HEIGHT);

}

g.setColor(c); //將畫筆的顏色設(shè)置回默認(rèn)的畫筆顏色

move(); //每次畫完坦克,都根據(jù)方向移動一下坦克

}

public void stay()

{

x = oldX ;

y = oldY ;

}

//坦克開火方法

public void fire()

{

if (!this.getLive())

{

return ;

}

Missile m ;

if (good)

{

m = new Missile(x + WIDTH/2 - Missile.WIDTH/2 , y + HEIGHT/2 - Missile.HEIGHT/2 , true , ptDir);

}

else

{

m = new Missile(x + WIDTH/2 - Missile.WIDTH/2 , y + HEIGHT/2 - Missile.HEIGHT/2 , false , ptDir);

}

missiles.add(m) ;

}

public void fire (Direction dir)

{

if (!this.getLive())

{

return ;

}

Missile m = new Missile(x + WIDTH/2 - Missile.WIDTH/2 , y + HEIGHT/2 - Missile.HEIGHT/2 , true , dir);

missiles.add(m) ;

}

public void superFire()

{

Direction[] dirs = Direction.values() ;

for (int x=0 ; xdirs.length - 1 ; x++)

{

this.fire(dirs[x]);

}

}

//根據(jù)坦克的方向,移動坦克

public void move()

{

oldX = x ;

oldY = y ;

if (tankDir == Direction.L)

{

x = x - speed ;

}

else if (tankDir == Direction.LU)

{

x = x - speed ;

y = y - speed ;

}

else if (tankDir == Direction.U)

{

y = y - speed ;

}

else if (tankDir == Direction.RU)

{

x = x + speed ;

y = y - speed ;

}

else if (tankDir == Direction.R)

{

x = x + speed ;

}

else if (tankDir == Direction.RD)

{

x = x + speed ;

y = y + speed ;

}

else if (tankDir == Direction.D)

{

y = y + speed ;

}

else if (tankDir == Direction.LD)

{

x = x - speed ;

y = y + speed ;

}

//只要坦克出界,那么坦克就回到上一步

if (x0 || y30 || (x+WIDTH) GameFrame.WIDTH || (y+HEIGHT) GameFrame.HEIGHT)

{

stay();

}

//判斷坦克碰撞

for (int x=0 ; xGameFrame.tanks.size() ; x++)

{

Tank t = GameFrame.tanks.get(x) ;

if (this != t)

{

if (this.getRect().intersects(t.getRect()))

{

this.stay();

t.stay();

}

}

}

temp-- ;

if (!good)

{

Direction[] dirs = Direction.values() ;

if (temp == 0)

{

temp = r.nextInt(12) + 3 ;

this.tankDir = dirs[r.nextInt(dirs.length)] ;

}

if (r.nextInt(50) 45)

{

this.fire();

}

}

}

//是否撞墻

public boolean hitWalls(Wall w)

{

for (int x=0 ; xGameFrame.tanks.size() ; x++)

{

Tank t = GameFrame.tanks.get(x) ;

if (t.getRect().intersects(w.getRect()))

{

t.stay();

return true ;

}

}

return false ;

}

public boolean hitWall(Wall w)

{

if (this.getRect().intersects(w.getRect()))

{

this.stay();

return true ;

}

return false ;

}

public void eat(Blood b)

{

if (good this.getRect().intersects(b.getRect()) b.getLive())

{

if (this.getLife() = 70 this.getLife() 0)

{

this.setLife(this.getLife() + 30) ;

}

else

{

this.setLife(100);

}

b.setLive(false);

}

}

//根據(jù)方向鍵是否被按下,設(shè)置坦克的方向

public void direction()

{

if (!bL !bU !bR !bD)

{

tankDir = Direction.STOP ;

}

else if (bL !bU !bR !bD)

{

tankDir = Direction.L ;

}

else if (bL bU !bR !bD)

{

tankDir = Direction.LU ;

}

else if (!bL bU !bR !bD)

{

tankDir = Direction.U ;

}

else if (!bL bU bR !bD)

{

tankDir = Direction.RU ;

}

else if (!bL !bU bR !bD)

{

tankDir = Direction.R ;

}

else if (!bL !bU bR bD)

{

tankDir = Direction.RD ;

}

else if (!bL !bU !bR bD)

{

tankDir = Direction.D ;

}

else if (bL !bU !bR bD)

{

tankDir = Direction.LD ;

}

}

//按鍵按下時的處理

public void keyPressed(KeyEvent e)

{

int key = e.getKeyCode() ;

switch (key)

{

case KeyEvent.VK_LEFT :

bL = true ;

break ;

case KeyEvent.VK_UP :

bU = true ;

break ;

case KeyEvent.VK_RIGHT :

bR = true ;

break ;

case KeyEvent.VK_DOWN :

bD = true ;

break ;

}

direction(); //設(shè)置坦克的方向

}

//按鍵抬起時的處理

public void keyReleased(KeyEvent e)

{

int key = e.getKeyCode() ;

switch (key)

{

case KeyEvent.VK_X :

if (!this.getLive())

{

this.setLive(true);

this.setLife(100);

}

break;

case KeyEvent.VK_C :

GameFrame.addTanks();

break ;

case KeyEvent.VK_Z :

superFire();

break ;

case KeyEvent.VK_CONTROL :

fire();

break ;

case KeyEvent.VK_LEFT :

bL = false ;

break ;

case KeyEvent.VK_UP :

bU = false ;

break ;

case KeyEvent.VK_RIGHT :

bR = false ;

break ;

case KeyEvent.VK_DOWN :

bD = false ;

break ;

}

direction(); //設(shè)置坦克的方向

}

//用來碰撞檢測的方法

public Rectangle getRect()

{

return new Rectangle(x , y , WIDTH , HEIGHT);

}

//得到坦克的好壞

public boolean getGood()

{

return good ;

}

//設(shè)置坦克的生死

public void setLive(boolean live)

{

this.live = live ;

}

//得到坦克的生死

public boolean getLive()

{

return live ;

}

public void setLife(int life)

{

this.life = life ;

}

public int getLife()

{

return life ;

}

private class BloodBar

{

public void draw(Graphics g)

{

Color c = g.getColor() ;

g.setColor(Color.RED);

g.drawRect(x, y-10, WIDTH, 10);

int w = WIDTH * life / 100 ;

g.fillRect(x, y-10, w, 10);

g.setColor(c);

}

}

}

網(wǎng)站欄目:tank.java代碼 java tank程序代碼
當(dāng)前鏈接:http://bm7419.com/article42/ddccpec.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、響應(yīng)式網(wǎng)站、全網(wǎng)營銷推廣、品牌網(wǎng)站制作、網(wǎng)站改版、微信公眾號

廣告

聲明:本網(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)站建設(shè)網(wǎng)站維護(hù)公司