package cn.yanxiaotong.Mygame;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.List;
import java.util.Random;
import cn.yanxiaotong.MyFrame.Constant;
import cn.yanxiaotong.MyFrame.MyFrame;
/**
* 如果想用MyFrame中的成员或者方法必须在构造成员方法的时候把值添加进去
*
* @author dell
*
*/
public class Tank {
private int x, y;
private int w = Constant.TW;
private int h = Constant.TH;
private int speed = Constant.TSPEED;
private boolean good = true;
private boolean tL = false, tR = false, tU = false, tD = false;
private boolean live = true;
private int life = 100;
private BloodBar bb=new BloodBar();
public int getTimes() {
return times;
}
public void setTimes(int times) {
this.times = times;
}
private int times=0;
public int getLife() {
return life;
}
public void setLife(int life) {
this.life = life;
}
private int oldX, oldY;
MyFrame mf;
public enum Direction {
L, LU, LD, R, RU, RD, U, D, STOP
};
private Direction ptDir = Direction.D;// 定义一个炮筒的方向,默认朝上
private Direction direction = Direction.STOP;
private static Random r = new Random();
private int step = r.nextInt(15) + 5;
public Tank(int x, int y, boolean good) {
this.x = x;
this.y = y;
this.oldX = x;
this.oldY = y;
this.good = good;
}
public Tank(int x, int y, boolean good, Direction direction, MyFrame mf) {
this(x, y, good);
this.direction = direction;
this.mf = mf;
}
public void draw(Graphics g) {
if (!live)
return;
if (good) {
Color c = g.getColor();
g.setColor(Color.white);
g.fillOval(x, y, Constant.TW, Constant.TH);
g.setColor(c);
bb.draw(g);
} else {
Color c = g.getColor();
g.setColor(Color.red);
g.fillOval(x, y, Constant.TW, Constant.TH);
g.setColor(c);
}
switch (ptDir) {
case L:
g.drawLine(x + w / 2, y + h / 2, x, y + h / 2);
break;
case LU:
g.drawLine(x + w / 2, y + h / 2, x, y);
break;
case LD:
g.drawLine(x + w / 2, y + h / 2, x, y + h);
break;
case R:
g.drawLine(x + w / 2, y + h / 2, x + w, y + h / 2);
break;
case RU:
g.drawLine(x + w / 2, y + h / 2, x + w, y);
break;
case RD:
g.drawLine(x + w / 2, y + h / 2, x + w, y + h);
break;
case U:
g.drawLine(x + w / 2, y + h / 2, x + w / 2, y);
break;
case D:
g.drawLine(x + w / 2, y + h / 2, x + w / 2, y + h);
break;
}
move();
}
public void reStart(){
while(!this.isLive()){
this.x=Constant.FWID - Constant.TW;
this.y=Constant.FHEI- Constant.TH;
this.setLive(true);
this.setLife(100);
times++;
}
}
public Missile fire() {
if (!this.live)
return null;
Missile m1 = null;
if (this.good) {
m1 = new Missile(x + w / 2 - Constant.MissileW / 2, y + h / 2
- Constant.MissileH / 2, ptDir, true, mf);
mf.missiles.add(m1);
} else {
m1 = new Missile(x + w / 2 - Constant.MissileW / 2, y + h / 2
- Constant.MissileH / 2, ptDir, false, mf);
mf.missiles.add(m1);
}
return m1;
}
public Missile fire(Direction dir) {// 在此方法中为子弹初始化
if (!this.isLive())
return null;
int x1 = this.x + Constant.TW / 2 - Constant.MissileW / 2;
int y1 = this.y + Constant.TH / 2 - Constant.MissileH / 2;
Missile m = new Missile(x1, y1, dir, this.good, this.mf);
mf.missiles.add(m);
return m;
}
public void superFire() {
Direction[] dirs = direction.values();
for (int i = 0; i < dirs.length - 1; i++) {
fire(dirs[i]);
}
}
public void keypressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
tL = true;
break;
case KeyEvent.VK_RIGHT:
tR = true;
break;
case KeyEvent.VK_UP:
tU = true;
break;
case KeyEvent.VK_DOWN:
tD = true;
break;
}
chooseDirection();
}
public void keyReleased(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_CONTROL:
fire();
break;
case KeyEvent.VK_SHIFT:
superFire();
break;
case KeyEvent.VK_LEFT:
tL = false;
break;
case KeyEvent.VK_RIGHT:
tR = false;
break;
case KeyEvent.VK_UP:
tU = false;
break;
case KeyEvent.VK_DOWN:
tD = false;
break;
case KeyEvent.VK_F1:
reStart();
break;
}
chooseDirection();
}
// 判断坦克的方向
public void chooseDirection() {
if (tL && !tR && !tU && !tD)
direction = Direction.L;
if (tL && !tR && tU && !tD)
direction = Direction.LU;
if (tL && !tR && !tU && tD)
direction = Direction.LD;
if (!tL && tR && !tU && !tD)
direction = Direction.R;
if (!tL && tR && tU && !tD)
direction = Direction.RU;
if (!tL && tR && !tU && tD)
direction = Direction.RD;
if (!tL && !tR && tU && !tD)
direction = Direction.U;
if (!tL && !tR && !tU && tD)
direction = Direction.D;
if (!tL && !tR && !tU && !tD)
direction = Direction.STOP;
}
public void move() {
oldX = x;
oldY = y;
switch (direction) {
case L:
x = x - speed;
break;
case LU:
x = x - speed;
y = y - speed;
break;
case LD:
x = x - speed;
y = y + speed;
break;
case R:
x = x + speed;
break;
case RU:
x = x + speed;
y = y - speed;
break;
case RD:
x = x + speed;
y = y + speed;
break;
case U:
y = y - speed;
break;
case D:
y = y + speed;
break;
}
if (direction != Direction.STOP) {
ptDir = direction;
}
if (x < 0)
x = 0;// 使坦克不会出界
if (y < 25)
y = 25;
if (x > Constant.FWID - Constant.TW)
x = Constant.FWID - Constant.TW;
if (y > Constant.FHEI - Constant.TH)
y = Constant.FHEI - Constant.TH;
if (!good) {
Direction[] dir = Direction.values();
if (step == 0) {
step = r.nextInt(15) + 5;
int randomNum = r.nextInt(dir.length);// 在不超过数组长度的条件下产生随机数
direction = dir[randomNum];
fire();
}
step--;
}
}
public boolean hitWall(greatWall gw) {
if (this.isLive() && this.getRect().intersects(gw.getRect())) {
stay();
return true;
}
return false;
}
public Rectangle getRect() {
Rectangle rt = new Rectangle(x, y, Constant.TW, Constant.TH);
return rt;
}
public boolean pengTank(List<Tank> enemyTanks) {
for (int i = 0; i < enemyTanks.size(); i++) {
Tank enemyTank = enemyTanks.get(i);
if (this != enemyTank) {
if (this.isLive() && enemyTank.isLive()
&& this.getRect().intersects(enemyTank.getRect())) {
stay();
return true;
}
}
}
return false;
}
private class BloodBar {
public void draw(Graphics g) {
Color c = g.getColor();
g.setColor(Color.green);
g.drawRect(x, y - 10, Constant.TW, 10);
g.fillRect(x, y - 10, Constant.TW * life / 100, 10);
}
}
private void stay() {
x = oldX;
y = oldY;
}
public boolean isLive() {
return live;
}
public boolean isGood() {
return good;
}
public void setGood(boolean good) {
this.good = good;
}
public void setLive(boolean live) {
this.live = live;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
坦克大战JavaSwing 实现_java
共22个文件
class:12个
java:7个
classpath:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 139 浏览量
2022-09-25
01:42:35
上传
评论
收藏 25KB ZIP 举报
温馨提示
java swing 复刻经典小游戏坦克大战
资源详情
资源评论
资源推荐
收起资源包目录
坦克大战JavaSwing 实现.zip (22个子文件)
坦克大战
bin
cn
yanxiaotong
Mygame
Tank$Direction.class 1KB
Tank$BloodBar.class 1KB
Missile.class 5KB
TankGame.class 488B
Explode.class 1KB
greatWall.class 999B
Tank.class 8KB
MyFrame
MyFrame$1.class 737B
MyFrame.class 5KB
Constant.class 796B
MyFrame$TThread.class 984B
MyFrame$keyMonitor.class 1016B
.settings
org.eclipse.jdt.core.prefs 598B
src
cn
yanxiaotong
Mygame
greatWall.java 530B
Explode.java 734B
Tank.java 7KB
Missile.java 3KB
TankGame.java 268B
MyFrame
MyFrame.java 4KB
Constant.java 553B
.project 385B
.classpath 301B
共 22 条
- 1
刘良运
- 粉丝: 61
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0