// default:
// break;
}
return b;
}
public void drawTank (int x , int y , Graphics g , int direct , int type){
switch(type){//坦克类型
case 0:
g.setColor(Color.RED);
break;
case 1:
g.setColor(Color.BLUE);
break;
}
switch (direct) {
case 0://向上
g.fill3DRect(x, y, 5, 30, false );
g.fill3DRect(x + 15, y, 5, 30, false );
g.fill3DRect(x + 5, y + 5, 10, 20, false );
g.fillOval(x + 5, y + 10, 10, 10);
g.drawLine(x + 10, y,x + 10, y + 15);
break;
case 1://向下
g.fill3DRect(x, y, 5, 30, false );
g.fill3DRect(x + 15, y, 5, 30, false );
g.fill3DRect(x + 5, y + 5, 10, 20, false );
g.fillOval(x + 5, y + 10, 10, 10);
g.drawLine(x + 10, y + 15,x + 10,y + 30 );
break;
case 2://向左
g.fill3DRect(x, y, 30, 5, false );
g.fill3DRect(x , y + 15, 30, 5, false );
g.fill3DRect(x + 5, y + 5, 20, 10, false );
g.fillOval(x + 10, y + 5, 10, 10);
g.drawLine(x , y + 10,x + 15,y + 10 );
break;
case 3://向右
g.fill3DRect(x, y, 30, 5, false );
g.fill3DRect(x , y + 15, 30, 5, false );
g.fill3DRect(x + 5, y + 5, 20, 10, false );
g.fillOval(x + 10, y + 5, 10, 10);
g.drawLine(x + 15,y + 10, x + 30 , y + 10);
break;
default:
break;
}
}
public MyPanel (){
hero = new Hero(30, 270 ,0,0,10 , true);
//tanks = new tank(100,100, 2 ,1);
//初始化敌人坦克
Recoder.setHero(hero);
for(int i = 0; i < enSize; i++){
//创建敌人坦克的对象
Recoder.getRecording();
EnemyTask et = new EnemyTask((i + 1)*50, 0, 1, 1, 5 ,true);
et.setEts(ets);
Recoder.setEtss(ets);//传ets到Recoder
Thread t = new Thread(et);
t.start();
shot s = new shot(et.x + 10 , et.y + 30, et.direct);
et.ss.add(s);
Thread t2 = new Thread(s);
t2.start();
ets.add(et);//加入到集合中
//et.setColor(1);
}
Recoder.setEnSize(enSize);
}
@Override
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
if(arg0.getKeyCode() == KeyEvent.VK_S){
//hero.setX(hero.getX() ++ 1);
// int i= hero.getY();
// i += hero.speed;
// hero.setY(i);
this.hero.MoveDown();
hero.setD(1);
}
if(arg0.getKeyCode() == KeyEvent.VK_W){
//hero.setX(hero.getX() ++ 1);
// int i= hero.getY();
// i -= hero.speed;
// hero.setY(i);
hero.MoveUp();
hero.setD(0);
}
if(arg0.getKeyCode() == KeyEvent.VK_D){
//hero.setX(hero.getX() ++ 1);
// int i= hero.getX();
// i += hero.speed;
// hero.setX(i);
hero.MoveRight();
hero.setD(3);
}
if(arg0.getKeyCode() == KeyEvent.VK_A){
//hero.setX(hero.getX() ++ 1);
// int i= hero.getX();
// i -= hero.speed;
// hero.setX(i);
hero.MoveLeft();
hero.setD(2);
}
//判断是否按下J
if(arg0.getKeyCode() == KeyEvent.VK_J){
if(hero.ss.size() <= 4){
this.hero.shotEnemy();
//System.out.println("j");
}
}
if(arg0.getKeyCode() == KeyEvent.VK_SPACE){//空格暂停
if(hero.speed != 0){
a = hero.speed ;
hero.speed = 0;
for(int i = 0; i < ets.size(); i ++){
//System.out.println("aaaaaaaaaaaaaa");
EnemyTask et = ets.get(i);
//System.out.println("speed" + et.speed);
b = et.speed;
et.speed = 0;
directs[i] = et.direct;
//System.out.println(et.direct);
//System.out.println(directs[i]);
//et.direct = 0;
et.isdirects = false;
for(int j = 0 ; j < et.ss.size(); j ++){
shot s = et.ss.get(j);
c = s.speed;
s.speed = 0;
System.out.println("aa");
}
}//System.out.println("b1 =" + b);
isPaintShot = false;
}
else{
hero.speed = a;
//System.out.println("a2 =" + a);
for(int i = 0; i < ets.size(); i ++){
//System.out.println("bbbbbbbbbbbbbbbbbbb");
EnemyTask et = ets.get(i);
et.isdirects = true;
et.speed = b;
et.direct = directs[i];
for(int j = 0 ; j < et.ss.size(); j ++){
shot s = et.ss.get(j);
s.speed = c;
isPaintShot = true;
}
}s.isShot = true;
}
}
if(arg0.getKeyCode() == KeyEvent.VK_K){
this.KeepExit();
System.exit(0);
}
if(arg0.getKeyCode() == KeyEvent.VK_L){
this.GetKeeper();
}
this.repaint();
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void run() {
//每隔100ms
while(true){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.hintEnemyTank();
this.hintMyTank();
// for(int i = 0; i < et.ss.size(); i ++){
// shot myshot = et.ss.get(i);
// if(et.isLive == true){
//
// if(hero.isLive = true){
// this.hintTank2(myshot, hero);
// }
//
// }
// }
this.repaint();
}
}
private void hintMyTank() {
for(int i = 0; i < ets.size(); i++){
EnemyTask et = ets.get(i);
for(int j = 0; j < et.ss.size(); j++ ){
shot s = et.ss.get(j);
if(s != null){
if(hero.isLive){
this.hintTank(s, hero);
if(this.hintTank(s, hero)){//判断是否击中,返回true则hero数量减一
Recoder.setMyLife(Recoder.getMyLife() - 1 );
}
}
}
}
}
}
private void hintEnemyTank() {
for(int i = 0; i < hero.ss.size(); i ++){
shot myshot = hero.ss.get(i);
if(myshot.isLive == true){
for(int j = 0; j < ets.size(); j ++){
EnemyTask et = ets.get(j);
if(et.isLive = true){
this.hintTank(myshot, et);
if(this.hintTank(myshot, et)){
hintEnemyTanks ++;
Recoder.setEnNum(Recoder.getEnNum() - 1);
}
}
}
}
}
}
private void KeepExit(){
int [] heros = new int[]{
hero.getX(),
hero.getY(),
hero.direct,
};
for(int i = 0 ; i < ets.size(); i ++){
EnemyTask et = ets.get(i);
TanksX[i] = et.getX();
TanksY[i] = et.getY();
ETdirects [i] = et.direct;
for(int j = 1 ; j < et.ss.size(); j++){
shot s = et.ss.get(j);
ShotsX[j] = s.x;
ShotsY[j] = s.y;
}
}
for(int i = 0 ; i < hero.ss.size(); i++ ){
shot s = hero.ss.get(i);
HeroShotsX[i] = s.x ;
HeroShotsY[i] = s.y;
}
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter("d:\\aa\\TanksX.txt"));
for(int i = 0 ; i < enSize ; i ++){
bw.write(TanksX[i] + "\r\n");
bw.write(TanksY[i] + "\r\n");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void GetKeeper (){//继续游戏时还原数据
BufferedReader br = null ;
try {
br = new BufferedReader(new FileReader("d:\\aa\\TanksX.txt"));
for(int i = 0; i < enSize*2 ; i ++){
if(i % 2 !=0){
TanksY[i] = Integer.parseInt(br.readLine());
System.out.println("TanksY" + TanksY[i]);
}else{
TanksX[i] = Integer.parseInt(br.readLine());
System.out.println("TanksX" + TanksX[i]);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Java游戏开发之坦克大战代码(2)
内容版权声明:除非注明,否则皆为本站原创文章。
转载注明出处:http://www.heiqu.com/34fa2e4cb3df2ac646cd1064dd4540ad.html