计科(智能)22-1孙梓容 202203200019
一.序言
通过Java代码编写,实现对小游戏坦克大战的复刻,主要负责对坦克类,子弹类,块类等内部元素的程序编写
二.运行图及所负责模块
三.设计思路
1.设计坦克父类,并通过继承,完成对敌我双方坦克参数的设置,并设计敌方的生成,运行,攻击确保游戏的运行
2.设计子弹类,确定子弹参数,根据子弹攻击力对子弹种类进行区分,并与生命值,墙体种类等参数结合,是子弹程序运行并传递信息,进行判定
3.设计墙体类,对墙体进行分类,为不同的墙体设置不同参数
4.设置营地类,作为玩家的保护目标和敌方破坏后胜利的条件
四.具体代码
1.Tank.java坦克父类
package TankWarrrrrrr;
import java.awt.*;
import java.util.Vector;
public class Tank{
//坦克类别常量
public final static int TANK_KIND_HERO = 0; // 我方坦克
public final static int TANK_KIND_ENEMY = 1; // 敌方坦克
//坦克颜色常量
public final static Color TANK_COLOR_HERO = Color.YELLOW; // 我方坦克颜色
public final static Color TANK_COLOR_ENEMY = Color.CYAN; // 敌方坦克颜色
//坦克方向常量
public final static int TANK_DIRECTION_UP = 0; // 向上
public final static int TANK_DIRECTION_RIGHT = 1; // 向右
public final static int TANK_DIRECTION_DOWN = 2; // 向下
public final static int TANK_DIRECTION_LEFT = 3; // 向左
//坦克状态常量
public final static int TANK_STATE_FREE = 0; // 空闲中
public final static int TANK_STATE_RUNNING = 1; // 运行中
//坦克最大血量值
public final static int TANK_MAX_BLOOD = 100;
//坦克尺寸常量
public final static int TANK_WIDTH = 40;
public final static int TANK_HEIGHT = 40;
private int x; //坦克左上角X坐标
private int y; //坦克左上角Y坐标
private int speed = 8; //坦克速度
private int hp = TANK_MAX_BLOOD; //坦克血量
private int direction = TANK_DIRECTION_UP; //坦克方向
private int tankKind = TANK_KIND_HERO; //坦克类别
private int state = TANK_STATE_FREE; //坦克状态
//子弹池(考虑线程安全用Vector,没用ArrayList)
private Vector<Bullet> bulletPool = new Vector<>();
//上次射击时间(用来设置2次射击最小时间间隔,禁止连续射击)
private long lastShootTime = System.currentTimeMillis();
public Tank(int tankKind, int x, int y, int direction){
this.tankKind = tankKind;
this.x = x;
this.y = y;
this.direction = direction;
}
public void draw(Graphics g){
// 绘制血条
g.setColor(Color.YELLOW);
g.fill3DRect(this.x,this.y,TANK_WIDTH,3,false); // 底色
g.setColor(Color.RED);
g.fill3DRect(this.x,this.y,(hp * TANK_WIDTH)/TANK_MAX_BLOOD,3,false); // 血量(计算宽度时由于都是int类型,因此除法是放到最后计算的,防止出现结果为0的问题【int取整了】)
g.setColor(Color.WHITE);
int tankToBloodHeight = 5; // 坦克到血条的高度
// 设置坦克颜色
if(this.tankKind == TANK_KIND_HERO) { // 我方坦克颜色
g.setColor(TANK_COLOR_HERO);
}
else { // 敌方坦克颜色
g.setColor(TANK_COLOR_ENEMY);
}
// 根据方向开始绘制坦克
switch (direction){
case TANK_DIRECTION_UP:
g.fill3DRect(this.x + tankToBloodHeight,this.y + tankToBloodHeight,7,30,false); // 左边轮子
g.fill3DRect(this.x + 23 + tankToBloodHeight,this.y + tankToBloodHeight,7,30,false); // 右边轮子
g.fill3DRect(this.x + 7 + tankToBloodHeight,this.y + 5 + tankToBloodHeight,16,20,false); // 驾驶室
g.fillOval(this.x + 9 + tankToBloodHeight,this.y + 9 + tankToBloodHeight,12,12); // 炮台
g.fill3DRect(x + 14 + tankToBloodHeight,y + tankToBloodHeight,3,15,false); // 炮管
break;
case TANK_DIRECTION_RIGHT:
g.fill3DRect(this.x + tankToBloodHeight,this.y + tankToBloodHeight,30,7,false); // 上边轮子
g.fill3DRect(this.x + tankToBloodHeight,this.y + 23 + tankToBloodHeight,30,7,false); // 下边轮子
g.fill3DRect(this.x + 5 + tankToBloodHeight,this.y + 7 + tankToBloodHeight,20,16,false); // 驾驶室
g.fillOval(this.x + 9 + tankToBloodHeight,this.y + 9 + tankToBloodHeight,12,12); // 炮台
g.fill3DRect(x + 15 + tankToBloodHeight,y + 14 + tankToBloodHeight,15,3,false); // 炮管
break;
case TANK_DIRECTION_DOWN:
g.fill3DRect(this.x + tankToBloodHeight,this.y + tankToBloodHeight,7,30,false); // 左边轮子
g.fill3DRect(this.x + 23 + tankToBloodHeight,this.y + tankToBloodHeight,7,30,false); // 右边轮子
g.fill3DRect(this.x + 7 + tankToBloodHeight,this.y + 5 + tankToBloodHeight,16,20,false); // 驾驶室
g.fillOval(this.x + 9 + tankToBloodHeight,this.y + 9 + tankToBloodHeight,12,12); // 炮台
g.fill3DRect(x + 14 + tankToBloodHeight,y + 15 + tankToBloodHeight,3,15,false); // 炮管
break;
case TANK_DIRECTION_LEFT:
g.fill3DRect(this.x + tankToBloodHeight,this.y + tankToBloodHeight,30,7,false); // 上边轮子
g.fill3DRect(this.x + tankToBloodHeight,this.y + 23 + tankToBloodHeight,30,7,false); // 下边轮子
g.fill3DRect(this.x + 5 + tankToBloodHeight,this.y + 7 + tankToBloodHeight,20,16,false); // 驾驶室
g.fillOval(this.x + 9 + tankToBloodHeight,this.y + 9 + tankToBloodHeight,12,12); // 炮台
g.fill3DRect(x + tankToBloodHeight,y + 14 + tankToBloodHeight,15,3,false); // 炮管
break;
}
}
//坦克移动,direction为方向
public void move(int direction){
if(this.direction == direction) { // 方向相同,加速前进(要判断边界及是否碰撞到别的对象)
if(!GameLogic.getInstance().tankMoveCollide(this)) { // 没有碰撞,可以移动
switch (direction){
case TANK_DIRECTION_UP:
this.y -= this.speed;
break;
case TANK_DIRECTION_RIGHT:
this.x += this.speed;
break;
case TANK_DIRECTION_DOWN:
this.y += this.speed;
break;
case TANK_DIRECTION_LEFT:
this.x -= this.speed;
break;
}
}
}
else { // 方向不同,仅调整方向不前进
this.direction = direction;
}
}
//坦克射击
public void shoot(){
// 禁止连续射击
long curShootTime = System.currentTimeMillis();
if((curShootTime - this.lastShootTime) >= 500) { // 2发/秒
this.lastShootTime = curShootTime;
}
else{
return;
}
// 在子弹池中寻找空闲的子弹
Bullet bullet = null;
for (int i = 0; i < bulletPool.size(); i++){
Bullet tmpBullet = bulletPool.get(i);
if(tmpBullet.getState() == Bullet.BULLET_STATE_FREE) {
bullet = tmpBullet;
break;
}
}
// 没有就增加一个
if(bullet == null){
bullet = new Bullet(this);
bulletPool.add(bullet);
}
// 设置子弹位置
switch (this.direction){
case TANK_DIRECTION_UP:
bullet.setPosition(this.x + TANK_WIDTH/2 - bullet.getWidth()/2,this.y - bullet.getHeight(),this.direction);
break;
case TANK_DIRECTION_RIGHT:
bullet.setPosition(this.x + TANK_WIDTH,this.y + TANK_HEIGHT/2 - bullet.getHeight()/2,this.direction);
break;
case TANK_DIRECTION_DOWN:
bullet.setPosition(this.x + TANK_WIDTH/2 - bullet.getWidth()/2,this.y + TANK_HEIGHT,this.direction);
break;
case TANK_DIRECTION_LEFT:
bullet.setPosition(this.x - bullet.getWidth(),this.y + TANK_HEIGHT/2 - bullet.getHeight()/2,this.direction);
break;
}
// 让子弹飞一会
bullet.fly();
}
//坦克受到伤害
public void hurt(Bullet bullet){
this.hp -= bullet.getAtk();
if(this.hp < 0){this.hp = 0;}
}
//坦克是否死亡
public boolean isDead(){
return this.hp <= 0;
}
//坦克开始工作
public void work() {
this.state = TANK_STATE_RUNNING;
}
//坦克重置
public void reset(){
this.setX(-100);
this.setY(-100);
this.setHp(Tank.TANK_MAX_BLOOD);
this.setState(TANK_STATE_FREE);
}
public int getX(){return x;}
public void setX(int x){this.x = x;}
public int getY(){return y;}
public void setY(int y){this.y = y;}
public int getSpeed(){return speed;}
public void setSpeed(int speed){this.speed = speed;}
public int getHp(){return hp;}
public void setHp(int hp){this.hp = hp;}
public int getDirection(){return direction;}
public void setDirection(int direction){this.direction = direction;}
public int getState(){return state;}
public void setState(int state){this.state = state;}
public int getTankKind(){return tankKind;}
public Vector<Bullet> getBulletPool(){return bulletPool;}
}
2.我方的坦克代码
package TankWarrrrrrr;
public class Hero extends Tank{
public Hero(int x, int y, int direction){
super(TANK_KIND_HERO, x, y, direction);
}
}
3.敌方的坦克代码
在继承父类坦克常量参数的基础上,通过线程和判定语句实现敌方坦克的运行,移动,攻击的行为,与我方坦克博弈
package TankWarrrrrrr;
public class Enemy extends Tank{
public Enemy(int x, int y, int direction){
super(TANK_KIND_ENEMY, x, y, direction);
}
@Override
public void work(){
super.work();
// 启动线程让坦克跑
new Thread(()->{
//System.out.println("坦克线程启动...");
while (this.getState() == TANK_STATE_RUNNING){
try{
Thread.sleep(200);
}
catch (InterruptedException e){
e.printStackTrace();
}
// 随机确认是原地不动还是移动(0-不动,其它-移动)
if(GameLogic.getRandomInt(0,8) != 0) { // 移动(概率设大点)
// 随机确认是改变方向还是向前移动(0-改变方向,其它-向前移动)
if(GameLogic.getRandomInt(0,12) == 0) { // 改变方向(概率设小点)
int newDirection = GameLogic.getRandomInt(TANK_DIRECTION_UP, TANK_DIRECTION_LEFT + 3);
if(newDirection > TANK_DIRECTION_LEFT){newDirection = TANK_DIRECTION_DOWN;} // 向下概率设大点
this.setDirection(newDirection);
}
else { // 向前移动
// 如果到边界了就换方向
boolean toBorder = false; // 默认未到边界
switch (this.getDirection()){
case TANK_DIRECTION_UP:
if((this.getY() - this.getSpeed() < 0)){
toBorder = true;
}
break;
case TANK_DIRECTION_RIGHT:
if((this.getX() + TANK_WIDTH + this.getSpeed() > GamePanel.GAME_ACTION_WIDTH)){
toBorder = true;
}
break;
case TANK_DIRECTION_DOWN:
if((this.getY() + TANK_HEIGHT + this.getSpeed() > GamePanel.GAME_ACTION_HEIGHT)){
toBorder = true;
}
break;
case TANK_DIRECTION_LEFT:
if((this.getX() - this.getSpeed() < 0)){
toBorder = true;
}
break;
}
if(toBorder) { // 到边界了换方向
int newDirection = GameLogic.getRandomInt(TANK_DIRECTION_UP, TANK_DIRECTION_LEFT);
this.setDirection(newDirection);
}
else { // 继续前进
this.move(this.getDirection());
}
}
}
// 随机确认是发射子弹还是省子弹(1-发射,其它-不发射)
if(GameLogic.getRandomInt(0,8) == 1){
this.shoot();
}
}
}).start();
}
}
4.Bullet.java子弹类
设置子弹常量和属性,确定子弹的基础参数,状态和攻击力上下限,通过线程完成子弹启动的运行,随机设定子弹的伤害,根据攻击力高低进行判定,从而确定子弹颜色
package TankWarrrrrrr;
import java.awt.*;
public class Bullet{
//子弹状态常量
public final static int BULLET_STATE_FREE = 0; // 空闲中
public final static int BULLET_STATE_RUNNING = 1; // 运行中
//子弹攻击力常量
public final static int BULLET_MAX_ATTACK = 100; // 最大攻击力
public final static int BULLET_MIN_ATTACK = 20; // 最小攻击力
private int x; //子弹左上角X坐标
private int y; //子弹左上角Y坐标
private int width = 10; //子弹宽度
private int height = 10; //子弹高度
private int direction; //子弹方向
private int speed = 16; //子弹速度
private int state = BULLET_STATE_FREE; //子弹状态
private Tank tank; //所属坦克
private int atk; //子弹攻击力
public Bullet(Tank tank){
this.tank = tank;
this.atk = GameLogic.getRandomInt(BULLET_MIN_ATTACK,BULLET_MAX_ATTACK);
}
//设置子弹位置
public void setPosition(int x,int y,int direction){
this.x = x;
this.y = y;
this.direction = direction;
}
//功能:子弹重置
public void reset() {
this.x = -100;
this.y = -100;
this.atk = GameLogic.getRandomInt(BULLET_MIN_ATTACK,BULLET_MAX_ATTACK); // 重新随机生成攻击力
this.state = BULLET_STATE_FREE;
}
//子弹飞行
public void fly(){
// 设置子弹状态为运行中
this.state = BULLET_STATE_RUNNING;
//启动子弹线程
new Thread(()->{
while (this.state == BULLET_STATE_RUNNING) { //tank.getState() == Tank.TANK_STATE_RUNNING &&
try{
Thread.sleep(50);
}
catch (InterruptedException e){
e.printStackTrace();
}
switch (direction){
case Tank.TANK_DIRECTION_UP:
if(this.y - this.speed >= 0){
this.y -= this.speed;
}
else{
this.reset();
}
break;
case Tank.TANK_DIRECTION_RIGHT:
if(this.x + this.width + this.speed <= GamePanel.GAME_ACTION_WIDTH){
this.x += this.speed;
}
else{
this.reset();
}
break;
case Tank.TANK_DIRECTION_DOWN:
if(this.y + this.height + this.speed <= GamePanel.GAME_ACTION_HEIGHT){
this.y += this.speed;
}
else{
this.reset();
}
break;
case Tank.TANK_DIRECTION_LEFT:
if(this.x >= this.speed) {
this.x -= this.speed;
}
else{
this.reset();
}
break;
}
}
}).start();
}
// 画子弹
public void draw(Graphics g){
if(tank.getTankKind() == Tank.TANK_KIND_HERO) { // 我方子弹
g.setColor(Tank.TANK_COLOR_HERO);
}
else { // 敌方子弹
g.setColor(Tank.TANK_COLOR_ENEMY);
}
g.fillOval(this.x,this.y,this.width,this.height);
// 根据攻击力画加强子弹
g.setColor(Color.RED);
if(this.atk == 100){
g.fillOval(this.x,this.y,this.width,this.height);
}
else if(this.atk >= 90) {
g.fillOval(this.x + 1,this.y + 1,this.width - 2,this.height - 2);
}
else if(this.atk >= 70){
g.fillOval(this.x + 2,this.y + 2,this.width - 4,this.height - 4);
}
else if(this.atk >= 50){
g.fillOval(this.x + 3,this.y + 3,this.width - 6,this.height - 6);
}
}
public int getX(){return x;}
public int getY(){return y;}
public int getWidth(){return width;}
public int getHeight(){return height;}
public int getDirection(){return direction;}
public int getSpeed(){return speed;}
public int getState(){return state;}
public int getAtk(){return atk;}
}
5. Bomb.java爆炸效果
设置爆炸参数,根据进度呈现爆炸效果,增添坦克消灭时的特效
package TankWarrrrrrr;
import java.awt.*;
public class Bomb{
//爆炸状态常量
public final static int BOMB_STATE_FREE = 0; // 空闲中
public final static int BOMB_STATE_RUNNING = 1; // 运行中
private int x; //爆炸左上角X坐标
private int y; //爆炸左上角Y坐标
private int width = 20; //爆炸宽度
private int height = 20; //爆炸高度
private int state = BOMB_STATE_FREE; //状态
//爆炸进度,根据它来决定显示哪种爆炸形状,-1-不显示爆炸,0-准备显示,1-显示第一种爆炸形状,2-显示第二种爆炸形状,3-显示第三种爆炸形状
public int bombProgress = -1;
public Bomb(int x, int y){
this.x = x;
this.y = y;
}
public void work(){
this.state = BOMB_STATE_RUNNING; // 设置爆炸状态为运行中
bombProgress = 0; // 爆炸进度,准备显示
}
//重置爆炸
public void reset(){
this.setX(-100);
this.setY(-100);
this.state = BOMB_STATE_FREE;
this.bombProgress = -1;
}
// 画爆炸,每帧显示一种形状,共三种,即3帧结束爆炸
public void draw(Graphics g) {
// 进度走起
this.bombProgress++;
// 根据进度显示某种爆炸形状
switch (this.bombProgress){
case 1: // 第一种形状
g.setColor(Color.WHITE);
g.fillRoundRect(this.x + 5,this.y + 5,20,20,10,10);
g.fillOval(this.x + 10,this.y,10,30);
g.setColor(Color.YELLOW);
g.drawOval(this.x + 10,this.y,10,30);
g.setColor(Color.WHITE);
g.fillOval(this.x,this.y + 10,30,10);
g.setColor(Color.YELLOW);
g.drawOval(this.x,this.y + 10,30,10);
break;
case 2: // 第二种形状
g.setColor(Color.WHITE);
g.fillOval(this.x + 12,this.y + 5,6,20);
g.setColor(Color.YELLOW);
g.drawOval(this.x + 12,this.y + 5,6,20);
g.setColor(Color.WHITE);
g.fillOval(this.x + 5,this.y + 12,20,6);
g.setColor(Color.YELLOW);
g.drawOval(this.x + 5,this.y + 12,20,6);
break;
case 3: // 第三种形状
g.setColor(Color.WHITE);
g.fillOval(this.x + 10,this.y + 10,10,10);
g.setColor(Color.YELLOW);
g.drawOval(this.x + 10,this.y + 10,10,10);
break;
default:
this.reset();
break;
}
}
public int getX(){return x;}
public void setX(int x){this.x = x;}
public int getY(){return y;}
public void setY(int y){this.y = y;}
public int getWidth(){return width;}
public void setWidth(int width){this.width = width;}
public int getHeight(){return height;}
public void setHeight(int height){this.height = height;}
public int getState(){return state;}
}
----------------------
package TankWarrrrrrr;
public class Brick extends Block{
public Brick(int x, int y){
super(BLOCK_KIND_BRICK, x, y);
}
}
6.Block.java砖块与铁块的父类
设置砖块和铁块的父类,设定不同的参数,确定砖块和铁块的外观和属性,使砖块可以通过攻击击破,铁块不可被击破
package TankWarrrrrrr;
import java.awt.*;
//块和铁块父类
public class Block{
public final static int BLOCK_KIND_BRICK = 1; // 砖块 //块类别常量
public final static int BLOCK_KIND_IRON = 2; // 铁块
public final static int BLOCK_WIDTH = Tank.TANK_WIDTH/2; //块尺寸常量,和坦克等大
public final static int BLOCK_HEIGHT = Tank.TANK_HEIGHT/2 ;
private int x; //块左上角X坐标
private int y; //块左上角Y坐标
private int blockKind = BLOCK_KIND_BRICK; //块类别
public Block(int blockKind,int x, int y){
this.blockKind = blockKind;
this.x = x;
this.y = y;
}
public void draw(Graphics g){ //画块
if(this.blockKind == BLOCK_KIND_BRICK) { // 画砖块
g.setColor(new Color(210, 105, 30));
g.fillRect(this.x,this.y,BLOCK_WIDTH,BLOCK_HEIGHT);
g.setColor(new Color(244, 164, 96));
g.drawLine(this.x,this.y,this.x + BLOCK_WIDTH - 1,this.y);
g.drawLine(this.x,this.y + BLOCK_HEIGHT / 2,this.x + BLOCK_WIDTH - 1,this.y + BLOCK_HEIGHT / 2);
g.drawLine(this.x,this.y,this.x,this.y + BLOCK_HEIGHT / 2);
g.drawLine(this.x + BLOCK_WIDTH / 2,this.y + BLOCK_HEIGHT / 2, this.x + BLOCK_WIDTH / 2, this.y + BLOCK_HEIGHT - 1);
}
else if(this.blockKind == BLOCK_KIND_IRON) { // 画铁块
g.setColor(new Color(190, 190, 190));
g.fillRect(this.x,this.y,BLOCK_WIDTH,BLOCK_HEIGHT);
g.setColor(Color.WHITE);
g.fillRect(this.x + 3,this.y + 3,BLOCK_WIDTH - 6,BLOCK_HEIGHT - 6);
g.draw3DRect(this.x + 3,this.y + 3,BLOCK_WIDTH - 6,BLOCK_HEIGHT - 6,true);
g.drawLine(this.x + 1,this.y + 1,this.x + 3,this.y + 3);
g.drawLine(this.x + BLOCK_WIDTH - 1,this.y + 1,this.x + BLOCK_WIDTH - 3,this.y + 3);
g.drawLine(this.x + 1,this.y + BLOCK_HEIGHT - 1,this.x + 3,this.y + BLOCK_HEIGHT - 3);
g.drawLine(this.x + BLOCK_WIDTH - 1,this.y + BLOCK_HEIGHT - 1,this.x + BLOCK_WIDTH - 3,this.y + BLOCK_HEIGHT - 3);
}
}
public int getX(){return x;}
public void setX(int x){this.x = x;}
public int getY(){return y;}
public void setY(int y){this.y = y;}
public int getBlockKind(){return blockKind;}
}
7.Brick.java砖块
package TankWarrrrrrr;
public class Brick extends Block{
public Brick(int x, int y){
super(BLOCK_KIND_BRIKCK, x, y);
}
}
8.Iron.java铁块
package TankWarrrrrrr;
public class Iron extends Block{
public Iron(int x, int y){
super(BLOCK_KIND_IRON, x, y);
}
}
9.Camp.java营地
package TankWarrrrrrr;
import java.awt.*;
public class Camp{
//基地状态常量
public final static int CAMP_STATE_FREE = 0; // 空闲中
public final static int CAMP_STATE_RUNNING = 1; // 运行中
private int x; // 基地左上角X坐标
private int y; //营地左上角Y坐标
private int width = Tank.TANK_WIDTH; //基地宽度
private int height = Tank.TANK_HEIGHT; //基地高度
private int state = CAMP_STATE_FREE; //基地状态
private int elementWidth = 2; //元素宽度
private int elementHeight = 2; //元素高度
private static byte[][] campMap = //基地地图
{
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,1,0,0,0,1,1,1,1,0,0,0,0,0,0,0,1,0,0},
{1,1,1,1,0,0,0,1,1,1,1,0,0,0,0,0,1,1,1,1},
{0,2,1,1,1,0,0,0,1,0,0,1,1,0,0,1,1,1,2,0},
{1,1,2,0,1,0,0,0,1,1,1,1,0,0,0,1,0,2,1,1},
{0,0,1,2,1,1,0,0,1,1,1,1,0,0,1,1,2,1,0,0},
{0,1,1,1,2,1,0,0,1,1,1,1,0,0,1,2,1,1,1,0},
{0,0,1,1,1,2,1,1,1,1,1,1,1,1,2,1,1,1,0,0},
{0,0,0,1,1,1,2,1,1,1,1,1,1,2,1,1,1,0,0,0},
{0,0,1,1,1,1,1,1,2,1,1,1,2,1,1,1,1,1,0,0},
{0,0,0,1,1,1,1,1,1,2,1,2,1,1,1,1,1,0,0,0},
{0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0},
{0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0},
{0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0},
{0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0},
{0,0,0,0,0,0,1,0,1,1,1,0,1,0,0,0,0,0,0,0},
{0,0,0,0,0,0,1,1,1,0,1,1,1,0,0,0,0,0,0,0},
{0,0,0,0,0,1,1,1,1,0,1,1,1,1,0,0,0,0,0,0},
{0,0,0,0,0,1,1,0,1,0,1,0,1,1,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
};
public Camp(int x, int y){
this.x = x;
this.y = y;
}
//画基地
public void draw(Graphics g){
int row = campMap.length;
int column = campMap[0].length;
for (int i = 0; i < row; i++){
for (int j = 0; j < column; j++){
int mapValue = campMap[i][j];
if(mapValue == 1){
g.setColor(Color.WHITE);
drawElement(g,this.x + elementWidth * j,this.y + elementHeight * i);
}
else if(mapValue == 2){
g.setColor(Color.LIGHT_GRAY);
drawElement(g,this.x + elementWidth * j,this.y + elementHeight * i);
}
}
}
}
//画元素
private void drawElement(Graphics g,int x,int y){
g.fillRect(x,y,elementWidth,elementHeight);
g.setColor(Color.WHITE);
g.draw3DRect(x,y,elementWidth,elementHeight,true);
}
public int getX(){return x;}
public void setX(int x){this.x = x;}
public int getY(){return y;}
public void setY(int y){this.y = y;}
public int getWidth(){return width;}
public int getHeight(){return height;}
public int getState(){return state;}
public void setState(int state){this.state = state;}
}
五.总结
通过这次课程设计使我明白了自己知识还比较欠缺,只是学习书本知识还是远远不够的,自己不会的东西还有太多,学习需要自己长期积累,比如在这次课程设计中在对于坦克父类的继承过程中出现了多次报错以及功能没有完全继承的问题,在调试的过程中不断纠错和调整最终完成了成品,所以我们要多动手,多钻研,多调试,才能达到最终期望,完成自己期待的结果。