资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

tank.java代码 java tank程序代码

java编程, Tank t1 level之间的关系是什么

t1和t2都是Tank类型的对象。

创新互联从2013年成立,先为琼山等服务建站,琼山等地企业,进行企业商务咨询服务。为琼山企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

而level是Tank类中的一个变量,每个Tank对象都有一个level。

可以理解为在一个游戏中,每个坦克都有一个等级,t1和t2是坦克的名字。

楼上说错了,level是Tank中的变量,不是t1中的方法

在java编写坦克大战游戏时,如何判断两辆坦克不能重叠运动,有什么简单的算法

对于这个小游里面的类的抽象很重要,对坦克及其它类我在这里面就不定义了,其实J2SE的API里面就有关于图形重叠的算法,就是这个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;

}

您可以根据自己的实际需求来改写,在我的百度文库里面有关于“坦克大战”的所有代码,如果有需要我可以把代码发给你,可以通过百度HI联系我。

这坦克可以上下左右移动!但是不能转向!他的炮筒一直是朝上的! 说的详细的加分!代码得打出来

package Tank;

/*

* 坦克 类

*/

//导入相关的包

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(); //随机数生成器

int temp = r.nextInt(12) + 3 ; //存放敌军坦克移动的步数

int x , y ; //坦克在窗口中出现的位置

int oldX , oldY ; //用来存放坦克移动前的坐标,也就是坦克的上一步的位置

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

private boolean good ; //该辆坦克的好坏,true为我方坦克,false为敌方坦克

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

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

private BloodBar bb = new BloodBar() ; //

//用来存放四个方向键是否被按下,默认情况下都是false

private boolean bL = false ;

private boolean bU = false ;

private boolean bR = false ;

private boolean bD = false ;

Direction tankDir = Direction.STOP ; //坦克的方向,默认是STOP

Direction ptDir = Direction.R ; //坦克炮筒的方向, 默认是向右

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

//坦克的构造方法

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() ; //拿到默认画笔的颜色

//判断,如果是好坦克,将画笔颜色设置成蓝色,如果是坏坦克,将画笔颜色设置成灰色

if (good)

{

g.setColor(Color.BLUE);

}

else

{

g.setColor(Color.GRAY);

}

g.fillOval(x, y, WIDTH, HEIGHT); //画实心圆,用来表示坦克

g.setColor(Color.BLACK);

//根据炮筒的方向,画出坦克的炮筒

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); //将画笔的颜色设置回默认的画笔颜色

move(); //每次画完坦克,都根据方向移动一下坦克

}

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]);

}

}

//根据坦克的方向,移动坦克

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);

}

}

//根据方向键是否被按下,设置坦克的方向

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(); //设置坦克的方向

}

//按键抬起时的处理

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(); //设置坦克的方向

}

//用来碰撞检测的方法

public Rectangle getRect()

{

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

}

//得到坦克的好坏

public boolean getGood()

{

return good ;

}

//设置坦克的生死

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);

}

}

}


网站题目:tank.java代码 java tank程序代码
本文来源:http://cdkjz.cn/article/ddccpec.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220