package tank;
import java.util.Random;
/**坦克父类,继承了线程*/
public class Tanks extends Thread{
private int x;//坦克坐标x
private int y;//坦克坐标y
private int step;//坦克的速度
private int direct;//方向,1表示向上,2表示向右,3表示向下,4表示向左
private int who;//坦克标识,0和-1表示自己的坦克,1,2,3表示敌人的坦克,颜色随机.
private boolean isLive = true;//判断坦克是否死亡
Shoot shoot;//shoot坦克的射击
public Tanks(int x, int y, int step, int direct,int who ,boolean isLive) {
super();
this.x = x;
this.y = y;
this.step = step+2;
this.direct = direct;
this.who = who;
this.isLive = isLive;
}
public boolean canMove(){//boolean返回值类型.判断坦克是否能移动,比如撞墙了.
if(x<0){
x=0;
return false;
}else if(x>690){
x=690;
return false;
}else if(y<0){
y=0;
return false;
}else if(y>450){
y=450;
return false;
}else if(x>270&&x<370&&y>100&&y<380){//撞到竖着的柱子
if(x-270<20){
x=270;
}else{
x=370;
}
return false;
}else if(x>85&&x<570&&y>195&&y<290){//撞到横着的柱子
if(y-195<10){
y=190;
}else{
y=290;
}
return false;
}
return true;
}
public void moveUp(){//坦克向上移动一步
if(canMove()){
y-=step;
direct=1;
}
}
public void moveDown(){//坦克向下移动一步
if(canMove()){
y+=step;
direct=3;
}
}
public void moveLeft(){//坦克向左移动一步
if(canMove()){
x-=step;
direct=4;
}
}
public void moveRight(){//坦克向右移动一步
if(canMove()){
x+=step;
direct=2;
}
}
public void shoot(){//每次射击调用此函数
shoot = new Shoot(x,y,direct,step,who);
}
public String toString() {//用于调试
return "Tanks [direct=" + direct + ", isLive="
+ isLive + ", step=" + step + ", x=" + x + ", y=" + y + "]";
}
/**以下是get() set()函数*/
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 getStep() {
return step;
}
public void setStep(int step) {
this.step = step;
}
public int getDirect() {
return direct;
}
public void setDirect(int direct) {
this.direct = direct;
}
public boolean isLive() {
return isLive;
}
public void setLive(boolean isLive) {
this.isLive = isLive;
}
public void setWho(int who) {
this.who = who;
}
public int getWho() {
return who;
}
}
class MyTank extends Tanks{//我的坦克继承了Tanks
public MyTank(int x, int y, int step, int direct, int who ,boolean isLive) {
super(x, y, step, direct, who, isLive);
}
}
class EnemyTanks extends Tanks{//敌人的坦克继承了tanks,也继承了父类的线程
private int time = 500;//线程占用时间500毫秒
boolean s=true;//定义一个boolean的开关,坦克死亡,关闭开关,线程死亡.
public EnemyTanks(int x, int y, int step, int direct, int who, boolean isLive) {
super(x, y, step, direct, who,isLive);
}
public void move(){//敌人的坦克自己移动
Random r = new Random();
int random = r.nextInt(50);
int r1=r.nextInt(4);
if(!(r1==0)){//如果r1=0,就射击一次
shoot();
}
for (int i = 0; i < random; i++) {//random表示坦克的一次随机移动的距离
try {
Thread.sleep(50);//线程sleep,移动使移动看起来自然一点
} catch (InterruptedException e) {
e.printStackTrace();
}
if(r1==0){//根据r1的值判断,此次移动往那个方向
if(this.getY()==0||this.getY()==290){
r1=3;
}else{
moveUp();
}
}else if(r1==1){
if(this.getX()==690||this.getX()==270){
r1=2;
}else{
moveRight();
}
}else if(r1==2){
if(this.getX()==0||this.getX()==370){
r1=1;
}else{
moveLeft();
}
}else if(r1==3){
if(this.getY()==450||this.getY()==190){
r1=0;
}else{
moveDown();
}
}
}
}
public void run(){//继承线程功能,必须实现run()函数
while(s){//s=true表示坦克没有死亡.s=false跳出死循环,坦克死亡
move();
try {
Thread.sleep(time);//每次线程占用时间500毫秒
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/********************Shoot类***********************/