![](https://img-blog.csdnimg.cn/71a5f511aa2c4cb59de7a8c1465a0a36.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAZ3lqMjAwMDE5OTgxMjAx,size_18,color_FFFFFF,t_70,g_se,x_16)
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);
}
}
该部分小游戏代码若涉及侵权!!!请麻烦联系本人博客私信;看到必有回复及删改!!!