全面战争JAVA_Java__线程---基础知识全面实战---坦克大战系列为例

1 packageTankGame5;2

3 import java.awt.*;4 importjava.awt.event.ActionEvent;5 importjava.awt.event.ActionListener;6 importjava.awt.event.KeyEvent;7 importjava.awt.event.KeyListener;8 importjava.io.BufferedReader;9 importjava.io.BufferedWriter;10 importjava.io.File;11 importjava.io.FileReader;12 importjava.io.FileWriter;13 importjava.io.IOException;14 import java.util.*;15 import java.io.*;16 importjava.util.Vector;17 importjavax.imageio.ImageIO;18 import javax.swing.*;19

20

21 public class MyTankGame5 extends JFrame implementsActionListener22 {23 //声明一个标志

24 int flag=0;25 //声明一个运行面板

26 MyPanel mp=null;27 //定义一个开始的面板

28 MyStartPanel msp=null;29

30 //做出我需要的菜单

31 JMenuBar jmb=null;32 //开始游戏

33 JMenu jm1=null;34 JMenuItem jmi1=null;35 //退出系统

36 JMenuItem jmi2=null;37 JMenuItem jmi3=null;38 JMenuItem jmi4=null;39

40 public static voidmain(String[] args)41 {42 MyTankGame5 mytankgame1=newMyTankGame5();43 mytankgame1.setLocationRelativeTo(null);//将JFrame设置在中间44 //mytankgame1.setLocation(210,140);//设置JFrame的位置

45 }46

47 public MyTankGame5() //构造

48 {//初始化菜单栏

49 jmb=newJMenuBar();50 jm1=new JMenu("游戏(G)");51

52 //设置快捷方式

53 jm1.setMnemonic('G');54 jmi1=new JMenuItem("开始新游戏(N)");55 jmi2=new JMenuItem("退出游戏(E)");56 jmi3=new JMenuItem("存盘退出(C)");57 jmi4=new JMenuItem("继续上次游戏(L)");58

59 jmi1.setMnemonic('N');60 jmi2.setMnemonic('E');61 jmi3.setMnemonic('C');62 jmi4.setMnemonic('L');63

64 //对jmi1响应,注册监听,标记消息源

65 jmi1.addActionListener(this);66 jmi1.setActionCommand("new game");67

68 jmi2.addActionListener(this);69 jmi2.setActionCommand("exit");70

71 jmi3.addActionListener(this);72 jmi3.setActionCommand("save and exit");73

74 jmi4.addActionListener(this);75 jmi4.setActionCommand("continue game");76

77 jm1.add(jmi1);78 jm1.add(jmi2);79 jm1.add(jmi3);80 jm1.add(jmi4);81 jmb.add(jm1);82

83 //声明初始化面板

84

85 msp=newMyStartPanel();86

87 Thread t=newThread(msp);88 t.start();89 //初始化窗口,构造函数的重点

90 this.setJMenuBar(jmb);//千万注意啊!!!!

91

92 this.add(msp);93 this.setSize(400,300);94 this.setVisible(true);95 this.setDefaultCloseOperation(EXIT_ON_CLOSE);96 }97 public voidactionPerformed(ActionEvent e)98 {99 //对用户不同的点击做出不同的处理100 //首先得到command字符串,判断消息源

101 if(e.getActionCommand().equals("new game"))102 {103 this.setSize(600,500);104 mp=newMyPanel(flag);105 //启动mp线程

106

107 Thread t1=newThread(mp);108

109 t1.start();110 //先删除旧的面板

111 this.remove(msp);112

113 this.add(mp);114 //注册监听

115 this.addKeyListener(mp);116 //显示

117 this.setVisible(true);118 }119 else if(e.getActionCommand().equals("exit"))120 {121 //用户退出122 //保存击毁敌人数量

123 Recorder.keepRecording();124 System.exit(0);125 }126

127 else if(e.getActionCommand().equals("save and exit"))128 {129 Recorder.keepRecAndEnemyTank(mp.ets);130 if(this.mp.hero.isLive==true)131 {132 Recorder.keepRecAndMyTank(this.mp.hero);133

134 }135 System.exit(0);136 }137 else if(e.getActionCommand().equals("continue game"))138 {139 this.setSize(600,500);140 flag=1;141 mp=newMyPanel(flag);142 //启动mp线程

143

144 Thread t1=newThread(mp);145

146 t1.start();147 //先删除旧的面板

148 this.remove(msp);149

150 this.add(mp);151 //注册监听

152 this.addKeyListener(mp);153 //显示

154 this.setVisible(true);155 }156 }157 }158

159

160 //开始面板起一个提示的作用

161 class MyStartPanel extends JPanel implementsRunnable162 {163 int times=0;164 public voidpaint(Graphics g)165 {166 super.paint(g);167 g.fillRect(0, 0, 400, 300);168 //提示信息

169 if(times%2==0)170 {171 g.setColor(Color.yellow);172 //开关信息的字体

173 try{174 Font myFont=new Font("华文新魏",Font.BOLD,30);175 g.setFont(myFont);176 } catch(Exception e){e.printStackTrace();}177

178 g.drawString("stage:1", 150, 150);179 }180 }181 public voidrun()182 {183 while(true)184 {185 try{186 Thread.sleep(500);187 } catch(Exception e) {188 //TODO Auto-generated catch block

189 e.printStackTrace();190 }191 this.repaint();192 times++;193 if(times==1000) times=0;194 }195 }196

197 }198

199

200 class MyPanel extends JPanel implementsKeyListener,Runnable201 {202 //定义我的坦克,成员变量

203 Hero hero=null;204

205 //判断是续上局还是新的游戏

206 String flag="newGame";207

208 //定义敌人的坦克组

209

210 Vector ets=new Vector();211 //全局变量,声明,敌人坦克数量

212 static int enSize=5;213 public static intgetEnSize() {214 returnenSize;215 }216 //定义炸弹集合

217 Vector bombs=new Vector();218 //paint 方法被面板适时自动调用

219 public voidpaint (Graphics g)220 {221 super.paint(g);222 g.fillRect(0,0,400,300);223

224 //画出提示信息

225 this.showInfo(g);226

227 //画出自己的坦克

228 if(hero.isLive==true)229 {230 this.drawTank(hero.getX(), hero.getY(), g, this.hero.direct, 1);231 }232 //从Vector ss中取出每一颗子弹,并画出,如果有子弹

233 for(int i=0;i

237 //画出子弹,只画一颗子弹

238 if (myShot!=null&&myShot.isLive==true)239 {240 g.draw3DRect(myShot.x, myShot.y, 1, 1, false);241

242 }243 if (myShot.isLive==false)244 {245 //从ss中删除该子弹

246 hero.ss.remove(myShot);247

248 }249 }250

251 //画出炸弹

252 for(int i=0;i

255 Bomb b=bombs.get(i);256

257 if(b.life>6)258 {259 g.drawOval(b.x, b.y, 30, 30);260 //g.drawImage(image1, b.x, b.y, 30,30,this);

261 }262 else if(b.life>3)263 {264 g.drawOval(b.x, b.y, 20, 20);265 //g.drawImage(image2, b.x, b.y, 30,30,this);

266

267 }268 else

269 {270 g.drawOval(b.x, b.y, 10, 10);271 //g.drawImage(image3, b.x, b.y, 30,30,this);

272 }273 //让b的生命值减小

274 b.lifeDown();275 //如果炸弹的生命值为0,我们就剔除

276 if(b.life==0) bombs.remove(b);277

278 }279

280

281 //画出敌人的坦克

282 for(int i=0;i

289

290 for(int j=0;j

293 Shot enemyShot=et.ss.get(j);294 if(enemyShot.isLive)295 {296 g.draw3DRect(enemyShot.x, enemyShot.y, 1, 1, false);297 }298 else

299 {300 //如果敌人的坦克死亡就从Vector去掉

301 et.ss.remove(enemyShot);302

303 }304

305 }306 }307 }308

309 }310 //判断我是否被击中了

311 public voidhitMe()312 {313 //取出敌人的每一颗子弹

314

315 for(int i=0;i

318 EnemyTank et=ets.get(i);319

320 //取出每一颗子弹

321 for (int j=0;j

324 Shot enemyShot=et.ss.get(j);325 if(hero.isLive)//&&Recorder.getMyLife()>=1

326 {327 this.hitTank(enemyShot, hero);328 //Recorder.setMyLife(Recorder.getMyLife()-1);

329 }330

331 }332

333 }334

335 }336

337

338 //判断我的子弹是否击中敌人的坦克

339 public voidhitEnemyTank()340 {341 for(int i=0;i

345 if(myShot.isLive)346 {347 //取出每个坦克,与它判断

348 for(int j=0;j

351 EnemyTank et=ets.get(j);352 if(et.isLive)353 {354 this.hitTank(myShot,et);355

356 }357 }358

359 }360 }361

362 }363

364 //写一个函数专门判断子弹是否击中敌人坦克

365 public voidhitTank(Shot s,Tank et)366 {367 //判断该坦克的方向

368 switch(et.direct)369 {370 //如果敌人的方向是上或者是下

371 case 0:372 case 2:373 if (s.x>et.x&&s.xet.y&&s.y

377 s.isLive=false;378 //敌人坦克也要死亡

379 et.isLive=false;380 //减少敌人数量

381 if (et.getClass()==EnemyTank.class)//反射机制

382 {383 Recorder.reduceEnNum();384 //增加我的记录

385 Recorder.addEnNumRec();386 }387

388 if (et.getClass()==Hero.class)//反射机制

389 {390

391 }392 //创建一个炸弹,放入Vector

393 Bomb b=newBomb(et.x,et.y);394 //放入Vector

395 bombs.add(b);396 }397 case 1:398 case 3:399 if (s.x>et.x&&s.xet.y&&s.y

403 s.isLive=false;404 //敌人坦克也要死亡

405 et.isLive=false;406 //减少敌人数量

407 if (et.getClass()==EnemyTank.class)//反射机制

408 {409 Recorder.reduceEnNum();410 //增加我的记录

411 Recorder.addEnNumRec();412 }413

414 if (et.getClass()==Hero.class)//反射机制

415 {416 Recorder.setMyLife(Recorder.getMyLife() - 1);417 }418 //创建一个炸弹,放入Vector

419 Bomb b=newBomb(et.x,et.y);420 //放入Vector

421 bombs.add(b);422 }423

424 }425

426 }427

428 //画出坦克函数(扩展)

429 public void drawTank(int x,int y,Graphics g,int direct,inttype)430 {431 //判断类型

432 switch(type)433 {434 case 0:435 g.setColor(Color.cyan);break;436 case 1:437 g.setColor(Color.yellow);break;438 }439 //判断方向

440

441 switch(direct)442 {443 //向上

444 case 0:445 //画出我的坦克(到时候再封装成一个函数)446 //1.画出左面的矩形447 //g.drawRect(hero.getX(), hero.getY(), 5, 30);

448 g.fill3DRect(x,y,5,30,false);449

450 //2.画出右边的矩形

451 g.fill3DRect(x+15,y,5,30,false);452

453 //3.画出坦克的中间矩形

454 g.fill3DRect(x+5, y+5, 10, 20,false);455 //画出中间的圆

456 g.fillOval(x+4, y+10,10,10);457 //画出线

458 g.drawLine(x+9, y+15, x+9, y);459 break;460 case 1:461 //炮筒向右462 //画出上面的矩形

463 g.fill3DRect(x,y,30, 5, false);464 g.fill3DRect(x, y+15, 30, 5, false);465 g.fill3DRect(x+5, y+5, 20, 10, false);466 g.fillOval(x+10, y+5, 10, 10);467 g.drawLine(x+15, y+10, x+30, y+10);468

469 break;470 case 2:471 //向下

472 g.fill3DRect(x,y,5,30,false);473 g.fill3DRect(x+15,y,5,30,false);474 g.fill3DRect(x+5, y+5, 10, 20,false);475 g.fillOval(x+4, y+10,10,10);476 g.drawLine(x+10, y+15, x+10, y+30);477 break;478

479 case 3:480 //向左

481 g.fill3DRect(x,y,30, 5, false);482 g.fill3DRect(x, y+15, 30, 5, false);483 g.fill3DRect(x+5, y+5, 20, 10, false);484 g.fillOval(x+10, y+5, 10, 10);485 g.drawLine(x+15, y+10, x, y+10);486 break;487 }488

489 }490

491

492 public voidrun()493 {494 //每个一百毫秒去重画子弹

495 while(true)496 {497 try{498 Thread.sleep(100);499 } catch(InterruptedException e) {500 e.printStackTrace();501 }502

503

504 this.hitEnemyTank();505

506 //函数,判断敌人的子弹是否击中我了

507 this.hitMe();508

509 this.repaint();510 //判断是否需要给坦克加入新的子弹

511 for(int i=0;i

520 Shot s=null;521 switch(et.direct)522 {523 case 0:524 s=new Shot(et.x+9,et.y-1,0);525 et.ss.add(s);526 break;527 case 1:528 s=new Shot(et.x+30,et.y+10,1);529 et.ss.add(s);530 break;531 case 2:532 s=new Shot(et.x+9,et.y+30,2);533 et.ss.add(s);534 break;535 case 3:536 s=new Shot(et.x-1,et.y+9,3);537 et.ss.add(s);538 break;539 }540

541 //启动子弹线程

542 Thread t=newThread(s);543 t.start();544 }545 }546 }547 }548 }549 //定义三张图片,三张图片切换组成一颗炸弹

550 Image image1=null;551 Image image2=null;552 Image image3=null;553 intflagOfContinue;554 //构造函数

555 public MyPanel(intflag)556 {557

558 this.flagOfContinue=flag;559 //恢复记录

560 Recorder.getRecording();561

562

563 if(this.flagOfContinue==0)564 {565 //如果不是继续上次就直接建一个

566 hero=new Hero(100,100);567 //初始化敌人的坦克

568 for(int i=0;i

571 EnemyTank et=new EnemyTank((i+1)*60,0);572 et.setColor(0);573 et.setDirect(2);574 //将MyPanel的敌人坦克向量交给该敌人坦克

575 et.setEts(ets);576

577

578 //启动敌人的坦克

579

580 Thread t=newThread(et);581 t.start();582 //给敌人坦克添加一颗子弹

583 Shot s=new Shot(et.x+10,et.y+30,2);584 et.ss.add(s);585 Thread t2=newThread(s);586 t2.start();587 //加入

588

589 ets.add(et);590 }591 }592 else if(this.flagOfContinue==1)593 {594 //如果是继续上次的就读取坐标

595 Vector nodes1=new Vector();596 nodes1=Recorder.getNodes();597

598 //初始化敌人的坦克

599 for(int i=0;i

604 EnemyTank et=newEnemyTank(nodes1.get(i).x,nodes1.get(i).y);605 et.setColor(0);606 et.setDirect(nodes1.get(i).direct);607 //将MyPanel的敌人坦克向量交给该敌人坦克

608 et.setEts(ets);609

610

611 //启动敌人的坦克

612

613 Thread t=newThread(et);614 t.start();615 //给敌人坦克添加一颗子弹

616 Shot s=new Shot(et.x+10,et.y+30,2);617 et.ss.add(s);618 Thread t2=newThread(s);619 t2.start();620 //加入

621

622 ets.add(et);623 }624 else if(nodes1.get(i).type==1)625 {626 //如果保存有我上次的坦克的信息,就按照信息保存

627 hero=newHero(nodes1.get(i).x,nodes1.get(i).y);628 hero.setDirect(nodes1.get(i).direct);629 }630 }631

632 }633

634 //image1=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/33.jpg"));//文件应该放在src文件夹里面635 //image2=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/22.jpg"));636 //image3=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/11.jpg"));637 //关于图像我们使用好一点的方法,引入java.io.File;和java.io.IOException;

638 try

639 {640 image1=ImageIO.read(new File("33.jpg"));//此时文件应该放在工程的根目录里面

641 image2=ImageIO.read(new File("22.jpg"));642 image3=ImageIO.read(new File("11.jpg"));643 }644 catch(IOException e)645 {646 e.printStackTrace();647 }648

649 }650 //画出提示信息

651 public voidshowInfo(Graphics g)652 {653 //画出提示信息的坦克 ,该坦克不参加战斗

654 this.drawTank(80,330,g,0,0);655 g.setColor(Color.black);656 g.drawString(Recorder.getEnNum()+"",110,350);657

658 this.drawTank(150,330,g,0,1);659 g.setColor(Color.black);660 g.drawString(Recorder.getMyLife()+"",180,350);661 //画出玩家的总成绩

662

663 g.setColor(Color.black);664 try{665 Font f=new Font("宋体",Font.BOLD,20);666 g.setFont(f);667 }catch(Exception e){ e.printStackTrace();}668 g.drawString("您的总成绩:",420,40);669

670 this.drawTank(420, 60, g, 0, 0);671

672 g.setColor(Color.BLACK);673 g.drawString(Recorder.getAllEnemy()+"",460,80);674

675 }676

677 //键按下处理a表示左,s表示向下,w表示向上,d表示向右

678 public voidkeyPressed(KeyEvent e)679 {680 if(hero.isLive)681 {682 if(e.getKeyCode()==KeyEvent.VK_UP&&hero.getY()>0)683 {684 //设置我的坦克的方向

685 this.hero.setDirect(0);686 this.hero.moveUp();687 }688

689 else if (e.getKeyCode()==KeyEvent.VK_DOWN&&hero.getY()<270)690 {691 this.hero.setDirect(2);692 this.hero.moveDown();693 }694

695 else if (e.getKeyCode()==KeyEvent.VK_RIGHT&&hero.getX()<370)696 {697 this.hero.setDirect(1);698 this.hero.moveRight();699

700 }701 else if (e.getKeyCode()==KeyEvent.VK_LEFT&&hero.getX()>0)702 {703 this.hero.setDirect(3);704 this.hero.moveLeft();705 }706

707 if (e.getKeyCode()==KeyEvent.VK_SPACE)708 {709 this.hero.shotEnemy();710 }711

712 //必须重新绘制Panel

713 this.repaint();714 }715 }716 public voidkeyReleased(KeyEvent e)717 {718

719 }720 public voidkeyTyped(KeyEvent e)721 {722

723 }724

725 }726

727

728 //----------------------------------------------------

729

730 classNode731 {732 intx;733 inty;734 intdirect;735 inttype;736 public Node(int x,int y,int direct,inttype)737 {738 this.x=x;739 this.y=y;740 this.direct=direct;741 this.type=type;742 }743

744 }745

746

747

748 //记录类

749 classRecorder750 {751 public static intgetEnNum() {752 returnenNum;753 }754 public static void setEnNum(intenNum) {755 Recorder.enNum =enNum;756 }757 public static intgetMyLife() {758 returnmyLife;759 }760 public static void setMyLife(intmyLife) {761 Recorder.myLife =myLife;762 }763 //记录每关多少敌人

764 private static int enNum=MyPanel.getEnSize();765 //设置我有多少可以用的人

766 public static int myLife=3;767 //记录总共消灭了多少敌人

768 private static int allEnNum=0;769 //从文件中恢复记录点

770 static Vector nodes=new Vector();771 private static FileWriter fw=null;772 private static BufferedWriter bw=null;773 private static FileReader fr=null;774 private static BufferedReader br=null;775

776 public static VectorgetNodes()777 {778 try{779 fr=new FileReader("d:\\myRecording.txt");780 br=newBufferedReader(fr);781 String n="";782

783 n=br.readLine();//读第一行

784 allEnNum=Integer.parseInt(n);785 while((n=br.readLine())!=null)786 {787 String []xyz=n.split(" ");//按照空格返回数组,新技能

788 Node node=new Node(Integer.parseInt(xyz[0]),Integer.parseInt(xyz[1]),Integer.parseInt(xyz[2]),Integer.parseInt(xyz[3]));789 nodes.add(node);790 }791 }792

793 catch(Exception e)794 {795 e.printStackTrace();796 }797 finally

798 {799 try{800 br.close();801 fr.close();802 } catch(Exception e) {803 e.printStackTrace();804 }805 }806 returnnodes;807 }808 public static void keepRecAndEnemyTank(Vectorets)809 {810 try

811 {812 //创建

813 fw=new FileWriter("d:\\myRecording.txt");814 bw=newBufferedWriter(fw);815

816 bw.write(allEnNum+"\r\n");817

818 //保存当前还活着的敌人坐标和方向

819 for(int i=0;i

822 EnemyTank et=ets.get(i);823 if(et.isLive)824 {825 //活着的保存

826 String record=et.x+" "+et.y+" "+et.direct+" "+0;827 //写入

828 bw.write(record+"\r\n");829 }830 }831

832 }833 catch(Exception e)834 {835 e.printStackTrace();836 }837 finally

838 {839 //关闭流

840 try{841 //谁先开谁后关

842 bw.close();843 fw.close();844 } catch(Exception e2) {845 }846 }847

848 }849

850 public static voidkeepRecAndMyTank(Hero hero)851 {852 try

853 {854 //创建

855 fw=new FileWriter("d:\\myRecording.txt",true);//追加

856 bw=newBufferedWriter(fw);857

858 //保存当前我的坐标和方向

859 String record=hero.x+" "+hero.y+" "+hero.direct+" "+1;860 //写入

861 bw.write(record+"\r\n");862

863 }864

865 catch(Exception e)866 {867 e.printStackTrace();868 }869 finally

870 {871 //关闭流

872 try

873 {874 //谁先开谁后关

875 bw.close();876 fw.close();877 }878 catch(Exception e2)879 {880 e2.printStackTrace();881 }882 }883

884 }885

886 //从文件中读取记录

887 public static voidgetRecording()888 {889 try{890 fr=new FileReader("d:\\myRecording.txt");891 br=newBufferedReader(fr);892 String n=br.readLine();893 allEnNum=Integer.parseInt(n);894 }895 catch(Exception e)896 {897 e.printStackTrace();898 }899 finally

900 {901 try{902 br.close();903 fr.close();904 } catch(Exception e) {905 e.printStackTrace();906 }907 }908

909 }910 //把玩家击毁敌人的坦克数量保存到文件中

911 public static voidkeepRecording()912 {913 try

914 {915 //创建

916 fw=new FileWriter("d:\\myRecording.txt");917 bw=newBufferedWriter(fw);918

919 bw.write(allEnNum+"\r\n");920 }921 catch(Exception e)922 {923 e.printStackTrace();924 }925 finally

926 {927 //关闭流

928 try{929 //谁先开谁后关

930 bw.close();931 fw.close();932 } catch(Exception e2) {933 }934 }935 }936

937 public static intgetAllEnemy() {938 returnallEnNum;939 }940 public static voidreduceEnNum()941 {942 enNum--;943 }944

945 //消灭敌人

946 public static voidaddEnNumRec()947 {948 allEnNum++;949 }950

951 }952

953

954 classBomb955 {956 //定义炸弹的坐标

957 intx,y;958 //炸弹的生命

959 int life=9;960 boolean isLive=true;961 public Bomb(int x,inty)962 {963 this.x=x;964 this.y=y;965 }966 //减少生命值

967 public voidlifeDown()968 {969 if(life >0) {life--;}970 else {this.isLive=false;}971

972 }973 }974

975

976 classTank977 {978

979 //设置坦克的速度

980 int speed=3;981 public intgetSpeed()982 {983 returnspeed;984 }985 public void setSpeed(intspeed)986 {987 this.speed =speed;988 }989 //表示坦克的横坐标

990 int x=0;991 //坦克的纵坐标

992 int y=0;993 int direct=0;994 intcolor;995 boolean isLive=true;996

997 //坦克方向,0表示上,1表示右,2表示下,3表示左

998 public intgetColor() {999 returncolor;1000 }1001 public void setColor(intcolor) {1002 this.color =color;1003 }1004 public intgetDirect()1005 {1006 returndirect;1007 }1008

1009 public void setDirect(intdirect)1010 {1011 this.direct =direct;1012 }1013 public Tank(int x,inty)1014 {1015 this.x=x;1016 this.y=y;1017 }1018

1019 public intgetX()1020 {1021 returnx;1022 }1023 public void setX(intx)1024 {1025 this.x =x;1026 }1027 public intgetY()1028 {1029 returny;1030 }1031 public void setY(inty)1032 {1033 this.y =y;1034 }1035

1036 }1037

1038

1039 class EnemyTank extends Tank implementsRunnable1040 {1041 //定义一个向量,可以访问到MyPanel上所有敌人的坦克

1042 Vector ets=new Vector();1043

1044 //定义一个向量,可以存放敌人的子弹

1045

1046 Vector ss=new Vector();1047 //敌人添加子弹应该在刚刚创建坦克和坦克子弹死亡之后

1048 public EnemyTank(int x,inty)1049 {1050 super(x,y);1051 }1052

1053 //得到MyPanel的敌人坦克向量

1054 public void setEts(Vectorvv)1055 {1056 this.ets=vv;1057

1058 }1059

1060

1061 //判断是否碰到了别人的坦克

1062 public booleanisTouchOtherEnemy()1063 {1064 boolean b=false;1065

1066 switch(this.direct)1067 {1068 case 0:1069

1070 //我的坦克向上1071 //取出所有的敌人坦克

1072 for(int i=0;i

1075 EnemyTank et=ets.get(i);1076 //如果不是自己

1077 if(et!=this)1078 {1079 //如果敌人的方向向上或者是向下

1080 if(et.direct==0||et.direct==2)1081 {1082 if(this.x>=et.x&&this.x<=et.x+20&&this.y>=et.y&&this.y<=et.y+30)1083 {1084 return true;1085 }1086 if(this.x+20>=et.x&&this.x<=et.x+20&&this.y>=et.y&&this.y<=et.y+30)1087 {1088 return true;1089 }1090

1091 }1092

1093 if(et.direct==1||et.direct==3)1094 {1095 if(this.x>=et.x&&this.x<=et.x+30&&this.y>=et.y&&this.y<=et.y+20)1096 {1097 return true;1098 }1099 if(this.x+20>=et.x && this.x+20<=et.x+30&&this.y>=et.y&&this.y<=et.y+30)1100 {1101 return true;1102 }1103 }1104 }1105

1106 }1107

1108 break;1109 case 1:1110 //坦克向右1111 //取出所有的敌人坦克

1112 for(int i=0;i

1115 EnemyTank et=ets.get(i);1116 //如果不是自己

1117 if(et!=this)1118 {1119 //如果敌人的方向向上或者是向下

1120 if(et.direct==0||et.direct==2)1121 {1122 if(this.x+30>=et.x&&this.x+30<=et.x+20&&this.y>=et.y&&this.y<=et.y+30)1123 {1124 return true;1125 }1126 if(this.x+30>=et.x&&this.x+30<=et.x+20&&this.y+20>=et.y&&this.y+20<=et.y+30)1127 {1128 return true;1129 }1130

1131 }1132

1133 if(et.direct==1||et.direct==3)1134 {1135 if(this.x+30>=et.x&&this.x+30<=et.x+30&&this.y>=et.y&&this.y<=et.y+20)1136 {1137 return true;1138 }1139 if(this.x+30>=et.x && this.x+30<=et.x+30&&this.y+20>=et.y&&this.y+20<=et.y+20)1140 {1141 return true;1142 }1143 }1144 }1145

1146 }1147

1148

1149 case 2:1150 //坦克向下1151 //取出所有的敌人坦克

1152 for(int i=0;i

1155 EnemyTank et=ets.get(i);1156 //如果不是自己

1157 if(et!=this)1158 {1159 //如果敌人的方向向上或者是向下

1160 if(et.direct==0||et.direct==2)1161 {1162 if(this.x>=et.x&&this.x<=et.x+20&&this.y+30>=et.y&&this.y+30<=et.y+30)1163 {1164 return true;1165 }1166 if(this.x+20>=et.x&&this.x<=et.x+20&&this.y+30>=et.y&&this.y+30<=et.y+30)1167 {1168 return true;1169 }1170

1171 }1172

1173 if(et.direct==1||et.direct==3)1174 {1175 if(this.x+20>=et.x&&this.x+20<=et.x+30&&this.y+30>=et.y&&this.y+30<=et.y+20)1176 {1177 return true;1178 }1179 if(this.x+20>=et.x && this.x+20<=et.x+30&&this.y+30>=et.y&&this.y+30<=et.y+30)1180 {1181 return true;1182 }1183 }1184 }1185

1186 }1187 break;1188 case 3:1189 //坦克向左1190 //取出所有的敌人坦克

1191 for(int i=0;i

1194 EnemyTank et=ets.get(i);1195 //如果不是自己

1196 if(et!=this)1197 {1198 //如果敌人的方向向上或者是向下

1199 if(et.direct==0||et.direct==2)1200 {1201 if(this.x>=et.x&&this.x<=et.x+20&&this.y>=et.y&&this.y<=et.y+30)1202 {1203 return true;1204 }1205 if(this.x>=et.x&&this.x<=et.x+20&&this.y>=et.y&&this.y<=et.y+30)1206 {1207 return true;1208 }1209

1210 }1211

1212 if(et.direct==1||et.direct==3)1213 {1214 if(this.x>=et.x&&this.x<=et.x+30&&this.y+20>=et.y&&this.y+20<=et.y+20)1215 {1216 return true;1217 }1218 if(this.x>=et.x && this.x<=et.x+30&&this.y+20>=et.y&&this.y+20<=et.y+20)1219 {1220 return true;1221 }1222 }1223 }1224

1225 }1226 }1227

1228 returnb;1229 }1230

1231 public voidrun() {1232 while(true)1233 {1234 switch(this.direct)1235 {1236 case 0:1237 for(int i=0;i

1240 {1241 Thread.sleep(50);1242 }1243 catch(Exception e)1244 {1245 e.printStackTrace();1246 }1247 if(y>=speed && !this.isTouchOtherEnemy())1248 {1249 y-=speed;1250 }1251 }1252 break;1253 case 1:1254 for(int i=0;i

1257 {1258 Thread.sleep(50);1259 }1260 catch(Exception e)1261 {1262 e.printStackTrace();1263 }1264 if(x<=(400-(speed+30))&& !this.isTouchOtherEnemy())1265 {1266 x+=speed;1267 }1268 }1269 break;1270 case 2:1271 for(int i=0;i

1274 {1275 Thread.sleep(50);1276 }1277 catch(Exception e)1278 {1279 e.printStackTrace();1280 }1281 if(y<=(300-(speed+30))&& !this.isTouchOtherEnemy())1282 {1283 y+=speed;1284 }1285 }1286 break;1287 case 3:1288 for(int i=0;i

1291 {1292 Thread.sleep(50);1293 }1294 catch(Exception e)1295 {1296 e.printStackTrace();1297 }1298 if(x>=speed && !this.isTouchOtherEnemy())1299 {1300 x-=speed;1301 }1302

1303 }1304 break;1305

1306 }1307

1308 //让坦克随机产生一个新的方向

1309 this.direct=(int)(Math.random()*4);1310

1311 //判断敌人坦克是否死亡

1312 if(this.isLive==false)1313 {1314 //让坦克死亡后,退出线程

1315 break;1316 }1317

1318 }1319

1320 }1321 }1322

1323

1324 //我的坦克

1325 class Hero extendsTank1326 {1327 //子弹1328 //Shot s=null;

1329 Vector ss=new Vector();1330 Shot s=null;1331

1332 public Hero(int x, inty)1333 {1334 super(x,y);1335 }1336 //坦克向上移动1337

1338 //坦克的开火的能力和动作

1339 public voidshotEnemy()1340 {1341 switch(this.direct)1342 {1343 case 0:1344 s=new Shot(x+9,y-1,0);1345 ss.add(s);1346 break;1347 case 1:1348 s=new Shot(x+30,y+10,1);1349 ss.add(s);1350 break;1351 case 2:1352 s=new Shot(x+9,y+30,2);1353 ss.add(s);1354 break;1355 case 3:1356 s=new Shot(x-1,y+9,3); ss.add(s);1357 ss.add(s);1358 break;1359 }1360

1361 Thread t=newThread(s);1362 t.start();1363

1364 }1365

1366

1367 public voidmoveUp()1368 {1369 this.y-=speed;1370 }1371 public voidmoveRight()1372 {1373 this.x+=speed;1374 }1375

1376 public voidmoveDown()1377 {1378 this.y+=speed;1379 }1380 public voidmoveLeft()1381 {1382 this.x-=speed;1383 }1384 }1385

1386 class Shot implementsRunnable1387 {1388 intx;1389 inty;1390 intdirect;1391 int speed=2;1392 //是否活着

1393

1394 boolean isLive=true;1395 public Shot(int x,int y,intdirect)1396 {1397 this.x=x;1398 this.y=y;1399 this.direct=direct;1400 }1401 public voidrun()1402 {1403 while(true)1404 {1405 try{1406 Thread.sleep(50);1407 } catch(InterruptedException e) {1408 e.printStackTrace();1409 }1410

1411 switch(direct)1412 {1413 case 0:1414 //向上

1415 y-=speed;break;1416 case 1:1417 x+=speed;break;1418 case 2:1419 y+=speed;break;1420 case 3:1421 x-=speed;break;1422

1423 }1424

1425 //子弹何时死亡?1426 //判断该子弹是否碰到边缘

1427 if(x<0||x>400||y<0||y>300)1428 {1429 this.isLive=false;1430 break;1431

1432 }1433

1434 }1435

1436 }1437 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值