package com.tank;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.Random;
/**
* 坦克类
* @author Administrator
*
*/
public class Tank {
/**
* 初始化坦克信息
* @param x
* @param y
* @param live
* @param dir
*/
public Tank(int x,int y,boolean live,Direction dir,boolean good,MainPanel mp){
this.x = x;
this.y = y;
this.OldX = x;
this.OldY = y;
this.live = live;
this.dir = dir;
this.good = good;
this.mp = mp;
}
//坦克横坐标移动位置
private static final int XSPEED = 5;
//坦克纵坐标移动的位置
private static final int YSPEED = 5;
//坦克的x坐标
private int x;
//坦克的y坐标
private int y;
//坦克是否还活着
private boolean live;
//坦克移动的方向
private Direction dir;
//坦克的方向
private Direction ptdir = Direction.U;
//判断是玩家坦克还是电脑坦克
private boolean good;
//控制坦克的方向
private boolean L = false,U = false,R = false,D = false;
//产生随机方向
private Random rand = new Random();
//产生一个随机数
private int step = rand.nextInt(15)+3;
//移动之前的x,y坐标
private int OldX,OldY;
//实例化一个血块
private Blood bb = new Blood();
//坦克的生命
private int life = 100;
//判断是否暂停
public boolean pause = true;
MainPanel mp;
/**
* 获取坦克的x坐标
* @return
*/
public int getX() {
return x;
}
/**
* 设置坦克的x坐标
* @param x
*/
public void setX(int x) {
this.x = x;
}
/**
* 获取坦克的y坐标
* @return
*/
public int getY() {
return y;
}
/**
* 设置坦克的y坐标
* @param y
*/
public void setY(int y) {
this.y = y;
}
/**
* 获取坦克是否还活着
* @return
*/
public boolean isLive() {
return live;
}
/**
* 设置坦克是否还活着
* @param live
*/
public void setLive(boolean live) {
this.live = live;
}
/**
* 获取坦克的方向
* @return
*/
public Direction getDir() {
return dir;
}
/**
* 设置坦克的方向
* @param dir
*/
public void setDir(Direction dir) {
this.dir = dir;
}
/**
* 获取是玩家坦克还是电脑坦克
* @return
*/
public boolean isGood() {
return good;
}
/**
* 设置是玩家坦克还是电脑坦克
* @param good
*/
public void setGood(boolean good) {
this.good = good;
}
/**
* 获取坦克的生命
* @return
*/
public int getLife() {
return life;
}
/**
* 设置坦克的生命
* @param life
*/
public void setLife(int life) {
this.life = life;
}
/**
* 根据方向的不同画出不同方向的坦克
*/
public void drawTank(Graphics g){
if(!isLive())return;
if(this.isGood())bb.drawBlood(g);
switch(this.ptdir){
case L:
g.drawImage(ImageInfo.mapTank.get("L"),this.getX(),this.getY(),null);
break;
case LU:
g.drawImage(ImageInfo.mapTank.get("LU"),this.getX(),this.getY(),null);
break;
case U:
g.drawImage(ImageInfo.mapTank.get("U"),this.getX(),this.getY(),null);
break;
case RU:
g.drawImage(ImageInfo.mapTank.get("RU"),this.getX(),this.getY(),null);
break;
case R:
g.drawImage(ImageInfo.mapTank.get("R"),this.getX(),this.getY(),null);
break;
case RD:
g.drawImage(ImageInfo.mapTank.get("RD"),this.getX(),this.getY(),null);
break;
case D:
g.drawImage(ImageInfo.mapTank.get("D"),this.getX(),this.getY(),null);
break;
case LD:
g.drawImage(ImageInfo.mapTank.get("LD"),this.getX(),this.getY(),null);
break;
}
moveTank();
}
/**
* 根据按键的方向移动坦克
*/
public void moveTank(){
if(!pause)return;
OldX = x;
OldY = y;
if(!isLive()){
mp.listForTank.remove(this);
return;
}
switch(this.getDir()){
case L:
x -= XSPEED;
break;
case LU:
x -= XSPEED;
y -= YSPEED;
break;
case U:
y -= YSPEED;
break;
case RU:
x += XSPEED;
y -= YSPEED;
break;
case R:
x += XSPEED;
break;
case RD:
x += XSPEED;
y += YSPEED;
break;
case D:
y += YSPEED;
break;
case LD:
x -= XSPEED;
y += YSPEED;
break;
case STOP:
break;
}
if(this.getDir() != Direction.STOP){
this.ptdir = this.getDir();
}
if(x <= 0) x = 0;
if(y <= 20) y = 20;
if(x >= MainPanel.GAME_WIDTH - 50) x = MainPanel.GAME_WIDTH - 50;
if(y >= MainPanel.GAME_HEIGHT - 50) y = MainPanel.GAME_HEIGHT - 50;
//判断是电脑的话,就随机方向移动
if(!good){
//产生一个方向的数组
Direction[] dire = Direction.values();
if(step == 0){
step = rand.nextInt(15) + 3;
int random = rand.nextInt(dire.length);
dir = dire[random];
}
step--;
if(rand.nextInt(30) > 20)this.fire();
}
}
/**
* 键盘按下事件
*/
public void keyPressed(KeyEvent kv) {
int keyCode = kv.getKeyCode();
switch(keyCode){
case KeyEvent.VK_UP:
U = true;
break;
case KeyEvent.VK_DOWN:
D = true;
break;
case KeyEvent.VK_LEFT:
L = true;
break;
case KeyEvent.VK_RIGHT:
R = true;
break;
}
locationTank();
}
/**
* 键盘弹起事件
*/
public void keyReleased(KeyEvent kv) {
int keyCode = kv.getKeyCode();
switch(keyCode){
case KeyEvent.VK_UP:
U = false;
break;
case KeyEvent.VK_DOWN:
D = false;
break;
case KeyEvent.VK_LEFT:
L = false;
break;
case KeyEvent.VK_RIGHT:
R = false;
break;
case KeyEvent.VK_J:
fire();
break;
case KeyEvent.VK_A:
superFire();
break;
case KeyEvent.VK_F2:
if(!this.isLive()){
this.setLife(100);
this.setLive(true);
}
break;
}
locationTank();
}
/**
* 定位坦克的方向
*/
public void locationTank(){
if(U && !D && !L && !R) this.setDir(Direction.U);
else if(!U && D && !L && !R) this.setDir(Direction.D);
else if(!U && !D && L && !R) this.setDir(Direction.L);
else if(!U && !D && !L && R) this.setDir(Direction.R);
else if(U && !D && L && !R) this.setDir(Direction.LU);
else if(U && !D && !L && R) this.setDir(Direction.RU);
else if(!U && D && !L && R) this.setDir(Direction.RD);
else if(!U && D && L && !R) this.setDir(Direction.LD);
else if(!U && !D && !L && !R) this.setDir(Direction.STOP);
}
/**
* 坦克开火
*/
public void fire(){
if(!isLive())return;
Missible m = new Missible(this.getX()+18,this.getY()+20,true,this.good,this.ptdir,mp);
mp.listMissible.add(m);
}
/**
* 重写坦克开火
*/
public void fire(Direction dir){
if(!isLive())return;
Missible m = new Missible(this.getX()+18,this.getY()+20,true,this.good,dir,mp);
mp.listMissible.add(m);
}
/**
* 超级开火
*/
public void superFire(){
Direction[] dires = Direction.values();
for(int i=0;i<dires.length;i++){
fire(dires[i]);
}
}
/**
* 判断是否碰撞
* @return
*/
public Rectangle getRect(){
return new Rectangle(this.getX(),this.getY(),ImageInfo.mapTank.get("L").getWidth(null),ImageInfo.mapTank.get("L").getHeight(null));
}
/**
* 坦克碰撞的时候停止
* @param tank
*/
public void hitTank(Tank tank){
if(this.isLive() && this.getRect().intersects(tank.getRect())){
this.stay();
tank.stay();
}
}
/**
* 碰撞时返回为碰撞之前的坐标
*/
public void stay(){
x = OldX;
y = OldY;
}
/**
* 内部类,控制玩家坦克的血量
* @author Administrator
*
*/
private class Blood{
/**
* 画处血条
* @param g
*/
public void drawBlood(Graphics g){
Color c = g.getColor();
g.setColor(Color.RED);
g.drawRect(x, y-10, ImageInfo.mapTank.get("L").getWidth(null), 10);
int w = ImageInfo.mapTank.get("L").getWidth(null) * life/100;
g.fillRect(x, y-10, w, 10);
g.setColor(c);
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
java做的坦克大战(带图片) (130个子文件)
Tank.class 7KB
Missible.class 5KB
MainPanel.class 3KB
ImageInfo.class 3KB
MainFrame.class 2KB
MainFrame$ActionListenerM.class 2KB
Explode.class 2KB
Tank$Blood.class 1KB
Direction.class 1KB
MainPanel$PaintThread.class 799B
MainPanel$HeroTankMove.class 785B
Test.class 452B
.classpath 301B
stylesheet.css 1KB
tankRU.gif 2KB
tankRU.gif 2KB
tankRD.gif 1KB
tankRD.gif 1KB
tankLU.gif 1KB
tankLU.gif 1KB
tankLD.gif 1KB
tankLD.gif 1KB
9.gif 1KB
9.gif 1KB
8.gif 1KB
8.gif 1KB
tankU.gif 1KB
tankD.gif 1KB
tankU.gif 1KB
tankD.gif 1KB
tankL.gif 1KB
tankL.gif 1KB
tankR.gif 1KB
tankR.gif 1KB
7.gif 1KB
7.gif 1KB
6.gif 1KB
6.gif 1KB
10.gif 1KB
10.gif 1KB
5.gif 1KB
5.gif 1KB
4.gif 1KB
4.gif 1KB
3.gif 1KB
3.gif 1KB
2.gif 936B
2.gif 936B
1.gif 868B
1.gif 868B
missileLD.gif 864B
missileLD.gif 864B
missileLU.gif 863B
missileRU.gif 863B
missileLU.gif 863B
missileRU.gif 863B
missileRD.gif 861B
missileRD.gif 861B
MissileU.gif 855B
MissileU.gif 855B
missileD.gif 852B
missileD.gif 852B
missileR.gif 839B
missileL.gif 839B
missileR.gif 839B
missileL.gif 839B
0.gif 824B
0.gif 824B
inherit.gif 57B
Tank.html 23KB
MainPanel.html 22KB
MainFrame.html 20KB
Missible.html 19KB
Direction.html 14KB
Explode.html 13KB
MainPanel.HeroTankMove.html 11KB
Direction.html 11KB
ImageInfo.html 11KB
MainFrame.ActionListenerM.html 10KB
MainPanel.PaintThread.html 10KB
index-11.html 9KB
index-14.html 9KB
Test.html 9KB
index-6.html 8KB
help-doc.html 8KB
serialized-form.html 8KB
Tank.html 8KB
MainPanel.html 8KB
package-tree.html 7KB
overview-tree.html 7KB
index-8.html 7KB
package-summary.html 7KB
index-9.html 7KB
index-3.html 7KB
index-15.html 7KB
index-7.html 7KB
index-4.html 7KB
index-12.html 6KB
index-16.html 6KB
index-10.html 6KB
共 130 条
- 1
- 2
资源评论
龙之魂lee
- 粉丝: 1
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功