Java编写坦克大战-GameObject类+Bullet类+EnemyBullet类

GameObject类

package com.learn.exercise.tanke;

import java.awt.*;

/**游戏父类:抽象类*/
public abstract class GameObject {
    //添加父类变量;实例化对象
    //图片
    public Image img;
    //坐标
    public int x;
    public int y;
    //界面
    public Panel panel;
    //构造函数

    /**
     *
     * @param img
     * @param x
     * @param y
     * @param panel
     */
    public GameObject(String img,int x,int y,Panel panel){
        this.img = Toolkit.getDefaultToolkit().getImage(img);
        this.x = x;
        this.y = y;
        this.panel = panel;
    }


    //抽象类下的构造方法:抽象方法没有方法体但有abstract做修饰
    //绘制方法
    public abstract void paintSelf(Graphics g);
    //检测方法(检测坦克和子弹有没有发生碰撞;返回坦克和子弹的矩形,判断这两个矩形是否相交)
    public abstract Rectangle gerRec();
}

Bullet类

package com.learn.exercise.tanke;

import java.awt.*;
import java.util.ArrayList;

/**
 * 子弹类
 */
public class Bullet extends GameObject{

    //尺寸
    int width = 10;
    int height = 10;
    //速度
    int speed = 7;
    //方向
    Direction direction;

    /**
     * @param img
     * @param x
     * @param y
     * @param panel
     */
    public Bullet(String img, int x, int y, Panel panel,Direction direction) {
        super(img, x, y, panel);
        this.direction = direction;
    }

    //子弹移动方向方法
    public void leftward(){
        x -= speed;
    }
    public void rightward(){
        x += speed;
    }
    public void upward(){
        y -= speed;
    }
    public void downward(){
        y += speed;
    }
    //子弹移动情况方法
    public void go(){
        switch (direction){
            case LEFT:
                leftward();
                break;
            case RIGHT:
                rightward();
                break;
            case UP:
                upward();
                break;
            case DOWN:
                downward();
                break;
        }
        //将碰撞功能添加到移动方法go中
        this.hitWall();
        this.moveToBorder();
        this.hitBase();
    }
    //子弹与敌方坦克碰撞检测的方法
    public void hitBot(){
        ArrayList<Bot> bots = this.panel.botList;
        for (Bot bot:bots){
            if (this.gerRec().intersects(bot.gerRec())){
                //添加爆炸位置
                this.panel.blastList.add(new Blast("",bot.x-34,bot.y-14,this.panel));
                this.panel.botList.remove(bot);
                this.panel.removeList.add(this);
                break;
            }
        }
    }
    /**子弹与墙壁碰撞检测:*/
    public void hitWall(){
        //围墙列表
        ArrayList<Wall>walls = this.panel.wallList;
        //遍历列表
        for (Wall wall:walls){
            //让子弹与每一个围墙进行检测
            if (this.gerRec().intersects(wall.gerRec())){
                //删去围墙和子弹
                this.panel.wallList.remove(wall);
                this.panel.removeList.add(this);
                //结束循环
                break;
            }
        }
    }
    /**子弹与基地碰撞检测:*/
    public void hitBase(){
        //围墙列表
        ArrayList<Base>bases = this.panel.baseList;
        //遍历列表
        for (Base base:bases){
            //让子弹与每一个围墙进行检测
            if (this.gerRec().intersects(base.gerRec())){
                //删去围墙和子弹
                this.panel.baseList.remove(base);
                this.panel.removeList.add(this);
                //结束循环
                break;
            }
        }
    }
    //出界的子弹要删除掉,不然会消耗内存
    public void moveToBorder(){
        if (x<0||x+width>this.panel.getWidth()){
            this.panel.removeList.add(this);
        }
        if (y<0||y+height>this.panel.getHeight()){
            this.panel.removeList.add(this);
        }
    }

    @Override
    public void paintSelf(Graphics g) {
        g.drawImage(img,x,y,null);
        //调用子弹移动方向
        this.go();
        this.hitBot();
        //this.hitWall();
    }

    @Override
    public Rectangle gerRec() {
        return new Rectangle(x,y,width,height);
    }
}

EnemyBullet类

package com.learn.exercise.tanke;

import java.awt.*;
import java.util.ArrayList;

/**
 * 敌方子弹类
 */
public class EnemyBullet extends Bullet{
    /**
     * @param img
     * @param x
     * @param y
     * @param panel
     * @param direction
     */
    public EnemyBullet(String img, int x, int y, Panel panel, Direction direction) {
        super(img, x, y, panel, direction);
    }
    //子弹与我方坦克碰撞的方法
    public void hitPlayer(){
        ArrayList<Tank> players = this.panel.playerList;
        for (Tank player:players){
            if (this.gerRec().intersects(player.gerRec())){
                //添加爆炸位置
                this.panel.blastList.add(new Blast("",player.x-34,player.y-14,this.panel));
                this.panel.playerList.remove(player);
                this.panel.removeList.add(this);
                //当坦克生命值为false坦克不能发射子弹
                player.alive = false;
                break;
            }
        }
    }
    //子弹移动
    public void paintSelf(Graphics g) {
        g.drawImage(img,x,y,null);
        //调用子弹移动方向
        this.go();
        this.hitPlayer();
    }

    public Rectangle gerRec() {
        return new Rectangle(x,y,width,height);
    }

}

该部分小游戏代码若涉及侵权!!!请麻烦联系本人博客私信;看到必有回复及删改!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值