您的位置:首页 > 编程语言 > Java开发

基于JAVA的简易坦克大战(七)

2013-12-05 09:47 190 查看
4.2.3 炮弹类算法

1、炮弹类实现了炮弹的draw方法和子弹的move方法。

2、Draw方法是通过调用JAVA封装的内部方法来实现的。Move方法是根据坦克炮筒的方向和子弹的速度来改变子弹的坐标。再通过坦克大战管理类的线程重画来实现子弹的动态移动效果的。

3、炮弹类设计源码:
import java.awt.*;
public class Shell {

private static final int Speech = 10;//子弹速度
private static final int width = 10;
private static final int height = 10;//子弹图形区域宽高
private int x;
private int y;//子弹坐标
private boolean good;//子弹敌我识别
private boolean live = true;
public boolean isLive() {
return live;
}
public void setLive(boolean live) {
this.live = live;
}
public boolean isGood() {
return good;
}
public void setGood(boolean good) {
this.good = good;
}
private TankClient tc = null;
Tank.Direction sDir = null;
public static void main(String[] args) {
// TODO Auto-generated method stub

}
public Shell(int x,int y,boolean good,Tank.Direction sDir,TankClient tc){
this.x = x;
this.y = y;
this.good = good;
this.sDir = sDir;
this.tc = tc;
}
public void drawShell(Graphics g){
Color c = g.getColor();
g.setColor(Color.WHITE);
g.fillOval(x, y, width, height);
g.setColor(c);

move();
}
//有方向弹药就会移动
public void move(){
switch(sDir){
case L:x -= Speech;break;
case LU:x -= Speech;y -= Speech;break;
case LD:x -= Speech;y += Speech;break;
case R:x += Speech;break;
case RU:x += Speech;y -= Speech;break;
case RD:x += Speech;y += Speech;break;
case U:y -= Speech;break;
case D:y += Speech;break;
case STOP:
}
if(x < 0 || y < 0 || x > 800 || y >600 || !isLive())
{
tc.shells.remove(this);
setLive(false);
}
}
//获取子弹所占区域
public Rectangle getRect(){
return new Rectangle(x,y,width,height);
}
}


4.2.4 胶囊类算法

1、胶囊类实现了胶囊的draw方法和move方法以及和我方坦克的碰撞检测方法。

2、Draw方法是通过调用JAVA内部封装的方法来实现的。

3、Move方法是通过随机数的方式来判定移动方向的,再根据方向来改变胶囊的坐标,通过坦克大战管理类的线程重画来实现不规则移动效果的。

4、胶囊类型的随机是在坦克大战管理类的paint方法中实现的,每次重画就会在一定范围内随机数,通过随机数来改变胶囊的类型。

5、我方坦克所占的方框范围与胶囊的所占的方框范围重合时,判断我方坦克“吃”掉了胶囊,胶囊属性live = false,此时在线程重画时就不再将胶囊画出,直到再次创建出新的胶囊。

//胶囊类的设计源码:
import java.awt.*;
import java.util.Random;

public class Blood {
private static final int Speech = 15;
private int x = 400;
private int y = 300;
private int number;//胶囊类型
private int oldX;
private int oldY;
private boolean live = true;
private int width = 20;
private int height = 20;
private TankClient tc = null;
enum Direction{L,LU,LD,R,RU,RD,U,D,STOP}
Direction dir = Direction.R;
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public boolean isLive() {
return live;
}
public void setLive(boolean live) {
this.live = live;
}
public Blood(int number,TankClient tc) {
super();
this.number = number;
this.tc = tc;
}

public void drawBlood(Graphics g){
Color c = g.getColor();
if(isLive() && number == 0)//0号胶囊为回血胶囊
{
g.setColor(Color.RED);
g.draw3DRect(x, y, width, height,true);
g.drawString("回血", x + width/2, y + height/2);
}
else if(isLive() && number == 1)//1号胶囊为减血胶囊
{
g.setColor(Color.ORANGE);
g.draw3DRect(x, y, width, height,true);
g.drawString("未知", x + width/2, y + height/2);
}
g.setColor(c);
move();
}
public void move(){
Random rn = new Random();
int bDir = rn.nextInt(9);
oldX = x;
oldY = y;
switch(bDir){
case 0:x -= Speech;break; //L
case 1:x -= Speech;y -= Speech;break;//LU
case 2:x -= Speech;y += Speech;break; //LD
case 3:x += Speech;break; //R
case 4:x += Speech;y -= Speech;break;//RU
case 5:x += Speech;y += Speech;break; //RD
case 6:y -= Speech;break; //U
case 7:y += Speech;break; //D
case 8:stay();break;
}
if(x < 0 || y < 30 || x > 770 || y > 570)
stay();
//碰撞检测
hitCheck();
}
public void stay(){
x = oldX;
y = oldY;
}
public Rectangle getRect(){
return new Rectangle(x,y,width,height);
}
public void hitCheck(){
if(getRect().intersects(tc.myTank.getRect()) && isLive() && number == 0)
{
tc.myTank.setLife(30);
setLive(false);
}
else if(getRect().intersects(tc.myTank.getRect()) && isLive() && number == 1)
{
int tempLife = tc.myTank.getLife();
tc.myTank.setLife(tempLife - 10);
setLive(false);
if(tc.myTank.getLife() == 0)
{
tc.myTank.setLive(false);
tc.myTank.setScore(0);
tc.myTank.setLevel(0);
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: